Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@
%left AR_SUBSCR


%precedence ")"
%precedence "else"


// When error recovery, bison may want to discard some symbols. So
// it is generally good practice to free any allocated memory here.

Expand Down Expand Up @@ -262,14 +266,15 @@ stmts: stmts[car] stmt[self] { $$ = $car; push_c
;


stmt: expr[c] ";"[op] { $$ = NEW_NODE($op->tok, Stmt); push_child($$, $c); }
stmt:
expr[c] ";"[op] { $$ = NEW_NODE($op->tok, Stmt); push_child($$, $c); }
| code_block[c] { $$ = NEW_NODE($c->tok, Stmt); push_child($$, $c); }
| print_stmt[c] ";"[op] { $$ = NEW_NODE($op->tok, Stmt); push_child($$, $c); }
| decl[c] ";"[op] { $$ = NEW_NODE($op->tok, Stmt); push_child($$, $c); }
| if_stmt[c] { $$ = NEW_NODE($c->tok, Stmt); push_child($$, $c); }
| for_stmt[c] { $$ = NEW_NODE($c->tok, Stmt); push_child($$, $c); }
| while_stmt[c] { $$ = NEW_NODE($c->tok, Stmt); push_child($$, $c); }
| do_while_stmt[c] { $$ = NEW_NODE($c->tok, Stmt); push_child($$, $c); }
| code_block[c] { $$ = NEW_NODE($c->tok, Stmt); push_child($$, $c); }
| ";" { $$ = NULL; }
| error { }
;
Expand Down Expand Up @@ -334,6 +339,10 @@ code_block: "{"[op] {symtable_begin_block(); } stmts[ss] "}"
| "{"[op] "}" { $$ = NEW_NODE($op->tok, CodeBlock); }
;

body:
stmt
;

for_1: decl
| assignment
| %empty { $$ = NULL; }
Expand All @@ -345,22 +354,20 @@ for_3: expr
| %empty { $$ = NULL; }
;

if_stmt: "if"[op] "(" expr[e] ")" code_block[cb] { $$ = NEW_NODE($op->tok, IfStmt); push_childs($$, 3, YYANARRAY { $e, $cb, NULL}); }
| "if"[op] "(" expr[e] ")" code_block[cbi] "else" code_block[cbe] { $$ = NEW_NODE($op->tok, IfStmt); push_childs($$, 3, YYANARRAY { $e, $cbi, $cbe}); }
| "if"[op] "(" expr[e] ")" code_block[cb] "else" if_stmt[car] { $$ = NEW_NODE($op->tok, IfStmt); push_childs($$, 3, YYANARRAY { $e, $cb, $car}); }
| "if"[op] "(" error ")" code_block[cb] { }
| "if"[op] "(" error ")" code_block[cbi] "else" code_block[cbe] { }
| "if"[op] "("error ")" code_block[cb] "else" if_stmt[car] { }
if_stmt: "if"[op] "(" expr[e] ")" body[cb] { $$ = NEW_NODE($op->tok, IfStmt); push_childs($$, 3, YYANARRAY { $e, $cb, NULL}); }
| "if"[op] "(" expr[e] ")" body[cbi] "else" body[cbe] { $$ = NEW_NODE($op->tok, IfStmt); push_childs($$, 3, YYANARRAY { $e, $cbi, $cbe}); }
| "if"[op] "(" error ")" body[cb] { }
| "if"[op] "(" error ")" body[cbi] "else" body[cbe] { }
;

for_stmt: "for"[op] "(" { symtable_begin_block(); } for_1[f1] ";" for_2[f2] ";" for_3[f3] ")" code_block[cb] { symtable_end_block(); $$ = NEW_NODE($op->tok, ForStmt); push_childs($$, 4, YYANARRAY {$f1, $f2, $cb, $f3} ); }
| "for" "(" error ")" code_block { }
for_stmt: "for"[op] "(" { symtable_begin_block(); } for_1[f1] ";" for_2[f2] ";" for_3[f3] ")" body[cb] { symtable_end_block(); $$ = NEW_NODE($op->tok, ForStmt); push_childs($$, 4, YYANARRAY {$f1, $f2, $cb, $f3} ); }
| "for" "(" error ")" body { }
;
while_stmt: "while"[op] "(" expr[e] ")" code_block[cb] { $$ = NEW_NODE($op->tok, WhileStmt); push_childs($$, 2, YYANARRAY {$e, $cb} ); }
| "while" "(" error ")" code_block { }
while_stmt: "while"[op] "(" expr[e] ")" body[cb] { $$ = NEW_NODE($op->tok, WhileStmt); push_childs($$, 2, YYANARRAY {$e, $cb} ); }
| "while" "(" error ")" body { }
;
do_while_stmt: "do"[op] code_block[cb] "while" "(" expr[e] ")" ";" { $$ = NEW_NODE($op->tok, DoWhileStmt); push_childs($$, 2, YYANARRAY {$cb, $e} ); }
| "do" code_block "while" "(" error ")" ";" { }
do_while_stmt: "do"[op] body[cb] "while" "(" expr[e] ")" ";" { $$ = NEW_NODE($op->tok, DoWhileStmt); push_childs($$, 2, YYANARRAY {$cb, $e} ); }
| "do" body "while" "(" error ")" ";" { }
;


Expand Down
52 changes: 0 additions & 52 deletions tests/invalid.dpl
Original file line number Diff line number Diff line change
Expand Up @@ -144,55 +144,3 @@
let a = 10.0f == 12.0f;
}
///////////////////////////////////////////////////////////////////////////////


// If blocks requires the brackets and cannot have a single statement
{
let a = 10;
if (a == 10);
}
///////////////////////////////////////////////////////////////////////////////
{
let a = 10;
if (a == 10)
print(a);
}
///////////////////////////////////////////////////////////////////////////////

// For blocks requires the brackets and cannot have a single statement
{
let a = 10;
for (a = 0; a < 10; a++);
}
///////////////////////////////////////////////////////////////////////////////
{
let a = 10;
for (a = 0; a < 10; a++)
print(a);
}
///////////////////////////////////////////////////////////////////////////////

// While blocks requires the brackets and cannot have a single statement
{
let a = 10;
while(a++ < 20);
}
///////////////////////////////////////////////////////////////////////////////
{
let a = 10;
while(a++ < 20)
print(a);
}
///////////////////////////////////////////////////////////////////////////////

// Do while blocks requires the brackets and cannot have a single statement
{
let a = 10;
do ; while(a++ < 20);
}
///////////////////////////////////////////////////////////////////////////////
{
let a = 10;
do print(a); while(a++ < 20);
}
///////////////////////////////////////////////////////////////////////////////
16 changes: 16 additions & 0 deletions tests/valid.dpl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ print("\n\nInteger var decl\n");
print(a);
}

///////////////////////////////////////////////////////////////////////////////
//@ If blocks can have 1 simple statement as a body
//@ a = 10
//@ a = 15
{
print("\n\nIf blocks can have 1 simple statement as a body\n");
let a = 10;
if (a == 10)
print(a);

while(a < 15)
a++;

print(a);
}

///////////////////////////////////////////////////////////////////////////////

//@ Float var decl
Expand Down