Skip to content

Commit 10ed446

Browse files
committed
Add more tests
1 parent 10cfd12 commit 10ed446

22 files changed

Lines changed: 254 additions & 56 deletions

File tree

.idea/workspace.xml

Lines changed: 34 additions & 27 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/config/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class Config {
5656
addArgument(new FileArgument(ArgumentName.OUTPUT_TS_FILE, "Output TS json file", List.of(".ts.json"), false, false, ""));
5757
addArgument(new EnumArg<>(ArgumentName.LANG, "Language for some parts of the compiler", Language.Values.TEACHER));
5858
addArgument(new EnumArg<>(ArgumentName.SORT_FUN, "Sort function for grammar rules", SortFun.START_AWARE_SORT));
59-
addArgument(new EnumArg<>(ArgumentName.OP, "Operation to run", Operation.PARSE));
59+
addArgument(new EnumArg<>(ArgumentName.OP, "Operation to run", Operation.AST));
6060
addArgument(new EnumArg<>(ArgumentName.TARGET, "Which grammar to use", Target.ATTR_TINY_RUST));
6161
}
6262

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,18 +1207,15 @@ private List<TinyRustLexerToken> matchToken_Stmt_star(){
12071207
private List<TinyRustLexerToken> matchToken_Stmt(){
12081208
enter("matchToken_Stmt");
12091209
List<TinyRustLexerToken> result = null;
1210-
if (peek(TinyRustTokenType.RETURN)){
1210+
if (peek(TinyRustTokenType.OPN_BRACE)){
1211+
st.newBlock();
1212+
TinyRustLexerToken v_1 = match(TinyRustTokenType.OPN_BRACE);
1213+
List<TinyRustLexerToken> v_2 = matchToken_Method_factored();
1214+
} else if (peek(TinyRustTokenType.RETURN)){
12111215
st.newReturn();
12121216
TinyRustLexerToken v_1 = match(TinyRustTokenType.RETURN);
12131217
List<TinyRustLexerToken> v_2 = matchToken_Stmt_factored();
12141218
st.endReturn();
1215-
} else if (peek(TinyRustTokenType.OPN_PAREN)){
1216-
st.newExpr();
1217-
TinyRustLexerToken v_1 = match(TinyRustTokenType.OPN_PAREN);
1218-
List<TinyRustLexerToken> v_2 = matchToken_ExpOr();
1219-
TinyRustLexerToken v_3 = match(TinyRustTokenType.CLS_PAREN);
1220-
TinyRustLexerToken v_4 = match(TinyRustTokenType.SEMI_COLON);
1221-
st.endExpr();
12221219
} else if (peek(TinyRustTokenType.IF)){
12231220
st.newIf();
12241221
TinyRustLexerToken v_1 = match(TinyRustTokenType.IF);
@@ -1229,6 +1226,17 @@ private List<TinyRustLexerToken> matchToken_Stmt(){
12291226
List<TinyRustLexerToken> v_6 = matchToken_Stmt();
12301227
List<TinyRustLexerToken> v_7 = matchToken_Stmt_factored_factored_factored();
12311228
st.ifEnd();
1229+
} else if (peek(TinyRustTokenType.ATTR_ID, TinyRustTokenType.SELF)){
1230+
st.newAssign();
1231+
List<TinyRustLexerToken> v_1 = matchToken_Assign();
1232+
TinyRustLexerToken v_2 = match(TinyRustTokenType.SEMI_COLON);
1233+
} else if (peek(TinyRustTokenType.OPN_PAREN)){
1234+
st.newExpr();
1235+
TinyRustLexerToken v_1 = match(TinyRustTokenType.OPN_PAREN);
1236+
List<TinyRustLexerToken> v_2 = matchToken_ExpOr();
1237+
TinyRustLexerToken v_3 = match(TinyRustTokenType.CLS_PAREN);
1238+
TinyRustLexerToken v_4 = match(TinyRustTokenType.SEMI_COLON);
1239+
st.endExpr();
12321240
} else if (peek(TinyRustTokenType.SEMI_COLON)){
12331241
st.newEmpty();
12341242
TinyRustLexerToken v_1 = match(TinyRustTokenType.SEMI_COLON);
@@ -1241,14 +1249,6 @@ private List<TinyRustLexerToken> matchToken_Stmt(){
12411249
st.whileBody();
12421250
List<TinyRustLexerToken> v_6 = matchToken_Stmt();
12431251
st.endWhile();
1244-
} else if (peek(TinyRustTokenType.ATTR_ID, TinyRustTokenType.SELF)){
1245-
st.newAssign();
1246-
List<TinyRustLexerToken> v_1 = matchToken_Assign();
1247-
TinyRustLexerToken v_2 = match(TinyRustTokenType.SEMI_COLON);
1248-
} else if (peek(TinyRustTokenType.OPN_BRACE)){
1249-
st.newBlock();
1250-
TinyRustLexerToken v_1 = match(TinyRustTokenType.OPN_BRACE);
1251-
List<TinyRustLexerToken> v_2 = matchToken_Method_factored();
12521252
} else {
12531253
handleUnexpectedToken("Stmt", Set.of(TinyRustTokenType.ATTR_ID, TinyRustTokenType.IF, TinyRustTokenType.OPN_BRACE, TinyRustTokenType.OPN_PAREN, TinyRustTokenType.RETURN, TinyRustTokenType.SELF, TinyRustTokenType.SEMI_COLON, TinyRustTokenType.WHILE));
12541254
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,7 @@ public void newBlock() {
292292

293293
@CalledByParser
294294
public void endBlock() {
295-
popStatementUntil(BlockStatementEntry.class, (block, statements) -> {
296-
block.setStatements(statements);
297-
});
295+
popStatementUntil(BlockStatementEntry.class, BlockStatementEntry::setStatements);
298296
}
299297

300298
@CalledByParser

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ public String toString() {
2828

2929
@Override
3030
public TypeEntry getType(Scope scope, boolean isImmediateChainedScope) {
31-
return switch (literal.type()) {
31+
TypeEntry type = switch (literal.type()) {
3232
case INT_DATA -> TypeEntry.ofClass(TinyRustSymbolTable.I32, literal.location());
3333
case STR_DATA -> TypeEntry.ofClass(TinyRustSymbolTable.STR, literal.location());
3434
case CHR_DATA -> TypeEntry.ofClass(TinyRustSymbolTable.CHAR, literal.location());
3535
case TRUE, FALSE -> TypeEntry.ofClass(TinyRustSymbolTable.BOOL, literal.location());
3636
case NULL -> TypeEntry.of(literal);
3737
default -> throw new RuntimeException("Unknown literal type: " + literal.type());
3838
};
39+
type.consolidate((TinyRustSymbolTable) scope.getSymbolTable(), false);
40+
return type;
3941
}
4042

4143
@Override

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public TypeEntry getType(Scope scope, boolean isImmediateChainedScope) {
4141
classEntry.setUsed(true);
4242
classEntry.constructor.setUsed(true);
4343
classEntry.constructor.verifyArguments(constructorArguments, scope, className.location());
44-
45-
return TypeEntry.ofClass(className.lexeme(), className.location());
44+
TypeEntry typeEntry = TypeEntry.ofClass(className.lexeme(), className.location());
45+
typeEntry.consolidate(symbolTable, false);
46+
return typeEntry;
4647
}
4748

4849
@Override

src/main/java/ko/carbonel/compiler/intermediate/tinyRust/ast/statement/IfStatementEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ boolean checkExpressions(Scope scope, TypeEntry returnType) {
4545
throw new IncompatibleTypesError(ifCondition.location(), "Condition must be boolean");
4646
}
4747
boolean bodyReturns = ifBody.checkExpressions(scope, returnType);
48-
boolean elseReturns = elseBody == null || elseBody.checkExpressions(scope, returnType);
48+
boolean elseReturns = elseBody != null && elseBody.checkExpressions(scope, returnType);
4949
return bodyReturns && elseReturns;
5050
}
5151
}

src/main/java/ko/carbonel/compiler/intermediate/tinyRust/ts/ChainedScope.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import java.util.Optional;
88

99
public class ChainedScope implements Scope {
10-
private TypeEntry chainBase;
11-
private Scope parent;
10+
private final TypeEntry chainBase;
11+
private final Scope parent;
1212

1313
public ChainedScope(TypeEntry chainBase, Scope parent) {
1414
chainBase.consolidate((TinyRustSymbolTable) parent.getSymbolTable(), false);

src/main/java/ko/carbonel/compiler/runner/Runners.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ protected void afterParse(AttrTinyRustParser parser) {
2626
super.afterParse(parser);
2727
String input = "";
2828
TinyRustSymbolTable st = ((AttributeParser<TinyRustSymbolTable>)parser).getSymbolTable();
29+
st.checkExpressions();
2930
String serialize = TSSerializer.serialize(st);
3031
Logger.output(serialize);
3132

src/test/java/ko/carbonel/compiler/generated/AttrTinyRustParserTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.List;
2828
import java.util.Objects;
2929
import java.util.Optional;
30-
import java.util.stream.Collectors;
3130
import java.util.stream.IntStream;
3231
import java.util.stream.Stream;
3332

@@ -215,8 +214,8 @@ private void handleAstFileOK(String file, String fileContents) {
215214
List<TinyRustLexerToken> original = new TinyRustLexer(new FileSourceReader(file + ".full")).stream().toList();
216215
List<String> ogLexemes = original.stream().map(TinyRustLexerToken::lexeme).toList();
217216
List<String> deLexemes = decompiled.stream().map(TinyRustLexerToken::lexeme).toList();
218-
String ogFormat = ogLexemes.stream().collect(Collectors.joining("\n"));
219-
String deFormat = deLexemes.stream().collect(Collectors.joining("\n"));
217+
String ogFormat = String.join("\n", ogLexemes);
218+
String deFormat = String.join("\n", deLexemes);
220219
Logger.output(decompile);
221220
Assertions.assertEquals(ogFormat, deFormat, "Decompiled code is not the same as the original one");
222221
}

0 commit comments

Comments
 (0)