add math ops

This commit is contained in:
zongor 2025-05-03 23:39:57 -04:00
parent 5a1a399d4f
commit fb6cc45f83
4 changed files with 42 additions and 2 deletions

View File

@ -8,6 +8,11 @@
typedef enum { typedef enum {
OP_NOOP, OP_NOOP,
OP_CONSTANT, OP_CONSTANT,
OP_ADD,
OP_SUBTRACT,
OP_MULTIPLY,
OP_DIVIDE,
OP_NEGATE,
OP_RETURN, OP_RETURN,
} OpCode; } OpCode;

View File

@ -38,8 +38,18 @@ int disassembleInstruction(Chunk* chunk, int offset) {
switch (instruction) { switch (instruction) {
case OP_NOOP: case OP_NOOP:
return simpleInstruction("OP_NOOP", offset); return simpleInstruction("OP_NOOP", offset);
case OP_NEGATE:
return simpleInstruction("OP_NEGATE", offset);
case OP_CONSTANT: case OP_CONSTANT:
return constantInstruction("OP_CONSTANT", chunk, offset); return constantInstruction("OP_CONSTANT", chunk, offset);
case OP_ADD:
return simpleInstruction("OP_ADD", offset);
case OP_SUBTRACT:
return simpleInstruction("OP_SUBTRACT", offset);
case OP_MULTIPLY:
return simpleInstruction("OP_MULTIPLY", offset);
case OP_DIVIDE:
return simpleInstruction("OP_DIVIDE", offset);
case OP_RETURN: case OP_RETURN:
return simpleInstruction("OP_RETURN", offset); return simpleInstruction("OP_RETURN", offset);
default: default:

View File

@ -10,8 +10,21 @@ int main(int argc, const char** argv) {
newChunk(&chunk); newChunk(&chunk);
int constant = addConstant(&chunk, 1.2); int constant = addConstant(&chunk, 1.2);
writeChunk(&chunk, OP_CONSTANT, 1); writeChunk(&chunk, OP_CONSTANT, 123);
writeChunk(&chunk, constant, 1); writeChunk(&chunk, constant, 123);
constant = addConstant(&chunk, 3.4);
writeChunk(&chunk, OP_CONSTANT, 123);
writeChunk(&chunk, constant, 123);
writeChunk(&chunk, OP_ADD, 123);
constant = addConstant(&chunk, 5.6);
writeChunk(&chunk, OP_CONSTANT, 123);
writeChunk(&chunk, constant, 123);
writeChunk(&chunk, OP_DIVIDE, 123);
writeChunk(&chunk, OP_NEGATE, 123);
writeChunk(&chunk, OP_RETURN, 1); writeChunk(&chunk, OP_RETURN, 1);

View File

@ -31,6 +31,12 @@ Value pop() {
static InterpretResult run() { static InterpretResult run() {
#define READ_BYTE() (*vm.ip++) #define READ_BYTE() (*vm.ip++)
#define READ_CONSTANT() (vm.chunk->constants.values[READ_BYTE()]) #define READ_CONSTANT() (vm.chunk->constants.values[READ_BYTE()])
#define BINARY_OP(op) \
do { \
double b = pop(); \
double a = pop(); \
push(a op b); \
} while (false)
for (;;) { for (;;) {
#ifdef DEBUG_TRACE_EXECUTION #ifdef DEBUG_TRACE_EXECUTION
@ -52,6 +58,11 @@ static InterpretResult run() {
push(constant); push(constant);
break; break;
} }
case OP_ADD: BINARY_OP(+); break;
case OP_SUBTRACT: BINARY_OP(-); break;
case OP_MULTIPLY: BINARY_OP(*); break;
case OP_DIVIDE: BINARY_OP(/); break;
case OP_NEGATE: push(-pop()); break;
case OP_RETURN: { case OP_RETURN: {
printValue(pop()); printValue(pop());
printf("\n"); printf("\n");
@ -62,6 +73,7 @@ static InterpretResult run() {
#undef READ_BYTE #undef READ_BYTE
#undef READ_CONSTANT #undef READ_CONSTANT
#undef BINARY_OP
} }
InterpretResult interpret(Chunk* chunk) { InterpretResult interpret(Chunk* chunk) {