Skip to content

Commit

Permalink
field access parse and ast node working
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Rowan committed Oct 10, 2011
1 parent 5fd3dfb commit 211fa7b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
31 changes: 31 additions & 0 deletions ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,37 @@ Ast = {};
FunCall.prototype = expr_proto;
exports.FunCall = FunCall;

function FieldAccess(base, field) {
this.node_type = "field_access";
this.node_parent = "expr";

this.type = undefined;
this.base = base;
this.field = field;

this.check = function() {
var base_sym = sym_tab.search(this.base);
if(!base_sym) {
throw "Variable " + this.base + " is not defined";
}

var field_sym = base_sym.get_field(this.field);
if(!field_sym) {
throw "Field " + this.field + " is not defined on " + this.base;
}

this.type = field_sym.type;
}

this.code_gen = function() {
emit(this.base);
emit('.');
emit(this.field);
}
}
FieldAccess.prototype = expr_proto;
exports.FieldAccess = FieldAccess;

/*
function function_decl(name, formals, statements) {
this.node_type = "function decl";
Expand Down
2 changes: 1 addition & 1 deletion parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var parse = (function(){
]);

var field_access = new Rule([
['iden', '.', 'iden'], function(ms) {return true}
['iden', '.', 'iden'], function(ms) {return new Ast.FieldAccess(ms[0], ms[1])}
]);

var expr = new Rule([
Expand Down
4 changes: 2 additions & 2 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<script>
function abc() {};
var def;
var bob;
var bob = {def:'hi'};
</script>
<script id="src">
abc(def, bob + 2 )
bob.def
</script>
<script type="text/javascript">

Expand Down

0 comments on commit 211fa7b

Please sign in to comment.