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

Second test batch and associated fixes

This commit is contained in:
Étienne Fildadut 2023-12-29 17:15:43 +01:00
parent 7abb116876
commit 9b7d1e436e
56 changed files with 760 additions and 27 deletions

View file

@ -17,7 +17,9 @@ AttachBlock = ast.abstract.Node {
end,
_format = function(self, state, priority, indentation, ...)
return self.expression:format(state, priority, indentation, ...).."\n\t"..self.block:format(state, priority, indentation + 1, ...)
local exp = self.expression:format(state, priority, indentation, ...)
if exp:sub(-2) == " _" then exp = exp:sub(1, -3) end
return exp.."\n\t"..self.block:format(state, priority, indentation + 1, ...)
end,
traverse = function(self, fn, ...)

View file

@ -50,12 +50,9 @@ local Definition = ast.abstract.Node {
symbol:prepare(state)
val:prepare(state)
if self.symbol.alias then
state.scope:define(symbol:with{ alias = false }, val) -- disable alias to avoid call in Identifier:_prepare
elseif Overloadable:issub(val) then
state.scope:define_overloadable(symbol, val)
else
state.scope:define(symbol, val)
-- predefine exported variables
if symbol.exported then
self:eval(state)
end
end
}

View file

@ -28,7 +28,7 @@ local VariableMetadata = ast.abstract.Runtime {
end
if self.symbol.type_check then
local r = self.symbol.type_check:call(state, ArgumentTuple:new(value))
if not r:truthy() then error(("type check failure for %s; %s does not satisfy %s"):format(self.symbol.string, value, self.symbol.type_check)) end
if not r:truthy() then error(("type check failure for %s; %s does not satisfy %s"):format(self.symbol.string, value, self.symbol.type_check), 0) end
end
if self.symbol.alias then
local assign_args = ArgumentTuple:new()
@ -185,14 +185,12 @@ local Environment = ast.abstract.Runtime {
return self:_get_variable(state, identifier):set(state, val)
end,
-- returns a list {[symbol]=val,...} of all exported variables in the current strict layer
-- returns a list {[symbol]=val,...} of all exported variables (evaluated) in the current strict layer
list_exported = function(self, state)
assert(self.export, "not an export scope layer")
local r = {}
for _, vm in self.variables:iter(state) do
if vm.symbol.exported then
r[vm.symbol] = vm:get(state)
end
r[vm.symbol] = vm:get(state)
end
return r
end,

View file

@ -165,7 +165,7 @@ Node = class {
local r = {}
for _, tr in ipairs(l) do
table.insert(r, "(("..tr.source.."))")
table.insert(r, Call:new(Identifier:new("_#_"), ArgumentTuple:new(tr.context, Identifier:new("_"))))
table.insert(r, Call:new(Identifier:new("_#_"), ArgumentTuple:new(tr.context, Identifier:new("_"))):format())
table.insert(r, "\t"..Call:new(Identifier:new("_->_"), ArgumentTuple:new(tr, tr)):format())
table.insert(r, "")
end

View file

@ -12,6 +12,15 @@ local parser = require("parser")
local binser = require("lib.binser")
local anselme
-- same as assert, but do not add position information
-- useful for errors raised from anselme (don't care about Lua error position)
local function assert0(v, message, ...)
if not v then
error(message, 0)
end
return v, message, ...
end
local State
State = class {
type = "anselme state",
@ -142,7 +151,7 @@ State = class {
run = function(self, code, source)
assert(not self:active(), "a script is already active")
self._coroutine = coroutine.create(function()
local r = assert(self:eval_local(code, source))
local r = assert0(self:eval_local(code, source))
event_manager:final_flush(self)
return "return", r
end)
@ -174,7 +183,7 @@ State = class {
assert(self:active(), "trying to interrupt but no script is currently active")
if code then
self._coroutine = coroutine.create(function()
local r = assert(self:eval_local(code, source))
local r = assert0(self:eval_local(code, source))
event_manager:final_flush(self)
self.scope:reset() -- scope stack is probably messed up after the switch
return "return", r

View file

@ -0,0 +1,33 @@
--# run #--
--- choice ---
=> | {}"a "|
> | {}"b "|
--- text ---
| {}"-> a"|
--- choice ---
> | {}"a "|
=> | {}"b "|
--- text ---
| {}"-> b"|
--- choice ---
=> | {}"b "|
--- text ---
| {}"-> b"|
--- choice ---
> | {}"a "|
=> | {1:25}"b "|
--- text ---
| {1:25}"-> b"|
--- choice ---
=> | {1:3}"b "|
--- text ---
| {1:3}"-> b"|
--- choice ---
=> | {1:12}"a "|
> | {1:3}"b "|
--- text ---
| {1:12}"-> a"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"" {}"*[3]" {}""|
--- text ---
| {}"" {}"*[3, 52]" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -1,6 +1,6 @@
--# run #--
--- error ---
./state/State.lua:145: trying to change the value of constant a
trying to change the value of constant a
↳ from test/tests/constant variable.ans:5:3 in assignment: a = 52
↳ from ? in block: ::a = 3…
--# saved #--

View file

@ -0,0 +1,11 @@
--# run #--
--- text ---
| {}"" {}"type(\"kg\", 5)" {}""|
--- text ---
| {}"" {}"type(\"kg\", 12)" {}""|
--- error ---
type check failure for weigh; 32 does not satisfy ($(x) type(x) == t)
↳ from test/tests/constrained variable assignement.ans:9:7 in assignment: weigh = 32
↳ from ? in block: :weigh::is("kg") = type(5, "kg")…
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"" {}"type(\"kg\", 5)" {}""|
--- text ---
| {}"" {}"12" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -1,4 +1,7 @@
--# parse error #--
--# run #--
--- error ---
a is already defined in the current scope
↳ from test/tests/define override function.ans:4:4 in definition: :a = 2
↳ from ? in block: :a = ($() _)…
↳ from ? in block: :a = ($() _)…
--# saved #--
{}

View file

@ -1,5 +1,8 @@
--# parse error #--
--# run #--
--- error ---
can't add an overload variant to non-overloadable variable a defined in the same scope
↳ from test/tests/define override variable.ans:3:1 in definition: :a = ($() _)
↳ from test/tests/define override variable.ans:3:1 in attach block: :a = ($() _)…
↳ from ? in block: :a = 2…
↳ from ? in block: :a = 2…
--# saved #--
{}

View file

@ -1,4 +1,7 @@
--# parse error #--
--# run #--
--- error ---
a is already defined in the current scope
↳ from test/tests/define override.ans:3:4 in definition: :a = 2
↳ from ? in block: :a = 5…
↳ from ? in block: :a = 5…
--# saved #--
{}

View file

@ -0,0 +1,27 @@
--# run #--
--- text ---
| {}"false = " {}"false" {}""|
--- text ---
| {}"false = " {}"false" {}""|
--- text ---
| {}"true = " {}"true" {}""|
--- text ---
| {}"false = " {}"false" {}""|
--- text ---
| {}"false = " {}"false" {}""|
--- text ---
| {}"false = " {}"false" {}""|
--- text ---
| {}"false = " {}"false" {}""|
--- text ---
| {}"true = " {}"true" {}""|
--- text ---
| {}"false = " {}"false" {}""|
--- text ---
| {}"true = " {}"true" {}""|
--- text ---
| {}"true = " {}"true" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,5 @@
--# run #--
--- return ---
"ok"
--# saved #--
{}

View file

@ -1,6 +1,6 @@
--# run #--
--- error ---
./state/State.lua:145: can't call closure ($(a, b) _): expected 2 arguments, received 1
can't call closure ($(a, b) _): expected 2 arguments, received 1
↳ from test/tests/function args arity check fail.ans:4:2 in call: f("ok")
↳ from ? in block: :f = ($(a, b) _)…
--# saved #--

View file

@ -0,0 +1,21 @@
--# run #--
--- text ---
| {}"local:"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"exported:"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"2" {}""|
--- text ---
| {}"" {}"3" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,8 @@
--# run #--
--- text ---
| {}"" {}"5" {}""|
| {}"" {}"2" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,8 @@
--# run #--
--- text ---
| {}"" {}"5" {}""|
| {}"" {}"2" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -1,6 +1,6 @@
--# run #--
--- error ---
./state/State.lua:145: identifier "b" is undefined in branch 0a138a38-3faa-4478-10f6f-1a9de1e0a8e1
identifier "b" is undefined in branch 0a138a38-3faa-4478-10f6f-1a9de1e0a8e1
↳ from test/tests/function scope wrong.ans:4:7 in identifier: b
↳ from test/tests/function scope wrong.ans:4:1 in text interpolation: | a: {b}|
↳ from test/tests/function scope wrong.ans:4:1 in translatable: | a: {b}|

View file

@ -0,0 +1,65 @@
--# run #--
--- text ---
| {}"new list each time:"|
--- text ---
| {}"start: " {}"*[]" {}""|
--- text ---
| {}"before recursion " {}"1" {}": " {}"*[1]" {}""|
--- text ---
| {}"start: " {}"*[]" {}""|
--- text ---
| {}"before recursion " {}"2" {}": " {}"*[2]" {}""|
--- text ---
| {}"start: " {}"*[]" {}""|
--- text ---
| {}"before recursion " {}"3" {}": " {}"*[3]" {}""|
--- text ---
| {}"start: " {}"*[]" {}""|
--- text ---
| {}"before recursion " {}"4" {}": " {}"*[4]" {}""|
--- text ---
| {}"start: " {}"*[]" {}""|
--- text ---
| {}"before recursion " {}"5" {}": " {}"*[5]" {}""|
--- text ---
| {}"after recursion " {}"4" {}": " {}"*[4]" {}""|
--- text ---
| {}"after recursion " {}"3" {}": " {}"*[3]" {}""|
--- text ---
| {}"after recursion " {}"2" {}": " {}"*[2]" {}""|
--- text ---
| {}"after recursion " {}"1" {}": " {}"*[1]" {}""|
--- text ---
| {}"pass list:"|
--- text ---
| {}"start: " {}"*[]" {}""|
--- text ---
| {}"before recursion " {}"1" {}": " {}"*[1]" {}""|
--- text ---
| {}"start: " {}"*[1]" {}""|
--- text ---
| {}"before recursion " {}"2" {}": " {}"*[1, 2]" {}""|
--- text ---
| {}"start: " {}"*[1, 2]" {}""|
--- text ---
| {}"before recursion " {}"3" {}": " {}"*[1, 2, 3]" {}""|
--- text ---
| {}"start: " {}"*[1, 2, 3]" {}""|
--- text ---
| {}"before recursion " {}"4" {}": " {}"*[1, 2, 3, 4]" {}""|
--- text ---
| {}"start: " {}"*[1, 2, 3, 4]" {}""|
--- text ---
| {}"before recursion " {}"5" {}": " {}"*[1, 2, 3, 4, 5]" {}""|
--- text ---
| {}"after recursion " {}"4" {}": " {}"*[1, 2, 3, 4, 5]" {}""|
--- text ---
| {}"after recursion " {}"3" {}": " {}"*[1, 2, 3, 4, 5]" {}""|
--- text ---
| {}"after recursion " {}"2" {}": " {}"*[1, 2, 3, 4, 5]" {}""|
--- text ---
| {}"after recursion " {}"1" {}": " {}"*[1, 2, 3, 4, 5]" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,61 @@
--# run #--
--- text ---
| {}"depth 1:"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}">" {}" depth 2, unscoped:"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}">" {}" depth 2, scoped:"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}">" {}" depth 2, unscoped:"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}">" {}" depth 2, scoped:"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}">" {}" depth 2, unscoped:"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}">" {}" depth 2, scoped:"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,33 @@
--# run #--
--- text ---
| {}"start: " {}"1" {}""|
--- text ---
| {}"before recursion " {}"1" {}": " {}"2" {}""|
--- text ---
| {}"start: " {}"1" {}""|
--- text ---
| {}"before recursion " {}"2" {}": " {}"2" {}""|
--- text ---
| {}"start: " {}"1" {}""|
--- text ---
| {}"before recursion " {}"3" {}": " {}"2" {}""|
--- text ---
| {}"start: " {}"1" {}""|
--- text ---
| {}"before recursion " {}"4" {}": " {}"2" {}""|
--- text ---
| {}"start: " {}"1" {}""|
--- text ---
| {}"before recursion " {}"5" {}": " {}"2" {}""|
--- text ---
| {}"after recursion " {}"4" {}": " {}"2" {}""|
--- text ---
| {}"after recursion " {}"3" {}": " {}"2" {}""|
--- text ---
| {}"after recursion " {}"2" {}": " {}"2" {}""|
--- text ---
| {}"after recursion " {}"1" {}": " {}"2" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,21 @@
--# run #--
--- text ---
| {}"paren:"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"no paren:"|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- text ---
| {}"" {}"1" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,12 @@
--# run #--
--- error ---
can't call overload overload<($(c::($(x) <lua function>), s::($(x) <lua function>)) = v; <lua function>), ($(c::($(x) <lua function>), s::($(x) <lua function>)) = v; <lua function>), ($(c::($(x) <lua function>), s::($(x) <lua function>)) <lua function>)>: no function match (overload<($(b) _), ($(x) _), ($() _)>, "a"), possible functions were:
• (c::($(x) <lua function>), s::($(x) <lua function>)) = v: expected 3 arguments, received 2
• (c::($(x) <lua function>), s::($(x) <lua function>)) = v: expected 3 arguments, received 2
• (c::($(x) <lua function>), s::($(x) <lua function>)): type check failure for parameter c in function (c::($(x) <lua function>), s::($(x) <lua function>))
↳ from test/tests/function separate variable from variants.ans:10:4 in call: f . "a"
↳ from test/tests/function separate variable from variants.ans:10:1 in text interpolation: | {(f . "a")} = 2|
↳ from test/tests/function separate variable from variants.ans:10:1 in translatable: | {(f . "a")} = 2|
↳ from ? in block: :f = ($() _)…
--# saved #--
{}

View file

@ -1,6 +1,6 @@
--# run #--
--- error ---
./state/State.lua:145: can't call overload overload<($(a::($(x) <lua function>)) _), ($(x::($(x) <lua function>)) _)>: more than one function match (5), matching functions were at least (specificity 1.3):
can't call overload overload<($(a::($(x) <lua function>)) _), ($(x::($(x) <lua function>)) _)>: more than one function match (5), matching functions were at least (specificity 1.3):
• (x::($(x) <lua function>))
• (a::($(x) <lua function>))
↳ from test/tests/function type dispatch ambigous.ans:7:3 in call: fn(5)

View file

@ -10,7 +10,7 @@
--- text ---
| {}"" {}"*[3, 12, 99]" {}""|
--- error ---
./state/State.lua:145: list index out of bounds
list index out of bounds
↳ from test/tests/list assignement.ans:21:6 in call: x(5) = 0
↳ from ? in block: :x = *[1, 2]…
--# saved #--

View file

@ -8,7 +8,7 @@
--- text ---
| {}"" {}"3" {}" == " {}"3" {}""|
--- error ---
./state/State.lua:145: tuple index out of bounds
tuple index out of bounds
↳ from test/tests/list index.ans:11:4 in call: x(-4)
↳ from test/tests/list index.ans:11:1 in text interpolation: | {x(-4)}|
↳ from test/tests/list index.ans:11:1 in translatable: | {x(-4)}|

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"" {}"50" {}" = 50"|
--- text ---
| {}"" {}"3" {}" = 3"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"a " {1:5}"" {1:5}"b" {1:5}"" {}" c"|
--- text ---
| {2:2}"a " {1:5, 2:2}"" {1:5, 2:2}"b" {1:5, 2:2}"" {2:2}" c"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {1:1}"lol"|
--- return ---
@| {}"a " {1:2}"d" {}" " {1:3}"t" {}" b"|
--# saved #--
{}

View file

@ -0,0 +1,7 @@
--# run #--
--- text ---
| {}"lol"|
--- return ---
@| {}"a " {}"d" {}" b"|
--# saved #--
{}

View file

@ -0,0 +1,11 @@
--# run #--
--- text ---
| {}"Hello"|
--- text ---
| {}"Bonjour"|
--- text ---
| {}"Hello"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"" {}"Hello" {}""|
--- text ---
| {}"" {}"Bonjour" {}""|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,9 @@
--# run #--
--- text ---
| {}"Hello"|
--- text ---
| {}"Bonjour"|
--- return ---
()
--# saved #--
{}

View file

@ -0,0 +1,35 @@
1 ~ | a |>
| -> a
| b |>
| -> b
:@choice = 1
1 ~ | a |>
| -> a
| b |>
| -> b
choice = 2
() ~ | a |>
| -> a
| b |>
| -> b
choice = 1
| a |>
| -> a
25 # | b |>
| -> b
choice = 2
12 # () ~ | a |>
| -> a
3 # | b |>
| -> b
choice = 1
12 # 1 ~ | a |>
| -> a
3 # | b |>
| -> b
choice = 1

View file

@ -0,0 +1,7 @@
::a = *[3]
|{a}
a!insert(52)
|{a}

View file

@ -0,0 +1,11 @@
:weigh::is("kg") = 5!type("kg")
|{weigh}
weigh = 12!type("kg")
|{weigh}
weigh = 32
|{weigh}

View file

@ -0,0 +1,7 @@
:weigh::($(x)x!type=="kg") = type(5, "kg")
|{weigh}
:not weigh::($(x)x!type=="kg") = 12
|{not weigh}

View file

@ -0,0 +1,29 @@
::a = [1:2]
|false = {a == [5:2]}
|false = {a == [1:3]}
|true = {a == [1:2]}
::b = [1,2,3]
|false = {b == a}
|false = {b == []}
|false = {b == [3,1,2]}
|false = {b == [1,2,3,4]}
|true = {b == [1,2,3]}
:c = *[1,2,3]
|false = {c == b}
|true = {c!to tuple == b}
::d = [1,2,3]
|true = {d == b}

View file

@ -0,0 +1,4 @@
:f = $
:@x = "ok"
f.x

View file

@ -0,0 +1,29 @@
:$ f
:a = 1
|{a}
a = a + 1
:$ g
:@a = 1
|{a}
a = a + 1
|local:
f!
f!
f!
|exported:
g!
g!
g!

View file

@ -0,0 +1,9 @@
:$ hey
:@$ foo
@2
@3
@5
|u
|{hey!}
|{hey.foo!}

View file

@ -0,0 +1,7 @@
:$ hey
:@$ foo
@2
@5
|{hey!}
|{hey.foo!}

View file

@ -0,0 +1,40 @@
:n = 0
:$ f(c=1)
:a = *[]
|start: {a}
a!insert(c)
n += 1
|before recursion {c}: {a}
n < 5 ~
f(c+1)
|after recursion {c}: {a}
|new list each time:
f!
:$ g(c=1, a=*[])
|start: {a}
a!insert(c)
n += 1
|before recursion {c}: {a}
n < 5 ~
g(c+1, a)
|after recursion {c}: {a}
|pass list:
n = 0
g!

View file

@ -0,0 +1,44 @@
:$ f()
:a = 1
|{a}
a = a + 1
:$ g
:a = 1
|{a}
a = a + 1
:$ h()
:a = 1
|{a}
a = a + 1
|\> depth 2, unscoped:
g!
g!
g!
|\> depth 2, scoped:
h!
h!
h!
|depth 1:
f!
f!
f!

View file

@ -0,0 +1,19 @@
:n = 0
:$ f(c=1)
:a = 1
|start: {a}
a = a + 1
n += 1
|before recursion {c}: {a}
n < 5 ~
f(c+1)
|after recursion {c}: {a}
f!

View file

@ -0,0 +1,29 @@
:$ f()
:a = 1
|{a}
a = a + 1
:$ g
:a = 1
|{a}
a = a + 1
|paren:
f!
f!
f!
|no paren:
g!
g!
g!

View file

@ -0,0 +1,10 @@
:$ f
:@a = 2
:$ f(x)
:a = 5
:$ f(b)
:a = 10
|{f.a} = 2

View file

@ -0,0 +1,13 @@
:$ fn
:i=0
@
i=50
i
| {fn!} = 50
:$ g
@
@3
| {g!} = 3

View file

@ -0,0 +1,7 @@
:$ f
@"b"
|a {5 # |{f!}} c
2:2 #
|a {5 # |{f!}} c

View file

@ -0,0 +1,8 @@
:$ f
1 # | lol
@2 # |d
:a = |a {f!} {3#|t} b
@a

View file

@ -0,0 +1,8 @@
:$ f
|lol
@|d
:a = |a {f!} b
@a

View file

@ -0,0 +1,8 @@
| Hello
"source": "test/tests/translate context.ans:6:1" #
| Hello| -> | Bonjour
| Hello
| Hello

View file

@ -0,0 +1,5 @@
|{%"Hello"}
%"Hello" -> "Bonjour"
|{%"Hello"}

View file

@ -0,0 +1,5 @@
| Hello
| Hello| -> | Bonjour
| Hello