Skip to content

Commit 1f34ac1

Browse files
committed
Constructors now work properly and parent detection
1 parent 8a9a78e commit 1f34ac1

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

src/main/java/org/luapp/language/Luapp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void load(){
5252
File newFile = new File(newPath);
5353
newFile.createNewFile();
5454
FileWriter writeFile = new FileWriter(newPath);
55-
writeFile.write("--[[\nWritten by nosharp (https://nosharp.cc),\ntom.bat (tomdotbat.dev),\nsammy milton (smilton.dev)\n]]--" + this.currentResult);
55+
writeFile.write("--[[\nWritten by nosharp (https://nosharp.cc),\ntom.bat (tomdotbat.dev),\nsamuel milton (smilton.dev)\n]]--" + this.currentResult);
5656
writeFile.close();
5757
}catch(IOException ex){
5858
ex.printStackTrace();

src/main/java/org/luapp/language/handlers/ConstructorHandler.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,28 @@ public void onSetManager() {
1818

1919
@Override
2020
public void onEnterContext(ParserRuleContext enterContext) {
21+
2122
luappParser.ConstructorContext constructorContext = (luappParser.ConstructorContext)enterContext;
2223
ParseTree funcParams = constructorContext.getChild(2);
2324
ParseTree funcBody = constructorContext.getChild(4);
2425
if(funcBody == null || funcParams == null){
2526
System.out.println("Function Body or Function Parameters are null");
2627
return;
2728
}
28-
2929
String params = this.getLuaPP().getRawFromContext((ParserRuleContext)funcParams);
3030
String body = this.getLuaPP().getRawFromContext((ParserRuleContext) funcBody);
3131

3232
String abstractClass = this.getLuaPP().currentAbstract == null ? "" : this.getLuaPP().currentAbstract;
3333
String currentClass = this.getLuaPP().currentClass == null ? "" : this.getLuaPP().currentClass;
34-
34+
System.out.println("Got here?");
3535
this.addToLuaPPResult("function " + currentClass + ":new(" + params + ")\n " +
36-
"local self = {}\n" +
37-
"setmetatable(self, " + currentClass +")" +
36+
"\tlocal self = {}\n" +
37+
"\tsetmetatable(self, " + currentClass +")" +
3838
"\n" + (abstractClass.isEmpty() ? "" : ("for k,v in pairs(" + abstractClass + ") do \n" +
3939
" self[k] = v\n" +
40-
" end " +
41-
"end")));
40+
" end ")) +
41+
"\n\t" + body +
42+
"\n\treturn self\nend");
4243

4344
}
4445

src/main/java/org/luapp/language/handlers/StatementHandler.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,22 @@ public StatementHandler() {
1515
}
1616

1717
public boolean isParentClass(ParserRuleContext context){
18-
return context.getParent().getRuleIndex() == luappParser.RULE_classbody;
18+
ParseTree parent = context.parent;
19+
for(int i =0; i < 1000000; i++){ // This is because I KNOW someone's gonna have some weird edge cases.
20+
ParserRuleContext prc = (ParserRuleContext)parent;
21+
if(prc == null) return false;
22+
if(prc.parent == null) return false;
23+
if(prc.getParent().getRuleIndex() == luappParser.RULE_classbody) {
24+
return true;
25+
}
26+
parent = prc.parent;
27+
}
28+
29+
return false;
1930
}
2031

32+
33+
2134
public boolean isChildIgnored(ParserRuleContext context){
2235

2336
for (ParseTree child : context.children) {
@@ -49,13 +62,20 @@ public void onEnterContext(ParserRuleContext enterContext) {
4962
}
5063
}
5164

65+
if(enterContext.parent instanceof luappParser.ClassbodyContext){
66+
System.out.println("Parent is class ignoring");
67+
return;
68+
}
69+
5270
if(this.isParentClass(enterContext)) {
5371
System.out.println("Parent is the class!");
5472
return;
5573
}
74+
5675
if(this.isChildIgnored(enterContext)){
5776
return;
5877
}
78+
5979
//System.out.println("NewLine:" + this.getLuaPP().getRawFromContext(enterContext));
6080
this.addToLuaPPResult(this.getLuaPP().getRawFromContext(enterContext));
6181
}

0 commit comments

Comments
 (0)