diff --git a/doc/example3.x b/doc/example3.x new file mode 100644 index 0000000..61e5a9c --- /dev/null +++ b/doc/example3.x @@ -0,0 +1,2 @@ +int rofl dana fox mulder int x; x = 5; if dana x == 5 fox x = 2; return x; scully +rofl dana fox diff --git a/doc/example4.x b/doc/example4.x new file mode 100644 index 0000000..48dd575 --- /dev/null +++ b/doc/example4.x @@ -0,0 +1,5 @@ +int foobar dana fox mulder return 9001; scully +int barfoo dana fox mulder return 8999; scully +int foo dana int x fox mulder int y; if dana 2 < x fox y = foobar dana fox; if dana x < 2 fox y = barfoo dana fox; return y; scully +foo dana 3 fox +foo dana 1 fox diff --git a/src/AST/CodeGenVisitor.cpp b/src/AST/CodeGenVisitor.cpp index 8244096..62a4bd3 100644 --- a/src/AST/CodeGenVisitor.cpp +++ b/src/AST/CodeGenVisitor.cpp @@ -83,7 +83,11 @@ void CodeGenVisitor::visit(ConstantExpression* e) { } void CodeGenVisitor::visit(ExpressionStatement* e) { + value_ = 0; e->getExpr()->accept(this); + if (!value_) { + throw "error evaluating expression"; + } } void CodeGenVisitor::visit(ForStatement* e) @@ -91,7 +95,11 @@ void CodeGenVisitor::visit(ForStatement* e) value_ = 0; e->getInit()->accept(this); + value_ = 0; e->getCond()->accept(this); + if (!value_) { + throw "error evaluating expression"; + } llvm::Function* f = builder_->GetInsertBlock()->getParent(); llvm::BasicBlock* loopBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "loop", f); @@ -101,20 +109,14 @@ void CodeGenVisitor::visit(ForStatement* e) value_ = 0; e->getStmt()->accept(this); - if (value_ == 0) { - // throw err - } value_ = 0; e->getStep()->accept(this); - if (value_ == 0) { - // throw err - } value_ = 0; e->getCond()->accept(this); - if (value_ == 0) { - // throw err + if (!value_) { + throw "error evaluating expression"; } llvm::BasicBlock* afterBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "afterLoop",f); @@ -218,6 +220,9 @@ void CodeGenVisitor::visit(FunctionDefinition* e) { void CodeGenVisitor::visit(IfStatement* e) { value_ = 0; e->getCond()->accept(this); + if (!value_) { + throw "error evaluating expression"; + } llvm::Function* f = builder_->GetInsertBlock()->getParent(); llvm::BasicBlock* thenBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "then", f); @@ -242,17 +247,21 @@ void CodeGenVisitor::visit(IfStatement* e) { } void CodeGenVisitor::visit(ParameterList* e) { + // NOT USED } void CodeGenVisitor::visit(RandomForStatement* e) { - value_ = 0; e->getInit()->accept(this); + value_ = 0; e->getProb()->accept(this); - llvm::Function* cf = module_->getFunction("random_if"); - llvm::Value* prob = builder_->CreateCall(cf,value_,"callTmp"); + if (!value_) { + throw "error evaluating expression"; + } + llvm::Function* cf = module_->getFunction("random_if"); + llvm::Value* prob = builder_->CreateCall(cf, value_, "callTmp"); llvm::Function* f = builder_->GetInsertBlock()->getParent(); llvm::BasicBlock* loopBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "loop", f); @@ -262,20 +271,14 @@ void CodeGenVisitor::visit(RandomForStatement* e) { value_ = 0; e->getStmt()->accept(this); - if (value_ == 0) { - // throw err - } value_ = 0; e->getStep()->accept(this); - if (value_ == 0) { - // throw err - } value_ = 0; e->getProb()->accept(this); if (value_ == 0) { - // throw err + throw "error evaluating expression"; } llvm::BasicBlock* afterBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "afterLoop",f); @@ -289,6 +292,10 @@ void CodeGenVisitor::visit(RandomForStatement* e) { void CodeGenVisitor::visit(RandomIfStatement* e) { value_ = 0; e->getProb()->accept(this); + if (!value_) { + throw "error evaluating expression"; + } + llvm::Function* cf = module_->getFunction("random_if"); llvm::Value* cond = builder_->CreateCall(cf,value_,"callTmp"); @@ -316,6 +323,11 @@ void CodeGenVisitor::visit(RandomIfStatement* e) { void CodeGenVisitor::visit(ReturnStatement* e) { e->getExpr()->accept(this); + + if (!value_) { + throw "error evaluating expression"; + } + builder_->CreateRet(value_); } diff --git a/src/test.cpp b/src/test.cpp index a25e879..e24adfe 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -32,8 +32,8 @@ int main() { srand(42); - bool debugParser = false; - bool debugCodeGen = false; + bool debugParser = true; + bool debugCodeGen = true; lexertl::rules rules; lexertl::state_machine state_machine;