Compiling Expressions 17

This commit is contained in:
nazrin 2025-06-04 19:49:53 +00:00
parent 8fb449825d
commit 41404633da
18 changed files with 546 additions and 64 deletions

View file

@ -7,6 +7,8 @@ import clox.value;
import clox.dbg;
import clox.util;
import clox.compiler;
import clox.container.stack;
import clox.container.varint;
enum stackMax = 256;
@ -18,11 +20,15 @@ struct VM{
this(int _) @nogc nothrow {
stack = typeof(stack)(0);
}
InterpretResult interpret(string source) @nogc nothrow {
compile(source);
return InterpretResult.Ok;
InterpretResult interpret(string source){
Chunk c = Chunk();
Compiler compiler;
if(!compiler.compile(source, &c))
return InterpretResult.CompileError;
chunk = &c;
return interpret(chunk);
}
InterpretResult interpret(Chunk* chunk) @nogc nothrow {
InterpretResult interpret(Chunk* chunk){
this.chunk = chunk;
ip = &chunk.code[0];
return run();
@ -30,11 +36,15 @@ struct VM{
private InterpretResult run() @nogc nothrow {
auto readByte() => *ip++;
auto readIns() => cast(OpCode)readByte();
auto readConstant() => chunk.constants[readByte()];
Value readConstant(){
VarUint constant = VarUint.read(ip[0 .. 4]);
ip += constant.len;
return chunk.constants[constant.i];
}
while(true){
debug(traceExec){
writeln(" ", stack.live);
debug disassembleInstruction(chunk, ip - &chunk.code[0]);
disassembleInstruction(chunk, ip - &chunk.code[0]);
}
OpCode instruction = readIns();
with(OpCode) opSwitch: final switch(instruction){