Created
January 2, 2012 16:26
-
-
Save itsbth/1551300 to your computer and use it in GitHub Desktop.
Uploaded by UploadToGist for Sublime Text 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Scope | |
| constructor: (@values={}) -> | |
| get: (name) -> @values[name] | |
| set: (name, value) -> @values[name] = value | |
| class Node | |
| class Statement extends Node | |
| execute: (scope) -> | |
| throw new Exception("WAT DO") | |
| class Assignment extends Statement | |
| constructor: (@identifier, @expression) -> | |
| execute: (scope) -> | |
| scope.set(@identifier, @expression.value(scope)) | |
| class Expression extends Node | |
| value: (scope) -> | |
| throw new Exception("WANNA VALUE") | |
| class Number extends Expression | |
| constructor: (@val) -> | |
| value: (scope) -> @val | |
| class Identifier extends Expression | |
| constructor: (@name) -> | |
| value: (scope) -> scope.get(@name) || 0 | |
| class Op extends Expression | |
| class BinOp extends Op | |
| constructor: (@left, @right) -> | |
| BinOps = | |
| '+': class extends BinOp | |
| value: (scope) -> | |
| @left.value(scope) + @right.value(scope) | |
| '-': class extends BinOp | |
| value: (scope) -> | |
| @left.value(scope) - @right.value(scope) | |
| '*': class extends BinOp | |
| value: (scope) -> | |
| @left.value(scope) * @right.value(scope) | |
| '/': class extends BinOp | |
| value: (scope) -> | |
| @left.value(scope) / @right.value(scope) | |
| class UnOp extends Op | |
| constructor: (@val) -> | |
| UnOps = | |
| '-': class extends UnOp | |
| value: (scope) -> -@val.value(scope) | |
| '!': class extends UnOp | |
| value: (scope) -> !@val.value(scope) | |
| op = new (BinOps['+'])(new Number(2), new (BinOps['-'])(new Number(4), new Identifier('foo'))) | |
| console.log op.value new Scope foo: 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment