Skip to content

Commit 584cfc2

Browse files
Initial commit
0 parents  commit 584cfc2

22 files changed

+3123
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
inputs/

ArithExpr.cpp

Lines changed: 621 additions & 0 deletions
Large diffs are not rendered by default.

ArithExpr.hpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#ifndef EXPRINTER_ARITHEXPR_HPP
2+
#define EXPRINTER_ARITHEXPR_HPP
3+
4+
#include "Token.hpp"
5+
#include "SymTab.hpp"
6+
7+
8+
class Functions;
9+
10+
// Classes in this file define the internal representation of expressions.
11+
12+
13+
// An ExprNode serves as the base class (super class) for an expression.
14+
// It forces the derived classes (subclasses) to implement two functions, print and
15+
// evaluate.
16+
class ExprNode {
17+
public:
18+
ExprNode(Token token);
19+
Token token() const;
20+
Token &token() { return _token; }
21+
virtual void print() = 0;
22+
virtual TypeDescriptor* evaluate(SymTab &symTab) = 0;
23+
24+
private:
25+
Token _token;
26+
};
27+
28+
29+
// An InfixExprNode is useful to represent binary arithmetic operators.
30+
class InfixExprNode: public ExprNode { // An expression tree node.
31+
32+
public:
33+
InfixExprNode(Token tk);
34+
35+
ExprNode *&left();
36+
ExprNode *&right();
37+
virtual void print();
38+
virtual TypeDescriptor* evaluate(SymTab &symTab);
39+
40+
private:
41+
ExprNode *_left, *_right;
42+
};
43+
44+
45+
// A RelExprNode is useful to represent binary relational operators.
46+
class RelExprNode: public ExprNode { // An expression tree node.
47+
48+
public:
49+
RelExprNode(Token tk);
50+
51+
ExprNode *&left();
52+
ExprNode *&right();
53+
virtual void print();
54+
virtual TypeDescriptor* evaluate(SymTab &symTab);
55+
56+
private:
57+
ExprNode *_left, *_right;
58+
};
59+
60+
// A BoolExprNode is useful to represent binary boolean operators.
61+
class BoolExprNode: public ExprNode { // An expression tree node.
62+
63+
public:
64+
BoolExprNode(Token tk);
65+
66+
ExprNode *&left();
67+
ExprNode *&right();
68+
virtual void print();
69+
virtual TypeDescriptor* evaluate(SymTab &symTab);
70+
71+
private:
72+
ExprNode *_left, *_right;
73+
};
74+
75+
// A NotExprNode is useful to represent the unary 'not' operator.
76+
class NotExprNode: public ExprNode { // An expression tree node.
77+
78+
public:
79+
NotExprNode(Token tk);
80+
81+
ExprNode *&right();
82+
virtual void print();
83+
virtual TypeDescriptor* evaluate(SymTab &symTab);
84+
85+
private:
86+
ExprNode *_right;
87+
};
88+
89+
// A Subscription is useful to represent a subscription into an array
90+
class Subscription: public ExprNode {
91+
public:
92+
Subscription(Token varName, ExprNode* subscript);
93+
94+
ExprNode *&subscript();
95+
virtual void print();
96+
virtual TypeDescriptor* evaluate(SymTab &symTab);
97+
98+
private:
99+
ExprNode *_subscript;
100+
};
101+
102+
// A Len is useful to represent a length call on an array
103+
class Len: public ExprNode {
104+
public:
105+
Len(Token varName);
106+
virtual void print();
107+
virtual TypeDescriptor* evaluate(SymTab &symTab);
108+
};
109+
110+
class Call: public ExprNode {
111+
public:
112+
Call(Token funcName, Functions* funcList, std::vector<ExprNode*> args);
113+
114+
std::vector<ExprNode*> &args();
115+
virtual void print();
116+
virtual TypeDescriptor* evaluate(SymTab &symTab);
117+
private:
118+
std::vector<ExprNode*> _args;
119+
Functions* _funcList;
120+
};
121+
122+
// WholeNumber is a leaf-node in an expression tree. It corresponds to
123+
// a terminal in the production rules of the grammar that describes the
124+
// syntax of expressions.
125+
126+
class WholeNumber: public ExprNode {
127+
public:
128+
WholeNumber(Token token);
129+
virtual void print();
130+
virtual TypeDescriptor* evaluate(SymTab &symTab);
131+
};
132+
133+
// Variable is a leaf-node in an expression tree. It corresponds to
134+
// a terminal in the production rules of the grammar that describes the
135+
// syntax of expressions.
136+
137+
class Variable: public ExprNode {
138+
public:
139+
Variable(Token token);
140+
virtual void print();
141+
virtual TypeDescriptor* evaluate(SymTab &symTab);
142+
};
143+
144+
// Double is a leaf-node in an expression tree. It corresponds to
145+
// a terminal in the production rules of the grammar that describes the
146+
// syntax of expressions.
147+
148+
class Double: public ExprNode {
149+
public:
150+
Double(Token token);
151+
virtual void print();
152+
virtual TypeDescriptor* evaluate(SymTab &symTab);
153+
};
154+
155+
// String is a leaf-node in an expression tree. It corresponds to
156+
// a terminal in the production rules of the grammar that describes the
157+
// syntax of expressions.
158+
159+
class String: public ExprNode {
160+
public:
161+
String(Token token);
162+
virtual void print();
163+
virtual TypeDescriptor* evaluate(SymTab &symTab);
164+
};
165+
166+
#endif //EXPRINTER_ARITHEXPR_HPP

