Skip to content

Commit 5b2eb48

Browse files
committed
classes now work properly.
1 parent 1f34ac1 commit 5b2eb48

File tree

9 files changed

+65
-18
lines changed

9 files changed

+65
-18
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public String getCurrentResult() {
7777
}
7878

7979
public void handleEnterContext(ParserRuleContext context){
80+
StackTraceElement[] cause = Thread.currentThread().getStackTrace();
81+
System.out.println(cause[2]);
8082
this.listenerManager.listeners.forEach((LuaPPListener listener)->{
8183

8284
listener.handleEnterContext(context);

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public boolean isClassGetSet(ParseTree context){
2323
public boolean isClassConstructor(ParseTree context){
2424
return ((ParserRuleContext)context).getRuleIndex() == luappParser.RULE_constructor;
2525
}
26-
26+
public boolean isClassName(ParseTree context){
27+
return ((ParserRuleContext)context).getRuleIndex() == luappParser.RULE_classname;
28+
}
2729

2830
@Override
2931
public void onSetManager() {
@@ -41,21 +43,27 @@ public void onEnterContext(ParserRuleContext enterContext) {
4143
for (ParseTree child : enterContext.children) {
4244
if(child instanceof TerminalNodeImpl) continue;
4345
if(isClassFunction(child)){
44-
System.out.println("Class function");
46+
this.listenerManager
47+
.GetInstangeByTarget(luappParser.RULE_classfunction)
48+
.handleEnterContext((ParserRuleContext)child);
4549
}else if(isClassConstructor(child)){
4650
System.out.println("Constructor!");
4751
this.listenerManager
4852
.GetInstangeByTarget(luappParser.RULE_constructor)
4953
.handleEnterContext((ParserRuleContext)child);
5054
}else if(isClassGetSet(child)){
51-
System.out.println("Get/Set");
55+
this.listenerManager
56+
.GetInstangeByTarget(luappParser.RULE_classfunction)
57+
.handleEnterContext((ParserRuleContext)child);
58+
}else if(isClassName(child)){
59+
5260
}
5361
}
5462

5563
}
5664

5765
@Override
5866
public void onExitContext(ParserRuleContext exitContext) {
59-
67+
this.getLuaPP().currentClass = "";
6068
}
6169
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.luapp.language.handlers;
22

33
import org.antlr.v4.runtime.ParserRuleContext;
4+
import org.antlr.v4.runtime.tree.ParseTree;
45
import org.luapp.language.generator.luappParser;
56
import org.luapp.language.listeners.LuaPPListener;
67

@@ -17,7 +18,11 @@ public void onSetManager() {
1718

1819
@Override
1920
public void onEnterContext(ParserRuleContext enterContext) {
20-
21+
ParseTree name = enterContext.getChild(1);
22+
ParseTree body = enterContext.getChild(2);
23+
System.out.println(name);
24+
this.addToLuaPPResult("function " + this.getLuaPP().currentClass
25+
+ ":" + name.getText() + this.getLuaPP().getRawFromContext((ParserRuleContext)body));
2126
}
2227

2328
@Override

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ public void onSetManager() {
1717

1818
@Override
1919
public void onEnterContext(ParserRuleContext enterContext) {
20+
// StackTraceElement[] cause = Thread.currentThread().getStackTrace();
21+
// System.out.println(cause[2]);
22+
System.out.println("Classname: " + enterContext.getText());
2023
this.getLuaPP().currentClass = enterContext.getText();
24+
String currentClass = enterContext.getText();
25+
this.addToLuaPPResult(currentClass + " = {}\n" + currentClass + ".__index = " + currentClass);
2126
}
2227

2328
@Override
2429
public void onExitContext(ParserRuleContext exitContext) {
25-
this.getLuaPP().currentClass = "";
30+
2631
}
2732
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public boolean isParentClass(ParserRuleContext context){
2929
return false;
3030
}
3131

32+
public boolean isClassName(ParseTree context){
33+
return ((ParserRuleContext)context).getRuleIndex() == luappParser.RULE_classname;
34+
}
3235

3336

3437
public boolean isChildIgnored(ParserRuleContext context){
@@ -52,6 +55,10 @@ public boolean isChildIgnored(ParserRuleContext context){
5255
return false;
5356
}
5457

58+
/**
59+
* This will handle all default geneartion.
60+
* @param enterContext The actual context.
61+
*/
5562
@Override
5663
public void onEnterContext(ParserRuleContext enterContext) {
5764

@@ -60,6 +67,7 @@ public void onEnterContext(ParserRuleContext enterContext) {
6067
this.listenerManager.GetInstangeByTarget(luappParser.RULE_classbody).handleEnterContext(enterContext);
6168
return;
6269
}
70+
6371
}
6472

6573
if(enterContext.parent instanceof luappParser.ClassbodyContext){
@@ -68,7 +76,7 @@ public void onEnterContext(ParserRuleContext enterContext) {
6876
}
6977

7078
if(this.isParentClass(enterContext)) {
71-
System.out.println("Parent is the class!");
79+
System.out.println("Parent is a class!");
7280
return;
7381
}
7482

src/main/java/org/luapp/language/listeners/LuaPPListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ public void handleEnterContext(ParserRuleContext context){
3939
}
4040

4141
public void handleExitContext(ParserRuleContext context){
42-
if (isCorrectContext(context)) onEnterContext(context);
42+
if (isCorrectContext(context)) onExitContext(context);
4343
}
4444

4545
public Luapp getLuaPP(){
4646
return Main.luaPPInstance;
4747
}
4848

4949
public void addToLuaPPResult(String result){
50+
5051
this.getLuaPP().currentResult += "\n" + result;
5152
}
5253

src/main/java/org/luapp/language/listeners/MasterLuaPPListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void enterStat(luappParser.StatContext ctx) {
4040

4141
@Override
4242
public void exitStat(luappParser.StatContext ctx) {
43-
Main.luaPPInstance.handleEnterContext(ctx);
43+
Main.luaPPInstance.handleExitContext(ctx);
4444
}
4545

4646
@Override
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
class animal
1+
class cat extends animal
22
constructor(name)
33
self.name = name
44
end
5+
6+
function isCat()
7+
return true
8+
end
59
end
10+
11+
print("hello world?")
Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
--[[
22
Written by nosharp (https://nosharp.cc),
33
tom.bat (tomdotbat.dev),
4-
sammy milton (smilton.dev)
4+
samuel milton (smilton.dev)
55
]]--
6-
class animal
7-
constructor(name)
8-
self.name = name
9-
end
6+
cat = {}
7+
cat.__index = cat
8+
function cat:new(name)
9+
local self = {}
10+
setmetatable(self, cat)
11+
12+
self.name = name
13+
return self
1014
end
15+
function cat:isCat()
16+
return true
17+
end
1118
self.name = name
12-
self.name = name
13-
class animal
19+
class cat extends animal
1420
constructor(name)
1521
self.name = name
1622
end
17-
end
23+
24+
function isCat()
25+
return true
26+
end
27+
end
28+
print("hello world?")
29+
print("hello world?")

0 commit comments

Comments
 (0)