replace / and mod of C and python implementation

This commit is contained in:
Koichi Nakamura 2021-01-10 18:37:27 +09:00
parent 66857c28c6
commit ffac921280
2 changed files with 13 additions and 3 deletions

View file

@ -144,6 +144,13 @@ defcode("x", exec) { (*(ip = (cfa) pop()))(); }
defcode("f", find_) { push((cell) find(pop())); next(); }
defcode("v", argv_) { push((cell) saved_argv); push(saved_argc); next(); }
defcode("V", impl) { push((cell) IMPLEMENTATION); next(); }
defcode("/", divmod) {
intptr_t b = pop();
intptr_t a = pop();
push(a%b);
push(a/b);
next();
}
#define defbinary(name, label, op, ty) \
defcode(name, label) { \
ty b = (ty) pop(); \
@ -153,8 +160,6 @@ defcode(name, label) { \
defbinary("+", add, +, intptr_t)
defbinary("-", sub, -, intptr_t)
defbinary("*", mul, *, intptr_t)
defbinary("/", div_, /, uintptr_t)
defbinary("%", mod, %, uintptr_t)
defbinary("&", and, &, uintptr_t)
defbinary("|", or, |, uintptr_t)
defbinary("^", xor, ^, uintptr_t)

View file

@ -209,10 +209,15 @@ def litstring(ip, np):
push(np + 4)
return next(np + 4 + read(np))
add_operator('S', litstring)
def divmod():
b = pop()
a = pop()
push(a%b)
push(a//b)
add_simple_operator('/', divmod)
add_binary_operator('+', operator.add)
add_binary_operator('-', operator.sub)
add_binary_operator('*', operator.mul)
add_binary_operator('/', operator.floordiv)
add_binary_operator('%', operator.mod)
add_binary_operator('&', operator.and_)
add_binary_operator('|', operator.or_)