Functions.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "Functions.hpp"
2+
3+
void Functions::addFunction(Function *func) {
4+
functions().push_back(func);
5+
}
6+
7+
Function* Functions::findFunction(std::string name) {
8+
for (int i = 0; i < _functions.size(); i++) {
9+
if (_functions[i]->name() == name)
10+
return _functions[i];
11+
}
12+
return nullptr;
13+
}

Functions.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef EXPRINTER_FUNCTIONS_HPP
2+
#define EXPRINTER_FUNCTIONS_HPP
3+
4+
#include <vector>
5+
#include "SymTab.hpp"
6+
#include "Statements.hpp"
7+
8+
class Function {
9+
public:
10+
Function();
11+
Function(std::string name, std::vector<std::string> args, Statements *body)
12+
: _name{name}, _args{args}, _body{body} {}
13+
14+
std::string &name() { return _name; }
15+
std::vector<std::string> &args() { return _args; }
16+
Statements *&body() {return _body; }
17+
18+
private:
19+
std::string _name;
20+
std::vector<std::string> _args;
21+
Statements *_body;
22+
};
23+
24+
25+
// Functions is a collection of Function.
26+
27+
class Functions {
28+
public:
29+
Functions() : _functions{std::vector<Function *>()} {}
30+
31+
std::vector<Function*> &functions() {return _functions; }
32+
33+
void addFunction(Function *func);
34+
Function *findFunction(std::string name);
35+
36+
private:
37+
std::vector<Function *> _functions;
38+
};
39+
40+
41+
#endif //EXPRINTER_FUNCTIONS_HPP

Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.SUFFIXES: .o .cpp .exe
2+
3+
CFLAGS = -ggdb -std=c++17
4+
objects = Token.o Tokenizer.o Parser.o ArithExpr.o SymTab.o Statements.o TypeDescriptor.o Range.o Functions.o main.o
5+
6+
python.exe: $(objects)
7+
g++ $(CFLAGS) -o python.exe $(objects)
8+
9+
.cpp.o:
10+
g++ $(CFLAGS) -c $< -o $@
11+
12+
13+
Token.o: Token.cpp Token.hpp
14+
Tokenizer.o: Tokenizer.cpp Tokenizer.hpp
15+
ArithExpr.o: ArithExpr.cpp ArithExpr.hpp Token.hpp SymTab.hpp Functions.hpp TypeDescriptor.hpp
16+
SymTab.o: SymTab.cpp SymTab.hpp TypeDescriptor.hpp
17+
Parser.o: Parser.cpp Token.hpp Parser.hpp Tokenizer.hpp SymTab.hpp ArithExpr.hpp Statements.hpp Range.hpp Functions.hpp
18+
Statements.o: Statements.cpp Statements.hpp ArithExpr.hpp SymTab.hpp Range.hpp TypeDescriptor.hpp
19+
TypeDescriptor.o: TypeDescriptor.cpp TypeDescriptor.hpp
20+
Range.o: Range.cpp Range.hpp ArithExpr.hpp
21+
Functions.o: Functions.cpp Functions.hpp SymTab.hpp Statements.hpp
22+
main.o: main.cpp Token.hpp Tokenizer.hpp Parser.hpp
23+
24+
clean:
25+
rm -fr *.o *~ *.exe

0 commit comments

Comments
 (0)