-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemeexpression.py
133 lines (88 loc) · 3.4 KB
/
schemeexpression.py
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
class Expression:
def accept(self, syntax_tree_visitor):
pass
class NumberLiteral(Expression):
def __init__(self, lexeme):
self.lexeme = lexeme
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_number_literal(self)
class BoolLiteral(Expression):
def __init__(self, lexeme):
self.lexeme = lexeme
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_bool_literal(self)
class CharLiteral(Expression):
def __init__(self, lexeme):
self.lexeme = lexeme
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_char_literal(self)
class StringLiteral(Expression):
def __init__(self, lexeme):
self.lexeme = lexeme
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_string_literal(self)
class QuotedList(Expression):
def __init__(self, elements=None):
self.elements = [] if elements is None else elements
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_list(self)
class Symbol(Expression):
def __init__(self, symbol):
self.symbol = symbol
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_symbol(self)
class Conditional(Expression):
def __init__(self, test, consequent, alternate=None):
self.test = test
self.consequent = consequent
self.alternate = alternate
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_conditional(self)
class Args:
def __init__(self):
self.args = []
def add(self, arg):
self.args.append(arg)
class Call(Expression):
def __init__(self, callee, args=None):
self.callee = callee
self.args = args if args is not None else Args()
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_call(self)
class VariableReference(Expression):
def __init__(self, variable_name):
self.variable_name = variable_name
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_variable_reference(self)
class FormalParameters:
def __init__(self):
self.fixed_parameters = []
self.has_list_parameter = False
self.list_parameter_name = ''
def set_list_parameter(self, name):
self.has_list_parameter = True
self.list_parameter_name = name
def append_parameter(self, name):
self.fixed_parameters.append(name)
class Lambda(Expression):
def __init__(self, formals, body):
self.formals = formals
self.body = body
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_lambda(self)
class Definition(Expression):
def __init__(self, variable, expression):
self.name = variable
self.expression = expression
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_definition(self)
class Assignment(Expression):
def __init__(self, variable, expression):
self.name = variable
self.expression = expression
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_assignment(self)
# user has no way to create this in source code, it is created by the parser to implement letrec
class UnAssigned(Expression):
def accept(self, syntax_tree_visitor):
return syntax_tree_visitor.visit_unassigned(self)