Skip to content

Commit 741d391

Browse files
committed
Changes
1 parent b3246c3 commit 741d391

4 files changed

Lines changed: 76 additions & 51 deletions

File tree

.idea/workspace.xml

Lines changed: 33 additions & 42 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/intermediate/tinyRust/TinyRustSymbolTable.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public final class TinyRustSymbolTable implements SymbolTable {
2727
public MethodEntry activeMethod = new MethodEntry(this, null);
2828
public ClassEntry activeClass = new ClassEntry(this);
2929
public Stack<StatementEntry> statementStack = new Stack<>();
30+
public Stack<ExpressionEntry> expressionStack = new Stack<>();
3031
public StatementEntry activeStatement = null;
3132
public ExpressionEntry activeExpression = null;
3233

@@ -153,6 +154,7 @@ public void newWile() {
153154
activeStatement = new WhileStatementEntry();
154155
}
155156

157+
@CalledByParser
156158
public void whileBody() {
157159
WhileStatementEntry active = (WhileStatementEntry) activeStatement;
158160
statementStack.push(active);
@@ -162,7 +164,21 @@ public void whileBody() {
162164
active.setCondition(activeExpression);
163165
}
164166

167+
@CalledByParser
165168
public void end(){
166169
activeStatement = statementStack.pop();
167170
}
171+
/**
172+
* Wrap the current expression in a binary expression
173+
*/
174+
@CalledByParser
175+
public void expWrapBin(List<TinyRustLexerToken> op){
176+
// TODO: Assert op size is 1
177+
String lex = op.get(0).lexeme();
178+
BinaryExpressionEntry nw = new BinaryExpressionEntry();
179+
nw.setLeft(activeExpression);
180+
nw.setOperator(lex);
181+
expressionStack.push(nw);
182+
activeExpression = nw;
183+
}
168184
}

src/main/java/ko/carbonel/compiler/intermediate/tinyRust/ast/BinaryExpressionEntry.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,22 @@ public class BinaryExpressionEntry extends ExpressionEntry {
44
ExpressionEntry left;
55
ExpressionEntry right;
66
String operator;
7+
public ExpressionEntry getLeft() {
8+
return left;
9+
}
10+
public void setLeft(ExpressionEntry left) {
11+
this.left = left;
12+
}
13+
public ExpressionEntry getRight() {
14+
return right;
15+
}
16+
public void setRight(ExpressionEntry right) {
17+
this.right = right;
18+
}
19+
public String getOperator() {
20+
return operator;
21+
}
22+
public void setOperator(String operator) {
23+
this.operator = operator;
24+
}
725
}

src/test/resources/syntax/main_attr.bnf

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ ExpAdRF ::= ExpAdR | "λ"
1717
ExpAdR ::= OpAd ExpMul ExpAdRF
1818
ExpAd ::= ExpMul ExpAdRF
1919
ExpAndF ::= ExpAndR | "λ"
20-
ExpAndR ::= "&&" ExpEq ExpAndF
20+
ExpAndR ::= "&&" {st.expWrapBin(List.of("&&"))} ExpEq ExpAndF
2121
ExpAnd ::= ExpEq ExpAndF
2222
ExpEqRF ::= ExpEqR | "λ"
23-
ExpEqR ::= OpEq ExpRel ExpEqRF
23+
ExpEqR ::= OpEq {st.expWrapBin(v_0)} ExpRel ExpEqRF
2424
ExpEq ::= ExpRel ExpEqRF
2525
ExpMulF ::= ExpMulR | "λ"
2626
ExpMulR ::= OpMul ExpUn ExpMulF
2727
ExpMul ::= ExpUn ExpMulF
2828
ExpOrF ::= ExpOrR | "λ"
29-
ExpOrR ::= "||" ExpAnd ExpOrF
29+
ExpOrR ::= "||" {st.expWrapBin(List.of("||"))} ExpAnd ExpOrF
3030
ExpOr ::= ExpAnd ExpOrF
31-
ExpRelF ::= OpRel ExpAd | "λ"
31+
ExpRelF ::= OpRel {st.expWrapBin(v_0)} ExpAd | "λ"
3232
ExpRel ::= ExpAd ExpRelF
3333
ExpUn ::= OpUn ExpUn | Operand
3434
ExprListF ::= "λ" | "," ExprList
@@ -44,11 +44,11 @@ MethodF ::= Stmt_star "}" | "}"
4444
Method ::= "STATIC" "FN" "ATTR_ID" { st.newMethod(true, v_2) } Params "-" ">" ReturnType {st.activeMethod.setReturn(v_7)} "{" programFFFF { st.addMethod() } | "FN" "ATTR_ID" { st.newMethod(false, v_1) } Params "-" ">" ReturnType { st.activeMethod.setReturn(v_6)} "{" programFFFF { st.addMethod() }
4545
NewCallFF ::= "CLASS_ID" Args StaticMethodCallF | PrimType "[" ExpOr "]"
4646
NewCall ::= "NEW" NewCallFF
47-
OpAd ::= "+" | "-"
48-
OpEq ::= "==" | "!="
49-
OpMul ::= "*" | "/" | "%"
50-
OpRel ::= "<" | ">" | "<=" | ">="
51-
OpUn ::= "+" | "-" | "!"
47+
OpAd ::= "+" {!} | "-" {!}
48+
OpEq ::= "==" {!} | "!=" {!}
49+
OpMul ::= "*" {!} | "/" {!} | "%" {!}
50+
OpRel ::= "<" {!} | ">" {!} | "<=" {!} | ">=" {!}
51+
OpUn ::= "+" {!} | "-" {!} | "!" {!}
5252
Operand ::= Primary StaticMethodCallF | Literal
5353
ParamListF ::= "," ParamList | "λ"
5454
ParamList ::= Type ":" "ATTR_ID" { st.activeMethod.addParameter(v_0, v_2) } ParamListF

0 commit comments

Comments
 (0)