Skip to content

Commit ef6c7f1

Browse files
committed
Implement new operator
1 parent b238633 commit ef6c7f1

10 files changed

Lines changed: 477 additions & 227 deletions

File tree

.idea/workspace.xml

Lines changed: 21 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/ko/carbonel/compiler/generated/AttrTinyRustParser.java

Lines changed: 154 additions & 150 deletions
Large diffs are not rendered by default.

src/main/java/ko/carbonel/compiler/intermediate/tinyRust/TinyRustSymbolTable.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ public final class TinyRustSymbolTable implements SymbolTable {
2727
static final String I_32 = "I32";
2828
static final String IO = "IO";
2929
static final String VOID = "void";
30-
@SerializeClass({TS, AST}) public final List<ClassEntry> classEntries;
31-
@SerializeClass({TS, AST}) public final MethodEntry main;
30+
@SerializeClass({TS, AST})
31+
public final List<ClassEntry> classEntries;
32+
@SerializeClass({TS, AST})
33+
public final MethodEntry main;
3234
public MethodEntry activeMethod = new MethodEntry(this, null);
3335
public ClassEntry activeClass = new ClassEntry(this);
3436
private final Stack<StatementEntry> statementStack = new Stack<>();
@@ -287,7 +289,7 @@ public void endBlock() {
287289
}
288290

289291
private <T extends StatementEntry> void popStatementUntil(Class<T> clazz, BiConsumer<T, List<StatementEntry>> callback) {
290-
popUntil(clazz, callback, statementStack, it->false);
292+
popUntil(clazz, callback, statementStack, it -> false);
291293
}
292294

293295
/**
@@ -302,7 +304,8 @@ private <T extends StatementEntry> void popStatementUntil(Class<T> clazz, BiCons
302304
*/
303305
private <T extends S, S> void popUntil(Class<T> clazz, BiConsumer<T, List<S>> callback, Stack<S> stack, Predicate<T> predicate) {
304306
List<S> statementList = new ArrayList<>();
305-
while (!(clazz.isInstance(stack.peek())) || predicate.test(clazz.cast(stack.peek()))) statementList.add(0, stack.pop());
307+
while (!(clazz.isInstance(stack.peek())) || predicate.test(clazz.cast(stack.peek())))
308+
statementList.add(0, stack.pop());
306309
callback.accept(clazz.cast(stack.peek()), statementList);
307310
}
308311

@@ -343,19 +346,42 @@ public void expMethodCall() {
343346
}
344347

345348
@CalledByParser
346-
public void endMethodCall(){
349+
public void endMethodCall() {
347350
popExpressionUntil(MethodCallExpressionEntry.class, (methodCall, expressions) -> {
348351
methodCall.setArguments(expressions);
349352
ExpressionEntry pop = expressionStack.pop();
350353
if (pop != methodCall) throw new RuntimeException("Method call not on top of stack");
351354
expressionStack.push(methodCall);
352355
Logger.output("Method call: " + methodCall);
353-
}, it->it.arguments!=null);
356+
}, it -> it.arguments() != null);
354357
}
358+
359+
@CalledByParser
360+
public void expConstructorCall() {
361+
NewClassExpressionEntry exp = new NewClassExpressionEntry();
362+
LiteralExpressionEntry name = (LiteralExpressionEntry) expressionStack.pop();
363+
exp.setClassName(name.literal());
364+
expressionStack.push(exp);
365+
}
366+
367+
@CalledByParser
368+
public void expNewClass() {
369+
popExpressionUntil(NewClassExpressionEntry.class, (newClass, expressions) -> {
370+
newClass.setConstructorArguments(expressions);
371+
ExpressionEntry pop = expressionStack.pop();
372+
if (pop != newClass) throw new RuntimeException("New class not on top of stack");
373+
expressionStack.push(newClass);
374+
Logger.output("New class: " + newClass);
375+
}, it -> it.constructorArguments() != null);
376+
}
377+
355378
@CalledByParser
356-
public void expNew(){
357-
NewExpressionEntry exp = new NewExpressionEntry();
379+
public void expNewArray(){
380+
ExpressionEntry count = expressionStack.pop();
381+
LiteralExpressionEntry type = (LiteralExpressionEntry) expressionStack.pop();
382+
NewArrayExpressionEntry exp = new NewArrayExpressionEntry();
383+
exp.setSize(count);
384+
exp.setType(type.literal());
358385
expressionStack.push(exp);
359-
Logger.output("New expression: " + exp);
360386
}
361387
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ko.carbonel.compiler.intermediate.tinyRust.ast.expression;
2+
3+
import ko.carbonel.compiler.stream.lexer.tinyRust.model.TinyRustLexerToken;
4+
import ko.carbonel.json.SerializeClass;
5+
6+
public class NewArrayExpressionEntry extends ExpressionEntry {
7+
@SerializeClass("ast") public ExpressionEntry size;
8+
@SerializeClass("ast") public TinyRustLexerToken type;
9+
10+
public ExpressionEntry size() {
11+
return size;
12+
}
13+
14+
public void setSize(ExpressionEntry size) {
15+
this.size = size;
16+
}
17+
18+
public TinyRustLexerToken type() {
19+
return type;
20+
}
21+
22+
public void setType(TinyRustLexerToken type) {
23+
this.type = type;
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ko.carbonel.compiler.intermediate.tinyRust.ast.expression;
2+
3+
import ko.carbonel.compiler.stream.lexer.tinyRust.model.TinyRustLexerToken;
4+
import ko.carbonel.json.SerializeClass;
5+
6+
import java.util.List;
7+
8+
public class NewClassExpressionEntry extends ExpressionEntry {
9+
@SerializeClass("ast") public TinyRustLexerToken className;
10+
@SerializeClass("ast") public List<ExpressionEntry> constructorArguments;
11+
12+
public TinyRustLexerToken className() {
13+
return className;
14+
}
15+
16+
public void setClassName(TinyRustLexerToken className) {
17+
this.className = className;
18+
}
19+
20+
public List<ExpressionEntry> constructorArguments() {
21+
return constructorArguments;
22+
}
23+
24+
public void setConstructorArguments(List<ExpressionEntry> constructorArguments) {
25+
this.constructorArguments = constructorArguments;
26+
}
27+
}

src/main/java/ko/carbonel/compiler/intermediate/tinyRust/ast/expression/NewExpressionEntry.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/resources/grammar/main_attr.bnf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ Member ::= "CREATE" {st.setConstructorActive(v_0)} Params "{" {st.newBlock()} pr
4141
MethodCall ::= "ATTR_ID" {st.expLit(v_0)} {st.expMethodCall()} Args {st.endMethodCall()} StaticMethodCallF
4242
MethodF ::= Stmt_star {st.endBlock()} "}" | {st.endBlock()} "}"
4343
Method ::= "STATIC" "FN" "ATTR_ID" { st.newMethod(true, v_2) } Params "-" ">" ReturnType {st.activeMethod.setReturn(v_7)} "{" {st.newBlock()} programFFFF { st.addMethod() } | "FN" "ATTR_ID" { st.newMethod(false, v_1) } Params "-" ">" ReturnType { st.activeMethod.setReturn(v_6)} "{" {st.newBlock()} programFFFF { st.addMethod() }
44-
NewCallFF ::= "CLASS_ID" Args StaticMethodCallF | PrimType "[" ExpOr "]"
45-
NewCall ::= "NEW" {st.expNew()} NewCallFF
44+
NewCallFF ::= "CLASS_ID" {st.expLit(v_0)} {st.expConstructorCall()} Args {st.expNewClass()} StaticMethodCallF | PrimType {st.expLit(v_0)} "[" ExpOr "]" {st.expNewArray()}
45+
NewCall ::= "NEW" NewCallFF
4646
OpAd ::= "+" {!} | "-" {!}
4747
OpEq ::= "==" {!} | "!=" {!}
4848
OpMul ::= "*" {!} | "/" {!} | "%" {!}

src/test/resources/ast/04.ast.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"classEntries": [
3+
{
4+
"name": {"lexeme": "I32"},
5+
"methods": []
6+
},
7+
{
8+
"name": {"lexeme": "Bool"},
9+
"methods": []
10+
},
11+
{
12+
"name": {"lexeme": "Char"},
13+
"methods": []
14+
},
15+
{
16+
"name": {"lexeme": "Str"},
17+
"methods": []
18+
},
19+
{
20+
"name": {"lexeme": "Object"},
21+
"methods": []
22+
},
23+
{
24+
"name": {"lexeme": "IO"},
25+
"methods": []
26+
},
27+
{
28+
"name": {"lexeme": "A"},
29+
"methods": [{
30+
"block": {"statements": [
31+
{
32+
"lhs": {"literal": {"lexeme": "a"}},
33+
"rhs": {
34+
"className": {"lexeme": "A"},
35+
"constructorArguments": [
36+
{"literal": {"lexeme": "1"}},
37+
{"literal": {"lexeme": "2"}},
38+
{"literal": {"lexeme": "3"}},
39+
{
40+
"className": {"lexeme": "B"},
41+
"constructorArguments": [{
42+
"size": {"literal": {"lexeme": "0"}},
43+
"type": {"lexeme": "Str"}
44+
}]
45+
}
46+
]
47+
}
48+
},
49+
{
50+
"lhs": {"literal": {"lexeme": "a"}},
51+
"rhs": {
52+
"size": {
53+
"left": {"literal": {"lexeme": "5"}},
54+
"operator": {"lexeme": "+"},
55+
"right": {"literal": {"lexeme": "5"}}
56+
},
57+
"type": {"lexeme": "I32"}
58+
}
59+
}
60+
]},
61+
"name": {"lexeme": "test"}
62+
}]
63+
}
64+
],
65+
"main": {
66+
"block": {"statements": null},
67+
"name": {"lexeme": "main"}
68+
}
69+
}

src/test/resources/ast/04.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class A{
22
fn test() -> void{
3-
a = new A();
4-
a = new I32[5];
3+
a = new A(1,2,3, new B(new Str[0]));
4+
a = new I32[5 + 5];
55
}
66
}
77
fn main(){

src/test/resources/ast/04.ts.json

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
{
2+
"classEntries": [
3+
{
4+
"name": {"lexeme": "I32"},
5+
"isExtendable": false,
6+
"isArrayValid": true,
7+
"fields": [],
8+
"constructor": {
9+
"parameters": [],
10+
"variables": [],
11+
"index": 0,
12+
"returnType": null,
13+
"name": null,
14+
"isStatic": false,
15+
"isPublic": false
16+
},
17+
"methods": []
18+
},
19+
{
20+
"name": {"lexeme": "Bool"},
21+
"isExtendable": false,
22+
"isArrayValid": true,
23+
"fields": [],
24+
"constructor": {
25+
"parameters": [],
26+
"variables": [],
27+
"index": 0,
28+
"returnType": null,
29+
"name": null,
30+
"isStatic": false,
31+
"isPublic": false
32+
},
33+
"methods": []
34+
},
35+
{
36+
"name": {"lexeme": "Char"},
37+
"isExtendable": false,
38+
"isArrayValid": true,
39+
"fields": [],
40+
"constructor": {
41+
"parameters": [],
42+
"variables": [],
43+
"index": 0,
44+
"returnType": null,
45+
"name": null,
46+
"isStatic": false,
47+
"isPublic": false
48+
},
49+
"methods": []
50+
},
51+
{
52+
"name": {"lexeme": "Str"},
53+
"isExtendable": false,
54+
"isArrayValid": true,
55+
"fields": [],
56+
"constructor": {
57+
"parameters": [],
58+
"variables": [],
59+
"index": 0,
60+
"returnType": null,
61+
"name": null,
62+
"isStatic": false,
63+
"isPublic": false
64+
},
65+
"methods": []
66+
},
67+
{
68+
"name": {"lexeme": "Object"},
69+
"isExtendable": true,
70+
"isArrayValid": false,
71+
"fields": [],
72+
"constructor": {
73+
"parameters": [],
74+
"variables": [],
75+
"index": 0,
76+
"returnType": null,
77+
"name": null,
78+
"isStatic": false,
79+
"isPublic": false
80+
},
81+
"methods": []
82+
},
83+
{
84+
"name": {"lexeme": "IO"},
85+
"isExtendable": false,
86+
"isArrayValid": false,
87+
"fields": [],
88+
"constructor": {
89+
"parameters": [],
90+
"variables": [],
91+
"index": 0,
92+
"returnType": null,
93+
"name": null,
94+
"isStatic": false,
95+
"isPublic": false
96+
},
97+
"methods": []
98+
},
99+
{
100+
"name": {"lexeme": "A"},
101+
"isExtendable": true,
102+
"isArrayValid": false,
103+
"fields": [],
104+
"constructor": {
105+
"parameters": [],
106+
"variables": [],
107+
"index": 0,
108+
"returnType": {
109+
"type": {"lexeme": "A"},
110+
"isArray": false
111+
},
112+
"name": null,
113+
"isStatic": false,
114+
"isPublic": false
115+
},
116+
"methods": [{
117+
"parameters": [],
118+
"variables": [],
119+
"index": 0,
120+
"returnType": {
121+
"type": {"lexeme": "void"},
122+
"isArray": false
123+
},
124+
"name": {"lexeme": "test"},
125+
"isStatic": false,
126+
"isPublic": true
127+
}]
128+
}
129+
],
130+
"main": {
131+
"parameters": [],
132+
"variables": [],
133+
"index": 0,
134+
"returnType": {
135+
"type": {"lexeme": "void"},
136+
"isArray": false
137+
},
138+
"name": {"lexeme": "main"},
139+
"isStatic": false,
140+
"isPublic": false
141+
}
142+
}

0 commit comments

Comments
 (0)