feat(Evaluator): add strict comparaison operators

This commit is contained in:
Matthieu Bessat 2022-05-15 20:49:23 +02:00
parent 35ca9c31ea
commit b72448c62d
4 changed files with 25 additions and 2 deletions

View file

@ -41,6 +41,7 @@ ToDo List:
- [ ] add basic number list support - [ ] add basic number list support
- [ ] add fully features strings support - [ ] add fully features strings support
- [ ] add [classic problem solving](https://rosettacode.org) with code examples - [ ] add [classic problem solving](https://rosettacode.org) with code examples
- [ ] add Web Assembly support and publish a demo website
## Installation ## Installation

View file

@ -1,5 +1,5 @@
set i to 0 set i to 0
while !(i = 10) do while i < 10 do
print_number(i) print_number(i)
set i to i+1 set i to i+1
end end

View file

@ -14,7 +14,9 @@ int is_operator(char candidate) {
candidate == '=' || candidate == '=' ||
candidate == '!' || candidate == '!' ||
candidate == '&' || candidate == '&' ||
candidate == '|' candidate == '|' ||
candidate == '>' ||
candidate == '<'
); );
} }
@ -83,6 +85,10 @@ int operate(
res = m_float_pow(a, b); res = m_float_pow(a, b);
} else if (operator == '=') { } else if (operator == '=') {
res = (int) (a == b); res = (int) (a == b);
} else if (operator == '>') {
res = (int) (a > b);
} else if (operator == '<') {
res = (int) (a < b);
} else { } else {
return 2; return 2;
} }
@ -105,6 +111,10 @@ int operate(
res = integer_pow(aRepr, bRepr); res = integer_pow(aRepr, bRepr);
} else if (operator == '=') { } else if (operator == '=') {
res = (int) (aRepr == bRepr); res = (int) (aRepr == bRepr);
} else if (operator == '<') {
res = (int) (aRepr < bRepr);
} else if (operator == '>') {
res = (int) (aRepr > bRepr);
} else { } else {
return 2; return 2;
} }

View file

@ -121,4 +121,16 @@ void test_evaluation()
evaluate(state, "1 & (1 | 0)", &resVal, &resType); evaluate(state, "1 & (1 | 0)", &resVal, &resType);
assert(resType == TYPE_INT); assert(resType == TYPE_INT);
assert(1 == resVal); assert(1 == resVal);
evaluate(state, "0 > 0", &resVal, &resType);
assert(resType == TYPE_INT);
assert(0 == resVal);
evaluate(state, "-45 < 1", &resVal, &resType);
assert(resType == TYPE_INT);
assert(1 == resVal);
evaluate(state, "5 > 0", &resVal, &resType);
assert(resType == TYPE_INT);
assert(1 == resVal);
} }