-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnodescpp.h
More file actions
166 lines (133 loc) · 5.12 KB
/
nodescpp.h
File metadata and controls
166 lines (133 loc) · 5.12 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#pragma once
//TODO: Figure out which includes are needed
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/ADT/StringRef.h"
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <memory>
#include <string>
#include <vector>
using namespace llvm;
//const?
//purpose of declaring name again in each derived class
//does problem of object slicing occur when
// a particular derived class is stored in expression?
//what is a namespace?
namespace {
class expression {
std::string name;
public:
explicit expression(llvm::StringRef name) : name(name) {}
virtual ~expression() = default;
virtual Value *codegen() = 0;
virtual const char* getType() = 0;
virtual const char *getName() { return name.c_str(); }
virtual void setName(llvm::StringRef newName) { name = newName; }
};
class binary_expression : public expression {
static constexpr const char* ExprType = "binary expression";
std::string Op;
expression* LHS;
expression* RHS;
public:
binary_expression(llvm::StringRef name, llvm::StringRef Op, expression* LHS, expression* RHS) : expression(name), Op(Op), LHS(LHS), RHS(RHS) {}
~binary_expression() { delete LHS; delete RHS; }
Value *codegen() override;
virtual const char *getType() { return ExprType; }
};
class variable : public expression {
static constexpr const char* ExprType = "variable";
public:
variable(llvm::StringRef name) : expression(name) {}
variable(expression* expr) : expression(expr->getName()) {}
Value *codegen() override;
virtual const char *getType() { return ExprType; }
};
//TODO: figure out how to store unnamed variable into IR (e.g. "%3 = i32 52")
//TODO: for integer, float, string, bool const, add "char* name" field
class integer_const : public expression {
int val;
static constexpr const char* ExprType = "integer expression";
public:
integer_const(llvm::StringRef name, int val) : expression(name), val(val) {}
Value *codegen() override;
virtual const char *getType() { return ExprType; }
};
class float_const : public expression {
float val;
static constexpr const char* ExprType = "float expression";
public:
float_const(llvm::StringRef name, float val) : expression(name), val(val) {}
Value *codegen() override;
virtual const char *getType() { return ExprType; }
};
class string_const : public expression {
std::string val;
static constexpr const char* ExprType = "string expression";
public:
string_const(llvm::StringRef name, std::string val) : expression(name), val(val) {}
Value *codegen() override;
virtual const char *getType() { return ExprType; }
};
class bool_const : public expression {
bool val;
static constexpr const char* ExprType = "bool expression";
public:
bool_const(llvm::StringRef name, bool val) : expression(name), val(val) {}
Value *codegen() override;
virtual const char *getType() { return ExprType; }
};
class function_call : public expression {
std::vector<expression*> args;
static constexpr const char* ExprType = "function call";
//TODO: Add variable for name of function that is called (not name of variable assigned to function call)
std::string function_name;
public:
function_call(llvm::StringRef name, llvm::StringRef function_name)
: expression(name), function_name(function_name) {}
Value *codegen() override;
virtual const char *getType() { return ExprType; }
void addExpression(expression* exp) { args.push_back(exp); }
};
class return_statement : public expression {
expression* return_val;
static constexpr const char* ExprType = "return statement";
public:
return_statement(llvm::StringRef name, expression* return_val) : expression(name), return_val(std::move(return_val)) {}
~return_statement() { delete return_val; }
Value *codegen() override;
virtual const char *getType() { return ExprType; }
};
class function_exp : public expression {
std::vector<int> arg_types;
std::vector<std::string> arg_names;
int ret_type;
std::vector<expression*>* body;
static constexpr const char* ExprType = "function expression";
public:
function_exp(llvm::StringRef name, std::vector<int> arg_types, std::vector<std::string> arg_names, int ret_type, std::vector<expression*>* body)
: expression(name), arg_types(std::move(arg_types)), arg_names(std::move(arg_names)), ret_type(ret_type), body(std::move(body)) {}
Function *codegen();
virtual const char *getType() { return ExprType; }
};
class body {
std::vector<expression*>* body_content;
std::map<llvm::StringRef, llvm::StringRef>* variables;
public:
body(std::vector<expression*>* body_content, std::map<llvm::StringRef, llvm::StringRef>* variables)
: body_content(body_content), variables(variables) {}
std::vector<expression*>* getBodyContent() { return body_content; }
void addExpression(expression* expr) { body_content->push_back(expr); }
};
}