diff --git a/dub.sdl b/dub.sdl index 1325a01..c56910c 100644 --- a/dub.sdl +++ b/dub.sdl @@ -3,27 +3,30 @@ description "A minimal D application." authors "tanya" copyright "Copyright © 2025, tanya" license "MPL-2.0" -dependency "commandr" version="~>1.1.0" -dependency "colored" version="~>0.0.33" targetType "executable" sourcePaths configuration "clox" { buildOptions "betterC" - /* debugVersions "memTrace" */ - /* debugVersions "traceExec" */ + sourcePaths "src/clox" + buildRequirements "requireContracts" +} + +configuration "clox-dbg" { + buildRequirements "requireBoundsCheck" "requireContracts" + debugVersions "memTrace" debugVersions "printCode" + debugVersions "traceExec" + dependency "colored" version="~>0.0.33" dflags "-checkaction=C" libs "libbacktrace" - targetType "executable" sourcePaths "src/clox" - buildRequirements "requireBoundsCheck" "requireContracts" } configuration "jlox" { - targetType "executable" + dependency "colored" version="~>0.0.33" + dependency "commandr" version="~>1.1.0" sourcePaths "src/jlox" "src/common" versions "LoxConcatNonStrings" "LoxExtraNativeFuncs" "LoxPrintMultiple" - buildRequirements "requireBoundsCheck" "requireContracts" } diff --git a/src/clox/chunk.d b/src/clox/chunk.d index 39a4f47..333feee 100644 --- a/src/clox/chunk.d +++ b/src/clox/chunk.d @@ -13,6 +13,7 @@ struct OpColour{ enum OpConst; enum OpStack; enum OpJump; +enum OpCall; enum OpCode : ubyte{ @OpConst @(OpColour("200", "200", "100")) Constant, @@ -48,7 +49,9 @@ enum OpCode : ubyte{ @(OpColour("200", "100", "100")) Not, @(OpColour("100", "100", "200")) Negate, @(OpColour("000", "200", "100")) Print, - @(OpColour("000", "200", "100")) Return, + + @OpCall @(OpColour("250", "200", "250")) Call, + @(OpColour("250", "190", "200")) Return, } struct Chunk{ @@ -68,12 +71,12 @@ struct Chunk{ code ~= b; lines ~= line; } - size_t addConstant(Value value){ - long index = constants[].countUntil(value); + int addConstant(Value value){ + int index = cast(int)constants[].countUntil(value); if(index >= 0) return index; constants ~= value; - return constants.count - 1; + return cast(int)constants.count - 1; } void free(){ code.free(); diff --git a/src/clox/compiler.d b/src/clox/compiler.d index 6a5b138..a9e4153 100644 --- a/src/clox/compiler.d +++ b/src/clox/compiler.d @@ -5,14 +5,21 @@ import core.stdc.stdio; import clox.scanner; import clox.parser; import clox.chunk; -import clox.emitter; import clox.dbg; +import clox.vm; +import clox.object; +import clox.value; import clox.container.dynarray; +import clox.container.varint; + +Parser parser; +Compiler* currentCompiler; struct Compiler{ - Scanner scanner; - Parser parser; - Emitter emitter; + Compiler* enclosing; + Obj.Function* func; + enum FunctionType{ None, Function, Script } + FunctionType ftype = FunctionType.Script; struct Local{ enum Uninitialised = -1; Token name; @@ -21,28 +28,58 @@ struct Compiler{ DynArray!Local locals; int scopeDepth; private Chunk* compilingChunk; - Chunk* currentChunk() => compilingChunk; - bool compile(const(char)* source, Chunk* chunk){ - scanner.initialise(source); - parser.initialise(&this); - emitter.initialise(&this); + Chunk* currentChunk() => &func.chunk; + void initialise(FunctionType ftype){ + this.ftype = ftype; + enclosing = currentCompiler; + currentCompiler = &this; locals.initialise(); - compilingChunk = chunk; + func = Obj.Function.create(); + if(ftype == FunctionType.Function){ + currentCompiler.func.name = Obj.String.copy(parser.previous.lexeme); + } + + Local local = Local(); + local.depth = 0; + local.name.lexeme = ""; + locals ~= local; + } + Obj.Function* compile(){ parser.advance(); while(!parser.match(Token.Type.EOF)){ parser.declaration(); } - end(); - return !parser.hadError; + + Obj.Function* f = end(); + return parser.hadError ? null : f; } - void end(){ - emitter.emitReturn(); - debug(printCode){ + Obj.Function* end(){ + emitReturn(); + Obj.Function* f = func; + debug(printCode) if(vm.printCode){ if(!parser.hadError) - currentChunk.disassembleChunk(); + currentChunk.disassembleChunk(func.name !is null ? func.name.chars.ptr : "