Organisation.
This commit is contained in:
+2
-3
@@ -4,6 +4,5 @@ then
|
||||
echo "lua could not be found"
|
||||
exit 1
|
||||
else
|
||||
lua main.lua "$@"
|
||||
fi
|
||||
|
||||
lua index.lua "$@"
|
||||
fi
|
||||
@@ -24,4 +24,4 @@ local function main(args)
|
||||
end
|
||||
|
||||
-- A list of arguments passed into this
|
||||
main({...})
|
||||
main({...})
|
||||
@@ -376,7 +376,7 @@ function Chain:pcall(func)
|
||||
table.concat(self:flatten(),"\n\t")
|
||||
))
|
||||
end
|
||||
return successs,result
|
||||
return success,result
|
||||
end
|
||||
function Chain:call(func)
|
||||
local success,result = self:pcall(func)
|
||||
@@ -458,11 +458,11 @@ function Scope:run(expression,chain,original)
|
||||
for index,item in pairs(binds) do
|
||||
print("index:",index,"item:",item)
|
||||
end
|
||||
if not binding then error("No binding") end -- todo: improve error
|
||||
return binding:call(
|
||||
original,
|
||||
bind,
|
||||
chain
|
||||
)
|
||||
end
|
||||
end
|
||||
return _G
|
||||
end
|
||||
+55
-55
@@ -1,14 +1,14 @@
|
||||
require("interpret")
|
||||
require("interpreter")
|
||||
|
||||
function B(scope,abstract,callback)
|
||||
function Bind(scope,abstract,callback)
|
||||
scope:insert(
|
||||
Binding(
|
||||
interpret(abstract),
|
||||
Parse(abstract),
|
||||
callback
|
||||
)
|
||||
)
|
||||
end
|
||||
function R(scope,expression,chain,name)
|
||||
function Run(scope,expression,chain,name)
|
||||
name = name or "?"
|
||||
assert_meta(Chain)(chain,"Arg 'chain' #3: %s")
|
||||
assert_meta(Scope)(scope,"Arg 'scope' #1: %s")
|
||||
@@ -76,10 +76,10 @@ local function Bexpands(s,e,c)
|
||||
map[binding] = _e[binding]
|
||||
end
|
||||
local expanded = substitute(e.expression,map)
|
||||
return R(_s,expanded,_c,"means: "..tostring(expanded))
|
||||
return Run(_s,expanded,_c,"means: "..tostring(expanded))
|
||||
end)
|
||||
end
|
||||
B(ns,"(abstract) means (expression)",Bexpands)
|
||||
Bind(ns,"(abstract) means (expression)",Bexpands)
|
||||
local function Bevaluate(s,e,c)
|
||||
return Binding(e.abstract,function(_s,_e,_c)
|
||||
local scope = Scope()
|
||||
@@ -87,18 +87,18 @@ local function Bevaluate(s,e,c)
|
||||
for name,expression in pairs(_e.items) do
|
||||
scope:insert(Binding(
|
||||
Expression({name}),
|
||||
R(_s,_e,_c,string.format("expands: %s:",name))
|
||||
Run(_s,_e,_c,string.format("expands: %s:",name))
|
||||
))
|
||||
end
|
||||
return R(scope,e,c,"evaluate: result")
|
||||
return Run(scope,e,c,"evaluate: result")
|
||||
end)
|
||||
end
|
||||
B(ns,"(abstract) becomes (expression)",Bevaluates)
|
||||
B(ns,"(identifier) is (expression)",function(s,e,c)
|
||||
Bind(ns,"(abstract) becomes (expression)",Bevaluates)
|
||||
Bind(ns,"(identifier) is (expression)",function(s,e,c)
|
||||
if not e.identifier:text(s,e,c) then
|
||||
error("nel: text-only expression expected.")
|
||||
end
|
||||
return Binding(e.identifier,R())
|
||||
return Binding(e.identifier,Run())
|
||||
end)
|
||||
local function doAll(s,e,c)
|
||||
local res
|
||||
@@ -111,7 +111,7 @@ local function doAll(s,e,c)
|
||||
)
|
||||
)
|
||||
end
|
||||
res = R(s,item,c,"do: line")
|
||||
res = Run(s,item,c,"do: line")
|
||||
end
|
||||
return res
|
||||
end
|
||||
@@ -120,16 +120,16 @@ end
|
||||
local function Bdo(s,e,c)
|
||||
local scope
|
||||
if e.scope then
|
||||
scope = R(s,e.scope,c,"do: scope")
|
||||
scope = Run(s,e.scope,c,"do: scope")
|
||||
else
|
||||
scope = Scope()
|
||||
scope.parent = s
|
||||
end
|
||||
return doAll(s,e.expression,c)
|
||||
end
|
||||
B(ns,"do (expression) in (scope)",Bdo)
|
||||
B(ns,"do (expression)",Bdo)
|
||||
B(ns,"(abstract) broken into (expressions) as do (results)",function(s,e,c)
|
||||
Bind(ns,"do (expression) in (scope)",Bdo)
|
||||
Bind(ns,"do (expression)",Bdo)
|
||||
Bind(ns,"(abstract) broken into (expressions) as do (results)",function(s,e,c)
|
||||
return Binding(e.abstract,function(_s,_e,_c)
|
||||
local params = List()
|
||||
for index,item in pairs(_e) do
|
||||
@@ -157,10 +157,10 @@ local _list = {}
|
||||
local function List()
|
||||
return setmetatable(_list,{})
|
||||
end
|
||||
B(ns,"a new list",function(s,e,c)
|
||||
Bind(ns,"a new list",function(s,e,c)
|
||||
return List()
|
||||
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()
|
||||
if not property then
|
||||
error(
|
||||
@@ -170,7 +170,7 @@ B(ns,"for each (item) in (list) do (expressions)",function(s,e,c)
|
||||
)
|
||||
)
|
||||
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")
|
||||
for index,item in ipairs(list) do
|
||||
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)
|
||||
end
|
||||
end)
|
||||
B(ns,"add (item) to (list)",function(s,e,c)
|
||||
local item = R(s,e.list,c,"")
|
||||
local list = R(s,e.list,c,"add: list")
|
||||
Bind(ns,"add (item) to (list)",function(s,e,c)
|
||||
local item = Run(s,e.list,c,"")
|
||||
local list = Run(s,e.list,c,"add: list")
|
||||
assert_meta(List)(list,"add: list: %s")
|
||||
table.insert(list,item)
|
||||
end)
|
||||
-- I even quite dislike these, they are prone to gumming things up
|
||||
local _table = {}
|
||||
B(ns,"a new table",function(s,e,c) return {} end)
|
||||
B(ns,"set (index) of (table) to (value)",function(s,e,c)
|
||||
Bind(ns,"a new table",function(s,e,c) return {} end)
|
||||
Bind(ns,"set (index) of (table) to (value)",function(s,e,c)
|
||||
local index = e.index:text()
|
||||
if not index then
|
||||
index = R(s,e.index,c,"set: index")
|
||||
index = Run(s,e.index,c,"set: index")
|
||||
end
|
||||
local tab = R(s,e.table,c,"set: table")
|
||||
local val = R(s,e.value,c,"set: value")
|
||||
local tab = Run(s,e.table,c,"set: table")
|
||||
local val = Run(s,e.value,c,"set: value")
|
||||
tab[index] = val
|
||||
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()
|
||||
if not index then
|
||||
index = R(s,e.index,c,"get: index")
|
||||
index = Run(s,e.index,c,"get: index")
|
||||
end
|
||||
local tab = R(s,e.table,c,"get: table")
|
||||
local tab = Run(s,e.table,c,"get: table")
|
||||
return tab[index]
|
||||
end)
|
||||
local _object = {}
|
||||
local function Object()
|
||||
return setmetatable(_object,{})
|
||||
end
|
||||
B(ns,"a new object",function(s,e,c)
|
||||
Bind(ns,"a new object",function(s,e,c)
|
||||
return Object()
|
||||
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()
|
||||
if not property then
|
||||
error(
|
||||
@@ -222,12 +222,12 @@ B(ns,"(property) of (object) is (value)",function(s,e,c)
|
||||
)
|
||||
)
|
||||
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")
|
||||
local value = R(s,e.value,c,"property is: value")
|
||||
local value = Run(s,e.value,c,"property is: value")
|
||||
object[property] = value
|
||||
end)
|
||||
B(ns,"(property) of (object)",function(s,e,c)
|
||||
Bind(ns,"(property) of (object)",function(s,e,c)
|
||||
local property = e.property:text()
|
||||
if not property then
|
||||
error(
|
||||
@@ -237,54 +237,54 @@ B(ns,"(property) of (object)",function(s,e,c)
|
||||
)
|
||||
)
|
||||
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")
|
||||
return object[property]
|
||||
end)
|
||||
B(ns,"error (text)",function(s,e,c)
|
||||
error("nel: "..R(s,e.text,c,"error: message"))
|
||||
Bind(ns,"error (text)",function(s,e,c)
|
||||
error("nel: "..Run(s,e.text,c,"error: message"))
|
||||
end)
|
||||
B(ns,"this scope",function(s,e,c)
|
||||
Bind(ns,"this scope",function(s,e,c)
|
||||
return s
|
||||
end)
|
||||
B(ns,"a new scope",function(s,e,c)
|
||||
Bind(ns,"a new scope",function(s,e,c)
|
||||
return Scope()
|
||||
end)
|
||||
-- Read a file in the current directly, more for compiling
|
||||
B(ns,"read file (path)",function(s,e,c)
|
||||
return read(R(s,e.path,c,"read: path"))
|
||||
Bind(ns,"read file (path)",function(s,e,c)
|
||||
return read(Run(s,e.path,c,"read: path"))
|
||||
end)
|
||||
-- Take some text and interpret it into an expression
|
||||
B(ns,"evaluate (text) in (scope)",function(s,e,c)
|
||||
local scope = R(s,e.scope,c,"evaluate: scope")
|
||||
return R(s,interpret(R(scope,e.text,c,"include: source")),c,"include: result")
|
||||
Bind(ns,"evaluate (text) in (scope)",function(s,e,c)
|
||||
local scope = Run(s,e.scope,c,"evaluate: scope")
|
||||
return Run(s,interpret(Run(scope,e.text,c,"include: source")),c,"include: result")
|
||||
end)
|
||||
B(ns,"print (text)",function(s,e,c)
|
||||
log(R(s,e.text,c,"print: text"))
|
||||
Bind(ns,"print (text)",function(s,e,c)
|
||||
log(Run(s,e.text,c,"print: text"))
|
||||
end)
|
||||
B(ns,"text (literal)",function(s,e,c)
|
||||
Bind(ns,"text (literal)",function(s,e,c)
|
||||
return e.literal:text()
|
||||
end)
|
||||
B(ns,"return (expression)",function(s,e,c)
|
||||
return R(s,e.expression,c,"return: result")
|
||||
Bind(ns,"return (expression)",function(s,e,c)
|
||||
return Run(s,e.expression,c,"return: result")
|
||||
end)
|
||||
B(ns,"(macro) means (expansion)",function(s,e,c)
|
||||
Bind(ns,"(macro) means (expansion)",function(s,e,c)
|
||||
|
||||
end)
|
||||
B(ns,"(identifier) is (value)",function(s,e,c)
|
||||
Bind(ns,"(identifier) is (value)",function(s,e,c)
|
||||
|
||||
end)
|
||||
B(ns,"(expression) becomes (result)",function(s,e,c)
|
||||
Bind(ns,"(expression) becomes (result)",function(s,e,c)
|
||||
|
||||
end)
|
||||
B(ns,"let (binding) in (scope)",function(s,e,c)
|
||||
Bind(ns,"let (binding) in (scope)",function(s,e,c)
|
||||
|
||||
end)
|
||||
|
||||
local Hpath = "nel/helper.nel"
|
||||
local Hchain = Chain("in internal: "..Hpath)
|
||||
local success,result = Hchain:call(function(chain)
|
||||
R(NelliScope,interpret(read(Hpath),Hpath,chain),chain)
|
||||
Run(NelliScope,Parse(read(Hpath),Hpath,chain),chain)
|
||||
end)
|
||||
|
||||
--[[ Documentation:
|
||||
@@ -1,9 +1,9 @@
|
||||
require("parse")
|
||||
require("interpreter")
|
||||
|
||||
-- TODO: This should really be a separate API and module
|
||||
-- please break it out
|
||||
-- Interpret some text
|
||||
function interpret(content,uri,chain)
|
||||
function Parse(content,uri,chain)
|
||||
-- LOCALS
|
||||
-- The pattern for words inbetween expressions
|
||||
local expressionWordPattern = "(.*)"
|
||||
@@ -191,6 +191,4 @@ function interpret(content,uri,chain)
|
||||
end
|
||||
return consumeExpression()
|
||||
-- TODO: check for later expressions please?
|
||||
end
|
||||
|
||||
return _G
|
||||
end
|
||||
Reference in New Issue
Block a user