Merge branch '10-vergleichsoperatoren' into 'master'

Resolve "Vergleichsoperatoren"

Closes #10

See merge request mkais001/klang!5
This commit is contained in:
Marvin Kaiser
2019-12-20 10:02:10 +01:00
19 changed files with 521 additions and 3 deletions

View File

@@ -18,7 +18,7 @@ runTest: ./src/test/test
./src/test/test
./src/test/test: ./src/test/tests.s
gcc -o ./src/test/test ./src/test/tests.s ./src/test/functionCall/functionCall.c ./src/test/recursive/recursive.c ./src/test/testCode.c
gcc -o ./src/test/test ./src/test/tests.s ./src/test/functionCall/functionCall.c ./src/test/recursive/recursive.c ./src/test/comparison/comparison.c ./src/test/testCode.c
./src/test/tests.s: target/klang-1.0-jar-with-dependencies.jar
java -cp target/klang-1.0-jar-with-dependencies.jar de.hsrm.compiler.Klang.Klang < ./src/test/tests.k > ./src/test/tests.s

View File

@@ -55,6 +55,13 @@ expression
| OPAR lhs=expression MUL rhs=expression CPAR #multiplicationExpression
| OPAR lhs=expression DIV rhs=expression CPAR #divisionExpression
| OPAR lhs=expression MOD rhs=expression CPAR #moduloExpression
| OPAR lhs=expression EQEQ rhs=expression CPAR #equalityExpression
| OPAR lhs=expression NEQ rhs=expression CPAR #NotEqualityExpression
| OPAR lhs=expression LT rhs=expression CPAR #lessThanExpression
| OPAR lhs=expression GT rhs=expression CPAR #greaterThanExpression
| OPAR lhs=expression LTE rhs=expression CPAR #lessThanOrEqualToExpression
| OPAR lhs=expression GTE rhs=expression CPAR #GreaterThanOrEqualToExpression
| SUB expression #negateExpression
| functionCall #functionCallExpression
;
@@ -86,6 +93,12 @@ OPAR: '(';
CPAR: ')';
COMMA: ',';
EQUAL: '=';
EQEQ: '==';
NEQ: '!=';
LT: '<';
GT: '>';
LTE: '<=';
GTE: '>=';
MUL: '*';
ADD: '+';

View File

