-
Notifications
You must be signed in to change notification settings - Fork 1
/
expressions.h
73 lines (59 loc) · 1.71 KB
/
expressions.h
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
#ifndef __EXPRESSIONS_H
#define __EXPRESSIONS_H
#include "ast.h"
#include "gen.h"
class Exp : public ASTnode
{
protected:
// Exp (int result, myType _type) : ASTnode () { this->_result = result; this->_type = _type; }
Exp() : type(UNKNOWN) {};
Exp(myType type) : type(type) {};
myType type;
public:
// return value is the variable in which the result of the expression will be stored.
// In case the expression is a numerical literal (for example 2 or 3.14) the literal
// is returned. Both variables and literals are represented here as Objects.
virtual Object genExp() = 0; // abstract class - every subclass should override and implement this func
myType getType() const { return type; };
};
class BinaryOp : public Exp
{
private:
enum op op;
Exp* left; // left operand
Exp* right; // right operand
int line; // source line number of operator
public:
BinaryOp(enum op op, Exp* left, Exp* right, int line);
Object genExp(); // override
};
class NumNode : public Exp
{
private:
union {
int ival;
double fval;
}value;
public:
NumNode(int ival);
NumNode(double fval);
Object genExp(); // override
int getIval() const { return value.ival; };
double getFval() const { return value.fval; };
void setIval(const int ival) { value.ival = ival; };
void setFval(const double fval) { value.fval = fval; };
};
class IdNode : public Exp
{
private:
char* name;
int line; // source line number
public:
IdNode(const char* name, int line);
IdNode(const IdNode& other);
~IdNode();
Object genExp(); // override
char* getName() const { return name; };
int getLine() const { return line; };
};
#endif