Compare commits
1 Commits
7965c89a60
...
af1021ed66
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af1021ed66 |
@@ -29,7 +29,7 @@ parameter
|
||||
;
|
||||
|
||||
braced_block
|
||||
: OBRK statement+ CBRK
|
||||
: OBRK (statement | functionCall SCOL)+ CBRK
|
||||
;
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ field_assignment
|
||||
;
|
||||
|
||||
return_statement
|
||||
: RETURN expression SCOL
|
||||
: RETURN expression? SCOL
|
||||
;
|
||||
|
||||
destroy_statement
|
||||
@@ -116,6 +116,7 @@ type
|
||||
| BOOLEAN
|
||||
| FLOAT
|
||||
| IDENT
|
||||
| VOID
|
||||
;
|
||||
|
||||
functionCall
|
||||
@@ -181,6 +182,7 @@ DIV: '/';
|
||||
BOOLEAN: 'bool';
|
||||
INTEGER: 'int';
|
||||
FLOAT: 'float';
|
||||
VOID: 'void';
|
||||
|
||||
INTEGER_LITERAL
|
||||
: [0-9]+
|
||||
|
||||
@@ -176,6 +176,11 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
||||
int col = ctx.start.getCharPositionInLine();
|
||||
Type declaredType = Type.getByName(ctx.type_annotation().type().getText());
|
||||
|
||||
if (declaredType.equals(Type.getVoidType())) {
|
||||
String error = "Type " + declaredType.getName() + " can not be used to declare variables.";
|
||||
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
|
||||
}
|
||||
|
||||
if (!declaredType.isPrimitiveType() && this.structs.get(declaredType.getName()) == null) {
|
||||
String error = "Type " + declaredType.getName() + " not defined.";
|
||||
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
|
||||
@@ -244,10 +249,21 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
||||
|
||||
@Override
|
||||
public Node visitReturn_statement(KlangParser.Return_statementContext ctx) {
|
||||
if (currentDeclaredReturnType.equals(Type.getVoidType())) {
|
||||
ReturnStatement result = new ReturnStatement();
|
||||
result.line = ctx.start.getLine();
|
||||
result.type = Type.getVoidType();
|
||||
result.col = ctx.start.getCharPositionInLine();
|
||||
if (ctx.expression() != null) {
|
||||
String error = "Cannot return an expression from a void function.";
|
||||
throw new RuntimeException(Helper.getErrorPrefix(result.line, result.col) + error);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Expression expression = (Expression) this.visit(ctx.expression());
|
||||
ReturnStatement result = new ReturnStatement(expression);
|
||||
result.type = expression.type;
|
||||
result.line = ctx.start.getLine();
|
||||
result.type = expression.type;
|
||||
result.col = ctx.start.getCharPositionInLine();
|
||||
return result;
|
||||
}
|
||||
@@ -721,7 +737,7 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
||||
Type returnType = Type.getByName(ctx.returnType.type().getText());
|
||||
this.currentDeclaredReturnType = returnType;
|
||||
|
||||
if (!returnType.isPrimitiveType() && this.structs.get(returnType.getName()) == null) {
|
||||
if (!returnType.isPrimitiveType() && this.structs.get(returnType.getName()) == null && !returnType.equals(Type.getVoidType())) {
|
||||
String error = "Type " + returnType.getName() + " not defined.";
|
||||
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
|
||||
}
|
||||
@@ -767,6 +783,11 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
||||
int col = ctx.start.getCharPositionInLine();
|
||||
Type type = Type.getByName(ctx.type_annotation().type().getText());
|
||||
|
||||
if (type.equals(Type.getVoidType())) {
|
||||
String error = "Type " + type.getName() + " cannot be used to declare a parameter.";
|
||||
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
|
||||
}
|
||||
|
||||
if (!type.isPrimitiveType() && this.structs.get(type.getName()) == null) {
|
||||
String error = "Type " + type.getName() + " not defined.";
|
||||
throw new RuntimeException(Helper.getErrorPrefix(line, col) + error);
|
||||
|
||||
@@ -12,6 +12,7 @@ import java.util.HashSet;
|
||||
|
||||
import de.hsrm.compiler.Klang.nodes.Node;
|
||||
import de.hsrm.compiler.Klang.nodes.StructDefinition;
|
||||
import de.hsrm.compiler.Klang.types.Type;
|
||||
import de.hsrm.compiler.Klang.visitors.*;
|
||||
import de.hsrm.compiler.Klang.helper.*;
|
||||
|
||||
@@ -115,7 +116,8 @@ public class Klang {
|
||||
PrettyPrintVisitor.ExWriter ex = new PrettyPrintVisitor.ExWriter(w);
|
||||
PrettyPrintVisitor printVisitor = new PrettyPrintVisitor(ex);
|
||||
root.welcome(printVisitor);
|
||||
System.out.println(w.toString());
|
||||
generateOutput(out, w.toString());
|
||||
return;
|
||||
}
|
||||
|
||||
if (evaluate) {
|
||||
@@ -123,7 +125,11 @@ public class Klang {
|
||||
System.out.println("\nEvaluating the source code:");
|
||||
EvalVisitor evalVisitor = new EvalVisitor(structs);
|
||||
Value result = root.welcome(evalVisitor);
|
||||
generateOutput(out, "Result was: " + result.asObject().toString());
|
||||
if (result.type.equals(Type.getVoidType())) {
|
||||
generateOutput(out, "Result was void");
|
||||
} else {
|
||||
generateOutput(out, "Result was: " + result.asObject().toString());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,10 @@ public class ReturnStatement extends Statement {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
public ReturnStatement() {
|
||||
this.expression = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> R welcome(Visitor<R> v) {
|
||||
return v.visit(this);
|
||||
|
||||
@@ -20,12 +20,17 @@ public abstract class Type {
|
||||
return NullType.getType();
|
||||
}
|
||||
|
||||
public static VoidType getVoidType() {
|
||||
return VoidType.getType();
|
||||
}
|
||||
|
||||
public static Type getByName(String name) {
|
||||
switch (name) {
|
||||
case "bool": return getBooleanType();
|
||||
case "int": return getIntegerType();
|
||||
case "float": return getFloatType();
|
||||
case "null": return getNullType();
|
||||
case "void": return getVoidType();
|
||||
default: return new StructType(name);
|
||||
}
|
||||
}
|
||||
|
||||
38
src/main/java/de/hsrm/compiler/Klang/types/VoidType.java
Normal file
38
src/main/java/de/hsrm/compiler/Klang/types/VoidType.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package de.hsrm.compiler.Klang.types;
|
||||
|
||||
public class VoidType extends Type {
|
||||
|
||||
private static VoidType instance;
|
||||
|
||||
public static VoidType getType() {
|
||||
if (instance != null) {
|
||||
return instance;
|
||||
}
|
||||
instance = new VoidType();
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "void";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type combine(Type that) {
|
||||
if (that.equals(this)) {
|
||||
return this;
|
||||
}
|
||||
throw new RuntimeException("Type missmatch: cannot combine " + this.getName() + " and " + that.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrimitiveType() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNumericType() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -413,6 +413,9 @@ public class EvalVisitor implements Visitor<Value> {
|
||||
|
||||
@Override
|
||||
public Value visit(ReturnStatement e) {
|
||||
if (e.expression == null) {
|
||||
return new Value(null, Type.getVoidType());
|
||||
}
|
||||
return e.expression.welcome(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -589,7 +589,9 @@ public class GenASM implements Visitor<Void> {
|
||||
|
||||
@Override
|
||||
public Void visit(ReturnStatement e) {
|
||||
e.expression.welcome(this);
|
||||
if (e.expression != null) {
|
||||
e.expression.welcome(this);
|
||||
}
|
||||
this.ex.write(" movq %rbp, %rsp\n");
|
||||
this.ex.write(" popq %rbp\n");
|
||||
this.ex.write(" ret\n");
|
||||
|
||||
@@ -194,7 +194,9 @@ class GetVars implements Visitor<Void> {
|
||||
|
||||
@Override
|
||||
public Void visit(ReturnStatement e) {
|
||||
e.expression.welcome(this);
|
||||
if (e.expression != null) {
|
||||
e.expression.welcome(this);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -304,8 +304,11 @@ public class PrettyPrintVisitor implements Visitor<Void> {
|
||||
|
||||
@Override
|
||||
public Void visit(ReturnStatement e) {
|
||||
ex.write("return ");
|
||||
e.expression.welcome(this);
|
||||
ex.write("return");
|
||||
if (e.expression != null) {
|
||||
ex.write(" ");
|
||||
e.expression.welcome(this);
|
||||
}
|
||||
ex.write(";");
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user