implement constructor call, eval and genasm are still stubs
This commit is contained in:
@@ -770,4 +770,44 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
||||
result.col = col;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node visitConstructorCallExpression(KlangParser.ConstructorCallExpressionContext ctx) {
|
||||
String name = ctx.IDENT().getText();
|
||||
int line = ctx.start.getLine();
|
||||
int col = ctx.start.getCharPositionInLine();
|
||||
|
||||
// Get the corresponding struct definition
|
||||
var struct = this.structs.get(name);
|
||||
if (struct == null) {
|
||||
String error = "Struct with name \"" + name + "\" not defined.";
|
||||
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
|
||||
}
|
||||
|
||||
// Make sure the number of arguments match the number of struct fields
|
||||
int fieldCount = struct.fields.length;
|
||||
int argCount = ctx.arguments().expression().size();
|
||||
if (argCount != fieldCount) {
|
||||
String error = "Struct \"" + name + "\" defined " + fieldCount + " fields, but got " + argCount + " constructor parameters.";
|
||||
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
|
||||
}
|
||||
|
||||
// Evaluate each expression
|
||||
Expression[] args = new Expression[argCount];
|
||||
for (int i = 0; i < argCount; i++) {
|
||||
Expression expr = (Expression) this.visit(ctx.arguments().expression(i));
|
||||
try {
|
||||
expr.type.combine(struct.fields[i].type); // Make sure the types are matching
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(Helper.getErrorPrefix(expr.line, expr.col) + "argument " + i + " " + e.getMessage());
|
||||
}
|
||||
args[i] = expr;
|
||||
}
|
||||
|
||||
ConstructorCall result = new ConstructorCall(name, args);
|
||||
result.type = struct.type;
|
||||
result.line = line;
|
||||
result.col = col;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -39,20 +39,20 @@ public class Klang {
|
||||
String out = null;
|
||||
|
||||
List<String> arguments = Arrays.asList(args);
|
||||
if (arguments.size() <= 0 || arguments.contains("-h") || arguments.contains("--help") || arguments.contains("?")) {
|
||||
System.out.println("\nKaiser Lang Compiler");
|
||||
System.out.println("Authors: Dennis Kaiser and Marvin Kaiser");
|
||||
System.out.println("");
|
||||
System.out.println("Last argument must be file");
|
||||
System.out.println("");
|
||||
System.out.println("Arguments:");
|
||||
System.out.println("--out <file>:\t File to write to");
|
||||
System.out.println("--evaluate:\t Evaluates the given source code");
|
||||
System.out.println("--pretty:\t Pretty print the given source code");
|
||||
System.out
|
||||
.println("--no-main:\t Do not generate main function, will be generated as 'start'. Useful for testing");
|
||||
return;
|
||||
}
|
||||
// if (arguments.size() <= 0 || arguments.contains("-h") || arguments.contains("--help") || arguments.contains("?")) {
|
||||
// System.out.println("\nKaiser Lang Compiler");
|
||||
// System.out.println("Authors: Dennis Kaiser and Marvin Kaiser");
|
||||
// System.out.println("");
|
||||
// System.out.println("Last argument must be file");
|
||||
// System.out.println("");
|
||||
// System.out.println("Arguments:");
|
||||
// System.out.println("--out <file>:\t File to write to");
|
||||
// System.out.println("--evaluate:\t Evaluates the given source code");
|
||||
// System.out.println("--pretty:\t Pretty print the given source code");
|
||||
// System.out
|
||||
// .println("--no-main:\t Do not generate main function, will be generated as 'start'. Useful for testing");
|
||||
// return;
|
||||
// }
|
||||
if (arguments.contains("--evaluate")) {
|
||||
evaluate = true;
|
||||
}
|
||||
@@ -71,7 +71,7 @@ public class Klang {
|
||||
}
|
||||
|
||||
// create a CharStream that reads from standard input
|
||||
CharStream input = CharStreams.fromFileName(arguments.get(arguments.size() - 1));
|
||||
CharStream input = CharStreams.fromFileName("code.k");
|
||||
|
||||
// create a lexer that feeds off of input CharStream
|
||||
KlangLexer lexer = new KlangLexer(input);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package de.hsrm.compiler.Klang.nodes.expressions;
|
||||
|
||||
import de.hsrm.compiler.Klang.visitors.Visitor;
|
||||
|
||||
public class ConstructorCall extends Expression {
|
||||
|
||||
public String structName;
|
||||
public Expression[] args;
|
||||
|
||||
public ConstructorCall(String structName, Expression[] args) {
|
||||
this.structName = structName;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> R welcome(Visitor<R> v) {
|
||||
return v.visit(this);
|
||||
}
|
||||
}
|
||||
@@ -490,4 +490,10 @@ public class EvalVisitor implements Visitor<Value> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value visit(ConstructorCall e) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -813,4 +813,10 @@ public class GenASM implements Visitor<Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(ConstructorCall e) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -255,4 +255,9 @@ class GetVars implements Visitor<Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(ConstructorCall e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -415,4 +415,21 @@ public class PrettyPrintVisitor implements Visitor<Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(ConstructorCall e) {
|
||||
ex.write("create " + e.structName + "(");
|
||||
boolean first = true;
|
||||
for (Expression arg : e.args) {
|
||||
if (!first) {
|
||||
ex.write(", ");
|
||||
} else {
|
||||
first = false;
|
||||
}
|
||||
arg.welcome(this);
|
||||
}
|
||||
ex.write(")");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -46,4 +46,5 @@ public interface Visitor<R> {
|
||||
R visit(StructDefinition e);
|
||||
R visit(StructField e);
|
||||
R visit(StructFieldAccessExpression e);
|
||||
R visit(ConstructorCall e);
|
||||
}
|
||||
Reference in New Issue
Block a user