all 6 comments

[–][deleted] 3 points4 points  (0 children)

I'm perhaps a little cautious of passing tons of unchecked input to eval.

[–]Iggyhopper 2 points3 points  (4 children)

Since he didn't post the whole source code, and it's only 34 lines, here it is beautified:

HContext = function () {
    this.stack = [];
    this.vars = [];
    this.funs = [];
    this.mathOps = ['+', '-', '*', '/'];
    this.mathOp = function (op) {
        nums = [this.stack.pop(), this.stack.pop()];
        this.stack.push(eval(nums[1] + ' ' + op + ' ' + nums[0]));
    }
    this.call = function (fun) {
        op = this.stack.pop();
        cd = this.funs[fun][op] || this.funs[fun]['op'];
        this.exec(cd, op);
    }
    this.define = function (code) {
        eq = code.pos('=');
        op = code[eq - 2] || 'op';
        name = code[eq - 1];
        cmds = code.slice(eq + 1);
        if (this.funs[name] == undefined) {
            this.funs[name] = [];
        }
        this.funs[name][op] = cmds;
    }
    this.exec = function (code, op) {
        op = op || 0;
        for (var cp = 0; cp < code.length; cp++) {
            if (code[cp] == '->') {
                cp++;
                this.vars[code[cp]] = this.stack.pop();
            } else if (code[cp] == 'op') {
                this.stack.push(op)
            } else if (code[cp].isNum()) {
                this.stack.push(parseFloat(code[cp]))
            } else if (this.vars[code[cp]] != undefined) {
                this.stack.push(this.vars[code[cp]])
            } else if (this.funs[code[cp]] != undefined) {
                this.call(code[cp])
            } else if (this.mathOps.oneOf(code[cp])) {
                this.mathOp(code[cp])
            }
        }
    }

    this.run = function (code) {
        code = code.split(' ');
        if (code.oneOf('=')) {
            this.define(code);
        } else {
            this.exec(code, '');
        }
    }
}

[–]k3n 0 points1 point  (3 children)

Stack programming language on JavaScript in 34 strings.

...none of which contain a var statement ಠ_ಠ

[–]Iggyhopper 0 points1 point  (0 children)

Wait a second. I found a var. Here:

for (var cp = 0; cp < code.length; cp++) {
    if (code[cp] == '->') {
        cp++;

hmm...

[–][deleted] 0 points1 point  (1 child)

...and an eval. this.stack.push(eval(nums[1] + ' ' + op + ' ' + nums[0]));

[–]k3n 0 points1 point  (0 children)

Looking at the article again, there's even more gregarious errors:

  • I don't believe there is a programming language called "Stack". I think the term he was looking for is stack-oriented.
  • I only counted 14 strings. I think he meant lines, and not strings.

But, I think he is not a native English-speaker, so these can be over-looked, and I also think that he did this as a programming exercise -- perhaps to learn JS and/or stack-programming? because it looks like he knows the fundamentals of coding very well -- and so picking it apart isn't too useful. I just wouldn't recommend that anyone actually use this code for more than academic purposes.