diff --git a/grammar/grammar.y b/grammar/grammar.y index 145fff0..7878c73 100644 --- a/grammar/grammar.y +++ b/grammar/grammar.y @@ -66,7 +66,6 @@ fundef(A) ::= type(T) T_IDENTIFIER(ID) T_LPAREN params(P) T_RPAREN T_BEGIN state %type type {Type} type(A) ::= T_BOOL. { A = Type::BOOL; } type(A) ::= T_INT. { A = Type::INT; } -type(A) ::= T_STRING. { A = Type::STRING; } type(A) ::= T_VOID. { A = Type::VOID; } %type params {ParameterList*} @@ -84,6 +83,7 @@ statement(A) ::= T_FOR T_LPAREN statement(INIT) expr(COND) T_SEMICOLON statement { A = new ForStatement(INIT, COND, STEP, S); } statement(A) ::= T_RFOR T_LPAREN statement(INIT) expr(P) T_SEMICOLON statement(STEP) T_RPAREN statement(S). { A = new RandomForStatement(INIT, P, STEP, S); } +statement(A) ::= T_RETURN T_SEMICOLON. { A = new ReturnStatement(0); } statement(A) ::= T_RETURN expr(E) T_SEMICOLON. { A = new ReturnStatement(E); } statement(A) ::= T_BEGIN statements(S) T_END. { A = new Scope(S); } statement(A) ::= vardef(V) T_SEMICOLON. { A = V; } diff --git a/src/AST/CodeGenVisitor.cpp b/src/AST/CodeGenVisitor.cpp index 2d23219..f1e8edf 100644 --- a/src/AST/CodeGenVisitor.cpp +++ b/src/AST/CodeGenVisitor.cpp @@ -332,12 +332,16 @@ void CodeGenVisitor::visit(RandomIfStatement* e) { } void CodeGenVisitor::visit(ReturnStatement* e) { - e->getExpr()->accept(this); - if (!value_) { - throw "error evaluating expression"; - } + if (e->getExpr() != 0) { + e->getExpr()->accept(this); + if (!value_) { + throw "error evaluating expression"; + } - builder_->CreateRet(value_); + builder_->CreateRet(value_); + } else { + builder_->CreateRetVoid(); + } } void CodeGenVisitor::visit(Scope* e) { diff --git a/src/test.cpp b/src/test.cpp index e24adfe..76d2094 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -47,7 +47,7 @@ int main() { rules.add("void", T_VOID); rules.add("rfor", T_RFOR); rules.add("rif", T_RIF); - rules.add("string", T_STRING); + //rules.add("string", T_STRING); // special characters //rules.add("\"(\"", T_LPAREN);