瀏覽代碼

Add compound statements

pull/5/head
mtardy 4 年之前
父節點
當前提交
42ad489c1e
共有 2 個檔案被更改,包括 34 行新增1 行删除
  1. +28
    -1
      parse.leg
  2. +6
    -0
      test1.txt

+ 28
- 1
parse.leg 查看文件

@ -7,7 +7,7 @@
*/ */
#define DO_PROTOS() \ #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(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(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) \ _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; 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" // this always creates the key in "object"
oop newVariable(oop object, oop key, oop value) 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 ELSE f:stmt { $$ = newIf(c, t, f ) }
| IF LPAREN c:exp RPAREN t:stmt { $$ = newIf(c, t, null) } | IF LPAREN c:exp RPAREN t:stmt { $$ = newIf(c, t, null) }
| WHILE LPAREN c:exp RPAREN e:stmt { $$ = newWhile(c, e) } | WHILE LPAREN c:exp RPAREN e:stmt { $$ = newWhile(c, e) }
| s:compoundStatement { $$ = newCompoundStatement(s) }
| l:IDENT ASSIGN e:exp { $$ = newAssign(l, e) } | l:IDENT ASSIGN e:exp { $$ = newAssign(l, e) }
| l:postfix DOT i:IDENT ASSIGN e:exp { $$ = newSetMember(l, i, 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) } | l:postfix LBRAC i:exp RBRAC ASSIGN e:exp { $$ = newSetIndex(l, i, e) }
| c:cond { $$ = c } | 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) } cond = c:logor QUERY t:exp COLON f:cond { $$ = newIf(c, t, f) }
| logor | logor
@ -587,6 +601,19 @@ oop eval(oop scope, oop ast)
} }
return get(func, Function, primitive)(args); 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: { case t_getMember: {
oop map = eval(scope, map_get(ast, map_symbol)); oop map = eval(scope, map_get(ast, map_symbol));
oop key = map_get(ast, key_symbol); oop key = map_get(ast, key_symbol);

+ 6
- 0
test1.txt 查看文件

@ -0,0 +1,6 @@
var a = 20
{
var a = 10
a
}
a

Loading…
取消
儲存