Implement operator precedence with and without parenthesis
This commit is contained in:
@@ -65,19 +65,20 @@ return_statement
|
|||||||
|
|
||||||
expression
|
expression
|
||||||
: atom #atomExpression
|
: atom #atomExpression
|
||||||
| OPAR lhs=expression ADD rhs=expression CPAR #additionExpression
|
| OPAR expression CPAR #parenthesisExpression
|
||||||
| OPAR lhs=expression SUB rhs=expression CPAR #substractionExpression
|
| lhs=expression MUL rhs=expression #multiplicationExpression
|
||||||
| OPAR lhs=expression MUL rhs=expression CPAR #multiplicationExpression
|
| lhs=expression DIV rhs=expression #divisionExpression
|
||||||
| OPAR lhs=expression DIV rhs=expression CPAR #divisionExpression
|
| lhs=expression MOD rhs=expression #moduloExpression
|
||||||
| OPAR lhs=expression MOD rhs=expression CPAR #moduloExpression
|
| lhs=expression ADD rhs=expression #additionExpression
|
||||||
| OPAR lhs=expression EQEQ rhs=expression CPAR #equalityExpression
|
| lhs=expression SUB rhs=expression #substractionExpression
|
||||||
| OPAR lhs=expression NEQ rhs=expression CPAR #NotEqualityExpression
|
| lhs=expression EQEQ rhs=expression #equalityExpression
|
||||||
| OPAR lhs=expression LT rhs=expression CPAR #lessThanExpression
|
| lhs=expression NEQ rhs=expression #NotEqualityExpression
|
||||||
| OPAR lhs=expression GT rhs=expression CPAR #greaterThanExpression
|
| lhs=expression LT rhs=expression #lessThanExpression
|
||||||
| OPAR lhs=expression LTE rhs=expression CPAR #lessThanOrEqualToExpression
|
| lhs=expression GT rhs=expression #greaterThanExpression
|
||||||
| OPAR lhs=expression GTE rhs=expression CPAR #GreaterThanOrEqualToExpression
|
| lhs=expression LTE rhs=expression #lessThanOrEqualToExpression
|
||||||
| OPAR lhs=expression OR rhs=expression CPAR #OrExpression
|
| lhs=expression GTE rhs=expression #GreaterThanOrEqualToExpression
|
||||||
| OPAR lhs=expression AND rhs=expression CPAR #AndExpression
|
| lhs=expression OR rhs=expression #OrExpression
|
||||||
|
| lhs=expression AND rhs=expression #AndExpression
|
||||||
| SUB expression #negateExpression
|
| SUB expression #negateExpression
|
||||||
| NOT expression #NotExpression
|
| NOT expression #NotExpression
|
||||||
| functionCall #functionCallExpression
|
| functionCall #functionCallExpression
|
||||||
|
|||||||
@@ -221,6 +221,11 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitParenthesisExpression(KlangParser.ParenthesisExpressionContext ctx) {
|
||||||
|
return this.visit(ctx.expression());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Node visitEqualityExpression(KlangParser.EqualityExpressionContext ctx) {
|
public Node visitEqualityExpression(KlangParser.EqualityExpressionContext ctx) {
|
||||||
Node lhs = this.visit(ctx.lhs);
|
Node lhs = this.visit(ctx.lhs);
|
||||||
|
|||||||
@@ -2,59 +2,83 @@
|
|||||||
#include "math.h"
|
#include "math.h"
|
||||||
#include "../print/print.h"
|
#include "../print/print.h"
|
||||||
|
|
||||||
int cAdd(int x, int y) {
|
int cAdd(int x, int y)
|
||||||
|
{
|
||||||
return x + y;
|
return x + y;
|
||||||
}
|
}
|
||||||
int cSub(int x, int y) {
|
int cSub(int x, int y)
|
||||||
|
{
|
||||||
return x - y;
|
return x - y;
|
||||||
}
|
}
|
||||||
int cMul(int x, int y) {
|
int cMul(int x, int y)
|
||||||
|
{
|
||||||
return x * y;
|
return x * y;
|
||||||
}
|
}
|
||||||
int cModulo(int x, int y) {
|
int cModulo(int x, int y)
|
||||||
|
{
|
||||||
return x % y;
|
return x % y;
|
||||||
}
|
}
|
||||||
int cNeg(int x) {
|
int cNeg(int x)
|
||||||
|
{
|
||||||
return -x;
|
return -x;
|
||||||
}
|
}
|
||||||
int cId(int x) {
|
int cId(int x)
|
||||||
|
{
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
int cSelfMinus(int x) {
|
int cSelfMinus(int x)
|
||||||
|
{
|
||||||
x = x - 1;
|
x = x - 1;
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int math_testExpected(char *name, int x, int y, int expected, int result)
|
||||||
int math_testExpected(char* name, int x, int y, int expected, int result) {
|
{
|
||||||
if (expected == result) {
|
if (expected == result)
|
||||||
|
{
|
||||||
succPrefixTwo(name, x, y, expected, result);
|
succPrefixTwo(name, x, y, expected, result);
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
errPrefixTwo(name, x, y, expected, result);
|
errPrefixTwo(name, x, y, expected, result);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int math_test(char* name, int (*correctFunction)(int, int), int (*testFunction)(int, int), int x, int y) {
|
int math_test(char *name, int (*correctFunction)(int, int), int (*testFunction)(int, int), int x, int y)
|
||||||
|
{
|
||||||
int expected = correctFunction(x, y);
|
int expected = correctFunction(x, y);
|
||||||
int result = testFunction(x, y);
|
int result = testFunction(x, y);
|
||||||
return math_testExpected(name, x, y, expected, result);
|
return math_testExpected(name, x, y, expected, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
int math_testOneArg(char* name, int (*correctFunction)(int), int (*testFunction)(int), int x) {
|
int math_testOneArg(char *name, int (*correctFunction)(int), int (*testFunction)(int), int x)
|
||||||
|
{
|
||||||
int expected = correctFunction(x);
|
int expected = correctFunction(x);
|
||||||
int result = testFunction(x);
|
int result = testFunction(x);
|
||||||
if (expected == result) {
|
if (expected == result)
|
||||||
|
{
|
||||||
succPrefixOne(name, x, expected, result);
|
succPrefixOne(name, x, expected, result);
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
errPrefixOne(name, x, expected, result);
|
errPrefixOne(name, x, expected, result);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int runMathTests() {
|
void math_simpleTest(char *name, int expected, int result) {
|
||||||
|
if (expected == result) {
|
||||||
|
succ(name, expected, result);
|
||||||
|
} else {
|
||||||
|
err(name, expected, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int runMathTests()
|
||||||
|
{
|
||||||
printf("\nAddition Tests \n");
|
printf("\nAddition Tests \n");
|
||||||
math_test("add", cAdd, add, 0, 0);
|
math_test("add", cAdd, add, 0, 0);
|
||||||
math_test("add", cAdd, add, 1, 1);
|
math_test("add", cAdd, add, 1, 1);
|
||||||
@@ -97,4 +121,12 @@ int runMathTests() {
|
|||||||
math_testOneArg("selfMinus", cSelfMinus, selfMinus, 0);
|
math_testOneArg("selfMinus", cSelfMinus, selfMinus, 0);
|
||||||
math_testOneArg("selfMinus", cSelfMinus, selfMinus, 100);
|
math_testOneArg("selfMinus", cSelfMinus, selfMinus, 100);
|
||||||
math_testOneArg("selfMinus", cSelfMinus, selfMinus, -50);
|
math_testOneArg("selfMinus", cSelfMinus, selfMinus, -50);
|
||||||
|
math_simpleTest("precedence t1", 16, t1());
|
||||||
|
math_simpleTest("precedence t2", 16, t2());
|
||||||
|
math_simpleTest("precedence t3", 16, t3());
|
||||||
|
math_simpleTest("precedence t4", 16, t4());
|
||||||
|
math_simpleTest("precedence t5", 16, t5());
|
||||||
|
math_simpleTest("precedence t6", 16, t6());
|
||||||
|
math_simpleTest("precedence t7", 16, t7());
|
||||||
|
math_simpleTest("precedence t8", 18, t8());
|
||||||
}
|
}
|
||||||
@@ -9,3 +9,11 @@ int modulo(int x, int y);
|
|||||||
int neg(int x);
|
int neg(int x);
|
||||||
int id(int x);
|
int id(int x);
|
||||||
int selfMinus(int x);
|
int selfMinus(int x);
|
||||||
|
int t1();
|
||||||
|
int t2();
|
||||||
|
int t3();
|
||||||
|
int t4();
|
||||||
|
int t5();
|
||||||
|
int t6();
|
||||||
|
int t7();
|
||||||
|
int t8();
|
||||||
@@ -174,4 +174,38 @@ function not(a: bool): bool {
|
|||||||
return !a;
|
return !a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function t1(): int {
|
||||||
|
return ((5 * 3) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function t2(): int {
|
||||||
|
return (1 + (5 * 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
function t3(): int {
|
||||||
|
return 5 * 3 + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function t4(): int {
|
||||||
|
return 1 + 5 * 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
function t5(): int {
|
||||||
|
return (5 * 3 + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function t6(): int {
|
||||||
|
return (1 + 5 * 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
function t7(): int {
|
||||||
|
return 1 + (5 * 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
function t8(): int {
|
||||||
|
return (1 + 5) * 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
add(1, 1);
|
add(1, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user