c985374

Faddle · faddle.git · Tickets

Branches > main > c985374 > files

Add bootstrap module

Grahp, 2026-07-19

Commit: c985374447c9a6b96e0a302957cddf6d0d4e5cfe
Parent: 0f9a9fe

4 changed files:

org/faddle/lang/bootstrap.lua added (view file)

@@ -0,0 +1,38 @@+local loaders = package.loaders or package.searchers++local read = require("org.faddle.lang.read").read+local eval = require("org.faddle.lang.eval").eval+local create_env = require("org.faddle.lang.eval").create_env++local faddle_path = ";?.fdl;?/init.fdl"++local function faddle_loader(mod)+  local rel = mod:gsub("%.", "/")+  for template in faddle_path:gmatch("[^;]+") do+    local path = template:gsub("%?", rel)+    local f = io.open(path, "rb")+    if f then+      local source = f:read("*a")+      f:close()+      return function()+        local env = create_env(mod)+        local forms = read(source)+        for i = 1, #forms do+          local form = forms[i]+          if i == #forms then+            return eval(form, env)+          else+            eval(form, env)+          end+        end+      end+    end+  end+end++local function bootstrap()+  table.insert(loaders, 2, faddle_loader)+  return require("org.faddle")+end++return {bootstrap = bootstrap}

org/faddle/lang/compile.lua modified (view file)

@@ -15,7 +15,6 @@ -- There are a handful of unimplemented special forms that can only be evalled but not compiled! local FN = intern("fn") local LET = intern("let")-local DO = intern("do")  local compile @@ -111,7 +110,7 @@ compile = function(form, statement)   if is_list(form) then     -- if count(form) == 0 then return "__rt.list()" end-    if count(form) == 0 then return ret(statement) .. "require('org.faddle.lang.rt').list()" end+    if count(form) == 0 then return ret(statement) .. "__rt.list()" end     -- If it's a literal number or string you can compile directly to a lookup     -- Symbols still need to be checked whether they resolve to a string or number though     if first(form) == FN then return compile_fn(form, statement) end@@ -146,9 +145,14 @@  local loadfn = loadstring or load +local function build_chunk(form)+  return loadfn("local __rt = require('org.faddle.lang.rt');" .. compile(form))+end+ local function execute(lua_source, chunk_name)   -- Really, needs to just have all of the std library loaded :p   print(lua_source)+  -- TODO core depends on itself...   local chunk, err = loadfn("local __rt = require('org.faddle.lang.rt');" .. lua_source, chunk_name)   if not chunk then     error(err)@@ -157,4 +161,4 @@   end end -return {compile = compile, execute = execute}+return {compile = compile, execute = execute, build_chunk = build_chunk}

org/faddle/lang/eval.lua modified (view file)

@@ -10,7 +10,9 @@ local is_symbol = symbol.is_symbol local intern = symbol.intern local munge = symbol.munge+local munge_part = symbol.munge_part local module = symbol.module+local name = symbol.name  local compiler = require("org.faddle.lang.compile") local compile = compiler.compile@@ -39,19 +41,14 @@  local function module_form(mod_name, env)   if type(mod_name) ~= "string" then error("First argument to module must be a string!") end-  -- Why not?-  if package.loaded[mod_name] and not modules[mod_name] then-    error("Cannot override already defined Lua module " .. mod_name)-  else-    -- This is not the return table but the module object...-    local M = modules[mod_name]-    if not M then-      M = {}-      modules[mod_name] = M-    end-    env.current_module = mod_name-    return mod_name+  -- This is not the return table but the module object...+  local M = modules[mod_name]+  if not M then+    M = {}+    modules[mod_name] = M   end+  env.current_module = mod_name+  return mod_name end  -- Would be nice if these were real functions/macros?@@ -83,7 +80,18 @@ local function lookup(sym, env)   -- Should fail if you're looking up a non-existent local, but not otherwise   -- There's currently no way to tell the difference between an undefined local and a nil local-  return temp_vars[sym] or modules[env.current_module][munge(sym)]+  if not module(sym) then+    -- If special form, error saying so+    return temp_vars[sym] or modules[env.current_module][munge(sym)]+  else+    -- Needs to find the table in the aliases, and then look up that table+    local global_alias = modules[env.current_module][munge_part(module(sym))]+    if global_alias then+      return global_alias[munge_part(name(sym))]+    end++    error("Cannot lookup qualified symbol")+  end    -- This is always looking up a global   -- So this is either a symbol with a module or a simple symbol that's been referred...

org/faddle/lang/symbol.lua modified (view file)

@@ -83,10 +83,16 @@   return name(simple_sym) end +local function munge_part(part)+  -- TODO this is no longer true when it comes to qualified symbols, which can have anything after the :...+  return part+end+ return {is_symbol = is_symbol,         intern = intern,         is_valid_symbol = is_valid_symbol,         name = name,         module = module,         str = str,-        munge = munge}+        munge = munge,+        munge_part = munge_part}