This repository has been archived by the owner on Jul 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodiLogic.rb
630 lines (542 loc) · 11.2 KB
/
CodiLogic.rb
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
623
624
625
626
627
628
629
630
require 'rubygems'
@@html = nil
class Begin
attr_reader :html, :css
def initialize()
@htmlname = "Result.html"
@@html = File.new("Result.html", "w")
IO.copy_stream('template.html', @htmlname)
return @@html
end
def html
return @htmlname
end
end
@@html = Begin.new().html()
class VariableList
attr_reader :list
def initialize()
@list = [{}]
end
def add(var)
@list[@@scope_counter][var.name] = var.value
@list
end
def increase()
if @@scope_counter == 0
@list << {}
@@scope_counter = @list.length - 1
end
end
def clear()
@list[@@scope_counter].clear()
end
def findValue(var)
@list.each do |varObj|
if varObj.name == var
return varObj.value
end
end
return false
end
def findVar(var)
@list.each do |varObj|
if varObj.name == var
return varObj
end
end
return false
end
end
@@variableList = VariableList.new()
@@scope_counter = 0
class MultVar
def initialize(var, multvar = nil)
@var = var
@varlist = multvar if multvar
@variables = []
end
def eval()
@varlist.eval if @varlist
@var.eval
end
def variables()
@variables.clear
if @varlist.class == MultVar
@variables += @varlist.variables
else
@variables << @varlist
end
if @var.class == MultVar
@variables += @var.variables
else
@variables << @var
end
@variables.reverse
end
end
class Var
attr_reader :value, :name, :type
def initialize(name)
@name = name
@value = nil
@type = nil
end
def variables
return [self]
end
def Var.define(name)
var = @@variableList.list[@@scope_counter][name]
if var
return var
else
Var.new(name)
end
end
def update(value)
@value = value
@type= value.class
end
def eval()
return self
end
def print
return @value
end
end
class Url < Var
def update(value)
if value.class == String
@type = "pre_def_var"
@value = 'href= '+ value
elsif value.class == List
@type = "pre_def_var"
@value = value
end
end
def Url.define(name)
url = @@variableList.list[@@scope_counter][name]
if url
return url
else
Url.new(name)
end
end
end
class Text < Var
def update(value)
@type = "text"
value.gsub!(/\"/, "")
@value = value
end
end
class Create
attr_reader :value
def initialize(obj, stmt_list)
@value = obj
@obj = obj
@stmt_list = stmt_list
end
def eval
scope_before = @@scope_counter
@@variableList.increase
scope_after = @@scope_counter
@stmt_list.eval
@obj.update(@stmt_list)
@obj.eval()
@@scope_counter = 0 if scope_before != scope_after
@stmt_list
end
def variables
@stmt_list.variables
end
end
class Stmt_list
attr_reader :variables
def initialize(obj, stmt_list = nil)
@stmt = obj
@stmt_list = stmt_list
@variables = []
end
def eval()
@stmt.eval() unless @stmt.class == Function
stmtVariables = retrieve_values(@stmt.variables)
if @stmt_list
@stmt_list.eval unless @stmt_list.class == Function
end
true
end
def retrieve_values(stmt_list)
variables = []
if stmt_list.class == Array
stmt_list.each { |x| variables << x }
else
variables << stmt_list
end
return variables
end
def reset_values()
@variables.clear
end
end
class Stmt
attr_reader :variables
def initialize(obj)
@stmt = obj
@variables = []
end
def eval()
@stmt.eval() unless @stmt.class == Function
retrieve_values(@stmt.variables)
true
end
def retrieve_values(stmt_list)
variables = []
variables << stmt_list
return variables
end
def reset_values()
@variables.clear
end
end
class Assignment
attr_reader :value
def initialize(var, value)
@var = var
@value = value
end
def eval()
if @value.class == Var
@var.update(@@variableList.list[@@scope_counter][@value.name])
elsif @value.class == String
@var.update(@value)
elsif @value.class == Operation
@var.update(@value.eval)
else
@var.update(@value)
end
@@variableList.add(@var)
@var
end
def variables
return [@var]
end
end
class Predefined_object
def initialize()
@content = ""
@htmlpage = File.open(@@html)
@css = 'style='
@html = ""
@stmt_list = nil
end
def add_css(value)
@css += value
end
def add_html(value)
@html += value
end
def add_content(value)
@content += value
end
def eval()
l = @@variableList.list[@@scope_counter]
l.each do |key, value|
if @attributes.has_key?(key)
add_css(value) if @attributes[key] == "css"
add_html(value) if @attributes[key] == "html"
add_content(value) if @attributes[key] == "content"
end
end
build
true
end
def build()
final = @pre_tag.insert(-2, +" " + @html+ " " + @css) + @content + @end_tag
fil = File.open(@@html)
read = fil.read
topSpan = findWriteLoc(read)
temp = (read).insert(topSpan, "\n"+final)
File.write(@@html, temp)
fil.close
end
def update(stmt_list)
@stmt_list = stmt_list
end
def findWriteLoc(string)
body_index = string.index("<body>")
if body_index
return body_index + 6
else
raise Exception.new("Invalid Template file, make sure <body> is included")
end
end
end
class Link < Predefined_object
def initialize
super
@attributes = {"url" => "html", "position" => "css", "text" => "content"}
@pre_tag = "<a >"
@end_tag = "</a>"
end
def clone
Link.new()
end
end
class Image < Predefined_object
def initialize
super
@attributes = {"path" => "html", "position" => "css"}
@pre_tag = "<img >"
@end_tag = "</img>"
end
def clone
Image.new()
end
end
class Paragraph < Predefined_object
def initialize
super
@attributes = {"text" => "content", "position" => "css", "color" => "css"}
@pre_tag = "<p >"
@end_tag = "</p>"
end
def clone
Paragraph.new
end
end
class Title < Predefined_object
def initialize
super
@attributes = {"text" => "content", "position" => "css", "color" => "css"}
@pre_tag = "<h1 >"
@end_tag = "</h1>"
end
def clone
Title.new
end
end
class Path < Var
def update(value)
@type = "pre_def_var"
@value = "src=" + value
end
def Path.define(name)
var = @@variableList.list[@@scope_counter][name]
if var
return var
else
Path.new(name)
end
end
end
class If
def initialize(expr, stmt_list)
@expr = expr
@stmt_list = stmt_list
@comparison_return = false
end
def eval()
@comparison_return = @expr.eval
if @comparison_return
@stmt_list.eval
else
@expr
end
end
def variables
if @comparison_return
@stmt_list.variables
else
[]
end
end
end
class Comparison
def initialize(var, comp_oper, value)
@var = var
@comp_oper = comp_oper
@value = value
end
def eval
var = @@variableList.list[@@scope_counter][@var.name]
if var
if @comp_oper == "=="
return var == @value
elsif @comp_oper == "!="
return var != @value
elsif @comp_oper == ">="
return var >= @value
elsif @comp_oper == ">"
return var > @value
elsif @comp_oper == "<="
return var <= @value
elsif @comp_oper == "<"
return var < @value
end
end
else
raise Exception.new("Undeclared Variable #{@var.name}")
end
end
class ExtComparison
def initialize(comp, comp_oper, comp2)
@comp_one = comp2
@extender = comp_oper
@comp_list = comp
end
def eval
if @extender == "or"
expr1 = @comp_one.eval
expr2 = @comp_list.eval
bool = expr1 || expr2
return bool
end
if @extender == "and"
expr1 = @comp_one.eval
expr2 = @comp_list.eval
bool = expr1 && expr2
return bool
end
end
end
class Create_Multiple
def initialize(number, obj, var, stmt_list)
@obj = obj
@times = number
@obj_list = []
@var = var
@stmt_list = stmt_list
end
def eval
scope_before = @@scope_counter
@@variableList.increase
scope_after = @@scope_counter
obj_amount = @times
obj_amount.times do
@obj_list << @obj.clone
end
rawStmt = @stmt_list
@obj_list.each_with_index do |obj, index|
@stmt_list = rawStmt
variable = Assignment.new(@var, index+1)
variable.eval
@stmt_list.eval
obj.update(@stmt_list)
obj.eval()
end
@@scope_counter = 0 if scope_before != scope_after
return true
end
def variables
@stmt_list.variables
end
end
class Position < Var
def update(value)
@type = "css"
@value = "position:absolute;#{value}"
end
end
class Color < Var
def update(value)
@type = "css"
value.gsub!(/\"/, "")
@value = "color:#{value};"
end
end
class Function
attr_reader :name, :status, :variables, :value
def initialize(name, stmt_list, varlist = nil)
@name = name
@stmt_list = stmt_list
@variables = varlist
@value = self
@@variableList.add(self)
true
end
def variables
return @variables.variables if @variables
return [] if not @variables
end
def eval
@stmt_list.eval
end
end
class FunctionCall
def initialize(name, varlist = nil)
@name = name
@varlist = varlist if varlist
end
def eval
l = @@variableList.list[@@scope_counter]
@@variableList.increase
l.clone.each do |key, value|
if key == @name
function_variables = l[key]
if (@varlist && function_variables) && @varlist.variables.length == function_variables.variables.length
@varlist.variables.each_with_index do |x, i|
Assignment.new(function_variables.variables[i], x).eval
end
elsif not @varlist
elsif @varlist.variables.length != function_variables.variables.length
raise Exception.new("Wrong number of arguments for function #{function_variables.name}")
end
return function_variables.eval
end
end
end
end
class Operation
attr_reader :variables
def initialize(operation, math_operation, value)
@value, @math_operation, @operation = value, math_operation, operation
@variables = []
end
def variables
@variables << @value
if @math_operation
@variables << @math_operation
end
if @operation
@variables += @operation.variables if @operation.class == Operation
@variables << @operation if @operation.class == Fixnum
end
return @variables
end
def eval
variables()
@math_operation.set_values(@operation.eval, @value) if @operation.class == Operation
@math_operation.set_values(@operation, @value) if @operation.class == Fixnum || @operation.class == Float
@math_operation.eval
end
end
class MathExpr
def initialize
@l_h = nil
@r_h = nil
end
def set_values(l_h, r_h)
@l_h, @r_h = l_h, r_h
end
end
class Multiplier < MathExpr
def eval
return @l_h * @r_h
end
end
class Divider < MathExpr
def eval
return @l_h / @r_h
end
end
class Adder < MathExpr
def eval
return @l_h + @r_h
end
end
class Subtractor < MathExpr
def eval
return @l_h - @r_h
end
end