add bool literal

This commit is contained in:
2020-01-20 15:26:55 +01:00
parent 1095d9dd83
commit 1186bb4550
12 changed files with 88 additions and 4 deletions

View File

@@ -78,7 +78,8 @@ expression
atom atom
: INTEGER_LITERAL #intAtom : INTEGER_LITERAL #intAtom
| IDENT # variable | BOOLEAN_LITERAL #boolAtom
| IDENT #variable
; ;
functionCall functionCall
@@ -137,6 +138,11 @@ INTEGER_LITERAL
: [0-9]+ : [0-9]+
; ;
BOOLEAN_LITERAL
: 'true'
| 'false'
;
IDENT IDENT
: [a-zA-Z][a-zA-Z0-9]* : [a-zA-Z][a-zA-Z0-9]*
; ;

View File

@@ -209,6 +209,13 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
return n; return n;
} }
@Override
public Node visitBoolAtom(KlangParser.BoolAtomContext ctx) {
Node n = new BooleanExpression(ctx.getText().equals("true") ? true : false);
n.type = Type.getBooleanType();
return n;
}
@Override @Override
public Node visitFunctionDef(KlangParser.FunctionDefContext ctx) { public Node visitFunctionDef(KlangParser.FunctionDefContext ctx) {
String name = ctx.funcName.getText(); String name = ctx.funcName.getText();

View File

@@ -10,4 +10,8 @@ public class Value {
public int asInteger() { public int asInteger() {
return (int) this.value; return (int) this.value;
} }
public boolean asBoolean() {
return (boolean) this.value;
}
} }

View File

@@ -0,0 +1,16 @@
package de.hsrm.compiler.Klang.nodes.expressions;
import de.hsrm.compiler.Klang.visitors.Visitor;
public class BooleanExpression extends Expression {
public boolean value;
public BooleanExpression(boolean value) {
this.value = value;
}
@Override
public <R> R welcome(Visitor<R> v) {
return v.visit(this);
}
}

View File

@@ -0,0 +1,20 @@
package de.hsrm.compiler.Klang.types;
public class BooleanType extends PrimitiveType {
private static BooleanType instance = null;
public static BooleanType getType() {
if (instance != null) {
return instance;
}
instance = new BooleanType();
return instance;
}
@Override
public boolean isBooleanType() {
return true;
}
}

View File

@@ -10,4 +10,8 @@ public abstract class PrimitiveType extends Type {
public boolean isIntegerType() { public boolean isIntegerType() {
return false; return false;
}; };
public boolean isBooleanType() {
return false;
};
} }

View File

@@ -8,5 +8,9 @@ public abstract class Type {
return IntegerType.getType(); return IntegerType.getType();
} }
public static BooleanType getBooleanType() {
return BooleanType.getType();
}
public abstract boolean isPrimitiveType(); public abstract boolean isPrimitiveType();
} }

View File

@@ -23,6 +23,11 @@ public class EvalVisitor implements Visitor<Value> {
return new Value(e.value); return new Value(e.value);
} }
@Override
public Value visit(BooleanExpression e) {
return new Value(e.value);
}
@Override @Override
public Value visit(EqualityExpression e) { public Value visit(EqualityExpression e) {
Value lhs = e.lhs.welcome(this); Value lhs = e.lhs.welcome(this);

View File

@@ -78,6 +78,12 @@ public class GenASM implements Visitor<Void> {
return null; return null;
} }
@Override
public Void visit(BooleanExpression e) {
this.ex.write(" movq $" + (e.value ? 1 : 0) + ", %rax\n");
return null;
}
@Override @Override
public Void visit(Variable e) { public Void visit(Variable e) {
this.ex.write(" movq " + this.env.get(e.name) + "(%rbp), %rax\n"); this.ex.write(" movq " + this.env.get(e.name) + "(%rbp), %rax\n");
@@ -324,7 +330,7 @@ public class GenASM implements Visitor<Void> {
this.ex.write(" jnz .L" + lblStart + "\n"); this.ex.write(" jnz .L" + lblStart + "\n");
return null; return null;
} }
@Override @Override
public Void visit(ForLoop e) { public Void visit(ForLoop e) {
int lblStart = ++lCount; int lblStart = ++lCount;
@@ -338,7 +344,7 @@ public class GenASM implements Visitor<Void> {
e.step.welcome(this); e.step.welcome(this);
this.ex.write(" jmp .L" + lblStart + "\n"); this.ex.write(" jmp .L" + lblStart + "\n");
this.ex.write(".L" + lblEnd + ":\n"); this.ex.write(".L" + lblEnd + ":\n");
return null; return null;
} }
@@ -453,7 +459,7 @@ public class GenASM implements Visitor<Void> {
this.ex.write("\n"); this.ex.write("\n");
} }
this.ex.write(".globl " + mainName + "\n"); this.ex.write(".globl " + mainName + "\n");
this.ex.write(".type " +mainName + ", @function\n"); this.ex.write(".type " + mainName + ", @function\n");
this.ex.write(mainName + ":\n"); this.ex.write(mainName + ":\n");
this.ex.write(" pushq %rbp\n"); this.ex.write(" pushq %rbp\n");
this.ex.write(" movq %rsp, %rbp\n"); this.ex.write(" movq %rsp, %rbp\n");

View File

@@ -22,6 +22,11 @@ class GetVars implements Visitor<Void> {
return null; return null;
} }
@Override
public Void visit(BooleanExpression e) {
return null;
}
@Override @Override
public Void visit(Variable e) { public Void visit(Variable e) {
return null; return null;

View File

@@ -72,6 +72,12 @@ public class PrettyPrintVisitor implements Visitor<Void> {
return null; return null;
} }
@Override
public Void visit(BooleanExpression e) {
ex.write(e.value);
return null;
}
@Override @Override
public Void visit(EqualityExpression e) { public Void visit(EqualityExpression e) {
ex.write("("); ex.write("(");

View File

@@ -9,6 +9,7 @@ import de.hsrm.compiler.Klang.nodes.statements.*;
public interface Visitor<R> { public interface Visitor<R> {
R visit(IntegerExpression e); R visit(IntegerExpression e);
R visit(BooleanExpression e);
R visit(Variable e); R visit(Variable e);
R visit(AdditionExpression e); R visit(AdditionExpression e);
R visit(EqualityExpression e); R visit(EqualityExpression e);