3
3
import java .util .ArrayDeque ;
4
4
import java .util .ArrayList ;
5
5
import java .util .Deque ;
6
+ import java .util .IdentityHashMap ;
6
7
import java .util .List ;
8
+ import java .util .Map ;
7
9
import java .util .Objects ;
8
10
import java .util .stream .Collectors ;
9
11
12
+ import org .antlr .v4 .runtime .ParserRuleContext ;
13
+ import org .antlr .v4 .runtime .tree .ParseTree ;
10
14
import org .antlr .v4 .runtime .tree .TerminalNode ;
11
15
16
+ import fi .benjami .code4jvm .lua .ir .DebugInfoNode ;
12
17
import fi .benjami .code4jvm .lua .ir .IrNode ;
13
18
import fi .benjami .code4jvm .lua .ir .LuaBlock ;
14
19
import fi .benjami .code4jvm .lua .ir .LuaLocalVar ;
15
- import fi .benjami .code4jvm .lua .ir .LuaType ;
16
20
import fi .benjami .code4jvm .lua .ir .LuaVariable ;
17
21
import fi .benjami .code4jvm .lua .ir .TableField ;
18
22
import fi .benjami .code4jvm .lua .ir .expr .ArithmeticExpr ;
91
95
92
96
public class IrCompiler extends LuaBaseVisitor <IrNode > {
93
97
98
+ private final String moduleName ;
94
99
private final Deque <LuaScope > scopes ;
95
100
96
- public IrCompiler (LuaScope rootScope ) {
97
- scopes = new ArrayDeque <>();
101
+ private int lastLine ;
102
+
103
+ public IrCompiler (String moduleName , LuaScope rootScope ) {
104
+ this .moduleName = moduleName ;
105
+ this .scopes = new ArrayDeque <>();
98
106
scopes .push (rootScope );
99
107
}
100
108
@@ -110,6 +118,19 @@ private void popScope() {
110
118
scopes .pop ();
111
119
}
112
120
121
+ @ Override
122
+ public IrNode visit (ParseTree tree ) {
123
+ if (tree instanceof ParserRuleContext ctx ) {
124
+ var line = ctx .getStart ().getLine ();
125
+ if (line != lastLine ) {
126
+ // Line number changed, make sure to record it
127
+ assert line > lastLine ;
128
+ return new DebugInfoNode (line , super .visit (tree ));
129
+ }
130
+ }
131
+ return super .visit (tree );
132
+ }
133
+
113
134
@ Override
114
135
public LuaBlock visitChunk (ChunkContext ctx ) {
115
136
return visitBlock (ctx .block ());
@@ -243,14 +264,16 @@ public IrNode visitFunction(FunctionContext ctx) {
243
264
for (var i = 1 ; i < ctx .Name ().size (); i ++) {
244
265
target = new VariableExpr (new TableField (target , new LuaConstant (ctx .Name (i ))));
245
266
}
246
- var function = visitFuncbody (ctx .funcbody (), ctx .oopPart != null );
267
+ // TODO incorporate the full name as part of function name
268
+ var function = visitFuncbody (ctx .Name (ctx .Name ().size () - 1 ).getText (), ctx .funcbody (), ctx .oopPart != null );
247
269
return new SetVariablesStmt (List .of (target .source ()), List .of (function ), false );
248
270
}
249
271
250
272
@ Override
251
273
public IrNode visitLocalFunction (LocalFunctionContext ctx ) {
252
- var function = visitFuncbody (ctx .funcbody (), false );
253
- return new SetVariablesStmt (List .of (currentScope ().declare (ctx .Name ().getText ())), List .of (function ), false );
274
+ var name = ctx .Name ().getText ();
275
+ var function = visitFuncbody (name , ctx .funcbody (), false );
276
+ return new SetVariablesStmt (List .of (currentScope ().declare (name )), List .of (function ), false );
254
277
}
255
278
256
279
@ Override
@@ -498,10 +521,10 @@ public IrNode visitArgs(ArgsContext ctx) {
498
521
499
522
@ Override
500
523
public IrNode visitFunctiondef (FunctiondefContext ctx ) {
501
- return visitFuncbody (ctx .funcbody (), false );
524
+ return visitFuncbody ("anonymous" , ctx .funcbody (), false );
502
525
}
503
526
504
- public IrNode visitFuncbody (FuncbodyContext ctx , boolean addSelfArg ) {
527
+ public IrNode visitFuncbody (String name , FuncbodyContext ctx , boolean addSelfArg ) {
505
528
pushScope (new LuaScope (currentScope (), true ));
506
529
var scope = currentScope ();
507
530
List <LuaLocalVar > args ;
@@ -528,7 +551,7 @@ public IrNode visitFuncbody(FuncbodyContext ctx, boolean addSelfArg) {
528
551
}
529
552
var body = visitBlock (ctx .block ());
530
553
popScope ();
531
- return new FunctionDeclExpr (scope .upvalues (), args , body );
554
+ return new FunctionDeclExpr (moduleName , name , scope .upvalues (), args , body );
532
555
}
533
556
534
557
@ Override
0 commit comments