Added modulo operator '%'

- added OP_MOD, which performs a modulo operation on the 2 <number> values on the stack. Pops the 2 values and pushes the result.
- also added TOKEN_PERCENT to the lexer, and extended binary() in cparse.c to support it.
This commit is contained in:
2021-01-01 00:47:15 -06:00
parent 58485d9375
commit 84f7895684
7 changed files with 22 additions and 1 deletions

View File

@@ -6,6 +6,8 @@
#include <stdarg.h>
#include <string.h>
#include <math.h>
COSMO_API void cosmoV_pushFString(CState *state, const char *format, ...) {
va_list args;
va_start(args, format);
@@ -802,6 +804,18 @@ int cosmoV_execute(CState *state) {
NUMBEROP(cosmoV_newNumber, /)
break;
}
case OP_MOD: {
StkPtr valA = cosmoV_getTop(state, 1);
StkPtr valB = cosmoV_getTop(state, 0);
if (IS_NUMBER(*valA) && IS_NUMBER(*valB)) {
cosmoV_setTop(state, 2); /* pop the 2 values */
cosmoV_pushValue(state, cosmoV_newNumber(fmod(cosmoV_readNumber(*valA), cosmoV_readNumber(*valB))));
} else { \
cosmoV_error(state, "Expected numbers, got %s and %s!", cosmoV_typeStr(*valA), cosmoV_typeStr(*valB));
return -1; \
} \
break;
}
case OP_NOT: {
cosmoV_pushBoolean(state, isFalsey(cosmoV_pop(state)));
break;