Skip to content

Commit

Permalink
Add modulo expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Franco Montenegro committed May 25, 2016
1 parent f0f8047 commit 4a3d013
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.ruke.vrjassc.translator.expression;

public class ModuloExpression extends Expression {

Expression a;
Expression b;

public ModuloExpression(Expression a, Expression b) {
this.a = a;
this.b = b;

this.a.setParent(this);
this.b.setParent(this);
}

@Override
public String translate() {
return "ModuloReal(" + this.a.translate() + "," + this.b.translate() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ public Expression visitInteger(IntegerContext ctx) {
return new RawExpression(ctx.getText(), this.scope.resolve("integer"));
}

@Override
public Expression visitModulo(ModuloContext ctx) {
Expression a = this.visit(ctx.left);
Expression b = this.visit(ctx.right);

return new ModuloExpression(a, b);
}

@Override
public Expression visitDiv(DivContext ctx) {
Expression a = this.visit(ctx.left);
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/ruke/vrjassc/vrjassc/compiler/FunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@

public class FunctionTest extends TestHelper {

@Test
public void modulo() {
String code =
"function foo\n" +
"local integer i = 2 % 1\n" +
"local real r = 2.1 % 1\n" +
"end";

String expected =
"globals\n" +
"endglobals\n" +
"function foo takes nothing returns nothing\n" +
"local integer i=ModuloReal(2,1)\n" +
"local real r=ModuloReal(2.1,1)\n" +
"endfunction";

assertEquals(expected, this.run(code));
}

@Test
public void continueStatement() {
String code =
Expand Down
2 changes: 2 additions & 0 deletions vrjass.g4
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ expression
| cast #ignoreCastExpression
| MINUS expression #Negative
| NOT expression #Not
| left=expression MOD right=expression #Modulo
| left=expression DIV right=expression #Div
| left=expression TIMES right=expression #Mult
| left=expression MINUS right=expression #Minus
Expand Down Expand Up @@ -303,6 +304,7 @@ PAREN_RIGHT: ')';
BRACKET_LEFT: '[';
BRACKET_RIGHT: ']';
COMMA: ',';
MOD: '%';
DIV: '/';
TIMES: '*';
MINUS: '-';
Expand Down

0 comments on commit 4a3d013

Please sign in to comment.