Organisation.

This commit is contained in:
2026-02-27 19:05:01 +00:00
parent 7edad0c5bb
commit b89e015170
7 changed files with 64 additions and 67 deletions

View File

@@ -4,6 +4,5 @@ then
echo "lua could not be found" echo "lua could not be found"
exit 1 exit 1
else else
lua main.lua "$@" lua index.lua "$@"
fi fi

View File

@@ -376,7 +376,7 @@ function Chain:pcall(func)
table.concat(self:flatten(),"\n\t") table.concat(self:flatten(),"\n\t")
)) ))
end end
return successs,result return success,result
end end
function Chain:call(func) function Chain:call(func)
local success,result = self:pcall(func) local success,result = self:pcall(func)
@@ -458,6 +458,7 @@ function Scope:run(expression,chain,original)
for index,item in pairs(binds) do for index,item in pairs(binds) do
print("index:",index,"item:",item) print("index:",index,"item:",item)
end end
if not binding then error("No binding") end -- todo: improve error
return binding:call( return binding:call(
original, original,
bind, bind,
@@ -465,4 +466,3 @@ function Scope:run(expression,chain,original)
) )
end end
end end
return _G

View File

@@ -1,14 +1,14 @@
require("interpret") require("interpreter")
function B(scope,abstract,callback) function Bind(scope,abstract,callback)
scope:insert( scope:insert(
Binding( Binding(
interpret(abstract), Parse(abstract),
callback callback
) )
) )
end end
function R(scope,expression,chain,name) function Run(scope,expression,chain,name)
name = name or "?" name = name or "?"
assert_meta(Chain)(chain,"Arg 'chain' #3: %s") assert_meta(Chain)(chain,"Arg 'chain' #3: %s")
assert_meta(Scope)(scope,"Arg 'scope' #1: %s") assert_meta(Scope)(scope,"Arg 'scope' #1: %s")
@@ -76,10 +76,10 @@ local function Bexpands(s,e,c)
map[binding] = _e[binding] map[binding] = _e[binding]
end end
local expanded = substitute(e.expression,map) local expanded = substitute(e.expression,map)
return R(_s,expanded,_c,"means: "..tostring(expanded)) return Run(_s,expanded,_c,"means: "..tostring(expanded))
end) end)
end end
B(ns,"(abstract) means (expression)",Bexpands) Bind(ns,"(abstract) means (expression)",Bexpands)
local function Bevaluate(s,e,c) local function Bevaluate(s,e,c)
return Binding(e.abstract,function(_s,_e,_c) return Binding(e.abstract,function(_s,_e,_c)
local scope = Scope() local scope = Scope()
@@ -87,18 +87,18 @@ local function Bevaluate(s,e,c)
for name,expression in pairs(_e.items) do for name,expression in pairs(_e.items) do
scope:insert(Binding( scope:insert(Binding(
Expression({name}), Expression({name}),
R(_s,_e,_c,string.format("expands: %s:",name)) Run(_s,_e,_c,string.format("expands: %s:",name))
)) ))
end end
return R(scope,e,c,"evaluate: result") return Run(scope,e,c,"evaluate: result")
end) end)
end end
B(ns,"(abstract) becomes (expression)",Bevaluates) Bind(ns,"(abstract) becomes (expression)",Bevaluates)
B(ns,"(identifier) is (expression)",function(s,e,c) Bind(ns,"(identifier) is (expression)",function(s,e,c)
if not e.identifier:text(s,e,c) then if not e.identifier:text(s,e,c) then
error("nel: text-only expression expected.") error("nel: text-only expression expected.")
end end
return Binding(e.identifier,R()) return Binding(e.identifier,Run())
end) end)
local function doAll(s,e,c) local function doAll(s,e,c)
local res local res
@@ -111,7 +111,7 @@ local function doAll(s,e,c)
) )
) )
end end
res = R(s,item,c,"do: line") res = Run(s,item,c,"do: line")
end end
return res return res
end end
@@ -120,16 +120,16 @@ end
local function Bdo(s,e,c) local function Bdo(s,e,c)
local scope local scope
if e.scope then if e.scope then
scope = R(s,e.scope,c,"do: scope") scope = Run(s,e.scope,c,"do: scope")
else else
scope = Scope() scope = Scope()
scope.parent = s scope.parent = s
end end
return doAll(s,e.expression,c) return doAll(s,e.expression,c)
end end
B(ns,"do (expression) in (scope)",Bdo) Bind(ns,"do (expression) in (scope)",Bdo)
B(ns,"do (expression)",Bdo) Bind(ns,"do (expression)",Bdo)
B(ns,"(abstract) broken into (expressions) as do (results)",function(s,e,c) Bind(ns,"(abstract) broken into (expressions) as do (results)",function(s,e,c)
return Binding(e.abstract,function(_s,_e,_c) return Binding(e.abstract,function(_s,_e,_c)
local params = List() local params = List()
for index,item in pairs(_e) do for index,item in pairs(_e) do
@@ -157,10 +157,10 @@ local _list = {}
local function List() local function List()
return setmetatable(_list,{}) return setmetatable(_list,{})
end end
B(ns,"a new list",function(s,e,c) Bind(ns,"a new list",function(s,e,c)
return List() return List()
end) end)
B(ns,"for each (item) in (list) do (expressions)",function(s,e,c) Bind(ns,"for each (item) in (list) do (expressions)",function(s,e,c)
local item = e.item:text() local item = e.item:text()
if not property then if not property then
error( error(
@@ -170,7 +170,7 @@ B(ns,"for each (item) in (list) do (expressions)",function(s,e,c)
) )
) )
end end
local list = R(s,e.list,c,"for each: list") local list = Run(s,e.list,c,"for each: list")
assert_meta(List)(list,"for each: list: %s") assert_meta(List)(list,"for each: list: %s")
for index,item in ipairs(list) do for index,item in ipairs(list) do
local scope = Scope() local scope = Scope()
@@ -179,40 +179,40 @@ B(ns,"for each (item) in (list) do (expressions)",function(s,e,c)
doAll(s,e.expressions,c) doAll(s,e.expressions,c)
end end
end) end)
B(ns,"add (item) to (list)",function(s,e,c) Bind(ns,"add (item) to (list)",function(s,e,c)
local item = R(s,e.list,c,"") local item = Run(s,e.list,c,"")
local list = R(s,e.list,c,"add: list") local list = Run(s,e.list,c,"add: list")
assert_meta(List)(list,"add: list: %s") assert_meta(List)(list,"add: list: %s")
table.insert(list,item) table.insert(list,item)
end) end)
-- I even quite dislike these, they are prone to gumming things up -- I even quite dislike these, they are prone to gumming things up
local _table = {} local _table = {}
B(ns,"a new table",function(s,e,c) return {} end) Bind(ns,"a new table",function(s,e,c) return {} end)
B(ns,"set (index) of (table) to (value)",function(s,e,c) Bind(ns,"set (index) of (table) to (value)",function(s,e,c)
local index = e.index:text() local index = e.index:text()
if not index then if not index then
index = R(s,e.index,c,"set: index") index = Run(s,e.index,c,"set: index")
end end
local tab = R(s,e.table,c,"set: table") local tab = Run(s,e.table,c,"set: table")
local val = R(s,e.value,c,"set: value") local val = Run(s,e.value,c,"set: value")
tab[index] = val tab[index] = val
end) end)
B(ns,"get (index) of (table)",function(s,e,c) Bind(ns,"get (index) of (table)",function(s,e,c)
local index = e.index:text() local index = e.index:text()
if not index then if not index then
index = R(s,e.index,c,"get: index") index = Run(s,e.index,c,"get: index")
end end
local tab = R(s,e.table,c,"get: table") local tab = Run(s,e.table,c,"get: table")
return tab[index] return tab[index]
end) end)
local _object = {} local _object = {}
local function Object() local function Object()
return setmetatable(_object,{}) return setmetatable(_object,{})
end end
B(ns,"a new object",function(s,e,c) Bind(ns,"a new object",function(s,e,c)
return Object() return Object()
end) end)
B(ns,"(property) of (object) is (value)",function(s,e,c) Bind(ns,"(property) of (object) is (value)",function(s,e,c)
local property = e.property:text() local property = e.property:text()
if not property then if not property then
error( error(
@@ -222,12 +222,12 @@ B(ns,"(property) of (object) is (value)",function(s,e,c)
) )
) )
end end
local object = R(s,e.object,c,"property is: object") local object = Run(s,e.object,c,"property is: object")
assert_meta(Object)(object,"nel: property is: object: %s") assert_meta(Object)(object,"nel: property is: object: %s")
local value = R(s,e.value,c,"property is: value") local value = Run(s,e.value,c,"property is: value")
object[property] = value object[property] = value
end) end)
B(ns,"(property) of (object)",function(s,e,c) Bind(ns,"(property) of (object)",function(s,e,c)
local property = e.property:text() local property = e.property:text()
if not property then if not property then
error( error(
@@ -237,54 +237,54 @@ B(ns,"(property) of (object)",function(s,e,c)
) )
) )
end end
local object = R(s,e.object,c,"property is: object") local object = Run(s,e.object,c,"property is: object")
assert_meta(Object)(object,"nel: property is: object: %s") assert_meta(Object)(object,"nel: property is: object: %s")
return object[property] return object[property]
end) end)
B(ns,"error (text)",function(s,e,c) Bind(ns,"error (text)",function(s,e,c)
error("nel: "..R(s,e.text,c,"error: message")) error("nel: "..Run(s,e.text,c,"error: message"))
end) end)
B(ns,"this scope",function(s,e,c) Bind(ns,"this scope",function(s,e,c)
return s return s
end) end)
B(ns,"a new scope",function(s,e,c) Bind(ns,"a new scope",function(s,e,c)
return Scope() return Scope()
end) end)
-- Read a file in the current directly, more for compiling -- Read a file in the current directly, more for compiling
B(ns,"read file (path)",function(s,e,c) Bind(ns,"read file (path)",function(s,e,c)
return read(R(s,e.path,c,"read: path")) return read(Run(s,e.path,c,"read: path"))
end) end)
-- Take some text and interpret it into an expression -- Take some text and interpret it into an expression
B(ns,"evaluate (text) in (scope)",function(s,e,c) Bind(ns,"evaluate (text) in (scope)",function(s,e,c)
local scope = R(s,e.scope,c,"evaluate: scope") local scope = Run(s,e.scope,c,"evaluate: scope")
return R(s,interpret(R(scope,e.text,c,"include: source")),c,"include: result") return Run(s,interpret(Run(scope,e.text,c,"include: source")),c,"include: result")
end) end)
B(ns,"print (text)",function(s,e,c) Bind(ns,"print (text)",function(s,e,c)
log(R(s,e.text,c,"print: text")) log(Run(s,e.text,c,"print: text"))
end) end)
B(ns,"text (literal)",function(s,e,c) Bind(ns,"text (literal)",function(s,e,c)
return e.literal:text() return e.literal:text()
end) end)
B(ns,"return (expression)",function(s,e,c) Bind(ns,"return (expression)",function(s,e,c)
return R(s,e.expression,c,"return: result") return Run(s,e.expression,c,"return: result")
end) end)
B(ns,"(macro) means (expansion)",function(s,e,c) Bind(ns,"(macro) means (expansion)",function(s,e,c)
end) end)
B(ns,"(identifier) is (value)",function(s,e,c) Bind(ns,"(identifier) is (value)",function(s,e,c)
end) end)
B(ns,"(expression) becomes (result)",function(s,e,c) Bind(ns,"(expression) becomes (result)",function(s,e,c)
end) end)
B(ns,"let (binding) in (scope)",function(s,e,c) Bind(ns,"let (binding) in (scope)",function(s,e,c)
end) end)
local Hpath = "nel/helper.nel" local Hpath = "nel/helper.nel"
local Hchain = Chain("in internal: "..Hpath) local Hchain = Chain("in internal: "..Hpath)
local success,result = Hchain:call(function(chain) local success,result = Hchain:call(function(chain)
R(NelliScope,interpret(read(Hpath),Hpath,chain),chain) Run(NelliScope,Parse(read(Hpath),Hpath,chain),chain)
end) end)
--[[ Documentation: --[[ Documentation:

View File

@@ -1,9 +1,9 @@
require("parse") require("interpreter")
-- TODO: This should really be a separate API and module -- TODO: This should really be a separate API and module
-- please break it out -- please break it out
-- Interpret some text -- Interpret some text
function interpret(content,uri,chain) function Parse(content,uri,chain)
-- LOCALS -- LOCALS
-- The pattern for words inbetween expressions -- The pattern for words inbetween expressions
local expressionWordPattern = "(.*)" local expressionWordPattern = "(.*)"
@@ -192,5 +192,3 @@ function interpret(content,uri,chain)
return consumeExpression() return consumeExpression()
-- TODO: check for later expressions please? -- TODO: check for later expressions please?
end end
return _G

View File