diff --git a/parse.leg b/parse.leg index c520417..e71c3a9 100644 --- a/parse.leg +++ b/parse.leg @@ -7,7 +7,7 @@ */ #define DO_PROTOS() \ - _DO(map) _DO(if) _DO(while) _DO(call) _DO(func) _DO(declaration) _DO(assign) _DO(symbol) _DO(integer) _DO(string) \ + _DO(map) _DO(if) _DO(while) _DO(call) _DO(func) _DO(compoundStatement) _DO(declaration) _DO(assign) _DO(symbol) _DO(integer) _DO(string) \ _DO(logor) _DO(logand) _DO(bitor) _DO(bitxor) _DO(bitand) \ _DO(equal) _DO(noteq) _DO(less) _DO(lesseq) _DO(greater) _DO(greatereq) _DO(shleft) _DO(shright) \ _DO(add) _DO(sub) _DO(mul) _DO(div) _DO(mod) _DO(not) _DO(neg) _DO(com) \ @@ -251,6 +251,13 @@ oop newCall(oop func, oop args) return call; } +oop newCompoundStatement(oop statements) +{ + oop obj = newObject(compoundStatement_proto); + map_set(obj, statements_symbol, statements); + return obj; +} + // this always creates the key in "object" oop newVariable(oop object, oop key, oop value) { @@ -300,11 +307,18 @@ exp = VAR l:IDENT ASSIGN e:exp { $$ = newDeclarati | IF LPAREN c:exp RPAREN t:stmt ELSE f:stmt { $$ = newIf(c, t, f ) } | IF LPAREN c:exp RPAREN t:stmt { $$ = newIf(c, t, null) } | WHILE LPAREN c:exp RPAREN e:stmt { $$ = newWhile(c, e) } + | s:compoundStatement { $$ = newCompoundStatement(s) } + | l:IDENT ASSIGN e:exp { $$ = newAssign(l, e) } | l:postfix DOT i:IDENT ASSIGN e:exp { $$ = newSetMember(l, i, e) } | l:postfix LBRAC i:exp RBRAC ASSIGN e:exp { $$ = newSetIndex(l, i, e) } | c:cond { $$ = c } +compoundStatement = LCB m:makeMap + ( e:exp { map_append(m, e) } + ) * + RCB { $$ = m } + cond = c:logor QUERY t:exp COLON f:cond { $$ = newIf(c, t, f) } | logor @@ -587,6 +601,19 @@ oop eval(oop scope, oop ast) } return get(func, Function, primitive)(args); } + case t_compoundStatement: { + oop statements = map_get(ast, statements_symbol); + int i = 0; + oop index; + oop statement, res; + oop localScope = newObject(scope); + while ((index = makeInteger(i)), map_hasKey(statements, index)) { + statement = map_get(statements, index); + res = eval(localScope, statement); + i++; + } + return res; + } case t_getMember: { oop map = eval(scope, map_get(ast, map_symbol)); oop key = map_get(ast, key_symbol); diff --git a/test1.txt b/test1.txt new file mode 100644 index 0000000..e83dac6 --- /dev/null +++ b/test1.txt @@ -0,0 +1,6 @@ +var a = 20 +{ + var a = 10 + a +} +a \ No newline at end of file