From c485352850bcaa86fd41c658361b43c869d52c96 Mon Sep 17 00:00:00 2001 From: Markus Hauschild Date: Sun, 2 Jun 2013 00:01:38 +0200 Subject: [PATCH] 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_); }