always add the declared type as the type of a variable declaration. do not use the combined type as the type of the variable that is declared

This commit is contained in:
2020-02-04 11:21:48 +01:00
parent 47390f6757
commit 1ae404b978

View File

@@ -187,12 +187,11 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
if (ctx.expression() != null) { if (ctx.expression() != null) {
Node expression = this.visit(ctx.expression()); Node expression = this.visit(ctx.expression());
try { try {
declaredType = declaredType.combine(expression.type); declaredType.combine(expression.type);
} catch (Exception e) { } catch (Exception e) {
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.type = declaredType; // add the type only if there is an expression
} else { } else {
result = new VariableDeclaration(name); result = new VariableDeclaration(name);
} }
@@ -202,6 +201,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
result.line = line; result.line = line;
result.col = col; result.col = col;
result.type = declaredType;
return result; return result;
} }