all repos — mgba @ 5d98f9c963100c5f369b653d843b89456057b3db

mGBA Game Boy Advance Emulator

Debugger: Add modulo operator
Vicki Pfau vi@endrift.com
Fri, 29 Dec 2017 14:01:55 -0500
commit

5d98f9c963100c5f369b653d843b89456057b3db

parent

49675d7c58e548d66fe418a9ceb5ab32c5ac6c8d

3 files changed, 50 insertions(+), 13 deletions(-)

jump to
M include/mgba/internal/debugger/parser.hinclude/mgba/internal/debugger/parser.h

@@ -18,6 +18,7 @@ OP_ADD,

OP_SUBTRACT, OP_MULTIPLY, OP_DIVIDE, + OP_MODULO, OP_AND, OP_OR, OP_XOR,
M src/debugger/parser.csrc/debugger/parser.c

@@ -39,6 +39,9 @@ break;

case '/': lvNext->operatorValue = OP_DIVIDE; break; + case '%': + lvNext->operatorValue = OP_MODULO; + break; case '&': lvNext->operatorValue = OP_AND; break;

@@ -68,6 +71,7 @@ case '+':

case '-': case '*': case '/': + case '%': case '&': case '|': case '^':

@@ -158,6 +162,7 @@ case '+':

case '-': case '*': case '/': + case '%': case '&': case '|': case '^':

@@ -304,6 +309,7 @@ case '+':

case '-': case '*': case '/': + case '%': case '&': case '|': case '^':

@@ -356,19 +362,20 @@ return adjusted;

} static const int _operatorPrecedence[] = { - 14, - 4, - 4, - 3, - 3, - 8, - 10, - 9, - 6, - 6, - 7, - 6, - 6 + [OP_ASSIGN] = 14, + [OP_ADD] = 4, + [OP_SUBTRACT] = 4, + [OP_MULTIPLY] = 3, + [OP_DIVIDE] = 3, + [OP_MODULO] = 3, + [OP_AND] = 8, + [OP_OR] = 10, + [OP_XOR] = 9, + [OP_LESS] = 6, + [OP_GREATER] = 6, + [OP_EQUAL] = 7, + [OP_LE] = 6, + [OP_GE] = 6 }; static struct ParseTree* _parseTreeCreate() {

@@ -504,6 +511,13 @@ break;

case OP_DIVIDE: if (next != 0) { current /= next; + } else { + return false; + } + break; + case OP_MODULO: + if (next != 0) { + current %= next; } else { return false; }
M src/debugger/test/lexer.csrc/debugger/test/lexer.c

@@ -230,6 +230,26 @@ assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);

assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_DIVIDE); } +M_TEST_DEFINE(lexModOperator) { + LEX("1%"); + + assert_int_equal(LexVectorSize(lv), 2); + assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE); + assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1); + assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE); + assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_MODULO); +} + +M_TEST_DEFINE(lexIdentifierModOperator) { + LEX("x%"); + + assert_int_equal(LexVectorSize(lv), 2); + assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_IDENTIFIER_TYPE); + assert_string_equal(LexVectorGetPointer(lv, 0)->identifierValue, "x"); + assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE); + assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_MODULO); +} + M_TEST_DEFINE(lexAndOperator) { LEX("1&");

@@ -428,6 +448,8 @@ cmocka_unit_test(lexMulOperator),

cmocka_unit_test(lexIdentifierMulOperator), cmocka_unit_test(lexDivOperator), cmocka_unit_test(lexIdentifierDivOperator), + cmocka_unit_test(lexModOperator), + cmocka_unit_test(lexIdentifierModOperator), cmocka_unit_test(lexAndOperator), cmocka_unit_test(lexIdentifierAndOperator), cmocka_unit_test(lexOrOperator),