This commit is contained in:
ChUrl
2020-12-14 15:11:05 +01:00
parent 1991255ee6
commit 50cb660c4c
3 changed files with 17 additions and 2 deletions

View File

@ -66,7 +66,7 @@ public final class TypeChecker {
ASTNode literalNode = root.getChildren().get(0);
String literalType = nodeTable.get(literalNode);
log("Validating Assignment: " + identifierType + ": " + identifier + " = " + literalNode.toString().trim());
log("Validating Assignment: " + identifierType + ": " + identifier + " = " + literalType);
if (!literalType.equals(identifierType)) {
throw new AssignmentTypeMismatchException("Trying to assign " + literalType + " to a " + identifierType + " variable.");

View File

@ -8,6 +8,7 @@ import parser.ast.AST;
import parser.ast.ASTBalancer;
import parser.ast.ASTCompacter;
import parser.grammar.Grammar;
import typechecker.TypeChecker;
import java.io.IOException;
import java.net.URISyntaxException;
@ -74,4 +75,18 @@ class Demo {
ASTBalancer.operatorPrecedence(tree);
System.out.println("After operator-precedence:\n" + tree);
}
@Test
void demoTypeCheck() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
Grammar grammar = Grammar.fromFile(path);
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
Lexer lex = this.initLexer("General.stups");
AST tree = stupsParser.parse(lex.getAllTokens(), lex.getVocabulary());
tree.postprocess(grammar);
TypeChecker.validate(tree);
}
}

View File

@ -1,6 +1,6 @@
class MyClass {
public static void main(String[] args) {
boolean b = true;
boolean b = true || (1 < 2) && (5 / 4) == 2 && !((1 / 5) == 2 + 1) || (-1 - -5) / 2 == 2 && true == false;
}
}