Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit ac3afe9

Browse files
committed
Add stringcall transpile support, ifeif chains in parser
* Stringcalls for lua transpiler (will always use _G) * Implemented ifeif chains for the parser and lua transpiler
1 parent cc8ad43 commit ac3afe9

3 files changed

Lines changed: 54 additions & 30 deletions

File tree

script.e2

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ while(1) {
1313
print("while true do end")
1414
}
1515

16+
"helloworld"()
17+
18+
if(1) {
19+
print("hi")
20+
} elseif(2) {
21+
print("no")
22+
} elseif(3) {
23+
print("bye")
24+
} else {
25+
print("else")
26+
}
27+
1628
Var = 500
1729
switch (Var) {
1830
case 500,

src/Base/Parser.hx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,31 @@ class Parser {
322322
return null;
323323
}
324324

325-
function acceptIfElseIf() {
326-
if (this.acceptRoamingToken("keyword", "elseif"))
327-
return this.instruction( this.getTokenTrace(), "if", [this.acceptCondition(), this.acceptBlock("else if condition"), true, this.acceptIfElseIf()] );
325+
function acceptIfElseIf(): Array<Instruction> {
326+
var args = [];
327+
while (true) {
328+
if (this.acceptRoamingToken("keyword", "elseif")) {
329+
args.push(
330+
this.instruction(this.getTokenTrace(), "if", [this.acceptCondition(), this.acceptBlock("elseif condition"), null, false])
331+
);
332+
} else if(this.acceptRoamingToken("keyword", "else")) {
333+
args.push(
334+
this.instruction(this.getTokenTrace(), "if", [null, this.acceptBlock("else"), null, true])
335+
);
336+
break;
337+
} else {
338+
break;
339+
}
340+
}
328341

329-
return this.acceptIfElse();
342+
return args;
330343
}
331344

332345
function acceptIfElse() {
333346
if (this.acceptRoamingToken("keyword", "else"))
334347
return this.acceptBlock("else");
335348

349+
trace("root?");
336350
return this.instruction( this.getTokenTrace(), "root", [] );
337351
}
338352

@@ -523,7 +537,7 @@ class Parser {
523537
function stmt1() {
524538
if (this.acceptRoamingToken("keyword", "if")) {
525539
var trace = this.getTokenTrace();
526-
return this.instruction( trace, "if", [this.acceptCondition(), this.acceptBlock("if condition"), false, this.acceptIfElseIf()] );
540+
return this.instruction( trace, "if", [this.acceptCondition(), this.acceptBlock("if condition"), this.acceptIfElseIf(), false] );
527541
}
528542
return this.stmt2();
529543
}
@@ -1136,7 +1150,7 @@ class Parser {
11361150

11371151
expr = this.instruction( trace, "stringcall", [expr, exprs, stype] );
11381152
} else {
1139-
expr = this.instruction( trace, "stringcall", [expr, exprs, ""] );
1153+
expr = this.instruction( trace, "stringcall", [expr, exprs, null] );
11401154
}
11411155
} else {
11421156
break;

src/Base/Transpiler/Lua.hx

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,25 @@ class Instructions {
4141
static function instr_literal(value: E2Type, raw: String)
4242
return raw;
4343

44-
static function instr_if(cond: Instruction, block: Instruction, is_elseif: Bool, ifeif: Instruction) {
45-
if (is_elseif) {
46-
return 'is_elseif';
47-
}
48-
49-
if( ifeif == null ) {
50-
return 'ifeif is null; $block $cond';
51-
}
52-
53-
if ( block == null ) {
54-
return 'block is null';
44+
static function instr_if(cond: Instruction, block: Instruction, ifeifs: Array<Instruction>, is_else: Bool) {
45+
46+
if (ifeifs != null) {
47+
// Top If
48+
var code = 'if ${ callInline(cond) } then\n' +
49+
'\t${ callBlock(block, true) }\n';
50+
for (ifeif in ifeifs) {
51+
code += callInline(ifeif);
52+
}
53+
return code + 'end';
54+
} else {
55+
if (is_else) {
56+
return 'else\n' +
57+
'\t${ callBlock(block,true) }\n';
58+
} else {
59+
return 'elseif ${ callInline(cond) } then\n' +
60+
'\t${ callBlock(block,true) }\n';
61+
}
5562
}
56-
57-
return 'if ${ callInline(cond) } then\n' +
58-
'\t${ callBlock(block, true) }\n' +
59-
'end';
60-
61-
/*return 'if ${ callInline(cond) } then\n' +
62-
'\t$block\n' +
63-
'\t${ callBlock(ifeif, true) }\n' +
64-
(is_elseif ? '<iselseif>' : 'end');*/
65-
66-
/*return build +
67-
'\t${ callBlock(block, true) }\n' +
68-
'end [${ ifeif != null ? callBlock(ifeif, false) : "" }]';*/
6963
}
7064

7165
static function instr_for(varname: String, start: Instruction, end: Instruction, ?inc: Instruction, block: Instruction) {
@@ -191,6 +185,10 @@ class Instructions {
191185
'end)';
192186
}
193187

188+
static function instr_stringcall(name:Instruction, args: Array<Instruction>, ret_type: Null<String>) {
189+
return '_G[${ callInline(name) }](${ args.map( (x) -> callInline(x) ).join(", ") })';
190+
}
191+
194192
// Bitwise ops
195193
static function instr_bor(v1: Instruction, v2: Instruction) {
196194
#if LUA54

0 commit comments

Comments
 (0)