-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAST.h
More file actions
122 lines (110 loc) · 3.24 KB
/
AST.h
File metadata and controls
122 lines (110 loc) · 3.24 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
/**
* @file AST.h
* @brief Declaration of classes defining an abstract syntax tree for the sample language.
*/
#ifndef AST_H_
#define AST_H_
#include "Type.h"
#include <iostream>
using namespace std;
class Visitor;
/// A node in an abstract syntax tree.
class Expr {
public:
Expr() { }
virtual ~Expr() { }
virtual const class Type &Type() const = 0;
virtual void Accept(Visitor *v) const = 0;
friend ostream &operator<<(ostream &out, const Expr *node);
};
/// An integer literal, like 0 or 5.
class IntegerLiteral: public Expr {
public:
explicit IntegerLiteral(int val) : value(val) { }
int Value() const { return value; }
virtual const class Type &Type() const;
virtual void Accept(Visitor *v) const;
private:
const int value;
};
/// A real (double precision floating-point) literal, like 3.5 or 1.0.
class RealLiteral: public Expr {
public:
explicit RealLiteral(double val) : value(val) { }
double Value() const { return value; }
virtual const class Type &Type() const;
virtual void Accept(Visitor *v) const;
private:
const double value;
};
/// The input() expression, which prompts the user to enter an integer value.
class InputExpr: public Expr {
public:
InputExpr() { }
~InputExpr() { }
virtual const class Type &Type() const;
virtual void Accept(Visitor *v) const;
private:
};
/// An integer(x) or real(n) expression, which converts a value from one type to another.
class CastExpr: public Expr {
public:
explicit CastExpr(const class Type &t, const Expr *e)
: type(t), expr(e) { }
const Expr *ExprToCast() const { return expr; }
virtual const class Type &Type() const;
virtual void Accept(Visitor *v) const;
private:
const class Type &type;
const Expr *expr;
};
/// Binary operators: + - * / < <= > >= = !=
typedef enum {
ADD, SUB, MUL, DIV,
LT, LTE, GT, GTE, EQ, NEQ,
} BinOp;
/// A binary expression, like 3+4 or 2<5
class BinExpr: public Expr {
public:
explicit BinExpr(const Expr *l, BinOp o, const Expr *r)
: lhs(l), op(o), rhs(r) { }
const Expr *Lhs() const { return lhs; }
BinOp Op() const { return op; }
const Expr *Rhs() const { return rhs; }
virtual const class Type &Type() const;
virtual void Accept(Visitor *v) const;
private:
const Expr *lhs;
const BinOp op;
const Expr *rhs;
};
/// An if-else expression: if c then t else e
class IfExpr: public Expr {
public:
explicit IfExpr(const Expr *c, const Expr *t, const Expr *e)
: condExpr(c), thenExpr(t), elseExpr(e) { }
const Expr *CondExpr() const { return condExpr; }
const Expr *ThenExpr() const { return thenExpr; }
const Expr *ElseExpr() const { return elseExpr; }
virtual const class Type &Type() const;
virtual void Accept(Visitor *v) const;
private:
const Expr *condExpr;
const Expr *thenExpr;
const Expr *elseExpr;
};
/**
* A Visitor, which can be used to traverse an abstract syntax tree.
*
* The Visit methods are responsible for traversing child nodes.
*/
class Visitor {
public:
virtual void Visit(const IntegerLiteral *node) = 0;
virtual void Visit(const RealLiteral *node) = 0;
virtual void Visit(const InputExpr *node) = 0;
virtual void Visit(const CastExpr *node) = 0;
virtual void Visit(const BinExpr *node) = 0;
virtual void Visit(const IfExpr *node) = 0;
};
#endif // AST_H_