Repository navigation
Expand file tree
/
Copy pathreformat_lua
More file actions
executable file
·4784 lines (4779 loc) · 136 KB
/
Copy pathreformat_lua
File metadata and controls
executable file
·4784 lines (4779 loc) · 136 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
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
#!/usr/local/bin/lua
package.preload['reformat'] =
function(...)
require('workshop.base')
local usage_text =
[[
Reformat Lua 5.5 code.
Usage: f_in f_out
┬ ┬ ┬
│ │ │
└── [options] ─┘
<options>:
--indent=<str> Use given string as indent chunk.
--right-margin=<int> Right margin.
-- Martin, 2026-10
]]
local get_params
do
local CommandlineProcessorClass =
request('!.mechs.command_line_processor.interface')
get_params =
function()
CommandlineProcessor = new(CommandlineProcessorClass)
CommandlineProcessor.allowed_params =
{
{ name = 'input_file_name', type = 'string' },
{ name = 'output_file_name', type = 'string' },
{ name = 'indent', type = 'key_str' },
{ name = 'right-margin', type = 'key_int' },
}
local Params = CommandlineProcessor:run(_G.arg)
return
{
input_file_name = Params.input_file_name,
output_file_name = Params.output_file_name,
right_margin = Params['right-margin'],
indent_chunk = Params.indent,
}
end
end
local file_to_str = request('!.convert.file_to_str')
local file_from_str = request('!.convert.file_from_str')
local str_format = string.format
local reformat = request('!.lua.code.reformat')
local DefaultOptions = { indent_chunk = ' ', right_margin = 72 }
local Params = get_params()
local input_file_name = Params.input_file_name
local output_file_name = Params.output_file_name
local indent_chunk = Params.indent_chunk or DefaultOptions.indent_chunk
local right_margin = Params.right_margin or DefaultOptions.right_margin
if not (input_file_name and output_file_name) then
print(usage_text)
return
end
print('( Lua code reformatter')
local input_code_str = file_to_str(input_file_name)
print(str_format(' Read code from "%s".', input_file_name))
file_from_str(output_file_name, reformat(input_code_str, Options))
print(str_format(' Formatted code to "%s".', output_file_name))
print(')')
end
package.preload['workshop.base'] =
function(...)
local set_base_prefix
local split_name
local get_base_prefix
local request
local stack_init
local stack_add
local stack_remove
do
local str_match = string.match
local str_find = string.find
local str_sub = string.sub
local tbl_pack = table.pack
local tbl_unpack = table.unpack
local require = require
local empty = ''
local stack_get
do
local Names
local depth
stack_init =
function()
Names = { }
depth = 1
end
stack_get =
function()
return Names[depth]
end
stack_add =
function(prefix, name)
depth = depth + 1
Names[depth] = { prefix = prefix, name = name }
end
stack_remove =
function()
depth = depth - 1
end
end
local get_caller_prefix =
function()
local NameRec = stack_get()
if not NameRec then
return empty
end
return NameRec.prefix
end
do
local prefix_name_capture = '^(.+%.)([^%.]+)$'
split_name =
function(qualified_name)
local prefix, name = str_match(qualified_name, prefix_name_capture)
if not prefix then
prefix = empty
if str_find(qualified_name, '%.') then
name = empty
else
name = qualified_name
end
end
return prefix, name
end
end
local apply_rel_prefix
do
local uplevel_capture = '(.+%.)[^%.]-%.$'
apply_rel_prefix =
function(base_prefix, rel_prefix)
while (str_sub(rel_prefix, 1, 2) == '^.') do
if (base_prefix == empty) then
error("Link is outside of caller's prefix.")
end
base_prefix = str_match(base_prefix, uplevel_capture) or empty
rel_prefix = str_sub(rel_prefix, 3)
end
return base_prefix .. rel_prefix
end
end
do
local base_prefix
set_base_prefix =
function(arg_base_prefix)
base_prefix = arg_base_prefix
end
get_base_prefix =
function()
return base_prefix
end
end
local get_require_name =
function(qualified_name)
local caller_prefix
local is_absolute_name = (str_sub(qualified_name, 1, 2) == '!.')
if is_absolute_name then
qualified_name = str_sub(qualified_name, 3)
caller_prefix = get_base_prefix()
else
caller_prefix = get_caller_prefix()
end
local prefix, name = split_name(qualified_name)
prefix = apply_rel_prefix(caller_prefix, prefix)
return prefix .. name
end
request =
function(qualified_name)
local require_name = get_require_name(qualified_name)
stack_add(split_name(require_name))
local Results = tbl_pack(require(require_name))
stack_remove()
return tbl_unpack(Results)
end
end
do
local our_require_name = (...)
set_base_prefix(split_name(our_require_name))
_G.request = request
_G.get_base_prefix = get_base_prefix
stack_init()
stack_add(empty, our_require_name)
request('!.system.install_is_functions')()
request('!.system.install_assert_functions')()
_G.new = request('!.table.new')
stack_remove()
end
end
package.preload['workshop.concepts.Indent'] =
function(...)
local get_indent_chunk =
function(Me)
return Me[1]
end
local set_indent_chunk =
function(Me, str)
Me[1] = str
end
local get_indent_level =
function(Me)
return Me[2]:GetValue()
end
local set_indent_level =
function(Me, level)
Me[2]:SetValue(level)
end
local get_max_indent_level =
function(Me)
return Me[2]:GetMaxValue()
end
local set_max_indent_level =
function(Me, level)
Me[2]:SetMaxValue(level)
end
local to_string
do
local str_rep = string.rep
to_string =
function(Me)
return str_rep(Me[1], Me[2]:GetValue())
end
end
local inc =
function(Me)
set_indent_level(Me, get_indent_level(Me) + 1)
end
local dec =
function(Me)
set_indent_level(Me, get_indent_level(Me) - 1)
end
local Interface
local create
do
local RangePointClass = request('!.concepts.RangePoint')
local min_indent = 0
local max_indent = 60
local indent_chunk = ' '
local attach_methods = request('!.table.attach_methods')
create =
function()
local RangePoint = RangePointClass.create()
RangePoint:SetMinValue(min_indent)
RangePoint:SetMaxValue(max_indent)
RangePoint:SetValue(min_indent)
local Core = { indent_chunk, RangePoint }
attach_methods(Core, Interface)
return Core
end
end
Interface =
{
create = create,
GetIndentChunk = get_indent_chunk,
SetIndentChunk = set_indent_chunk,
GetIndentLevel = get_indent_level,
SetIndentLevel = set_indent_level,
GetMaxIndentLevel = get_max_indent_level,
SetMaxIndentLevel = set_max_indent_level,
ToString = to_string,
Inc = inc,
Dec = dec,
}
return Interface
end
package.preload['workshop.concepts.RangePoint'] =
function(...)
local get_min_value =
function(Me)
return Me[2]
end
local set_min_value =
function(Me, val)
Me[2] = val
end
local get_max_value =
function(Me)
return Me[3]
end
local set_max_value =
function(Me, val)
Me[3] = val
end
local get_value
local set_value
do
local min = math.min
local max = math.max
get_value =
function(Me)
return min(max(Me[1], Me[2]), Me[3])
end
set_value =
function(Me, value)
Me[1] = min(max(value, Me[2]), Me[3])
end
end
local Interface
local create
do
local attach_methods = request('!.table.attach_methods')
create =
function()
local Core = { 0, 0, 1 }
attach_methods(Core, Interface)
return Core
end
end
Interface =
{
create = create,
GetMinValue = get_min_value,
SetMinValue = set_min_value,
GetMaxValue = get_max_value,
SetMaxValue = set_max_value,
GetValue = get_value,
SetValue = set_value,
}
return Interface
end
package.preload['workshop.concepts.ShellCommand'] =
function(...)
local to_string
do
local quote = request('!.concepts.shell.quote')
local add_to_list = request('!.concepts.list.add_item')
local glue_words = request('!.concepts.words.to_string')
to_string =
function(Me)
local command = Me[1]
local Args = Me[2]
local Words = { }
add_to_list(Words, quote(command))
for _, arg in ipairs(Args) do
add_to_list(Words, quote(arg))
end
return glue_words(Words)
end
end
local execute
do
local execute_shell_command = request('!.concepts.shell.execute')
execute =
function(Me)
return execute_shell_command(to_string(Me))
end
end
local Interface
do
local create
do
local attach_methods = request('!.table.attach_methods')
create =
function(Core)
do
assert_table(Core)
assert(#Core == 2)
local command = Core[1]
local Args = Core[2]
assert_string(command)
assert_table(Args)
for _, arg in ipairs(Args) do
assert_string(arg)
end
end
attach_methods(Core, Interface)
return Core
end
end
Interface = { create = create, ToString = to_string, Execute = execute }
end
return Interface
end
package.preload['workshop.concepts.Ascii.Chars'] =
function(...)
local Chars
do
local Codes = request('Codes')
local str_char = string.char
Chars = { }
for name, code in pairs(Codes) do
Chars[name] = str_char(code)
end
end
return Chars
end
package.preload['workshop.concepts.Ascii.Codes'] =
function(...)
return
{
bell = 7,
backspace = 8,
tab = 9,
newline = 10,
vertical_tab = 11,
form_feed = 12,
carriage_return = 13,
space = 32,
delete = 127,
plus = 43,
minus = 45,
asterisk = 42,
slash = 47,
less_than = 60,
equals = 61,
greater_than = 62,
dot = 46,
comma = 44,
colon = 58,
semicolon = 59,
single_quote = 39,
double_quote = 34,
backtick = 96,
backslash = 92,
number_sign = 35,
question_mark = 63,
bang = 33,
percent = 37,
ampersand = 38,
dollar_sign = 36,
at_sign = 64,
caret = 94,
underscore = 95,
pipe = 124,
tilde = 126,
opening_paren = 40,
closing_paren = 41,
opening_bracket = 91,
closing_bracket = 93,
opening_brace = 123,
closing_brace = 125,
}
end
package.preload['workshop.concepts.list.add_item'] =
function(...)
local tbl_insert = table.insert
return
function(OurList, item)
tbl_insert(OurList, item)
end
end
package.preload['workshop.concepts.list.add_list'] =
function(...)
local tbl_move = table.move
return
function(OurList, AnotherList)
assert(OurList ~= AnotherList)
tbl_move(AnotherList, 1, #AnotherList, #OurList + 1, OurList)
end
end
package.preload['workshop.concepts.list.select_seg'] =
function(...)
local is_natural = request('!.number.is_natural')
local tbl_move = table.move
return
function(List, start_idx, len)
assert(is_natural(start_idx))
assert(is_natural(len))
local stop_idx = start_idx + len - 1
local Result = { }
tbl_move(List, start_idx, stop_idx, 1, Result)
return Result
end
end
package.preload['workshop.concepts.list.to_string'] =
function(...)
local tbl_concat = table.concat
return
function(List, separator_str)
assert_table(List)
separator_str = separator_str or ''
assert_string(separator_str)
return tbl_concat(List, separator_str)
end
end
package.preload['workshop.concepts.lua.Keywords'] =
function(...)
return
{
'nil',
'true',
'false',
'not',
'and',
'or',
'local',
'global',
'do',
'end',
'goto',
'if',
'then',
'elseif',
'else',
'while',
'repeat',
'until',
'for',
'in',
'break',
'function',
'return',
}
end
package.preload['workshop.concepts.lua.NumberTypeNames'] =
function(...)
return { 'integer', 'float' }
end
package.preload['workshop.concepts.lua.TokensAccessor'] =
function(...)
local match_string =
function(Me, str)
if (Me[2] > Me[3]) then
return false
end
if (Me[1][Me[2]][1] ~= str) then
return false
end
Me[2] = Me[2] + 1
return true
end
local get_position =
function(Me)
return Me[2]
end
local set_position =
function(Me, position)
Me[2] = position
end
local get_segment =
function(Me, start, len)
if (len ~= 1) then
return
end
if (start > Me[3]) then
return
end
return Me[1][start][1]
end
local read_type =
function(Me, type)
if (Me[2] > Me[3]) then
return false
end
if (Me[1][Me[2]].type ~= type) then
return false
end
Me[2] = Me[2] + 1
return true
end
local is_name =
function(Me)
return read_type(Me, 'name')
end
local is_string =
function(Me)
return read_type(Me, 'string')
end
local is_number =
function(Me)
return read_type(Me, 'number')
end
local Interface
local create
do
local attach_methods = request('!.table.attach_methods')
create =
function(TokensList)
local Me = { TokensList, 1, #TokensList }
attach_methods(Me, Interface)
return Me
end
end
Interface =
{
create = create,
match_string = match_string,
get_position = get_position,
set_position = set_position,
get_segment = get_segment,
IsName = is_name,
IsString = is_string,
IsNumber = is_number,
}
return Interface
end
package.preload['workshop.concepts.lua.TokensSyntax'] =
function(...)
local Delim = request('TokensSyntax.Delim')
local String = request('TokensSyntax.String')
local Number = request('TokensSyntax.Number')
local Name = request('TokensSyntax.Name')
local Syntel = request('TokensSyntax.Syntel')
local handy = request('!.mechs.processor.handy')
local opt_rep = handy.opt_rep
local cho = handy.cho
return
{
Delim,
opt_rep(
cho(
{ name = 'string', String },
{ name = 'number', Number },
{ name = 'name', Name },
{ name = 'syntel', Syntel }
),
Delim
),
}
end
package.preload['workshop.concepts.lua.TypeNames'] =
function(...)
return
{
'nil',
'boolean',
'number',
'string',
'function',
'thread',
'userdata',
'table',
}
end
package.preload['workshop.concepts.lua.run_formatter'] =
function(...)
local FormatterClass = request('formatter.interface')
return
function(FormatterAst, Options)
local Formatter = new(FormatterClass, Options)
Formatter.data_struc = FormatterAst
Formatter:init()
return Formatter:run()
end
end
package.preload['workshop.concepts.lua.syntax'] =
function(...)
local link = request('!.mechs.processor.link')
local optimize = request('!.mechs.processor.optimize')
local expression = request('syntax.expression')
local statements = request('syntax.statements')
local producers, consumers = { }, { }
link(expression, producers, consumers)
link(statements, producers, consumers)
assert(not next(consumers))
optimize(statements)
return statements
end
package.preload['workshop.concepts.lua.transform_ast'] =
function(...)
local Transformer = request('ast_transformer.interface')
return
function(SyntaxTree)
return Transformer:run(SyntaxTree)
end
end
package.preload['workshop.concepts.lua.TokensSyntax.Delim'] =
function(...)
local LongBrackets = request('Particles.LongBrackets')
local match_regexp = request('!.mechs.parser.handy').match_regexp
local handy = request('!.mechs.processor.handy')
local opt_rep = handy.opt_rep
local cho = handy.cho
local Comment = { '--', cho(LongBrackets, match_regexp('[^\n\r]*')) }
return { opt_rep(cho(match_regexp('[ \t\n\r]+'), Comment)) }
end
package.preload['workshop.concepts.lua.TokensSyntax.Name'] =
function(...)
local name_pattern = '^[_A-Za-z][_A-Za-z0-9]*'
local Keywords_Map
do
local Keywords = request('!.concepts.lua.Keywords')
local map_values = request('!.table.map_values')
Keywords_Map = map_values(Keywords)
end
return
function(Input)
local init_pos = Input:get_position()
if Input:match_regexp(name_pattern) then
local capture =
Input:get_segment(init_pos, Input:get_position() - init_pos)
if not Keywords_Map[capture] then
return true
else
Input:set_position(init_pos)
return false
end
end
end
end
package.preload['workshop.concepts.lua.TokensSyntax.Number'] =
function(...)
local handy = request('!.mechs.processor.handy')
local opt = handy.opt
local cho = handy.cho
local match_regexp = request('!.mechs.parser.handy').match_regexp
local Int_16 = match_regexp('%x+')
local HexNumber =
{
match_regexp('0[xX]'),
cho({ '.', Int_16 }, { Int_16, opt('.', opt(Int_16)) }),
opt(match_regexp('[pP][%+%-]?%d+')),
}
local Int_10 = match_regexp('%d+')
local DecNumber =
{
cho({ '.', Int_10 }, { Int_10, opt('.', opt(Int_10)) }),
opt(match_regexp('[eE][%+%-]?%d+')),
}
return cho(HexNumber, DecNumber)
end
package.preload['workshop.concepts.lua.TokensSyntax.String'] =
function(...)
local handy = request('!.mechs.processor.handy')
local opt_rep = handy.opt_rep
local cho = handy.cho
local match_regexp = request('!.mechs.parser.handy').match_regexp
local LongBrackets = request('Particles.LongBrackets')
return
{
cho(
LongBrackets,
{
'"',
opt_rep(cho(match_regexp([[[^%\%"]+]]), match_regexp([[%\.]]))),
'"',
},
{
"'",
opt_rep(cho(match_regexp([[[^%\%']+]]), match_regexp([[%\.]]))),
"'",
}
),
}
end
package.preload['workshop.concepts.lua.TokensSyntax.Syntel'] =
function(...)
local name_pattern = '^[_A-Za-z][_A-Za-z0-9]*'
local Keywords_Map
do
local Keywords = request('!.concepts.lua.Keywords')
local map_values = request('!.table.map_values')
Keywords_Map = map_values(Keywords)
end
local is_keyword =
function(Input)
local init_pos = Input:get_position()
if Input:match_regexp(name_pattern) then
local capture =
Input:get_segment(init_pos, Input:get_position() - init_pos)
if Keywords_Map[capture] then
return true
else
Input:set_position(init_pos)
return false
end
end
end
local match_regexp = request('!.mechs.parser.handy').match_regexp
local cho = request('!.mechs.processor.handy').cho
return
{
cho(
is_keyword,
'==',
'~=',
'<=',
'<<',
'>=',
'>>',
'...',
'..',
'//',
'::',
match_regexp('[%=%~%<%>%.%:%,%;%/%(%)%[%]%{%}%-%+%#%*%%%^%&%|]')
),
}
end
package.preload['workshop.concepts.lua.TokensSyntax.Particles.LongBrackets'] =
function(...)
local match_regexp = request('!.mechs.parser.handy').match_regexp
return match_regexp('%[(%=*)%[.-%]%1%]')
end
package.preload['workshop.concepts.lua.ast_transformer.align_nodes'] =
function(...)
return
function(Me, Parent, index)
local Node = Parent[index]
if not is_table(Node) then
return
end
for subnode_index in ipairs(Node) do
Me:align_nodes(Node, subnode_index)
end
if (#Node == 1) and is_string(Node[1]) then
Node.value = Node[1]
Node[1] = nil
end
if (Node.type == 'assign_or_call') then
if (Node[1].type == 'function_call') then
Node = Node[1]
else
Node.type = 'assignment'
end
elseif (Node.type == 'var_or_call') then
if (Node[#Node].type == 'func_args') then
Node.type = 'function_call'
else
Node.type = 'var_ref'
end
elseif (Node.type == 'local_statement') then
if (Node[1].type == 'named_function') then
Node[1].type = 'local_named_function'
else
Node[1].type = 'local_assignment'
end
Node = Node[1]
end
Parent[index] = Node
end
end
package.preload['workshop.concepts.lua.ast_transformer.interface'] =
function(...)
return
{
run = request('run'),
handlers = request('handlers.interface'),
align_nodes = request('align_nodes'),
restruc_nodes = request('restruc_nodes'),
}
end
package.preload['workshop.concepts.lua.ast_transformer.restruc_nodes'] =
function(...)
return
function(Me, Node)
for _, Subnode in ipairs(Node) do
Me:restruc_nodes(Subnode)
end
local handler = Me.handlers[Node.type]
if handler then
handler(Me, Node)
end
end
end
package.preload['workshop.concepts.lua.ast_transformer.run'] =
function(...)
return
function(Me, SyntaxTree)
local Result = SyntaxTree
for subnode_index in ipairs(Result) do
Me:align_nodes(Result, subnode_index)
end
Me:restruc_nodes(Result)
return Result
end
end
package.preload['workshop.concepts.lua.ast_transformer.handlers.assignment'] =
function(...)
local replace = request('!.table.replace')
return
function(self, node)
local dest_list = { type = 'ref_list' }
for i = 1, (#node - 1) do
dest_list[#dest_list + 1] = node[i]
end
local result =
{ type = 'assignment', dest_list = dest_list, val_list = node[#node] }
replace(node, result)
end
end
package.preload['workshop.concepts.lua.ast_transformer.handlers.attr_name_list'] =
function(...)
local tbl_remove = table.remove
return
function(Me, Node)
if (Node[1].type == 'attribute') then
Node.list_attr = Node[1]
tbl_remove(Node, 1)
end
end
end
package.preload['workshop.concepts.lua.ast_transformer.handlers.else_part'] =
function(...)
return
function(self, node)
node.body = node[1]
node[1] = nil
end
end
package.preload['workshop.concepts.lua.ast_transformer.handlers.elseif_part'] =
function(...)
return
function(self, node)
node.condition = node[1]
node[1] = nil
node.body = node[2]
node[2] = nil
end
end
package.preload['workshop.concepts.lua.ast_transformer.handlers.expression'] =
function(...)
local replace = request('!.table.replace')
local add_to_list = request('!.concepts.list.add_item')
return
function(Me, Node)
local Result = { }
local i = 1
local num_items = #Node
local Term = { }
if (Node[i].type == 'un_op') then
local UnOps = { }
while (Node[i].type == 'un_op') and (i <= num_items) do
add_to_list(UnOps, Node[i].value)
i = i + 1
end
Term.un_ops = UnOps
end
Term.operand = Node[i]
Node[i] = nil
i = i + 1
if (i < num_items) and (Node[i].type == 'bin_op') then
Term.bin_op = Node[i].value
i = i + 1
end
add_to_list(Result, Term)
if (i == num_items) then
local LastNode = Node[i]
if (LastNode.type == 'expression') then
for _, LastNodeItem in ipairs(LastNode) do
add_to_list(Result, LastNodeItem)
end
end
end
Result.type = Node.type
replace(Node, Result)
end
end
package.preload['workshop.concepts.lua.ast_transformer.handlers.function_call'] =
function(...)
local replace = request('!.table.replace')
return
function(self, node)
local result
local name_part
local seek_pos = 0
while true do
if (seek_pos < #node) then
name_part = { result }
seek_pos = seek_pos + 1
else
break
end
name_part.type = 'name_parts'
while (node[seek_pos].type ~= 'func_args') do
name_part[#name_part + 1] = node[seek_pos]
seek_pos = seek_pos + 1
end
local args = node[seek_pos]
result =
{ type = 'function_call', name_part = name_part, args = args }
end
replace(node, result)
end
end
package.preload[
'workshop.concepts.lua.ast_transformer.handlers.function_params'
] =
function(...)
local add_to_list = request('!.concepts.list.add_item')
local replace = request('!.table.replace')
return
function(Me, Node)
local Result = { type = Node.type }
if Node[1] then
if (Node[1].type == 'name_list') then
for _, NameRec in ipairs(Node[1]) do
add_to_list(Result, NameRec)
end
else
add_to_list(Result, Node[1])
end
end
if Node[2] then
add_to_list(Result, Node[2])
end
Result = new(Result)
replace(Node, Result)
end
end
package.preload[
'workshop.concepts.lua.ast_transformer.handlers.generic_for_block'
] =
function(...)
return
function(self, node)
node.names = node[1]
node[1] = nil