-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExp.h
More file actions
166 lines (130 loc) · 4.42 KB
/
Exp.h
File metadata and controls
166 lines (130 loc) · 4.42 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
#include "Type.h"
#include "Visitor.h"
#include <iostream>
#include <memory>
#include <vector>
/// Base class for an expression, which holds its type.
class Exp
{
public:
/// Construct expression. Most expression types are unknown until typechecking,
/// except for constants.
explicit Exp( Type type = kTypeUnknown )
: m_type( type )
{
}
/// The destructor is virtual, ensuring that the destructor for a derived
/// class will be properly invoked.
virtual ~Exp() {}
/// Get the expression type (usually kTypeUnknown if not yet typechecked).
Type GetType() const { return m_type; }
/// Set the expression type.
void SetType( Type type ) { m_type = type; }
/// Dispatch to a visitor. \see ExpVisitor
virtual void* Dispatch( ExpVisitor& visitor ) = 0;
private:
Type m_type;
};
/// Unique pointer to expression.
using ExpPtr = std::unique_ptr<Exp>;
/// Boolean constant expression.
class BoolExp : public Exp
{
public:
/// Construct boolean constant expression.
BoolExp( bool value )
: Exp( kTypeBool )
, m_value( value )
{
}
/// Get the value of this constant.
bool GetValue() const { return m_value; }
/// Dispatch to visitor.
void* Dispatch( ExpVisitor& visitor ) override { return visitor.Visit( *this ); }
private:
bool m_value;
};
/// Integer constant expression
class IntExp : public Exp
{
public:
/// Construct integer constant expression.
IntExp( int value )
: Exp( kTypeInt )
, m_value( value )
{
}
/// Get the value of this constant.
int GetValue() const { return m_value; }
/// Dispatch to visitor.
void* Dispatch( ExpVisitor& visitor ) override { return visitor.Visit( *this ); }
private:
int m_value;
};
/// Variable expression.
class VarExp : public Exp
{
public:
/// Construct variable expression.
VarExp( const std::string& name )
: m_name( name )
{
}
/// Get the variable name.
const std::string& GetName() const { return m_name; }
/// Get the variable's declaration (null if not yet typechecked).
const VarDecl* GetVarDecl() const { return m_varDecl; }
/// Link this variable expression to the variable's declaration. Called from the typechecker.
void SetVarDecl( const VarDecl* varDecl ) { m_varDecl = varDecl; }
/// Dispatch to a visitor.
void* Dispatch( ExpVisitor& visitor ) override { return visitor.Visit( *this ); }
private:
std::string m_name;
const VarDecl* m_varDecl; // assigned by the typechecker.
};
/// Function call expression.
class CallExp : public Exp
{
public:
/// Construct function call expression with arbitrary arguments.
CallExp( const std::string& funcName, std::vector<ExpPtr>&& args )
: m_funcName( funcName )
, m_args( std::move( args ) )
, m_funcDef( nullptr )
{
}
/// Construct a unary function call (for convenience).
CallExp( const std::string& funcName, ExpPtr exp )
: m_funcName( funcName )
, m_args( 1 )
, m_funcDef( nullptr )
{
m_args[0] = std::move( exp );
}
/// Construct a binary function call (for convenience).
CallExp( const std::string& funcName, ExpPtr leftExp, ExpPtr rightExp )
: m_funcName( funcName )
, m_args( 2 )
, m_funcDef( nullptr )
{
m_args[0] = std::move( leftExp );
m_args[1] = std::move( rightExp );
}
/// Get the function name.
const std::string& GetFuncName() const { return m_funcName; }
/// Get the argument expressions.
const std::vector<ExpPtr>& GetArgs() const { return m_args; }
/// Get the function definition (null until typechecked).
const FuncDef* GetFuncDef() const { return m_funcDef; }
/// Link this function call to the function definition. Called from typechecker.
void SetFuncDef( const FuncDef* funcDef ) { m_funcDef = funcDef; }
/// Dispatch to visitor.
void* Dispatch( ExpVisitor& visitor ) override { return visitor.Visit( *this ); }
private:
std::string m_funcName;
std::vector<ExpPtr> m_args;
const FuncDef* m_funcDef; // set by typechecker.
};
/// Unique pointer to function call expression
using CallExpPtr = std::unique_ptr<CallExp>;