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:
parent
8562e8f18b
commit
bd93dc43dc
10 changed files with 82 additions and 5 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
},
|
||||
}
|
||||
|
|
|
|||
3
ideas.md
3
ideas.md
|
|
@ -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.
|
||||
|
||||
---
|
||||
|
|
|
|||
8
test/results/load file error.ans
Normal file
8
test/results/load file error.ans
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
--# run #--
|
||||
--- error ---
|
||||
[0m[31m[0m[31m[0m[31mno variable "b" defined in environment[0m
|
||||
↳ from [4mtest/tests/load file error.ans:3:4[0m in call: [2me . "b"[0m[0m
|
||||
↳ from [4mtest/tests/load file error.ans:3:1[0m in text interpolation: [2m| {e . "b"} |[0m[0m
|
||||
↳ from [4m?[0m in block: [2m:e = load("test/tests/import/test.ans")…[0m
|
||||
--# saved #--
|
||||
{}
|
||||
9
test/results/load file import.ans
Normal file
9
test/results/load file import.ans
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
--# run #--
|
||||
--- text ---
|
||||
| {}"" {}"1" {}"" |
|
||||
--- text ---
|
||||
| {}"" {}"3" {}"" |
|
||||
--- return ---
|
||||
()
|
||||
--# saved #--
|
||||
{}
|
||||
9
test/results/load file.ans
Normal file
9
test/results/load file.ans
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
--# run #--
|
||||
--- text ---
|
||||
| {}"" {}"1" {}"" |
|
||||
--- text ---
|
||||
| {}"" {}"3" {}"" |
|
||||
--- return ---
|
||||
()
|
||||
--# saved #--
|
||||
{}
|
||||
3
test/tests/import/test.ans
Normal file
3
test/tests/import/test.ans
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
:@a = 1
|
||||
:b = 2
|
||||
:@c = 3
|
||||
3
test/tests/load file error.ans
Normal file
3
test/tests/load file error.ans
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
:e = load("test/tests/import/test.ans")
|
||||
|
||||
|{e.b}
|
||||
5
test/tests/load file import.ans
Normal file
5
test/tests/load file import.ans
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
load("test/tests/import/test.ans")!import[:a, :c]
|
||||
|
||||
|{a}
|
||||
|
||||
|{c}
|
||||
5
test/tests/load file.ans
Normal file
5
test/tests/load file.ans
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
:e = load("test/tests/import/test.ans")
|
||||
|
||||
|{e.a}
|
||||
|
||||
|{e.c}
|
||||
Loading…
Add table
Add a link
Reference in a new issue