From 4446f10ea78f03202e963ec67ff6010e56637287 Mon Sep 17 00:00:00 2001 From: mryouse Date: Thu, 8 Jun 2023 22:10:15 -0400 Subject: bugfix (wrong): builtins need to be available to HOFs --- compiler.d | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/compiler.d b/compiler.d index 1ef18c9..bbe7550 100644 --- a/compiler.d +++ b/compiler.d @@ -44,6 +44,8 @@ class Compiler { Form outer; int outerIdx; + int[string] globals; + // CONSTANTS void compileAtom(Form form, const ValueType expecting) { Atom atom = cast(Atom)form; @@ -254,6 +256,28 @@ class Compiler { void compileSymbol(Form form, ValueType expecting = ValueType.ANY) { Symbol sym = cast(Symbol)form; + // do global things, if applicable + // TODO this is awful and hacky + // i hate this with a passion but it works for now + int* globalSym; + globalSym = sym.name in this.globals; + if (globalSym !is null) { + string args; + + // TODO this only allows up to 5 variables + char[] dumbNames = ['a', 'b', 'c', 'd', 'e']; + for (int i = 0; i < *globalSym; i++) { + if (i > 0) { + args ~= " "; + } + args ~= format("%s", to!string(dumbNames[i])); + } + string b = format("(lambda (%s)(%s %s))", args, sym.name, args); + Parser _p = new Parser(b); + this.resolve(_p.parseForm()); + return; + } + int arg = this.resolveLocal(sym); if (arg != -1) { this.func.chunk.writeOp(OpCode.OP_GET_LOCAL, sym.line); @@ -1068,6 +1092,49 @@ class Compiler { return this.func; } + void populateGlobals() { + // TODO most of this is wrong + // MATH + this.globals["+"] = 2; + this.globals["-"] = 2; + this.globals["*"] = 2; + this.globals["/"] = 2; + + // BOOLEAN + this.globals["<"] = 2; + this.globals["<="] = 2; + this.globals[">"] = 2; + this.globals[">="] = 2; + this.globals["not"] = 1; + this.globals["eq?"] = 2; + + // HOF + this.globals["map"] = 2; + this.globals["reduce"] = 3; + + // STRINGS + this.globals["concat"] = 2; + + // TYPES + this.globals["any?"] = 1; + this.globals["nil?"] = 1; + this.globals["->string"] = 1; + + // LISTS + this.globals["append"] = 2; + this.globals["first"] = 1; + this.globals["in?"] = 2; + this.globals["last"] = 1; + this.globals["length"] = 1; + this.globals["list"] = 5; + this.globals["most"] = 1; + this.globals["rest"] = 1; + this.globals["reverse"] = 1; + + // OTHER + this.globals["print"] = 1; + } + this(ObjType type, Parser* parser, string name = "") { this.parser = parser; this.func = new Function(type, name); @@ -1075,5 +1142,7 @@ class Compiler { this.locals ~= Local(new Symbol("", -1), 0); this.localCount++; + + this.populateGlobals(); } } -- cgit v1.2.3