BinOp enum

This commit is contained in:
2013-06-01 16:13:23 +02:00
parent 96731379b4
commit 99e44c8e70
5 changed files with 68 additions and 33 deletions

View File

@@ -96,12 +96,12 @@ statements(A) ::= statements(B) statement(C). { B->addStatement(C); A = B; }
%type expr {Expression*}
expr(A) ::= T_IDENTIFIER(ID). { A = new LoadExpression(ID->getText()); }
expr(A) ::= T_IDENTIFIER(ID) T_ASSIGN expr(E). { A = new AssignmentExpression(ID->getText(), E); }
expr(A) ::= expr(B) T_EQUALS expr(C). { A = new BinOpExpression(B, "==", C); }
expr(A) ::= expr(B) T_LESS expr(C). { A = new BinOpExpression(B, "<", C); }
expr(A) ::= expr(B) T_PLUS expr(C). { A = new BinOpExpression(B, "+", C); }
expr(A) ::= expr(B) T_MINUS expr(C). { A = new BinOpExpression(B, "-", C); }
expr(A) ::= expr(B) T_TIMES expr(C). { A = new BinOpExpression(B, "*", C); }
expr(A) ::= expr(B) T_DIV expr(C). { A = new BinOpExpression(B, "/", C); }
expr(A) ::= expr(B) T_EQUALS expr(C). { A = new BinOpExpression(B, OP_EQUALS, C); }
expr(A) ::= expr(B) T_LESS expr(C). { A = new BinOpExpression(B, OP_LESS, C); }
expr(A) ::= expr(B) T_PLUS expr(C). { A = new BinOpExpression(B, OP_PLUS, C); }
expr(A) ::= expr(B) T_MINUS expr(C). { A = new BinOpExpression(B, OP_MINUS, C); }
expr(A) ::= expr(B) T_TIMES expr(C). { A = new BinOpExpression(B, OP_TIMES, C); }
expr(A) ::= expr(B) T_DIV expr(C). { A = new BinOpExpression(B, OP_DIV, C); }
expr(A) ::= T_CINT(I). { A = new ConstantExpression(I->getText()); }
expr(A) ::= T_TRUE. { A = new ConstantExpression("true"); }
expr(A) ::= T_FALSE. { A = new ConstantExpression("false"); }

View File

@@ -1,15 +1,23 @@
#ifndef BINOP_H
#define BINOP_H
#include <string>
#include "AST/Expression.h"
enum BinOp {
OP_EQUALS,
OP_LESS,
OP_PLUS,
OP_MINUS,
OP_TIMES,
OP_DIV
};
class BinOpExpression : public Expression {
public:
BinOpExpression(Expression* leftExp ,std::string op, Expression* rightExp);
BinOpExpression(Expression* leftExp ,BinOp op, Expression* rightExp);
virtual ~BinOpExpression();
std::string getOp();
BinOp getOp();
Expression* getLeftExp();
Expression* getRightExp();
virtual void accept(ASTVisitor* visitor);
@@ -17,7 +25,7 @@ public:
private:
std::string op_;
BinOp op_;
Expression* leftExp_;
Expression* rightExp_;

View File

@@ -1,7 +1,7 @@
#include "AST/BinOpExpression.h"
#include "AST/ASTVisitor.h"
BinOpExpression::BinOpExpression(Expression *leftExp, std::string op, Expression *rightExp) :
BinOpExpression::BinOpExpression(Expression *leftExp, BinOp op, Expression *rightExp) :
leftExp_(leftExp), op_(op), rightExp_(rightExp) {
}
@@ -9,7 +9,7 @@ BinOpExpression::~BinOpExpression() {
//
}
std::string BinOpExpression::getOp() {
BinOp BinOpExpression::getOp() {
return op_;
}

View File

@@ -24,26 +24,26 @@ void CodeGenVisitor::visit(BinOpExpression* e) {
return;
}
// switch (e->getOp()) {
// case "+":
switch (e->getOp()) {
case OP_PLUS:
value_ = builder_->CreateAdd(lhs, rhs, "addtmp");
// break;
// case "-":
// value_ = builder_->CreateSub(lhs, rhs, "subtmp");
// break;
// case "*":
// value_ = builder_->CreateMul(lhs, rhs, "multmp");
// break;
// case "/":
// value_ = builder_->CreateSDiv(lhs, rhs, "divtmp");
// break;
// case "==":
// value_ = builder_->CreateICmpEQ(lhs, rhs, "eqtmp");
// break;
// default:
// // TODO error
// break;
// }
break;
case OP_MINUS:
value_ = builder_->CreateSub(lhs, rhs, "subtmp");
break;
case OP_TIMES:
value_ = builder_->CreateMul(lhs, rhs, "multmp");
break;
case OP_DIV:
value_ = builder_->CreateSDiv(lhs, rhs, "divtmp");
break;
case OP_EQUALS:
value_ = builder_->CreateICmpEQ(lhs, rhs, "eqtmp");
break;
default:
// TODO error
break;
}
value_->dump();
}

View File

@@ -2,6 +2,7 @@
#include <iostream>
#include <sstream>
PrintVisitor::PrintVisitor() : level_(0) {
//
}
@@ -24,7 +25,33 @@ void PrintVisitor::visit(BinOpExpression* e) {
println("BinOpExpression");
level_++;
std::stringstream ss;
ss << "Operator: " << e->getOp();
std::string opStr;
switch (e->getOp()) {
case OP_DIV:
opStr = "/";
break;
case OP_EQUALS:
opStr = "==";
break;
case OP_LESS:
opStr = "<";
break;
case OP_MINUS:
opStr = "-";
break;
case OP_PLUS:
opStr = "+";
break;
case OP_TIMES:
opStr = "*";
break;
default:
opStr = "Unknown Op";
break;
}
ss << "Operator: " << opStr;
println(ss.str());
println("LHS:");
e->getLeftExp()->accept(this);