Skip to content

Commit f5fed41

Browse files
committed
Consider external tests
1 parent 07c4a1e commit f5fed41

17 files changed

Lines changed: 234 additions & 161 deletions

File tree

.idea/workspace.xml

Lines changed: 121 additions & 50 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: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,15 +1207,18 @@ 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.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)){
1210+
if (peek(TinyRustTokenType.RETURN)){
12151211
st.newReturn();
12161212
TinyRustLexerToken v_1 = match(TinyRustTokenType.RETURN);
12171213
List<TinyRustLexerToken> v_2 = matchToken_Stmt_factored();
12181214
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();
12191222
} else if (peek(TinyRustTokenType.IF)){
12201223
st.newIf();
12211224
TinyRustLexerToken v_1 = match(TinyRustTokenType.IF);
@@ -1226,17 +1229,6 @@ private List<TinyRustLexerToken> matchToken_Stmt(){
12261229
List<TinyRustLexerToken> v_6 = matchToken_Stmt();
12271230
List<TinyRustLexerToken> v_7 = matchToken_Stmt_factored_factored_factored();
12281231
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();
12401232
} else if (peek(TinyRustTokenType.SEMI_COLON)){
12411233
st.newEmpty();
12421234
TinyRustLexerToken v_1 = match(TinyRustTokenType.SEMI_COLON);
@@ -1249,6 +1241,14 @@ private List<TinyRustLexerToken> matchToken_Stmt(){
12491241
st.whileBody();
12501242
List<TinyRustLexerToken> v_6 = matchToken_Stmt();
12511243
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/ast/expression/ArrayAccessExpressionEntry.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ko.carbonel.compiler.intermediate.tinyRust.ast.expression;
22

3+
import ko.carbonel.compiler.exception.semantic.IncompatibleTypesError;
34
import ko.carbonel.compiler.intermediate.tinyRust.ts.Scope;
45
import ko.carbonel.compiler.intermediate.tinyRust.ts.TypeEntry;
56
import ko.carbonel.compiler.stream.reader.FileLocation;
@@ -29,7 +30,15 @@ public void setIndex(ExpressionEntry index) {
2930

3031
@Override
3132
public TypeEntry getType(Scope scope, boolean isImmediateChainedScope) {
32-
return TypeEntry.of(array.getType(scope, false).type);
33+
TypeEntry type = index.getType(scope, false);
34+
if (!type.equals(TypeEntry.I32)) {
35+
throw new IncompatibleTypesError(index.location(), "Array index must be an integer, " + type + " found");
36+
}
37+
TypeEntry arrType = array.getType(scope, false);
38+
if (!arrType.isArray){
39+
throw new IncompatibleTypesError(array.location(), "Array access can only be done on arrays, " + arrType + " found");
40+
}
41+
return TypeEntry.of(arrType.type);
3342
}
3443

3544
@Override

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ko.carbonel.compiler.intermediate.tinyRust.ast.expression;
22

3+
import ko.carbonel.compiler.exception.semantic.ExpressionError;
34
import ko.carbonel.compiler.exception.semantic.FieldNotFoundError;
45
import ko.carbonel.compiler.intermediate.tinyRust.ts.MethodEntry;
56
import ko.carbonel.compiler.intermediate.tinyRust.ts.Scope;
@@ -42,6 +43,7 @@ public TypeEntry getType(Scope scope, boolean isImmediateChainedScope) {
4243
if (!isImmediateChainedScope) {
4344
scope = scope.unchained();
4445
}
46+
if (methodName.lexeme().equals(MethodEntry.CONSTRUCTOR)) throw new ExpressionError(methodName.location(), "Constructor should not be called directly, use the new operator");
4547
Scope finalScope = scope;
4648
MethodEntry methodEntry = scope.resolveMethodName(methodName)
4749
.orElseThrow(() -> new FieldNotFoundError(methodName.location(), "Method " + methodName.lexeme() + " not found in current scope (" + finalScope + ")"));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ boolean checkExpressions(Scope scope, TypeEntry returnType) {
3535
if (type.isArray || !type.type.lexeme().equals(TinyRustSymbolTable.BOOL)) {
3636
throw new IncompatibleTypesError(whileCondition.location(), "Condition must be boolean");
3737
}
38-
return whileBody.checkExpressions(scope, returnType);
38+
whileBody.checkExpressions(scope, returnType);
39+
return false;
3940
}
4041
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ private int getInheritedMethodCount() {
259259
private void overrideMethod(MethodEntry localMethod) {
260260
MethodEntry parentMethod = superClassLink.resolveMethodName(localMethod.name)
261261
.orElseThrow(() -> new RuntimeException("Strange error: A method that was detected to exist in the class was not found in the class"));
262+
if (localMethod.name.lexeme().equals(MethodEntry.CONSTRUCTOR)){
263+
// Constructors are directly inherited, doesn't matter the types or arguments
264+
localMethod.index = parentMethod.index;
265+
return;
266+
}
262267
List<BiConsumer<MethodEntry, MethodEntry>> checks = List.of(
263268
ClassEntry::checkReturnType,
264269
ClassEntry::checkParameterCount,
@@ -316,6 +321,7 @@ private void checkForCycles(TinyRustLexerToken original, Set<String> visited) {
316321
* @param name The name of the method to resolve
317322
* @return The method entry if found, empty otherwise
318323
*/
324+
@Override
319325
public Optional<MethodEntry> resolveMethodName(TinyRustLexerToken name) {
320326
return methods.stream().filter(it->it.name.sameLexeme(name)).findFirst()
321327
.or(() -> superClassLink == null ? Optional.empty() : superClassLink.resolveMethodName(name));

src/test/resources/ast/00.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class Fibonacci {
2020
j=suma;
2121
(imprimo_sucesion(suma));
2222
}
23-
return 0;
2423
}
24+
return 0;
2525
}
2626
create(){
2727
i=0;

src/test/resources/ast/00.rs.full

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class Fibonacci : Object {
2020
j=suma;
2121
(imprimo_sucesion(suma));
2222
}
23-
return 0;
2423
}
24+
return 0;
2525
}
2626
create(){
2727
i=0;

src/test/resources/ast/00rsC

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

src/test/resources/ast/01.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@ class Fibonacci {
88
Object: b;
99
IO: c;
1010
i=0; j=0; suma=0;
11-
while (i <= n){
12-
if (i==0){
13-
(imprimo_numero(i));
14-
(imprimo_sucesion(suma));
15-
return suma;
16-
} else if(i==1){
17-
(imprimo_numero(i));
18-
suma=suma+i;
19-
(imprimo_sucesion(suma));
20-
return suma;
21-
} else{
22-
(imprimo_numero(i));
23-
suma=suma+j;
24-
j=suma;
25-
(imprimo_sucesion(suma));
26-
return suma;
27-
}
11+
if (i==0){
12+
(imprimo_numero(i));
13+
(imprimo_sucesion(suma));
14+
return suma;
15+
} else if(i==1){
16+
(imprimo_numero(i));
17+
suma=suma+i;
18+
(imprimo_sucesion(suma));
19+
return suma;
20+
} else{
21+
(imprimo_numero(i));
22+
suma=suma+j;
23+
j=suma;
24+
(imprimo_sucesion(suma));
25+
return suma;
2826
}
2927
}
3028
create(I32: num){

0 commit comments

Comments
 (0)