WIP: Add enum support
This commit is contained in:
@@ -12,7 +12,8 @@ public class AndTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): bool { return 1 && 2; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:30 && is only defined for bool.", e.getMessage());
|
||||
|
||||
@@ -13,7 +13,8 @@ public class ConstructorCallTest {
|
||||
ParseTree tree = Helper.prepareParser("struct bar { a: int; } function foo(): bar { return create schwurbel(1); } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:52 Struct with name \"schwurbel\" not defined.", e.getMessage());
|
||||
@@ -24,7 +25,8 @@ public class ConstructorCallTest {
|
||||
ParseTree tree = Helper.prepareParser("struct bar { a: int; } function foo(): bar { return create bar(1, false); } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:52 Struct \"bar\" defined 1 fields, but got 2 constructor parameters.", e.getMessage());
|
||||
@@ -35,7 +37,8 @@ public class ConstructorCallTest {
|
||||
ParseTree tree = Helper.prepareParser("struct bar { a: int; } function foo(): bar { return create bar(false); } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:63 argument 0 Type missmatch: cannot combine bool and int", e.getMessage());
|
||||
|
||||
@@ -13,7 +13,8 @@ public class DestroyStatementTest {
|
||||
ParseTree tree = Helper.prepareParser("struct bar { a: int; } function foo(): int { destroy x; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:45 Variable with name \"x\" not defined.", e.getMessage());
|
||||
|
||||
@@ -13,7 +13,8 @@ public class FieldAssignmentTest {
|
||||
ParseTree tree = Helper.prepareParser("struct test { a: int; } function foo(): int { str.a = 1; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:46 Variable with name str not defined.", e.getMessage());
|
||||
@@ -24,7 +25,8 @@ public class FieldAssignmentTest {
|
||||
ParseTree tree = Helper.prepareParser("struct test { a: int; } function foo(): int { let x: int = 0; x.a = 0; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:62 Variable must reference a struct but references int.", e.getMessage());
|
||||
|
||||
@@ -13,7 +13,8 @@ public class FunctionCallTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { return 1; } bar();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:34 Function with name \"bar\" not defined.", e.getMessage());
|
||||
@@ -24,7 +25,8 @@ public class FunctionCallTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { return 1; } foo(5);");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:34 Function \"foo\" expects 0 parameters, but got 1.", e.getMessage());
|
||||
@@ -35,7 +37,8 @@ public class FunctionCallTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(x: int): int { return x; } foo(false);");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:40 argument 0 Expected int but got: bool", e.getMessage());
|
||||
|
||||
@@ -13,7 +13,8 @@ public class FunctionDefinitionTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): schwurbel { return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:0 Type schwurbel not defined.", e.getMessage());
|
||||
@@ -24,7 +25,8 @@ public class FunctionDefinitionTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { let x: int; x = 0; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:0 Function foo has to return something of type int.", e.getMessage());
|
||||
|
||||
@@ -18,19 +18,19 @@ public class Helper {
|
||||
|
||||
public static Map<String, FunctionInformation> getFuncs(ParseTree tree) {
|
||||
var functionDefinitions = new HashMap<String, FunctionInformation>();
|
||||
new GetFunctions(functionDefinitions).visit(tree);
|
||||
new GetDefinitions(functionDefinitions, new HashMap<>(), new HashMap<>()).visit(tree);
|
||||
return functionDefinitions;
|
||||
}
|
||||
|
||||
public static Set<String> getStructNames(ParseTree tree) {
|
||||
var structNames = new HashSet<String>();
|
||||
new GetStructNames(structNames).visit(tree);
|
||||
return structNames;
|
||||
}
|
||||
|
||||
public static Map<String, StructDefinition> getStructs(ParseTree tree) {
|
||||
var structs = new HashMap<String, StructDefinition>();
|
||||
new GetStructs(getStructNames(tree), structs).visit(tree);
|
||||
new GetDefinitions(new HashMap<>(), structs, new HashMap<>()).visit(tree);
|
||||
return structs;
|
||||
}
|
||||
|
||||
public static Map<String, EnumDefinition> getEnums(ParseTree tree) {
|
||||
var enums = new HashMap<String, EnumDefinition>();
|
||||
new GetDefinitions(new HashMap<>(), new HashMap<>(), enums).visit(tree);
|
||||
return enums;
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,8 @@ public class ModuloTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): float { return 1.0 % 2.3; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:31 Only integers are allowed for modulo.", e.getMessage());
|
||||
|
||||
@@ -13,7 +13,8 @@ public class OrTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): bool { return 1 || 2; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:30 || is only defined for bool.", e.getMessage());
|
||||
|
||||
@@ -12,7 +12,8 @@ public class StructFieldAccessTest {
|
||||
ParseTree tree = Helper.prepareParser("struct test { a: int; } function foo(): int { return str.a; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:53 Variable with name str not defined.", e.getMessage());
|
||||
@@ -23,7 +24,8 @@ public class StructFieldAccessTest {
|
||||
ParseTree tree = Helper.prepareParser("struct test { a: int; } function foo(): int { let x: int = 0; return x.a; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:69 Variable must reference a struct but references int.", e.getMessage());
|
||||
|
||||
@@ -13,7 +13,8 @@ public class VariableAssignmentTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { x = 1; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:22 Variable with name \"x\" not defined.", e.getMessage());
|
||||
|
||||
@@ -12,7 +12,8 @@ public class VariableDeclarationTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { let X: unk; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:22 Type unk not defined.", e.getMessage());
|
||||
@@ -24,7 +25,8 @@ public class VariableDeclarationTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { let x: int; let x: bool; return 1; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:34 Redeclaration of variable with name \"x\".", e.getMessage());
|
||||
|
||||
@@ -13,7 +13,8 @@ public class VariableTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { return x; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:29 Variable with name \"x\" not defined.", e.getMessage());
|
||||
@@ -24,7 +25,8 @@ public class VariableTest {
|
||||
ParseTree tree = Helper.prepareParser("function foo(): int { let x: int; return x; } foo();");
|
||||
var funcs = Helper.getFuncs(tree);
|
||||
var structs = Helper.getStructs(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs);
|
||||
var enums = Helper.getEnums(tree);
|
||||
ContextAnalysis ctxAnal = new ContextAnalysis(funcs, structs, enums);
|
||||
|
||||
Exception e = assertThrows(RuntimeException.class, () -> ctxAnal.visit(tree));
|
||||
assertEquals("Error in line 1:41 Variable with name \"x\" has not been initialized.", e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user