feat(Evaluator): add modulus operator
This commit is contained in:
parent
7700e61019
commit
738d16f2fc
9 changed files with 39 additions and 3 deletions
|
@ -26,7 +26,7 @@ ToDo List:
|
|||
- [X] add unit tests
|
||||
- [X] allow to set variables
|
||||
- [X] read line comments
|
||||
- [ ] add modulus operator '%'
|
||||
- [X] add modulus operator '%'
|
||||
- [ ] add support for multiple characters operators
|
||||
- [ ] add inclusive operators like '!=', '>=', '<='
|
||||
- [ ] add input_number() std function
|
||||
|
|
2
examples/sandbox.ltor
Normal file
2
examples/sandbox.ltor
Normal file
|
@ -0,0 +1,2 @@
|
|||
# test modulus operator
|
||||
print_number(20.2 % 10)
|
|
@ -2,7 +2,7 @@
|
|||
#ifndef EVALUATOR_H_
|
||||
#define EVALUATOR_H_
|
||||
|
||||
#define EVALUATOR_DEBUG_LEVEL 1
|
||||
#define EVALUATOR_DEBUG_LEVEL 0
|
||||
|
||||
int evaluate(struct StateContainer* state, char* inputStr, int* resultPtr, unsigned char* typePtr);
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ int is_operator(char candidate) {
|
|||
return (
|
||||
candidate == '*' ||
|
||||
candidate == '/' ||
|
||||
candidate == '%' ||
|
||||
candidate == '+' ||
|
||||
candidate == '-' ||
|
||||
candidate == '^' ||
|
||||
|
@ -81,6 +82,8 @@ int operate(
|
|||
res = a*b;
|
||||
} else if (operator == '/') {
|
||||
res = a/b;
|
||||
} else if (operator == '%') {
|
||||
res = m_float_modulus(a, b);
|
||||
} else if (operator == '^') {
|
||||
res = m_float_pow(a, b);
|
||||
} else if (operator == '=') {
|
||||
|
@ -109,6 +112,8 @@ int operate(
|
|||
res = aRepr*bRepr;
|
||||
} else if (operator == '^') {
|
||||
res = integer_pow(aRepr, bRepr);
|
||||
} else if (operator == '%') {
|
||||
res = (aRepr % bRepr);
|
||||
} else if (operator == '=') {
|
||||
res = (int) (aRepr == bRepr);
|
||||
} else if (operator == '<') {
|
||||
|
|
16
src/utils.c
16
src/utils.c
|
@ -84,6 +84,22 @@ float m_float_pow(float base, int exponent)
|
|||
return r;
|
||||
}
|
||||
|
||||
float m_float_modulus(float a, float b)
|
||||
{
|
||||
// find the remainder
|
||||
float t = a;
|
||||
float res = 0;
|
||||
int i = 0;
|
||||
while (i < 1000) {
|
||||
t = t - b;
|
||||
if (t < 0) {
|
||||
return t+b;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int m_factorial(int x)
|
||||
{
|
||||
if (x == 0 || x == 1) {
|
||||
|
|
|
@ -20,6 +20,8 @@ int integer_pow(int base, int exponent);
|
|||
|
||||
int float_almost_equal(float a, float b);
|
||||
|
||||
float m_float_modulus(float a, float mod);
|
||||
|
||||
int m_factorial(int x);
|
||||
|
||||
float m_float_pow(float base, int exponent);
|
||||
|
|
|
@ -8,9 +8,9 @@
|
|||
int main()
|
||||
{
|
||||
printf("== UNIT TESTS == \n");
|
||||
test_utils();
|
||||
test_var_store();
|
||||
test_stack();
|
||||
test_utils();
|
||||
test_evaluation();
|
||||
test_line_processing();
|
||||
}
|
||||
|
|
|
@ -133,4 +133,12 @@ void test_evaluation()
|
|||
evaluate(state, "5 > 0", &resVal, &resType);
|
||||
assert(resType == TYPE_INT);
|
||||
assert(1 == resVal);
|
||||
|
||||
evaluate(state, "5 % 10", &resVal, &resType);
|
||||
assert(resType == TYPE_INT);
|
||||
assert(5 == resVal);
|
||||
|
||||
evaluate(state, "5.3 % 10", &resVal, &resType);
|
||||
assert(resType == TYPE_FLOAT);
|
||||
assert(float_almost_equal(5.3, get_float_from_int_rep(resVal)));
|
||||
}
|
||||
|
|
|
@ -39,6 +39,9 @@ void test_utils()
|
|||
assert(m_factorial(3) == 6);
|
||||
assert(m_factorial(4) == 2*3*4);
|
||||
|
||||
assert(float_almost_equal(2.22, m_float_modulus(2.22, 10)));
|
||||
assert(float_almost_equal(28.619, m_float_modulus(200.22, 34.32)));
|
||||
|
||||
assert(float_almost_equal(2.7182, m_exp(1.0)));
|
||||
assert(float_almost_equal(1, m_exp(0)));
|
||||
|
||||
|
|
Loading…
Reference in a new issue