-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.lua
1847 lines (1714 loc) · 44.3 KB
/
parser.lua
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local Parser = {}
Parser.__index = Parser
Tokenizer = require("plugins.easyLanguageServer.lua.tokenizer")
local function print_node(node, indent)
indent = indent or 0
local indent_str = string.rep(" ", indent)
local keys = {}
for k in pairs(node) do
table.insert(keys, k)
end
table.sort(keys)
for _, k in ipairs(keys) do
local v = node[k]
if type(v) == "table" then
print(indent_str .. k .. ":")
print_node(v, indent + 1)
else
print(indent_str .. k .. ": " .. tostring(v))
end
end
end
local PRECEDENCE = {
["or"] = 1,
["and"] = 2,
["=="] = 3,
["~="] = 3,
["<"] = 3,
[">"] = 3,
["<="] = 3,
[">="] = 3,
["|"] = 4,
["~"] = 5,
["&"] = 6,
["+"] = 7,
["-"] = 7,
["*"] = 8,
["/"] = 8,
["#"] = 8,
["%"] = 8,
["<<"] = 9,
[">>"] = 9,
[".."] = 10,
["^"] = 11,
["not"] = 12,
}
function Parser:new(tokens)
for i = #tokens, 1, -1 do
if tokens[i].type == Tokenizer.TOKEN_TYPES.LINE_BREAK then
table.remove(tokens, i)
end
end
local instance = {
tokens = tokens,
position = 1,
}
setmetatable(instance, Parser)
return instance
end
function Parser:loadFile(filename)
local success, msg = pcall(function()
local code = fs.open(filename, "r").readAll()
local tokens = Tokenizer.tokenize(code)
local parser = Parser:new(tokens)
local ast = parser:parse()
return ast
end)
return success, msg
end
function Parser:getIdentifiersAsList(filename)
local identifiers = {}
local code = fs.open(filename, "r").readAll()
if code then
local tokens = Tokenizer.tokenize(code)
local parser = Parser:new(tokens)
local ast = parser:parse()
local function traverse(node)
if node.type == "identifier" then
table.insert(identifiers, node.value)
end
for _, v in pairs(node) do
if type(v) == "table" then
traverse(v)
end
end
end
traverse(ast)
end
return identifiers
end
function Parser:require(expected_type, expected_value)
local token = self:current_token()
if expected_type and token.type ~= expected_type then
error(
"Expected token type " .. expected_type .. ", but found " .. token.type .. " at position " .. self.position
)
end
if expected_value and token.value ~= expected_value then
error(
"Expected token value '"
.. expected_value
.. "', but found '"
.. token.value
.. "' at position "
.. self.position
)
end
self:advance()
return token
end
function Parser:advance()
self.position = self.position + 1
return self.tokens[self.position]
end
function Parser:peek(n)
n = n or 1
local peek_position = self.position + n
if peek_position <= #self.tokens then
return self.tokens[peek_position]
else
return nil
end
end
function Parser:current_token()
return self.tokens[self.position]
end
function Parser:parse()
return self:parse_block()
end
function Parser:parse_identifier()
local token = self:current_token()
if token.type ~= Tokenizer.TOKEN_TYPES.IDENTIFIER then
error("Expected identifier at position " .. self.position .. ", found: " .. token.value)
end
self:advance()
return { type = "identifier", value = token.value, position = token.position }
end
function Parser:parse_table()
local table_elements = {}
self:advance()
if self:current_token().type == Tokenizer.TOKEN_TYPES.VARARG then
self:advance()
if self:current_token().type ~= Tokenizer.TOKEN_TYPES.SEPARATOR or self:current_token().value ~= "}" then
error("Varargs (...) must be the only element in the table constructor")
end
self:advance()
local node = { type = "table", position = self.position, elements = { { key = nil, value = "..." } } }
return node
end
while self.position <= #self.tokens do
local token = self:current_token()
if token.type == Tokenizer.TOKEN_TYPES.SEPARATOR and token.value == "}" then
self:advance()
break
end
local key, value
if token.type == Tokenizer.TOKEN_TYPES.SEPARATOR and token.value == "[" then
self:advance()
key = self:parse_expression()
if self:current_token().type ~= Tokenizer.TOKEN_TYPES.SEPARATOR or self:current_token().value ~= "]" then
error("Expected ']' after table key expression at position " .. self.position)
end
self:advance()
if self:current_token().type ~= Tokenizer.TOKEN_TYPES.OPERATOR or self:current_token().value ~= "=" then
error("Expected '=' after table key at position " .. self.position)
end
self:advance()
value = self:parse_expression()
elseif
token.type == Tokenizer.TOKEN_TYPES.IDENTIFIER
or token.type == Tokenizer.TOKEN_TYPES.STRING
or token.type == Tokenizer.TOKEN_TYPES.NUMBER
or token.type == Tokenizer.TOKEN_TYPES.LITERAL
then
key = self:parse_primary_expression()
if self:current_token().type == Tokenizer.TOKEN_TYPES.OPERATOR and self:current_token().value == "=" then
self:advance()
value = self:parse_expression()
else
value = key
key = nil
end
elseif token.type == Tokenizer.TOKEN_TYPES.VARARG then
error("Varargs (...) can only be used as the sole element in a table constructor")
else
error("Invalid token in table constructor: " .. token.value)
end
table.insert(table_elements, { key = key, value = value })
if self:current_token().type == Tokenizer.TOKEN_TYPES.SEPARATOR and self:current_token().value == "," then
self:advance()
elseif self:current_token().type ~= Tokenizer.TOKEN_TYPES.SEPARATOR or self:current_token().value ~= "}" then
error("Expected ',' or '}' after table element at position " .. self.position)
end
end
local node = { type = "table", elements = table_elements, position = self.position }
return node
end
function Parser:parse_function_call(fn)
self:advance()
local args = self:parse_expression_list()
self:require("separator", ")")
local node = {
type = "function_call",
fn = fn,
args = args,
position = self.position,
}
return node
end
function Parser:parse_literal()
local token = self:current_token()
local node
if
token.type == Tokenizer.TOKEN_TYPES.NUMBER
or token.type == Tokenizer.TOKEN_TYPES.STRING
or token.type == Tokenizer.TOKEN_TYPES.LITERAL
then
node = { type = "literal", value = token.value, position = token.position }
self:advance()
elseif token.type == Tokenizer.TOKEN_TYPES.IDENTIFIER then
node = self:parse_identifier()
elseif token.type == Tokenizer.TOKEN_TYPES.SEPARATOR and token.value == "{" then
node = self:parse_table()
else
error("Unexpected token in literal: " .. token.value .. " at position " .. self.position)
end
return node
end
function Parser:is_unary_operator(token)
return token.type == Tokenizer.TOKEN_TYPES.OPERATOR
and (token.value == "-" or token.value == "not" or token.value == "#" or token.value == "~")
end
function Parser:parse_unary_expression()
local token = self:current_token()
if not self:is_unary_operator(token) then
error("Expected unary operator at position " .. self.position)
end
self:advance()
return {
type = "unary_expression",
operator = token.value,
operand = self:parse_primary_expression(),
position = self.position,
}
end
function Parser:parse_primary_expression()
local token = self:current_token()
local node
if self:is_unary_operator(token) then
self:advance()
local operand = self:parse_primary_expression()
node = { type = "unary_expression", operator = token.value, operand = operand, position = self.position }
elseif
token.type == Tokenizer.TOKEN_TYPES.NUMBER
or token.type == Tokenizer.TOKEN_TYPES.STRING
or token.type == Tokenizer.TOKEN_TYPES.LITERAL
then
self:advance()
node = { type = "literal", value = token.value, position = self.position }
elseif token.type == Tokenizer.TOKEN_TYPES.IDENTIFIER then
local name = token.value
self:advance()
node = { type = "identifier", value = name, position = self.position }
elseif token.type == Tokenizer.TOKEN_TYPES.SEPARATOR and token.value == "(" then
self:advance()
node = self:parse_expression()
self:require("separator", ")")
elseif token.type == Tokenizer.TOKEN_TYPES.SEPARATOR and token.value == "{" then
node = self:parse_table()
elseif token.type == Tokenizer.TOKEN_TYPES.KEYWORD and token.value == "function" then
self:advance()
self:require("separator", "(")
local args = self:parse_expression_list()
self:require("separator", ")")
local block = self:parse_block()
self:require("keyword", "end")
node = { type = "function", args = args, block = block, position = self.position }
else
node = self:parse_literal()
end
while true do
token = self:current_token()
if token == nil then
break
end
if token.type == Tokenizer.TOKEN_TYPES.SEPARATOR then
if token.value == "." then
self:advance()
local property = self:require("identifier").value
node = {
type = "access",
access_type = "property",
table = node,
property = property,
position = self.position,
}
elseif token.value == "[" then
self:advance()
local index = self:parse_expression()
self:require("separator", "]")
node = { type = "access", access_type = "index", table = node, index = index, position = self.position }
elseif token.value == ":" then
self:advance()
local method = self:require("identifier").value
self:require("separator", "(")
local args = self:parse_expression_list()
self:require("separator", ")")
node = { type = "method_call", method = method, object = node, args = args, position = self.position }
elseif token.value == "(" then
node = self:parse_function_call(node)
else
break
end
else
break
end
end
return node
end
function Parser:should_apply_operator(precedence, token)
if token.type == Tokenizer.TOKEN_TYPES.OPERATOR and PRECEDENCE[token.value] then
if self:is_unary_operator(token) then
return PRECEDENCE[token.value] > precedence
else
return PRECEDENCE[token.value] > precedence
end
else
return false
end
end
function Parser:parse_expression(precedence)
precedence = precedence or 0
local left = self:parse_primary_expression()
while true do
local token = self:current_token()
if token == nil then
break
end
if token.type == Tokenizer.TOKEN_TYPES.OPERATOR and self:should_apply_operator(precedence, token) then
self:advance()
local right = self:parse_expression(PRECEDENCE[token.value])
left = { type = "binary_expression", operator = token.value, left = left, right = right }
else
break
end
end
return left
end
function Parser:parse_expression_list()
local expressions = {}
while true do
local current_token = self:current_token()
if current_token.type == Tokenizer.TOKEN_TYPES.KEYWORD and not (current_token.value == "function") then
break
end
if current_token.type == Tokenizer.TOKEN_TYPES.SEPARATOR and current_token.value == ")" then
break
end
if current_token.type == "vararg" then
table.insert(expressions, { type = "vararg", value = "..." })
self:advance()
break
end
local expr = self:parse_expression()
table.insert(expressions, expr)
local next_token = self:current_token()
if next_token == nil then
break
end
if next_token.type == Tokenizer.TOKEN_TYPES.SEPARATOR and next_token.value == "," then
self:advance()
else
break
end
end
return expressions
end
function Parser:parse_assignment(is_local)
local exprs = self:parse_expression_list()
local current_token = self:current_token()
if not current_token or current_token.type ~= Tokenizer.TOKEN_TYPES.OPERATOR or current_token.value ~= "=" then
if #exprs > 1 then
error("Was expecting multiple assignments but did not find = operator at" .. self.position)
end
local node = { type = "primary_expression", value = exprs[1], position = self.position }
return node
end
self:require("operator", "=")
local expressions = self:parse_expression_list()
local node =
{ type = "assignment", left = exprs, expressions = expressions, is_local = is_local, position = self.position }
return node
end
function Parser:parse_function(is_local)
self:advance()
local name_node
if self:current_token().type == Tokenizer.TOKEN_TYPES.IDENTIFIER then
name_node = self:parse_primary_expression()
end
if not name_node and name_node.access ~= nil then
error("Expected function name at position " .. self.position)
end
local args = {}
if self:current_token().type == Tokenizer.TOKEN_TYPES.SEPARATOR and self:current_token().value == "(" then
self:advance()
args = self:parse_expression_list()
self:advance()
end
local body = self:parse_block()
if self:current_token().type ~= Tokenizer.TOKEN_TYPES.KEYWORD or self:current_token().value ~= "end" then
error("Expected 'end' to close function body at position " .. self.position)
end
self:advance()
return {
type = "function",
identifier = name_node,
args = args,
block = body,
is_local = is_local or false,
position = self.position,
}
end
function Parser:parse_control_structure(
start_token,
parse_condition,
end_token,
parse_body,
finish_token,
parse_post_body_condition
)
self:advance()
local condition = nil
if parse_condition and parse_condition ~= "" then
condition = parse_condition(self)
end
if end_token and end_token ~= "" and (self:current_token().value ~= end_token) then
error("Expected keyword after condition at position " .. self.position)
elseif end_token and end_token ~= "" then
self:advance()
end
local body = parse_body(self)
local current_token = self:current_token()
if current_token and finish_token then
if current_token.type ~= Tokenizer.TOKEN_TYPES.KEYWORD or current_token.value ~= finish_token then
error(
"Expected '" .. finish_token .. "' to close '" .. start_token .. "' block at position " .. self.position
)
end
elseif finish_token ~= nil and current_token == nil then
error(
"Expected '"
.. finish_token
.. "' to close '"
.. start_token
.. "' block at position "
.. self.position
.. " instead we reached the end of the file"
)
elseif finish_token == nil then
local node = {
type = start_token .. "_structure",
condition = condition,
structure = body,
position = self.position,
}
return node
end
self:advance()
if parse_post_body_condition then
condition = parse_post_body_condition(self)
end
local node = {
type = start_token .. "_structure",
condition = condition,
block = body,
position = self.position,
}
return node
end
function Parser:parse_if_statement(else_if)
local function parse_condition(parser)
return parser:parse_expression()
end
local function parse_body(parser)
local body = parser:parse_block()
local current_token = parser:current_token()
if current_token and current_token.type == Tokenizer.TOKEN_TYPES.KEYWORD then
if current_token.value == "else" then
parser:advance()
body["else"] = parser:parse_block()
elseif current_token.value == "elseif" then
body["elif"] = parser:parse_if_statement(true)
end
end
return body
end
local fin_token
local start_token
if else_if then
fin_token = nil
start_token = "elseif"
else
fin_token = "end"
start_token = "if"
end
return self:parse_control_structure(start_token, parse_condition, "then", parse_body, fin_token)
end
function Parser:parse_while_loop()
local while_loop = self:parse_control_structure(
"while",
function(parser)
return parser:parse_expression()
end,
"do",
function(parser)
return parser:parse_block()
end,
"end"
)
return while_loop
end
function Parser:parse_do_end()
return self:parse_control_structure("do", nil, "", function(parser)
return parser:parse_block()
end, "end")
end
function Parser:parse_repeat_until()
return self:parse_control_structure(
"repeat",
nil,
nil,
function(parser)
return parser:parse_block()
end,
"until",
function(parser)
return parser:parse_expression()
end
)
end
function Parser:parse_for_loop()
local parse_init = function(parser)
local loop_info = {}
local in_present = false
local i = 1
while true do
local token = parser:peek(i)
if token.type == Tokenizer.TOKEN_TYPES.KEYWORD and token.value == "in" then
in_present = true
break
elseif token.type == Tokenizer.TOKEN_TYPES.KEYWORD and token.value == "do" then
break
end
i = i + 1
end
if in_present then
local expressions = parser:parse_expression_list()
parser:advance()
loop_info.type = "generic"
loop_info.left = expressions
loop_info.iterator = parser:parse_expression_list()
else
local assignment = parser:parse_assignment(true)
local start_expr = assignment.expressions[1]
local stop_expr = assignment.expressions[2]
local step_expr = nil
if assignment.type ~= "assignment" then
error("Expected assignment in numeric for loop")
end
if #assignment.expressions > 2 then
step_expr = assignment.expressions[3]
end
loop_info.type = "numeric"
loop_info.start_left = assignment.left
loop_info.start_expr = start_expr
loop_info.stop_expr = stop_expr
loop_info.step_expr = step_expr
end
return loop_info
end
local for_loop = self:parse_control_structure(
"for",
parse_init,
"do",
function(parser)
return parser:parse_block()
end,
"end"
)
return for_loop
end
function Parser:parse_goto()
self:advance()
local token = self:current_token()
if token.type ~= Tokenizer.TOKEN_TYPES.IDENTIFIER then
return { type = "error", message = "Expected label name after 'goto'" }
end
local node = {
type = "goto",
label = token.value,
position = token.position,
}
self:advance()
return node
end
function Parser:parse_label()
local label_start = self:current_token()
if label_start.type ~= Tokenizer.TOKEN_TYPES.LABEL then
error("Expected '::' at position " .. self.position)
end
self:advance()
local token = self:current_token()
if token.type ~= Tokenizer.TOKEN_TYPES.IDENTIFIER then
error("Expected label name at position " .. self.position)
end
local node = {
type = "label",
value = token.value,
position = token.position,
}
self:advance()
local label_end = self:current_token()
if label_end.type ~= Tokenizer.TOKEN_TYPES.LABEL then
error(
"Expected '::' to close label at position "
.. self.position
.. ", found "
.. label_end.type
.. " "
.. label_end.value
.. " instead"
)
end
self:advance()
return node
end
function Parser:parse_return()
self:advance()
local expressions = {}
while self.position <= #self.tokens do
local token = self:current_token()
if token.type == Tokenizer.TOKEN_TYPES.SEPARATOR and token.value == "," then
self:advance()
elseif token.type == Tokenizer.TOKEN_TYPES.SEPARATOR and token.value == ")" then
break
else
table.insert(expressions, self:parse_expression())
end
token = self:current_token()
if
not token
or token.type == Tokenizer.TOKEN_TYPES.KEYWORD
and (token.value == "end" or token.value == "else" or token.value == "elseif" or token.value == "until")
then
break
elseif token.type == Tokenizer.TOKEN_TYPES.SEPARATOR and token.value == "," then
self:advance()
else
break
end
end
return { type = "return", expressions = expressions, position = self.position }
end
function Parser:parse_break()
self:advance()
return { type = "break", position = self.position }
end
function Parser:parse_statement(is_local)
local token = self:current_token()
if token.type == Tokenizer.TOKEN_TYPES.KEYWORD then
if token.value == "if" then
return self:parse_if_statement()
elseif token.value == "while" then
return self:parse_while_loop()
elseif token.value == "do" then
return self:parse_do_end()
elseif token.value == "repeat" then
return self:parse_repeat_until()
elseif token.value == "for" then
return self:parse_for_loop()
elseif token.value == "function" then
return self:parse_function(is_local)
elseif token.value == "goto" then
return self:parse_goto()
elseif token.value == "return" then
return self:parse_return()
elseif token.value == "break" then
return self:parse_break()
elseif token.value == "local" then
self:advance()
return self:parse_statement(true)
end
elseif token.type == Tokenizer.TOKEN_TYPES.IDENTIFIER then
local next_token = self:peek()
if not next_token then
error("Unexpected end of input after identifier at position " .. self.position)
end
if next_token and next_token.type == Tokenizer.TOKEN_TYPES.OPERATOR then
return self:parse_assignment(is_local)
elseif next_token and next_token.type == Tokenizer.TOKEN_TYPES.SEPARATOR then
if next_token.value ~= "{" and next_token.value ~= "(" then
return self:parse_assignment(is_local)
else
return self:parse_primary_expression()
end
else
error("Unexpected token: " .. token.value .. "after identifier at position " .. self.position)
end
elseif token.type == Tokenizer.TOKEN_TYPES.LABEL then
return self:parse_label()
else
error("Unexpected token '" .. token.value .. "' at position " .. self.position)
end
end
function Parser:parse_block()
local statements = {}
while self.position <= #self.tokens do
local token = self:current_token()
if
token.type == Tokenizer.TOKEN_TYPES.KEYWORD
and (token.value == "end" or token.value == "else" or token.value == "elseif" or token.value == "until")
then
break
end
local statement = self:parse_statement()
if statement then
table.insert(statements, statement)
else
error("Invalid statement at position " .. self.position)
end
end
return { type = "block", body = statements }
end
return Parser
-- local code = [=[
-- -- -- ===========================
-- -- -- Variable Declarations and Assignments
-- -- -- ===========================
-- x = 10
-- local y = 20
-- local t = {key1 = "value1", key2 = "value2"}
-- t.key1 = "new value"
-- t["key1"] = "new value"
-- t[1] = "value3"
-- t[x] = value4
-- x = x + 1
-- local x,y = 1,2
-- x = x * y + 10 / 2
-- x = 10 / 2 + x * y
-- x = 10 / (2 + x) * y
-- --
-- -- -- ===========================
-- -- -- Control Structures
-- -- -- ===========================
-- --
-- while x < 5 do
-- print(x)
-- x = x + 1
-- end
--
-- repeat
-- print("This will print once")
-- until false
--
-- repeat a = a + 1 until a > 10
--
-- if true then
-- return 3
-- end
--
-- if a < 5 then
-- print("a is less than 5")
-- elseif a == 5 then
-- print("a is equal to 5")
-- end
--
-- if a < 69 then
-- print("a is less than 69")
-- else
-- print("a is greater than 69")
-- end
--
-- if a > 420 then
-- print("a is greater than 420")
-- elseif a < 420 then
-- print("a is less than 420")
-- x = 10
-- if x == 10 then
-- print("x is 10")
-- end
-- else
-- print("a is equal to 420")
-- end
--
-- --Testing for loop
-- for stuff in pairs(t) do
-- print(stuff)
-- end
-- for stuff, value in pairs(t) do
-- print(stuff, value)
-- end
--
-- -- Testing numeric for loop
-- for i = 1, 10 do
-- print(i)
-- end
--
-- --Testing numeric for loop with step
-- for i = 1, 10, 2 do
-- print(i)
-- end
--
-- -- Using do-end block for local scoping
-- do
-- local x = 10
-- print(x)
-- end
--
-- -- Goto statement and label
-- goto test_label
-- print("This will be skipped")
-- ::test_label::
-- print("This will be printed")
--
-- -- ===========================
-- -- Functions and Function Calls
-- -- ===========================
--
-- function test_function(a, b)
-- return a + b
-- end
-- --Testing functions with variable arguments
-- function varargs_example(...)
-- args = {...}
-- for i, v in ipairs(args) do
-- print(i, v)
-- end
-- end
-- varargs_example(1, 2, 3, "hello")
-- -- Anonymous functions
-- local anonymous = function()
-- print("WHAT IS THIS????!")
-- end
-- local anonwithargs = function(a, b)
-- print(a + b)
-- end
-- -- Nested functions
-- function test()
-- print("Hello, World!")
-- function test_two()
-- print("Hello again!")
-- end
-- local function test_three()
-- print("Hello for the third time!")
-- end
-- end
--
-- -- -- ===========================
-- -- -- Table Operations
-- -- -- ===========================
--
-- -- Complex table with various key types
-- local complex_table = {
-- [1] = "one",
-- ["key"] = "value",
-- [x + 1] = "expr"
-- }
-- -- Function setting a metatable
-- setmetatable(complex_table, {