BinOp enum
This commit is contained in:
@@ -96,12 +96,12 @@ statements(A) ::= statements(B) statement(C). { B->addStatement(C); A = B; }
|
|||||||
%type expr {Expression*}
|
%type expr {Expression*}
|
||||||
expr(A) ::= T_IDENTIFIER(ID). { A = new LoadExpression(ID->getText()); }
|
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) ::= 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_EQUALS expr(C). { A = new BinOpExpression(B, OP_EQUALS, C); }
|
||||||
expr(A) ::= expr(B) T_LESS expr(C). { A = new BinOpExpression(B, "<", 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, "+", 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, "-", 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, "*", 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, "/", 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_CINT(I). { A = new ConstantExpression(I->getText()); }
|
||||||
expr(A) ::= T_TRUE. { A = new ConstantExpression("true"); }
|
expr(A) ::= T_TRUE. { A = new ConstantExpression("true"); }
|
||||||
expr(A) ::= T_FALSE. { A = new ConstantExpression("false"); }
|
expr(A) ::= T_FALSE. { A = new ConstantExpression("false"); }
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
#ifndef BINOP_H
|
#ifndef BINOP_H
|
||||||
#define BINOP_H
|
#define BINOP_H
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include "AST/Expression.h"
|
#include "AST/Expression.h"
|
||||||
|
|
||||||
|
enum BinOp {
|
||||||
|
OP_EQUALS,
|
||||||
|
OP_LESS,
|
||||||
|
OP_PLUS,
|
||||||
|
OP_MINUS,
|
||||||
|
OP_TIMES,
|
||||||
|
OP_DIV
|
||||||
|
};
|
||||||
|
|
||||||
class BinOpExpression : public Expression {
|
class BinOpExpression : public Expression {
|
||||||
public:
|
public:
|
||||||
BinOpExpression(Expression* leftExp ,std::string op, Expression* rightExp);
|
BinOpExpression(Expression* leftExp ,BinOp op, Expression* rightExp);
|
||||||
virtual ~BinOpExpression();
|
virtual ~BinOpExpression();
|
||||||
|
|
||||||
std::string getOp();
|
BinOp getOp();
|
||||||
Expression* getLeftExp();
|
Expression* getLeftExp();
|
||||||
Expression* getRightExp();
|
Expression* getRightExp();
|
||||||
virtual void accept(ASTVisitor* visitor);
|
virtual void accept(ASTVisitor* visitor);
|
||||||
@@ -17,7 +25,7 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string op_;
|
BinOp op_;
|
||||||
Expression* leftExp_;
|
Expression* leftExp_;
|
||||||
Expression* rightExp_;
|
Expression* rightExp_;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "AST/BinOpExpression.h"
|
#include "AST/BinOpExpression.h"
|
||||||
#include "AST/ASTVisitor.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) {
|
leftExp_(leftExp), op_(op), rightExp_(rightExp) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -9,7 +9,7 @@ BinOpExpression::~BinOpExpression() {
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string BinOpExpression::getOp() {
|
BinOp BinOpExpression::getOp() {
|
||||||
return op_;
|
return op_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,26 +24,26 @@ void CodeGenVisitor::visit(BinOpExpression* e) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// switch (e->getOp()) {
|
switch (e->getOp()) {
|
||||||
// case "+":
|
case OP_PLUS:
|
||||||
value_ = builder_->CreateAdd(lhs, rhs, "addtmp");
|
value_ = builder_->CreateAdd(lhs, rhs, "addtmp");
|
||||||
// break;
|
break;
|
||||||
// case "-":
|
case OP_MINUS:
|
||||||
// value_ = builder_->CreateSub(lhs, rhs, "subtmp");
|
value_ = builder_->CreateSub(lhs, rhs, "subtmp");
|
||||||
// break;
|
break;
|
||||||
// case "*":
|
case OP_TIMES:
|
||||||
// value_ = builder_->CreateMul(lhs, rhs, "multmp");
|
value_ = builder_->CreateMul(lhs, rhs, "multmp");
|
||||||
// break;
|
break;
|
||||||
// case "/":
|
case OP_DIV:
|
||||||
// value_ = builder_->CreateSDiv(lhs, rhs, "divtmp");
|
value_ = builder_->CreateSDiv(lhs, rhs, "divtmp");
|
||||||
// break;
|
break;
|
||||||
// case "==":
|
case OP_EQUALS:
|
||||||
// value_ = builder_->CreateICmpEQ(lhs, rhs, "eqtmp");
|
value_ = builder_->CreateICmpEQ(lhs, rhs, "eqtmp");
|
||||||
// break;
|
break;
|
||||||
// default:
|
default:
|
||||||
// // TODO error
|
// TODO error
|
||||||
// break;
|
break;
|
||||||
// }
|
}
|
||||||
|
|
||||||
value_->dump();
|
value_->dump();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
PrintVisitor::PrintVisitor() : level_(0) {
|
PrintVisitor::PrintVisitor() : level_(0) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
@@ -24,7 +25,33 @@ void PrintVisitor::visit(BinOpExpression* e) {
|
|||||||
println("BinOpExpression");
|
println("BinOpExpression");
|
||||||
level_++;
|
level_++;
|
||||||
std::stringstream ss;
|
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(ss.str());
|
||||||
println("LHS:");
|
println("LHS:");
|
||||||
e->getLeftExp()->accept(this);
|
e->getLeftExp()->accept(this);
|
||||||
|
|||||||
Reference in New Issue
Block a user