-
Notifications
You must be signed in to change notification settings - Fork 0
/
l4u_type.gleam
223 lines (185 loc) · 4.49 KB
/
l4u_type.gleam
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
////let assert [Scope(tvars), ..] = closure_env.scopes
// latest
import gleam/list
import gleam/dict.{type Dict}
import gleam/string
import gleam/regex
import gleam/result
import gleam/int
import gleam/float
import gleam/dynamic.{type Dynamic}
import gleam/option
import gleam/order
@external(erlang, "l4u_ffi", "unsafe_corece")
@external(javascript, "../l4u_sys_bridge_ffi.mjs", "unsafe_corece")
pub fn unsafe_corece(value: a) -> b
@external(erlang, "l4u@sys_bridge_ffi", "to_native_dictionary")
@external(javascript, "../l4u_sys_bridge_ffi.mjs", "to_native_dictionary")
pub fn to_native_dictionary(xs: List(x)) -> any
@target(erlang)
pub fn native_undefined() -> any {
Nil
|> unsafe_corece
}
@target(erlang)
pub fn native_nil() -> any {
Nil
|> unsafe_corece
}
@target(erlang)
pub fn native_true() -> any {
True
|> unsafe_corece
}
@target(erlang)
pub fn native_false() -> any {
False
|> unsafe_corece
}
@target(javascript)
@external(javascript, "../l4u_sys_bridge_ffi.mjs", "native_undefined")
pub fn native_undefined() -> any
@target(javascript)
@external(javascript, "../l4u_sys_bridge_ffi.mjs", "native_nil")
pub fn native_nil() -> any
@target(javascript)
@external(javascript, "../l4u_sys_bridge_ffi.mjs", "native_true")
pub fn native_true() -> any
@target(javascript)
@external(javascript, "../l4u_sys_bridge_ffi.mjs", "native_false")
pub fn native_false() -> any
// ----------------------------------------------------------------------------
// # types
// ----------------------------------------------------------------------------
pub type PDic
pub type Ref
pub type ErrorEx {
ErrorEx2(msg: String, detail: Dynamic)
}
pub type Info {
Info(name: String, level: Int)
}
pub type Description {
Description(name: String, text: String)
}
pub type Scope {
Scope(bindings: Dict(String, Expr))
ProcessDict(PDic)
}
pub type EnvOptions {
EnvOptions(verbose: Bool, trace: Bool)
}
pub type EnvOption {
VERBOSE
TRACE
}
pub type Env {
Env(
info: Info,
pdic: PDic,
local_vars: Dict(String, Expr),
a_binding: Dict(String, Expr),
scopes: List(Scope),
opts: EnvOptions,
)
}
pub type Expr {
PRINTABLE(Expr)
UNDEFINED
NIL
TRUE
FALSE
DELIMITER
WithEnv(Expr, Env)
LocalContinuation(List(Expr), Env, Env)
Continuation(List(Expr), Env)
ATOM(Ref)
INT(Int)
FLOAT(Float)
STRING(String)
SYMBOL(String)
ExVAR(String, String)
KEYWORD(String)
LIST(List(Expr))
VECTOR(List(Expr))
DICT(List(Expr))
BIF(Description, fn(List(Expr), Env) -> Expr)
BISPFORM(Description, fn(List(Expr), Env) -> Expr)
CLOSURE(Description, List(String), List(Expr), Env)
MACRO(Description, List(String), List(Expr), Env)
WithMeta(Expr, Expr)
NativeValue(String, Dynamic)
NativeFunction(String, Dynamic)
ReplCommand(String, Expr)
CustomType(String, Expr)
CustomTypeDef(String, Expr)
}
pub fn unbox_l4u(expr: Expr) -> any {
case expr {
PRINTABLE(x) -> unbox_l4u(x)
UNDEFINED -> native_undefined()
NIL -> native_nil()
TRUE -> native_true()
FALSE -> native_false()
INT(x) -> unsafe_corece(x)
FLOAT(x) -> unsafe_corece(x)
STRING(x) -> unsafe_corece(x)
SYMBOL(x) -> unsafe_corece(x)
KEYWORD(x) -> unsafe_corece(x)
LIST(xs) ->
list.map(xs, unbox_l4u)
|> unsafe_corece
VECTOR(xs) ->
list.map(xs, unbox_l4u)
|> unsafe_corece
DICT(xs) ->
to_native_dictionary(list.map(xs, unbox_l4u))
|> unsafe_corece
NativeValue(_, x) -> unsafe_corece(x)
NativeFunction(_, x) -> unsafe_corece(x)
CustomType(_, x) -> unsafe_corece(x)
//_ ->
// "unsupported conversion."
// |> unsafe_corece
else_val -> unsafe_corece(else_val)
}
}
pub fn box_l4u_int(x: Int) -> Expr {
INT(x)
}
pub fn box_l4u_float(x: Float) -> Expr {
FLOAT(x)
}
pub fn box_l4u_string(x: String) -> Expr {
STRING(x)
}
pub fn box_l4u_symbol(x: String) -> Expr {
SYMBOL(x)
}
pub fn box_l4u_keyword(x: String) -> Expr {
KEYWORD(x)
}
pub fn box_l4u_list(x: List(Expr)) -> Expr {
LIST(x)
}
pub fn box_l4u_vector(x: List(Expr)) -> Expr {
VECTOR(x)
}
pub fn box_l4u_dict(x: List(Expr)) -> Expr {
DICT(x)
}
pub fn box_l4u_native_value(name: String, x: Dynamic) -> Expr {
NativeValue(name, x)
}
pub fn box_l4u_native_function(name: String, x: Dynamic) -> Expr {
NativeFunction(name, x)
}
pub fn box_l4u_custom_type(name: String, x: Expr) -> Expr {
CustomType(name, x)
}
pub fn box_l4u_custom_type_def(name: String, x: Expr) -> Expr {
CustomTypeDef(name, x)
}
pub fn box_l4u_ok(x: any) -> Result(any, a) {
Ok(x)
}