-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_builder_expressions.rb
More file actions
222 lines (194 loc) · 4.96 KB
/
Copy pathstring_builder_expressions.rb
File metadata and controls
222 lines (194 loc) · 4.96 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
class Expression < RLTK::ASTNode; end
class SimpleInstruction < Expression; end
class Number < Expression
value :value, Fixnum
def exec(variables)
@value
end
end
class Text < Expression
value :value, String
def exec(variables)
@value
end
end
class Variable < Expression
value :name, String
value :type, Symbol
def exec(variables)
var = variables[@name]
raise "You referenced #{@name} like a Number but it is a Text" if var.is_a?(String) && @type == :number
raise "You referenced #{@name} like a Text but it is a Number" if var.is_a?(Fixnum) && @type == :text
var
end
end
class NumberOperation < Expression
value :type, String
child :left, Expression
child :right, Expression
def exec(vars)
case @type
when '+'
@left.exec(vars) + @right.exec(vars)
when '-'
@left.exec(vars) - @right.exec(vars)
when '*'
@left.exec(vars) * @right.exec(vars)
when '/'
@left.exec(vars) / @right.exec(vars)
when '%'
@left.exec(vars) % @right.exec(vars)
else
raise "Unknown NumberOperation for #{@type}"
end
end
end
class Assign < SimpleInstruction
value :name, String
child :right, Expression
def exec(variables)
var = @right.exec(variables)
raise "You tried to assign variable #{@name} like a Text but it is a Number" if var.is_a?(Fixnum) && @type == :text
raise "You tried to assign variable #{@name} like a Number but it is a Text" if var.is_a?(String) && @type == :number
variables[@name] = var
var
end
end
class Call < SimpleInstruction
value :type, Symbol
child :first, Expression
child :second, Expression
child :third, Expression
child :fourth, Expression
def exec(variables)
case @type
when :print then puts(@first.exec(variables))
when :concat then
val1 = @first.exec(variables)
val2 = @second.exec(variables)
if val1.is_a?(String) && val2.is_a?(String)
val1+val2
else
raise "One of values is not a String: val1=#{val1.class} val2=#{val2.class}"
end
when :length then
@first.exec(variables).size
when :position then
@first.exec(variables).index(@second.exec(variables)).to_i
when :substr
@first.exec(variables)[@second.exec(variables), @third.exec(variables)]
when :readint
gets().to_i
when :readstr
gets()
when :exit
exit()
else
raise "Unknown type #{@type}"
end
end
end
# class BoolValue < Expression
# value :value, :symbol
# def ==(compare_to, return_bool: false)
# :true
# end
# end
class LogicCompareExpression < Expression
value :type, String
end
class BoolExpression < LogicCompareExpression
child :left, LogicCompareExpression
child :right, LogicCompareExpression
def exec(vars)
case @type
when 'not' then
if @left.nil?
return false
else
!@left.exec(vars)
end
when 'and' then
@left.exec(vars) && @right.exec(vars)
when 'or' then
@left.exec(vars) || @right.exec(vars)
else
raise "Unknown BoolExpression #{@type}"
end
end
end
class NumberRelation < LogicCompareExpression
child :left, Expression
child :right, Expression
def exec(var)
case @type
when '<' then
@left.exec(var) < @right.exec(var)
when '=' then
@left.exec(var) == @right.exec(var)
when '<>' then
@left.exec(var) != @right.exec(var)
when '<=' then
@left.exec(var) <= @right.exec(var)
when '>=' then
@left.exec(var) >= @right.exec(var)
when '>' then
@left.exec(var) > @right.exec(var)
else
raise "Unknown operation #{@type} on numbers"
end
end
end
class TextRelation < LogicCompareExpression
child :left, Expression
child :right, Expression
def exec(var)
case @type
when '==' then
@left.exec(var) == @right.exec(var)
when '!=' then
@left.exec(var) != @right.exec(var)
else
raise "Unknown operation #{@type} on texts"
end
end
end
class Instruction < Expression
child :simple, SimpleInstruction
child :instruction, Instruction
def exec(variables)
if not @instruction.nil?
@instruction.exec(variables)
end
if not @simple.nil?
@simple.exec(variables)
end
end
end
class BeginEndBlock < SimpleInstruction
child :instruction, Instruction
def exec(variables)
@instruction.exec(variables)
end
end
class IfClause < SimpleInstruction
child :condition, LogicCompareExpression
child :first, SimpleInstruction
child :second, SimpleInstruction
def exec(vars)
if @condition.exec(vars)
@first.exec(vars)
else
@second.exec(vars) unless @second.nil?
end
end
end
class WhileClause < SimpleInstruction
child :condition, BoolExpression
child :block, SimpleInstruction
def exec(variables)
while(@condition.exec(variables))
@block.exec(variables)
end
end
end