Repository navigation
Expand file tree
/
Copy pathfunctionNodes.rb
More file actions
217 lines (199 loc) · 6.64 KB
/
Copy pathfunctionNodes.rb
File metadata and controls
217 lines (199 loc) · 6.64 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
####################################
# CMM FUNCTION #
####################################
class CmmFunction < Node
attr_accessor :type, :params, :codeBlock
def initialize(type, params = [], codeBlock)
@type = type
@params = params
@codeBlock = codeBlock
end
def evaluate(values)
$scope << {}
$scope_depth +=1
replacePossible = false
values.each_with_index do |value,index|
if @params[index][@params[0].keys()[index]] == "int" && values[index].class == CmmInteger
replacePossible = true
elsif @params[index][@params[0].keys()[index]] == "int" && values[index].evaluate.class == CmmInteger
value = values[index].evaluate
replacePossible = true
elsif @params[index][@params[0].keys()[index]] == "float" && values[index].class == CmmFloat
replacePossible = true
elsif @params[index][@params[0].keys()[index]] == "float" && values[index].evaluate.class == CmmFloat
value = values[index].evaluate
replacePossible = true
elsif @params[index][@params[0].keys()[index]] == "bool" && values[index].class == CmmBoolean
replacePossible = true
elsif @params[index][@params[0].keys()[index]] == "bool" && values[index].evaluate.class == CmmBoolean
value = values[index].evaluate
replacePossible = true
elsif @params[index][@params[0].keys()[index]] == "char" && values[index].class == CmmChar
replacePossible = true
elsif @params[index][@params[0].keys()[index]] == "char" && values[index].evaluate.class == CmmChar
value = values[index].evaluate
replacePossible = true
elsif @params[index][@params[0].keys()[index]] == "string" && values[index].class == CmmString
replacePossible = true
elsif @params[index][@params[0].keys()[index]] == "string" && values[index].evaluate.class == CmmString
value = values[index].evaluate
replacePossible = true
end
if replacePossible
$scope[$scope_depth][@params[index].keys()[0]] = value
else
begin
raise(SyntaxError)
rescue SyntaxError
abort("ERROR: faulty parameter, you dumb fuck".red())
end
end
end
ret = 0
@codeBlock.each do |x|
if x.is_a?(ContainerNode) and x.will_return().is_a?(CmmReturn)
return x.evaluate()
elsif x.is_a?(CmmReturn)
return x.evaluate()
else
x.evaluate
end
end
$scope_depth -=1
ret = $scope.pop()
if ret.is_a?(Hash)
unless @type == "void"
abort("ERROR: function does not return a '#{@type}', you dumb fuck".red())
end
end
end
end
####################################
# CMM RETURN #
####################################
class CmmReturn < Node
attr_accessor :thing, :type, :reset_thing
def initialize(thing = nil)
@thing = thing
@reset_thing = thing
@type = nil
end
def evaluate()
thing = @thing
if thing.is_a?(LookUp) or thing.is_a?(ExpressionNode)
thing = thing.evaluate
if thing == true
thing = CmmBoolean.new(true)
elsif thing == false
thing = CmmBoolean.new(false)
end
end
case thing
when CmmInteger
check = "int"
when CmmFloat
check = "float"
when CmmBoolean
check = "bool"
when CmmChar
check = "char"
when CmmString
check = "string"
when FunctionCall
check = thing.gettype()
else
begin
raise(SyntaxError)
rescue SyntaxError
if thing == nil
abort("ERROR: #{@type}-function must '#{@type}', you dumb fuck".red())
else
abort("ERROR: '#{thing}' is not returnable, you dumb fuck".red())
end
end
end
if @type == "void" and thing != nil
begin
raise(SyntaxError)
rescue SyntaxError
abort("ERROR: void function can not have 'return', you dumb fuck".red())
end
end
if check == @type
if thing.is_a?(FunctionCall)
thing.evaluate()
else
$scope_depth -=1
$scope.pop()
return thing
end
else
begin
raise(SyntaxError)
rescue SyntaxError
abort("ERROR: '#{check}' is not of type '#{@type}', you dumb fuck".red())
end
end
end
end
####################################
# CMM FUNCTION CALL #
####################################
class FunctionCall < Node
attr_accessor :name, :params
def initialize(name, params = [])
@name = name
@params = params
end
def gettype()
return $funcs[@name].type
end
def evaluate()
$funcs[@name].codeBlock.each do |x|
if x.is_a?(ContainerNode)
x.has_return().each do |c|
if c.type == nil
c.type = gettype()
end
c.thing = c.reset_thing
end
if x.is_a?(ElifStatment)
x.returns = []
end
elsif x.is_a?(CmmReturn)
if x.type == nil
x.type = gettype()
end
x.thing = x.reset_thing
end
end
$funcs[@name].evaluate(@params)
end
end
####################################
# CMM FUNCTION DECLARATION #
####################################
class FunctionDeclaration < Node
attr_accessor :identifier, :function
def initialize(identifier, func)
@identifier = identifier
@function = func
end
def evaluate()
$funcs[@identifier] = @function
end
end
####################################
# MAIN FUNCTION #
####################################
class MainFunction < Node
attr_accessor :statements
def initialize(statements)
@statements = statements
end
def evaluate()
@statements.each do |x|
x.evaluate()
end
end
end