Merge branch '28-static-type-check-bug-with-if-clause-in-function' into 'master'

Resolve "Static type check bug with if clause in function"

Closes #28

See merge request mkais001/klang!14
This commit is contained in:
Marvin Kaiser
2020-02-04 12:07:03 +01:00
2 changed files with 12 additions and 0 deletions

View File

@@ -192,6 +192,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage()); throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
} }
result = new VariableDeclaration(name, (Expression) expression); result = new VariableDeclaration(name, (Expression) expression);
result.initialized = true;
} else { } else {
result = new VariableDeclaration(name); result = new VariableDeclaration(name);
} }
@@ -227,6 +228,9 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage()); throw new RuntimeException(Helper.getErrorPrefix(line, col) + e.getMessage());
} }
// Since we assigned a value to this variable, we can consider it initialized
var.initialized = true;
// Create a new node and add the type of the expression to it // Create a new node and add the type of the expression to it
Node result = new VariableAssignment(name, expression); Node result = new VariableAssignment(name, expression);
result.line = line; result.line = line;
@@ -503,6 +507,12 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error); throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
} }
// Make sure the variable has been initialized before it can be used
if (!var.initialized) {
String error = "Variable with name \"" + name + "\" has not been initialized.";
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
}
Variable result = new Variable(ctx.IDENT().getText()); Variable result = new Variable(ctx.IDENT().getText());
result.type = var.type; result.type = var.type;
result.line = line; result.line = line;
@@ -555,6 +565,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
// add the param as a variable // add the param as a variable
VariableDeclaration var = new VariableDeclaration(param.name); VariableDeclaration var = new VariableDeclaration(param.name);
var.initialized = true; // parameters can always be considered initialized
var.type = param.type; var.type = param.type;
this.vars.put(param.name, var); this.vars.put(param.name, var);
} }

View File

@@ -6,6 +6,7 @@ import de.hsrm.compiler.Klang.visitors.Visitor;
public class VariableDeclaration extends Statement { public class VariableDeclaration extends Statement {
public String name; public String name;
public Expression expression; public Expression expression;
public boolean initialized = false; // Whether or not this variable has been initialized
public VariableDeclaration(String name, Expression expression) { public VariableDeclaration(String name, Expression expression) {
this.name = name; this.name = name;