-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.rb
More file actions
622 lines (560 loc) · 10.7 KB
/
nodes.rb
File metadata and controls
622 lines (560 loc) · 10.7 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
##### Scope #####
$current_scope = 0
$highest_scope = 0
$current_scope_list = [0]
$var_list = [{}]
# $func_list is is created in order for functions to be saved
$func_list = {}
##### Scope functions #####
#Returns value of variable found in var_list. if nothing is found returns nil.
def find_variable(variable)
for scope in $current_scope_list.sort{|a, b| b <=> a}
if $var_list[scope][variable] != nil
return $var_list[scope][variable]
end
end
return nil
end
#Returns an index to the scope which the given variable can be found
def create_variable(variable)
for i in $current_scope_list.sort{|a, b| b <=> a}
if $var_list[i][variable] != nil
return i
end
end
return $current_scope_list[-1]
end
#Is called when a new scope is created
def add_scope()
$highest_scope += 1
$current_scope_list << $highest_scope
$var_list << {}
end
#is called when we want to close scope
def close_scope()
$highest_scope -= 1
$current_scope_list.pop
end
#is called when we want to close scope when exiting ex: functions
def close_var_list()
$var_list.pop
close_scope()
end
##### Useable functions #####
#Node for round function. Round numbers to the nearest specified amount of decimals.
class Round_func_node
def initialize(value, decimals, direction = nil)
@value = value
@decimals = decimals
@direction = direction
end
def eval()
if @decimals.is_a?(Variable_node)
@decimals = @decimals.eval
end
if @value.is_a?(Variable_node)
scope = create_variable(@value.id())
result_value = @value.eval
if @direction != nil
$var_list[scope][@value.id()] = result_value.to_f.round(@decimals, half: @direction)
else
$var_list[scope][@value.id()] = result_value.to_f.round(@decimals)
end
elsif @direction != nil
@value.to_f.round(@decimals, half: @direction)
else
@value.to_f.round(@decimals)
end
end
end
##### Statements #####
#Node for handling statements. Statements are used in loops/if-else and functions.
class Statement_node
def initialize(*stmts)
@stmts = stmts
end
def eval()
for stmt in @stmts
ret = stmt.eval
if stmt.is_a? Retrieve_node
break
end
end
return ret
end
end
#Node for expressions. Are used within functions parameters.
class Expression_node
def initialize(nn)
@next_node = nn
end
def eval()
@next_node.eval
end
end
##### Functions #####
#Node for creating functions.
class Func_create_node
def initialize(name, params, stmts)
@name = name
@params = params
@stmts = stmts
end
def eval()
$func_list[@name] = @stmts,@params
end
end
#Node for using functions.
class Func_use_node
def initialize(name, params)
@name = name
@params = params
end
def eval()
if $func_list[@name]
stmts = $func_list[@name][0]
else
raise "Function #{@name} not declared"
end
add_scope()
if @params
@params.eval($func_list[@name][1])
end
result_value = stmts.eval()
close_var_list()
return result_value
end
end
#Used when calling functions. Is used for calling the function with parameters.
class Call_param_node
def initialize(*params)
@params = params
end
def eval(vars)
vars.eval(@params)
end
end
#Used to create parameters for functions.
class Create_param_node
def initialize(*params)
@params = params
end
def id()
return nil
end
def eval(values)
index = 0
for param in @params
if param.id() == nil
if values[index]
values[index].eval(param)
else
raise "Wrong number of arguments"
end
else
$var_list[$highest_scope][param.id()] = values[index].eval()
end
index += 1
end
end
end
##### if/else ######
#Node for If statements.
class If_node
def initialize(trueValue, stmt)
@trueValue = trueValue
@stmt = stmt
end
def eval()
if @trueValue.eval
result_value = @stmt.eval
return result_value
end
end
end
#Node for Elsif and else statements.
class IfElse_node
def initialize(trueValue, stmt, stmt2)
@trueValue = trueValue
@stmt = stmt
@stmt2 = stmt2
end
def eval()
if @trueValue.eval
result_value = @stmt.eval
return result_value
# @stmt.eval
else
result_value = @stmt2.eval
return result_value
end
end
end
###### Iterators ######
#Node for creating for loops.
class For_node
def initialize(range, stmt, start = 0, variable = nil)
@range = range
@stmt = stmt
@start = start
@variable = variable
end
def eval()
if @variable
$var_list[$current_scope][@variable.id()] = 0
end
iterations = @range.eval
if @start != 0
@start = @start.eval
end
while(@start < iterations)
iterations -= 1
if @variable
$var_list[$current_scope][@variable.id()] = @variable.eval() + 1
end
result_value = @stmt.eval
end
return result_value
end
end
#Node for creating while loops.
class While_node
def initialize(stmt, whileLog)
@stmt = stmt
@whileLog = whileLog
end
def eval()
while(@whileLog.eval)
result_value = @stmt.eval
end
return result_value
end
end
###### Logicals ######
#Node for when using multiple expressions and both needing to return true.
class And_node
def initialize(exp1, exp2)
@exp1 = exp1
@exp2 = exp2
end
def eval()
res = @exp1.eval && @exp2.eval
res
end
end
#Node for when calling two expressions. Atleast one expression needs to return true.
class Or_node
def initialize(exp1, exp2)
@exp1 = exp1
@exp2 = exp2
end
def eval()
res = @exp1.eval || @exp2.eval
res
end
end
#Node for the value True.
class True_node
def initialize()
@value = true
end
def eval()
@value
end
end
#Node for the value False.
class False_node
def initialize()
@value = false
end
def eval()
@value
end
end
#Node for using the operator ">".
class Greaters_node
def initialize(a, b)
@a = a
@b = b
end
def eval()
@a.eval > @b.eval
end
end
#Node for using the operator "<".
class Smallers_node
def initialize(a, b)
@a = a
@b = b
end
def eval()
@a.eval < @b.eval
end
end
#Node for using the operator ">=".
class Greater_node
def initialize(a, b)
@a = a
@b = b
end
def eval()
@a.eval >= @b.eval
end
end
#Node for using the operator "<=".
class Smaller_node
def initialize(a, b)
@a = a
@b = b
end
def eval()
@a.eval <= @b.eval
end
end
#Node for using the operator "==".
class Equals_node
def initialize(a, b)
@a = a
@b = b
end
def eval()
@a.eval == @b.eval
end
end
#Node for using the operator "!=".
class Not_node
def initialize(a, b)
@a = a
@b = b
end
def eval()
@a.eval != @b.eval
end
end
##### Maths #####
#Node for using addition.
class Addition_node
def initialize(a, b)
@a = a
@b = b
end
def eval()
# print("ADDITION_NODE RESULT: ", @a.eval + @b.eval)
@a.eval + @b.eval
end
end
#Node for using subtraction.
class Subtraction_node
def initialize(a, b)
@a = a
@b = b
end
def eval()
@a.eval - @b.eval
end
end
#Node for using the multiplication.
class Multiplication_node
def initialize(a, b)
@a = a
@b = b
end
def eval()
@a.eval * @b.eval
end
end
#Node for using the division.
class Division_node
def initialize(a, b)
@a = a
@b = b
end
def eval()
@a.eval / @b.eval
end
end
#Node for incrementing Integers and Floats.
class Increment_node
def initialize(a)
@a = a
end
def eval()
if @a.eval.is_a?(Integer)
$var_list[$highest_scope][@a.id()] = @a.eval() + 1
@a.eval
end
end
end
#Node for decrementing Integers and Floats.
class Decrement_node
def initialize(a)
@a = a
end
def eval()
if @a.eval.is_a?(Integer)
$var_list[$highest_scope][@a.id()] = @a.eval() - 1
@a.eval
end
end
end
#################################
#Node for printing variables, values, operators.
class Print_node
def initialize(statement)
@to_print = statement
end
def eval()
result = @to_print.eval
if result == nil
raise "ERROR: Variable has not been declared in this scope"
else
puts "#{result}" # #{} is used in for arrays to printed as well.
end
end
end
##### Assign/Return #####
#Node for variable assignment.
class Assign_node
def initialize(a, b)
@a = a
@b = b
end
def eval()
scope = create_variable(@a.id())
$var_list[scope][@a.id()] = @b.eval
end
end
#Node for Returning values or variables.
class Retrieve_node
def initialize(var)
@variable = var
end
def eval()
@variable.eval
end
end
#Node for taking a user input from the terminal.
class Input_node
def initialize()
@ret = nil
end
def eval()
@ret = STDIN.gets()
int = Integer(@ret) rescue nil
float = Float(@ret) rescue nil
if int
return int
elsif float
return float
else
return @ret
end
end
end
##### Variables #####
#Node for variables.
class Variable_node
def initialize(var)
@variable = var
end
def eval()
find_variable(@variable)
end
def id()
@variable
end
end
#Node for datatype array.
class Array_node
def initialize(lst)
@lst = lst
end
def eval()
if @lst
@lst.each_with_index{|element, index| @lst[index] = element.eval()}
@lst
else
[]
end
end
end
#Node for specific values in an array.
class Array_value_node
def initialize(arr, pos)
@arr = arr
@pos = pos
end
def eval()
if @arr.is_a? Variable_node
@arr = @arr.eval
end
return @arr[@pos]
end
end
#Node for adding values to list.
class Array_add_node
def initialize(val, arr)
@arr = arr
@val = val
end
def eval()
@arr.eval().append @val.eval()
end
end
#Node for removing values from list.
class Array_remove_node
def initialize(val, arr, by_value)
@arr = arr
@val = val
@by_value = by_value
end
def eval()
if @by_value
@arr.eval().delete(@val.eval())
else
@arr.eval().delete_at(@val)
end
end
end
#Node for datatype Integers.
class Int_node
def initialize(i, negative = nil)
@number = i
@negative = negative
end
def eval()
if @negative
@number * -1
else
@number
end
end
end
#Node for datatype Floats.
class Float_node
def initialize(i, negative = nil)
@number = i
@negative = negative
end
def eval()
if @negative
@number * -1
else
@number
end
end
end
#Node for datatype Strings.
class String_node
def initialize(s)
@string = s
end
def eval()
@string
end
end
#Used for comments
class Empty_node
def eval()
end
end