From f152ac6119f7005523880cd0986c07c628c547dd Mon Sep 17 00:00:00 2001 From: Florian Sattler Date: Sat, 1 Jun 2013 23:44:25 +0200 Subject: [PATCH 1/7] added example3 --- doc/example3.x | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 doc/example3.x 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 From c1d113a23ac42b7a644ce50a20b0a8ac58da5d6c Mon Sep 17 00:00:00 2001 From: Florian Sattler Date: Sat, 1 Jun 2013 23:51:21 +0200 Subject: [PATCH 2/7] added example4 testing if statement --- doc/example4.x | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 doc/example4.x diff --git a/doc/example4.x b/doc/example4.x new file mode 100644 index 0000000..f5d80ed --- /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 if dana 2 < x fox foobar dana fox; if dana x < 2 fox barfoo dana fox; return 0; scully +foo dana 3 fox; +foo dana 1 fox; From 3e62dfb277b99e4bacf283ced53556adae825224 Mon Sep 17 00:00:00 2001 From: Markus Hauschild Date: Sat, 1 Jun 2013 23:51:43 +0200 Subject: [PATCH 3/7] Enable debug stuff by default --- src/test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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; From f84c532f16da6979be29ebb0e2e3af2da839174e Mon Sep 17 00:00:00 2001 From: Florian Sattler Date: Sat, 1 Jun 2013 23:56:35 +0200 Subject: [PATCH 4/7] fixed example4 --- doc/example4.x | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/example4.x b/doc/example4.x index f5d80ed..64d4978 100644 --- a/doc/example4.x +++ b/doc/example4.x @@ -1,5 +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 if dana 2 < x fox foobar dana fox; if dana x < 2 fox barfoo dana fox; return 0; 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; From 9c92ae4fbe42076cc6934c221b80f561180adc3a Mon Sep 17 00:00:00 2001 From: Florian Sattler Date: Sat, 1 Jun 2013 23:59:40 +0200 Subject: [PATCH 5/7] exapel4 works perfect for markus mom --- doc/example4.x | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/example4.x b/doc/example4.x index 64d4978..48dd575 100644 --- a/doc/example4.x +++ b/doc/example4.x @@ -1,5 +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; +foo dana 3 fox +foo dana 1 fox From c485352850bcaa86fd41c658361b43c869d52c96 Mon Sep 17 00:00:00 2001 From: Markus Hauschild Date: Sun, 2 Jun 2013 00:01:38 +0200 Subject: [PATCH 6/7] More error handling --- src/AST/CodeGenVisitor.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/AST/CodeGenVisitor.cpp b/src/AST/CodeGenVisitor.cpp index 74fcd46..f8488db 100644 --- a/src/AST/CodeGenVisitor.cpp +++ b/src/AST/CodeGenVisitor.cpp @@ -214,6 +214,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); @@ -238,17 +241,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); @@ -258,20 +265,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); @@ -285,6 +286,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"); @@ -312,6 +317,11 @@ void CodeGenVisitor::visit(RandomIfStatement* e) { void CodeGenVisitor::visit(ReturnStatement* e) { e->getExpr()->accept(this); + + if (!value_) { + throw "error evaluating expression"; + } + builder_->CreateRet(value_); } From c4e22b86278f59af25cbb9a6a02b416c7f555f57 Mon Sep 17 00:00:00 2001 From: Markus Hauschild Date: Sun, 2 Jun 2013 00:05:26 +0200 Subject: [PATCH 7/7] More exception handling ... --- src/AST/CodeGenVisitor.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/AST/CodeGenVisitor.cpp b/src/AST/CodeGenVisitor.cpp index f8488db..a3eabaf 100644 --- a/src/AST/CodeGenVisitor.cpp +++ b/src/AST/CodeGenVisitor.cpp @@ -79,7 +79,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) @@ -87,7 +91,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); @@ -97,20 +105,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);