@@ -97,6 +97,36 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
return new AdditionExpression((Expression) this.visit(ctx.lhs), (Expression) this.visit(ctx.rhs));
}
@Override
public Node visitEqualityExpression(KlangParser.EqualityExpressionContext ctx) {
return new EqualityExpression((Expression) this.visit(ctx.lhs), (Expression) this.visit(ctx.rhs));
}
@Override
public Node visitNotEqualityExpression(KlangParser.NotEqualityExpressionContext ctx) {
return new NotEqualityExpression((Expression) this.visit(ctx.lhs), (Expression) this.visit(ctx.rhs));
}
@Override
public Node visitLessThanExpression(KlangParser.LessThanExpressionContext ctx) {
return new LTExpression((Expression) this.visit(ctx.lhs), (Expression) this.visit(ctx.rhs));
}
@Override
public Node visitGreaterThanExpression(KlangParser.GreaterThanExpressionContext ctx) {
return new GTExpression((Expression) this.visit(ctx.lhs), (Expression) this.visit(ctx.rhs));
}
@Override
public Node visitLessThanOrEqualToExpression(KlangParser.LessThanOrEqualToExpressionContext ctx) {
return new LTEExpression((Expression) this.visit(ctx.lhs), (Expression) this.visit(ctx.rhs));
}
@Override
public Node visitGreaterThanOrEqualToExpression(KlangParser.GreaterThanOrEqualToExpressionContext ctx) {
return new GTEExpression((Expression) this.visit(ctx.lhs), (Expression) this.visit(ctx.rhs));
}
@Override
public Node visitSubstractionExpression(KlangParser.SubstractionExpressionContext ctx) {
return new SubstractionExpression((Expression) this.visit(ctx.lhs), (Expression) this.visit(ctx.rhs));

View File

@@ -0,0 +1,14 @@
package de.hsrm.compiler.Klang.nodes.expressions;
import de.hsrm.compiler.Klang.visitors.Visitor;
public class EqualityExpression extends BinaryExpression {
public EqualityExpression(Expression lhs, Expression rhs) {
super(lhs, rhs);
}
@Override
public <R> R welcome(Visitor<R> v) {
return v.visit(this);
}
}

View File

@@ -0,0 +1,14 @@
package de.hsrm.compiler.Klang.nodes.expressions;
import de.hsrm.compiler.Klang.visitors.Visitor;
public class GTEExpression extends BinaryExpression {
public GTEExpression(Expression lhs, Expression rhs) {
super(lhs, rhs);
}
@Override
public <R> R welcome(Visitor<R> v) {
return v.visit(this);
}
}

View File

@@ -0,0 +1,14 @@
package de.hsrm.compiler.Klang.nodes.expressions;
import de.hsrm.compiler.Klang.visitors.Visitor;
public class GTExpression extends BinaryExpression {
public GTExpression(Expression lhs, Expression rhs) {
super(lhs, rhs);
}
@Override
public <R> R welcome(Visitor<R> v) {
return v.visit(this);
}
}

View File

@@ -0,0 +1,14 @@
package de.hsrm.compiler.Klang.nodes.expressions;
import de.hsrm.compiler.Klang.visitors.Visitor;
public class LTEExpression extends BinaryExpression {
public LTEExpression(Expression lhs, Expression rhs) {
super(lhs, rhs);
}
@Override
public <R> R welcome(Visitor<R> v) {
return v.visit(this);
}
}

View File

@@ -0,0 +1,14 @@
package de.hsrm.compiler.Klang.nodes.expressions;
import de.hsrm.compiler.Klang.visitors.Visitor;
public class LTExpression extends BinaryExpression {
public LTExpression(Expression lhs, Expression rhs) {
super(lhs, rhs);
}
@Override
public <R> R welcome(Visitor<R> v) {
return v.visit(this);
}
}

View File

@@ -0,0 +1,14 @@
package de.hsrm.compiler.Klang.nodes.expressions;
import de.hsrm.compiler.Klang.visitors.Visitor;
public class NotEqualityExpression extends BinaryExpression {
public NotEqualityExpression(Expression lhs, Expression rhs) {
super(lhs, rhs);
}
@Override
public <R> R welcome(Visitor<R> v) {
return v.visit(this);
}
}

View File

@@ -20,6 +20,66 @@ public class EvalVisitor implements Visitor<Value> {
return new Value(e.value);
}
@Override
public Value visit(EqualityExpression e) {
Value lhs = e.lhs.welcome(this);
Value rhs = e.rhs.welcome(this);
if (lhs.asInteger() == rhs.asInteger()) {
return new Value(1);
}
return new Value(0);
}
@Override
public Value visit(NotEqualityExpression e) {
Value lhs = e.lhs.welcome(this);
Value rhs = e.rhs.welcome(this);
if (lhs.asInteger() != rhs.asInteger()) {
return new Value(1);
}
return new Value(0);
}
@Override
public Value visit(GTExpression e) {
Value lhs = e.lhs.welcome(this);
Value rhs = e.rhs.welcome(this);
if (lhs.asInteger() > rhs.asInteger()) {
return new Value(1);
}
return new Value(0);
}
@Override
public Value visit(GTEExpression e) {
Value lhs = e.lhs.welcome(this);
Value rhs = e.rhs.welcome(this);
if (lhs.asInteger() >= rhs.asInteger()) {
return new Value(1);
}
return new Value(0);
}
@Override
public Value visit(LTExpression e) {
Value lhs = e.lhs.welcome(this);
Value rhs = e.rhs.welcome(this);
if (lhs.asInteger() < rhs.asInteger()) {
return new Value(1);
}
return new Value(0);
}
@Override
public Value visit(LTEExpression e) {
Value lhs = e.lhs.welcome(this);
Value rhs = e.rhs.welcome(this);
if (lhs.asInteger() <= rhs.asInteger()) {
return new Value(1);
}
return new Value(0);
}
@Override
public Value visit(AdditionExpression e) {
Value lhs = e.lhs.welcome(this);

View File

@@ -74,6 +74,132 @@ public class GenASM implements Visitor<Void> {
return null;
}
@Override
public Void visit(EqualityExpression e) {
int lblTrue = ++lCount;
int lblEnd = ++lCount;
e.lhs.welcome(this);
this.ex.write(" pushq %rax\n");
e.rhs.welcome(this);
this.ex.write(" popq %rbx\n");
this.ex.write(" cmp %rax, %rbx\n");
this.ex.write(" je .L" + lblTrue + "\n");
// false
this.ex.write(" movq $0, %rax\n");
this.ex.write(" jmp .L" + lblEnd + "\n");
this.ex.write(".L" + lblTrue + ":\n");
// true
this.ex.write(" movq $1, %rax\n");
this.ex.write(".L" + lblEnd + ":\n");
return null;
}
@Override
public Void visit(NotEqualityExpression e) {
int lblTrue = ++lCount;
int lblEnd = ++lCount;
e.lhs.welcome(this);
this.ex.write(" pushq %rax\n");
e.rhs.welcome(this);
this.ex.write(" popq %rbx\n");
this.ex.write(" cmp %rax, %rbx\n");
this.ex.write(" jne .L" + lblTrue + "\n");
// false
this.ex.write(" movq $0, %rax\n");
this.ex.write(" jmp .L" + lblEnd + "\n");
this.ex.write(".L" + lblTrue + ":\n");
// true
this.ex.write(" movq $1, %rax\n");
this.ex.write(".L" + lblEnd + ":\n");
return null;
}
@Override
public Void visit(GTExpression e) {
int lblTrue = ++lCount;
int lblEnd = ++lCount;
e.lhs.welcome(this);
this.ex.write(" pushq %rax\n");
e.rhs.welcome(this);
this.ex.write(" popq %rbx\n");
this.ex.write(" cmp %rax, %rbx\n");
this.ex.write(" jg .L" + lblTrue + "\n");
// false
this.ex.write(" movq $0, %rax\n");
this.ex.write(" jmp .L" + lblEnd + "\n");
this.ex.write(".L" + lblTrue + ":\n");
// true
this.ex.write(" movq $1, %rax\n");
this.ex.write(".L" + lblEnd + ":\n");
return null;
}
@Override
public Void visit(GTEExpression e) {
int lblTrue = ++lCount;
int lblEnd = ++lCount;
e.lhs.welcome(this);
this.ex.write(" pushq %rax\n");
e.rhs.welcome(this);
this.ex.write(" popq %rbx\n");
this.ex.write(" cmp %rax, %rbx\n");
this.ex.write(" jge .L" + lblTrue + "\n");
// false
this.ex.write(" movq $0, %rax\n");
this.ex.write(" jmp .L" + lblEnd + "\n");
this.ex.write(".L" + lblTrue + ":\n");
// true
this.ex.write(" movq $1, %rax\n");
this.ex.write(".L" + lblEnd + ":\n");
return null;
}
@Override
public Void visit(LTExpression e) {
int lblTrue = ++lCount;
int lblEnd = ++lCount;
e.lhs.welcome(this);
this.ex.write(" pushq %rax\n");
e.rhs.welcome(this);
this.ex.write(" popq %rbx\n");
this.ex.write(" cmp %rax, %rbx\n");
this.ex.write(" jl .L" + lblTrue + "\n");
// false
this.ex.write(" movq $0, %rax\n");
this.ex.write(" jmp .L" + lblEnd + "\n");
this.ex.write(".L" + lblTrue + ":\n");
// true
this.ex.write(" movq $1, %rax\n");
this.ex.write(".L" + lblEnd + ":\n");
return null;
}
@Override
public Void visit(LTEExpression e) {
int lblTrue = ++lCount;
int lblEnd = ++lCount;
e.lhs.welcome(this);
this.ex.write(" pushq %rax\n");
e.rhs.welcome(this);
this.ex.write(" popq %rbx\n");
this.ex.write(" cmp %rax, %rbx\n");
this.ex.write(" jle .L" + lblTrue + "\n");
// false
this.ex.write(" movq $0, %rax\n");
this.ex.write(" jmp .L" + lblEnd + "\n");
this.ex.write(".L" + lblTrue + ":\n");
// true
this.ex.write(" movq $1, %rax\n");
this.ex.write(".L" + lblEnd + ":\n");
return null;
}
@Override
public Void visit(AdditionExpression e) {
e.lhs.welcome(this);
@@ -286,4 +412,5 @@ public class GenASM implements Visitor<Void> {
this.ex.write(" ret\n");
return null;
}
}

View File

@@ -24,6 +24,48 @@ class GetVars implements Visitor<Void> {
return null;
}
@Override
public Void visit(EqualityExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(NotEqualityExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(GTExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(GTEExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(LTExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(LTEExpression e) {
e.lhs.welcome(this);
e.rhs.welcome(this);
return null;
}
@Override
public Void visit(AdditionExpression e) {
e.lhs.welcome(this);

View File

@@ -71,6 +71,66 @@ public class PrettyPrintVisitor implements Visitor<Void> {
return null;
}
@Override
public Void visit(EqualityExpression e) {
ex.write("(");
e.lhs.welcome(this);
ex.write(" == ");
e.rhs.welcome(this);
ex.write(")");
return null;
}
@Override
public Void visit(NotEqualityExpression e) {
ex.write("(");
e.lhs.welcome(this);
ex.write(" != ");
e.rhs.welcome(this);
ex.write(")");
return null;
}
@Override
public Void visit(GTExpression e) {
ex.write("(");
e.lhs.welcome(this);
ex.write(" > ");
e.rhs.welcome(this);
ex.write(")");
return null;
}
@Override
public Void visit(GTEExpression e) {
ex.write("(");
e.lhs.welcome(this);
ex.write(" >= ");
e.rhs.welcome(this);
ex.write(")");
return null;
}
@Override
public Void visit(LTExpression e) {
ex.write("(");
e.lhs.welcome(this);
ex.write(" < ");
e.rhs.welcome(this);
ex.write(")");
return null;
}
@Override
public Void visit(LTEExpression e) {
ex.write("(");
e.lhs.welcome(this);
ex.write(" <= ");
e.rhs.welcome(this);
ex.write(")");
return null;
}
@Override
public Void visit(AdditionExpression e) {
ex.write("(");
@@ -121,7 +181,6 @@ public class PrettyPrintVisitor implements Visitor<Void> {
return null;
}
@Override
public Void visit(NegateExpression e) {
ex.write(" - ");

View File

@@ -10,6 +10,12 @@ public interface Visitor<R> {
R visit(IntegerExpression e);
R visit(Variable e);
R visit(AdditionExpression e);
R visit(EqualityExpression e);
R visit(NotEqualityExpression e);
R visit(GTExpression e);
R visit(GTEExpression e);
R visit(LTExpression e);
R visit(LTEExpression e);
R visit(SubstractionExpression e);
R visit(MultiplicationExpression e);
R visit(DivisionExpression e);

View File

@@ -0,0 +1,56 @@
#include <stdio.h>
#include "comparison.h"
void printSuccessComp(char* name, int x, int y, int expected, int result) {
printf("SUCCESS:\t%d %s %d\tGOT: %d\tExpected: %d\n", x, name, y, result, expected);
}
void printErrorComp(char* name, int x, int y, int expected, int result) {
printf("ERROR:\t\t%d %s %d\tGOT: %d\tExpected: %d\n", x, name, y, result, expected);
}
int comparisonTest(char* name, int x, int y, int expected, int result) {
if (expected == result) {
printSuccessComp(name, x, y, expected, result);
return 0;
} else {
printErrorComp(name, x, y, expected, result);
return 1;
}
}
int runComparisonTests() {
printf("\nComparison Tests \n");
int failed = 0;
failed += comparisonTest("==", 1, 1, 1, eq(1, 1));
failed += comparisonTest("==", 1, 0, 0, eq(1, 0));
failed += comparisonTest("==", 0, 1, 0, eq(0, 1));
failed += comparisonTest("==", 0, 0, 1, eq(0, 0));
failed += comparisonTest("!=", 1, 1, 0, neq(1, 1));
failed += comparisonTest("!=", 1, 0, 1, neq(1, 0));
failed += comparisonTest("!=", 0, 1, 1, neq(0, 1));
failed += comparisonTest("!=", 0, 0, 0, neq(0, 0));
failed += comparisonTest("<", 1, 1, 0, lt(1, 1));
failed += comparisonTest("<", 1, 0, 0, lt(1, 0));
failed += comparisonTest("<", 0, 1, 1, lt(0, 1));
failed += comparisonTest("<", 0, 0, 0, lt(0, 0));
failed += comparisonTest("<=", 1, 1, 1, lte(1, 1));
failed += comparisonTest("<=", 1, 0, 0, lte(1, 0));
failed += comparisonTest("<=", 0, 1, 1, lte(0, 1));
failed += comparisonTest("<=", 0, 0, 1, lte(0, 0));
failed += comparisonTest(">", 1, 1, 0, gt(1, 1));
failed += comparisonTest(">", 1, 0, 1, gt(1, 0));
failed += comparisonTest(">", 0, 1, 0, gt(0, 1));
failed += comparisonTest(">", 0, 0, 0, gt(0, 0));
failed += comparisonTest(">=", 1, 1, 1, gte(1, 1));
failed += comparisonTest(">=", 1, 0, 1, gte(1, 0));
failed += comparisonTest(">=", 0, 1, 0, gte(0, 1));
failed += comparisonTest(">=", 0, 0, 1, gte(0, 0));
return failed;
}

View File

@@ -0,0 +1,6 @@
int eq(int x, int y);
int neq(int x, int y);
int lt(int x, int y);
int lte(int x, int y);
int gt(int x, int y);
int gte(int x, int y);

View File

@@ -111,6 +111,9 @@ int main(){
// Tests for recursive funtions
failed += runRecursiveTests();
// Tests for comparison expressions
failed += runComparisonTests();
printf("\n=== Failed Tests: %d\n", failed);
if (failed > 0) {

View File

@@ -3,4 +3,5 @@
*/
int runFunctionCallTests();
int runRecursiveTests();
int runRecursiveTests();
int runComparisonTests();

View File

@@ -49,6 +49,7 @@ function get3() {
function arg4(a, b, c, d, e, f, g, h, i ,j) {
return d;
}
function get4() {
return arg4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
@@ -56,6 +57,7 @@ function get4() {
function arg5(a, b, c, d, e, f, g, h, i ,j) {
return e;
}
function get5() {
return arg5(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
@@ -63,6 +65,7 @@ function get5() {
function arg6(a, b, c, d, e, f, g, h, i ,j) {
return f;
}
function get6() {
return arg6(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
@@ -70,6 +73,7 @@ function get6() {
function arg7(a, b, c, d, e, f, g, h, i ,j) {
return g;
}
function get7() {
return arg7(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
@@ -77,6 +81,7 @@ function get7() {
function arg8(a, b, c, d, e, f, g, h, i ,j) {
return h;
}
function get8() {
return arg8(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
@@ -84,6 +89,7 @@ function get8() {
function arg9(a, b, c, d, e, f, g, h, i ,j) {
return i;
}
function get9() {
return arg9(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
@@ -91,6 +97,7 @@ function get9() {
function arg10(a, b, c, d, e, f, g, h, i ,j) {
return j;
}
function get10() {
return arg10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
@@ -102,4 +109,28 @@ function fac(x) {
return 1;
}
function eq(x, y) {
return (x == y);
}
function neq(x, y) {
return (x != y);
}
function lt(x, y) {
return (x < y);
}
function lte(x, y) {
return (x <= y);
}
function gt(x, y) {
return (x > y);
}
function gte(x, y) {
return (x >= y);
}
add(1, 1);