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

Add file loading functions to stdlib

This commit is contained in:
Étienne Fildadut 2024-01-08 19:28:14 +01:00
parent 8562e8f18b
commit bd93dc43dc
10 changed files with 82 additions and 5 deletions

View file

@ -50,13 +50,13 @@ State = class {
self.scope:pop()
local exported = self.scope:capture()
self.scope:pop()
-- redefine operators
for name, var in exported.variables:iter(self) do
if operator_priority[name.name] then
self.scope:define(var:get_symbol(), var:get(self))
end
end
-- load translated functions
self.scope:push_partial(Identifier:new("stdlib"))
self.scope:define(Identifier:new("stdlib"):to_symbol(), exported)
parser(require("anselme.stdlib.language."..language), "stdlib/language/"..language..".ans"):eval(self)

View file

@ -1,6 +1,7 @@
local ast = require("anselme.ast")
local Nil, Boolean, Definition = ast.Nil, ast.Boolean, ast.Definition
local assert0 = require("anselme.common").assert0
local parser = require("anselme.parser")
return {
{
@ -40,4 +41,41 @@ return {
return r
end
},
{
"import", "(env::is environment, symbol tuple::is tuple)",
function(state, env, l)
for _, sym in l:iter(state) do
Definition:new(sym, env:get(state, sym:to_identifier())):eval(state)
end
return env
end
},
{
"import", "(env::is environment, symbol::is symbol)",
function(state, env, sym)
Definition:new(sym, env:get(state, sym:to_identifier())):eval(state)
return env
end
},
{
"load", "(path::is string)",
function(state, path)
-- read file
local f = assert(io.open(path.string, "r"))
local block = parser(f:read("a"), path.string)
f:close()
-- exec in new scope
state.scope:push_global()
state.scope:push_export()
state.scope:push()
block:eval(state)
state.scope:pop()
local exported = state.scope:capture()
state.scope:pop()
state.scope:pop()
return exported
end
},
}

View file

@ -12,8 +12,6 @@ Documentation:
Translation.
Translation model for stdlib: ?
Do some more fancy scope work to allow the translation to access variables defined in the translation file?
---
@ -22,7 +20,6 @@ Standard library.
* Text and string manipulation would make sense, but that would require a full UTF-8/Unicode support library like https://github.com/starwing/luautf8.
- retag/add tags
* Something to load other files. Maybe not load it by default to let the calling game sandbox Anselme. Probably create an export scope per file to perform some nice module loading.
* And in general, clean up everything.
---

View file

@ -0,0 +1,8 @@
--# run #--
--- error ---
no variable "b" defined in environment
↳ from test/tests/load file error.ans:3:4 in call: e . "b"
↳ from test/tests/load file error.ans:3:1 in text interpolation: | {e . "b"} |
↳ from ? in block: :e = load("test/tests/import/test.ans")…
--# saved #--
{}

View file

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

View file

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

View file

@ -0,0 +1,3 @@
:@a = 1
:b = 2
:@c = 3

View file

@ -0,0 +1,3 @@
:e = load("test/tests/import/test.ans")
|{e.b}

View file

@ -0,0 +1,5 @@
load("test/tests/import/test.ans")!import[:a, :c]
|{a}
|{c}

5
test/tests/load file.ans Normal file
View file

@ -0,0 +1,5 @@
:e = load("test/tests/import/test.ans")
|{e.a}
|{e.c}