1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-28 00:59:31 +00:00

Style cleanup in interpreter.expression

This commit is contained in:
Étienne Fildadut 2022-06-13 00:17:24 +09:00
parent 4ac0d3f8f7
commit 92a496e584

View file

@ -6,7 +6,7 @@ local run
local unpack = table.unpack or unpack local unpack = table.unpack or unpack
--- evaluate an expression --- evaluate an expression
-- returns evaluated value if success -- returns evaluated value (table) if success
-- returns nil, error if error -- returns nil, error if error
local function eval(state, exp) local function eval(state, exp)
-- nil -- nil
@ -24,7 +24,7 @@ local function eval(state, exp)
-- string -- string
elseif exp.type == "string" then elseif exp.type == "string" then
local t, e = eval_text(state, exp.text) local t, e = eval_text(state, exp.text)
if not t then return t, e end if not t then return nil, e end
return { return {
type = "string", type = "string",
value = t value = t
@ -36,7 +36,7 @@ local function eval(state, exp)
elseif exp.type == "list_brackets" then elseif exp.type == "list_brackets" then
if exp.expression then if exp.expression then
local v, e = eval(state, exp.expression) local v, e = eval(state, exp.expression)
if not v then return v, e end if not v then return nil, e end
if exp.expression.type == "list" then if exp.expression.type == "list" then
return v return v
-- contained a single element, wrap in list manually -- contained a single element, wrap in list manually
@ -58,7 +58,7 @@ local function eval(state, exp)
local l = {} local l = {}
for _, ast in ipairs(flat) do for _, ast in ipairs(flat) do
local v, e = eval(state, ast) local v, e = eval(state, ast)
if not v then return v, e end if not v then return nil, e end
table.insert(l, v) table.insert(l, v)
end end
return { return {
@ -70,7 +70,7 @@ local function eval(state, exp)
if exp.left.type == "variable" then if exp.left.type == "variable" then
local name = exp.left.name local name = exp.left.name
local val, vale = eval(state, exp.right) local val, vale = eval(state, exp.right)
if not val then return val, vale end if not val then return nil, vale end
set_variable(state, name, val) set_variable(state, name, val)
return val return val
else else
@ -79,10 +79,10 @@ local function eval(state, exp)
-- lazy boolean operators -- lazy boolean operators
elseif exp.type == "&" then elseif exp.type == "&" then
local left, lefte = eval(state, exp.left) local left, lefte = eval(state, exp.left)
if not left then return left, lefte end if not left then return nil, lefte end
if truthy(left) then if truthy(left) then
local right, righte = eval(state, exp.right) local right, righte = eval(state, exp.right)
if not right then return right, righte end if not right then return nil, righte end
if truthy(right) then if truthy(right) then
return { return {
type = "number", type = "number",
@ -96,7 +96,7 @@ local function eval(state, exp)
} }
elseif exp.type == "|" then elseif exp.type == "|" then
local left, lefte = eval(state, exp.left) local left, lefte = eval(state, exp.left)
if not left then return left, lefte end if not left then return nil, lefte end
if truthy(left) then if truthy(left) then
return { return {
type = "number", type = "number",
@ -104,7 +104,7 @@ local function eval(state, exp)
} }
end end
local right, righte = eval(state, exp.right) local right, righte = eval(state, exp.right)
if not right then return right, righte end if not right then return nil, righte end
return { return {
type = "number", type = "number",
value = truthy(right) and 1 or 0 value = truthy(right) and 1 or 0
@ -112,10 +112,10 @@ local function eval(state, exp)
-- conditional -- conditional
elseif exp.type == "~" then elseif exp.type == "~" then
local right, righte = eval(state, exp.right) local right, righte = eval(state, exp.right)
if not right then return right, righte end if not right then return nil, righte end
if truthy(right) then if truthy(right) then
local left, lefte = eval(state, exp.left) local left, lefte = eval(state, exp.left)
if not left then return left, lefte end if not left then return nil, lefte end
return left return left
end end
return { return {
@ -125,15 +125,15 @@ local function eval(state, exp)
-- while loop -- while loop
elseif exp.type == "~?" then elseif exp.type == "~?" then
local right, righte = eval(state, exp.right) local right, righte = eval(state, exp.right)
if not right then return right, righte end if not right then return nil, righte end
local l = {} local l = {}
while truthy(right) do while truthy(right) do
local left, lefte = eval(state, exp.left) local left, lefte = eval(state, exp.left)
if not left then return left, lefte end if not left then return nil, lefte end
table.insert(l, left) table.insert(l, left)
-- next iteration -- next iteration
right, righte = eval(state, exp.right) right, righte = eval(state, exp.right)
if not right then return right, righte end if not right then return nil, righte end
end end
return { return {
type = "list", type = "list",
@ -142,11 +142,11 @@ local function eval(state, exp)
-- tag -- tag
elseif exp.type == "#" then elseif exp.type == "#" then
local right, righte = eval(state, exp.right) local right, righte = eval(state, exp.right)
if not right then return right, righte end if not right then return nil, righte end
tags:push(state, right) tags:push(state, right)
local left, lefte = eval(state, exp.left) local left, lefte = eval(state, exp.left)
tags:pop(state) tags:pop(state)
if not left then return left, lefte end if not left then return nil, lefte end
return left return left
-- variable -- variable
elseif exp.type == "variable" then elseif exp.type == "variable" then
@ -168,7 +168,7 @@ local function eval(state, exp)
local args = {} local args = {}
if exp.argument then if exp.argument then
local arg, arge = eval(state, exp.argument) local arg, arge = eval(state, exp.argument)
if not arg then return arg, arge end if not arg then return nil, arge end
args = arg.value args = arg.value
end end
-- function reference: call the referenced function -- function reference: call the referenced function
@ -202,7 +202,7 @@ local function eval(state, exp)
if exp.assignment then if exp.assignment then
local arge local arge
assignment, arge = eval(state, exp.assignment) assignment, arge = eval(state, exp.assignment)
if not assignment then return assignment, arge end if not assignment then return nil, arge end
end end
-- try to select a function -- try to select a function
local tried_function_error_messages = {} local tried_function_error_messages = {}
@ -243,7 +243,7 @@ local function eval(state, exp)
-- check type annotation -- check type annotation
if param.type_annotation then if param.type_annotation then
local v, e = eval(state, param.type_annotation) local v, e = eval(state, param.type_annotation)
if not v then return v, e end if not v then return nil, e end
local depth = is_of_type(val, v) local depth = is_of_type(val, v)
if not depth then if not depth then
ok = false ok = false
@ -286,7 +286,7 @@ local function eval(state, exp)
local param = fn.assignment local param = fn.assignment
if param.type_annotation then if param.type_annotation then
local v, e = eval(state, param.type_annotation) local v, e = eval(state, param.type_annotation)
if not v then return v, e end if not v then return nil, e end
local depth = is_of_type(assignment, v) local depth = is_of_type(assignment, v)
if not depth then if not depth then
ok = false ok = false
@ -346,7 +346,7 @@ local function eval(state, exp)
-- checkpoint: no args and resume execution -- checkpoint: no args and resume execution
elseif fn.subtype == "checkpoint" then elseif fn.subtype == "checkpoint" then
local r, e = run(state, fn.child, not paren_call) local r, e = run(state, fn.child, not paren_call)
if not r then return r, e end if not r then return nil, e end
return r return r
-- other functions -- other functions
else else
@ -376,12 +376,12 @@ local function eval(state, exp)
local final_args = {} local final_args = {}
for j, param in ipairs(fn.params) do for j, param in ipairs(fn.params) do
local v, e = get_variable(state, param.full_name) local v, e = get_variable(state, param.full_name)
if not v then return v, e end if not v then return nil, e end
final_args[j] = v final_args[j] = v
end end
if fn.assignment then if fn.assignment then
local v, e = get_variable(state, fn.assignment.full_name) local v, e = get_variable(state, fn.assignment.full_name)
if not v then return v, e end if not v then return nil, e end
final_args[#final_args+1] = v final_args[#final_args+1] = v
end end
-- execute function -- execute function
@ -438,10 +438,10 @@ local function eval(state, exp)
-- resume at last checkpoint -- resume at last checkpoint
else else
local expr, err = expression(checkpoint.value[1], state, fn.namespace) local expr, err = expression(checkpoint.value[1], state, fn.namespace)
if not expr then return expr, err end if not expr then return nil, err end
ret, e = eval(state, expr) ret, e = eval(state, expr)
end end
if not ret then return ret, e end if not ret then return nil, e end
end end
-- update function vars -- update function vars
set_variable(state, fn.namespace.."👁️", { set_variable(state, fn.namespace.."👁️", {
@ -504,7 +504,7 @@ local function eval(state, exp)
events:append(state, "text", { text = text, tags = current_tags }) events:append(state, "text", { text = text, tags = current_tags })
end) end)
events:pop_buffer(state) events:pop_buffer(state)
if not v then return v, e end if not v then return nil, e end
return { return {
type = "event buffer", type = "event buffer",
value = l value = l