first stuff of randomif
This commit is contained in:
@@ -59,6 +59,7 @@ SET(SCULLY_SOURCE
|
|||||||
src/test.cpp
|
src/test.cpp
|
||||||
|
|
||||||
src/Token.cpp
|
src/Token.cpp
|
||||||
|
src/libscully.c
|
||||||
|
|
||||||
src/AST/ASTElement.cpp
|
src/AST/ASTElement.cpp
|
||||||
src/AST/ASTVisitor.cpp
|
src/AST/ASTVisitor.cpp
|
||||||
|
|||||||
@@ -176,6 +176,29 @@ void CodeGenVisitor::visit(RandomForStatement* e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CodeGenVisitor::visit(RandomIfStatement* e) {
|
void CodeGenVisitor::visit(RandomIfStatement* e) {
|
||||||
|
value_ = 0;
|
||||||
|
e->getProb()->accept(this);
|
||||||
|
|
||||||
|
llvm::Function* f = builder_->GetInsertBlock()->getParent();
|
||||||
|
llvm::BasicBlock* thenBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "then", f);
|
||||||
|
llvm::BasicBlock* elseBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "else");
|
||||||
|
llvm::BasicBlock* mergeBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "merge");
|
||||||
|
|
||||||
|
builder_->CreateCondBr(value_, thenBB, elseBB);
|
||||||
|
|
||||||
|
// then
|
||||||
|
builder_->SetInsertPoint(thenBB);#
|
||||||
|
e->getStmt()->accept(this);
|
||||||
|
|
||||||
|
builder_->CreateBr(mergeBB);
|
||||||
|
|
||||||
|
f->getBasicBlockList().push_back(elseBB);
|
||||||
|
builder_->SetInsertPoint(elseBB);
|
||||||
|
|
||||||
|
builder_->CreateBr(mergeBB);
|
||||||
|
|
||||||
|
f->getBasicBlockList().push_back(mergeBB);
|
||||||
|
builder_->SetInsertPoint(mergeBB);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CodeGenVisitor::visit(ReturnStatement* e) {
|
void CodeGenVisitor::visit(ReturnStatement* e) {
|
||||||
|
|||||||
9
src/libscully.c
Normal file
9
src/libscully.c
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include <time.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
bool random_if(int p) {
|
||||||
|
srand(time(NULL));
|
||||||
|
int r = rand() % 100;
|
||||||
|
|
||||||
|
return (r < p);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user