add bool literal
This commit is contained in:
@@ -78,7 +78,8 @@ expression
|
||||
|
||||
atom
|
||||
: INTEGER_LITERAL #intAtom
|
||||
| IDENT # variable
|
||||
| BOOLEAN_LITERAL #boolAtom
|
||||
| IDENT #variable
|
||||
;
|
||||
|
||||
functionCall
|
||||
@@ -137,6 +138,11 @@ INTEGER_LITERAL
|
||||
: [0-9]+
|
||||
;
|
||||
|
||||
BOOLEAN_LITERAL
|
||||
: 'true'
|
||||
| 'false'
|
||||
;
|
||||
|
||||
IDENT
|
||||
: [a-zA-Z][a-zA-Z0-9]*
|
||||
;
|
||||
|
||||
@@ -209,6 +209,13 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
||||
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
|
||||
public Node visitFunctionDef(KlangParser.FunctionDefContext ctx) {
|
||||
String name = ctx.funcName.getText();
|
||||
|
||||
@@ -10,4 +10,8 @@ public class Value {
|
||||
public int asInteger() {
|
||||
return (int) this.value;
|
||||
}
|
||||
|
||||
public boolean asBoolean() {
|
||||
return (boolean) this.value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
20
src/main/java/de/hsrm/compiler/Klang/types/BooleanType.java
Normal file
20
src/main/java/de/hsrm/compiler/Klang/types/BooleanType.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,4 +10,8 @@ public abstract class PrimitiveType extends Type {
|
||||
public boolean isIntegerType() {
|
||||
return false;
|
||||
};
|
||||
|
||||
public boolean isBooleanType() {
|
||||
return false;
|
||||
};
|
||||
}
|
||||
@@ -8,5 +8,9 @@ public abstract class Type {
|
||||
return IntegerType.getType();
|
||||
}
|
||||
|
||||
public static BooleanType getBooleanType() {
|
||||
return BooleanType.getType();
|
||||
}
|
||||
|
||||
public abstract boolean isPrimitiveType();
|
||||
}
|
||||
@@ -23,6 +23,11 @@ public class EvalVisitor implements Visitor<Value> {
|
||||
return new Value(e.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value visit(BooleanExpression e) {
|
||||
return new Value(e.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value visit(EqualityExpression e) {
|
||||
Value lhs = e.lhs.welcome(this);
|
||||
|
||||
@@ -78,6 +78,12 @@ public class GenASM implements Visitor<Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(BooleanExpression e) {
|
||||
this.ex.write(" movq $" + (e.value ? 1 : 0) + ", %rax\n");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(Variable e) {
|
||||
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");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Void visit(ForLoop e) {
|
||||
int lblStart = ++lCount;
|
||||
@@ -338,7 +344,7 @@ public class GenASM implements Visitor<Void> {
|
||||
e.step.welcome(this);
|
||||
this.ex.write(" jmp .L" + lblStart + "\n");
|
||||
this.ex.write(".L" + lblEnd + ":\n");
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -453,7 +459,7 @@ public class GenASM implements Visitor<Void> {
|
||||
this.ex.write("\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(" pushq %rbp\n");
|
||||
this.ex.write(" movq %rsp, %rbp\n");
|
||||
|
||||
@@ -22,6 +22,11 @@ class GetVars implements Visitor<Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(BooleanExpression e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(Variable e) {
|
||||
return null;
|
||||
|
||||
@@ -72,6 +72,12 @@ public class PrettyPrintVisitor implements Visitor<Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(BooleanExpression e) {
|
||||
ex.write(e.value);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(EqualityExpression e) {
|
||||
ex.write("(");
|
||||
|
||||
@@ -9,6 +9,7 @@ import de.hsrm.compiler.Klang.nodes.statements.*;
|
||||
|
||||
public interface Visitor<R> {
|
||||
R visit(IntegerExpression e);
|
||||
R visit(BooleanExpression e);
|
||||
R visit(Variable e);
|
||||
R visit(AdditionExpression e);
|
||||
R visit(EqualityExpression e);
|
||||
|
||||
Reference in New Issue
Block a user