forked from Fundament-Software/Alicorn0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.lua
148 lines (135 loc) · 3.63 KB
/
modules.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
local types = require "./typesystem"
local evaluator = require "./alicorn-evaluator"
local environment = require "./environment"
local metalang = require "./metalanguage"
local treemap = require "./lazy-prefix-tree"
local module_type = {
kind = {
kind_name = "module_kind",
type_name = function()
return "module"
end,
duplicable = function()
return true
end,
discardable = function()
return true
end,
},
params = {},
}
local function module_retain(binding)
if binding.kind == "reusable" then
return true, binding
else
return false
end
end
local function new_mod(values)
local self = {
values = values:filtermap_values(module_retain),
}
if self.values == nil then
error "tried to new_mod something that becomes nil"
end
return { type = module_type, val = self }
end
local function index_mod(mod, name)
return mod.values:get(name)
end
local function use_mod(mod, env)
-- print "in use_mod"
-- p(mod)
return environment.new_env {
locals = env.locals,
nonlocals = env.nonlocals:extend(mod.values),
carrier = env.carrier,
perms = env.perms,
}
end
local function open_mod(mod, env)
return environment.new_env {
locals = env.locals:extend(mod.values),
nonlocals = env.nonlocals,
carrier = env.carrier,
perms = env.perms,
}
end
local function use_mod_op_impl(syntax, env)
local ok, modval, env = syntax:match({
evaluator.evaluates_args(metalang.accept_handler, env),
}, metalang.failure_handler, nil)
if not ok then
return ok, modval
end
if #modval ~= 1 or modval[1].type ~= module_type then
return false, "using syntax should take exactly one argument, a module"
end
return true, types.unit_val, use_mod(modval[1].val, env)
end
local function open_mod_op_impl(syntax, env)
local ok, modval, env = syntax:match({
evaluator.evaluates_args(metalang.accept_handler, env),
}, metalang.failure_handler, nil)
if not ok then
return ok, modval
end
if #modval ~= 1 or modval[1].type ~= module_type then
return false, "open-mod syntax should take exactly one argument, a module"
end
return true, types.unit_val, open_mod(modval[1].val, env)
end
local function get_op_impl(syntax, env)
local ok, modval, env, name = syntax:match({
metalang.listmatch(
metalang.accept_handler,
{ evaluator.evaluates(metalang.accept_handler, env), metalang.issymbol(metalang.accept_handler) }
),
}, metalang.failure_handler, nil)
if not ok then
return ok, modval
end
print("module get", ok, modval, env, name)
if not modval.type == module_type then
return false, "first argument of module get must be a module"
end
return true, index_mod(modval.val)
end
local function mod_op_impl(syntax, env)
local childenv = env:child_scope()
local ok, val, childenv = syntax:match({
evaluator.block(metalang.accept_handler, childenv),
}, metalang.failure_handler, nil)
if not ok then
return ok, val
end
local mod = new_mod(childenv.locals)
env = env:exit_child_scope(childenv)
return true, mod, env
end
local function build_mod(tab)
local tmp = {}
for k, v in pairs(tab) do
tmp[k] = { kind = "reusable", val = v }
end
local val = { values = treemap.build(tmp) }
if val.values == nil then
error "tried to build a module that went to nil"
end
return { type = module_type, val = val }
end
local mod_mod = build_mod {
module = evaluator.primitive_operative(mod_op_impl),
get = evaluator.primitive_operative(get_op_impl),
["use-mod"] = evaluator.primitive_operative(use_mod_op_impl),
["open-mod"] = evaluator.primitive_operative(open_mod_op_impl),
}
return {
new_mod = new_mod,
index_mod = index_mod,
use_mod = use_mod,
open_mod = open_mod,
build_mod = build_mod,
module_mod = mod_mod.val,
module = mod_mod.val,
}