-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinop.cpp
More file actions
42 lines (34 loc) · 915 Bytes
/
binop.cpp
File metadata and controls
42 lines (34 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "binop.hpp"
#include "symbol.h"
BinOp::BinOp(Expr *l, char o, Expr *r): left(l), op(o), right(r) {}
BinOp::~BinOp() { delete left; delete right; }
void BinOp::printAST(std::ostream &out) const {
out << op << "(" << *left << ", " << *right << ")";
}
void BinOp::sem()
{
left->type_check(typeInteger);
right->type_check(typeInteger);
expr_type = typeInteger;
}
llvm::Value* BinOp::compile() {
llvm::Value *L = left->compile();
llvm::Value *R = right->compile();
if (!L || !R)
return nullptr;
switch (op) {
case '+':
return Builder.CreateAdd(L, R, "addtmp");
case '-':
return Builder.CreateSub(L, R, "subtmp");
case '*':
return Builder.CreateMul(L, R, "multmp");
case '/':
return Builder.CreateSDiv(L, R, "divtmp");
case '%':
return Builder.CreateSRem(L, R, "modtmp");
default:
return nullptr;
return nullptr;
}
}