alicorn 0.1.0

Rust embedding of the Alicorn compiler
Documentation
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2025 Fundament Software SPC <https://fundament.software>

use mlua::{
    prelude::*,
    Either::{self, Left, Right},
};

const ALICORN: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/alicorn.lua"));
const PRELUDE: &[u8] = include_bytes!("prelude.alc");
const GLSL_PRELUDE: &[u8] = include_bytes!("glsl-prelude.alc");

extern "C-unwind" {
    fn luaopen_lpeg(L: *mut mlua::lua_State) -> std::ffi::c_int;
    fn luaopen_lfs(L: *mut mlua::lua_State) -> std::ffi::c_int;
}

pub struct Alicorn {
    lua: Lua,
    runner: AlicornRunner,
}

pub struct AlicornRunner {
    runner: LuaTable,
}

#[repr(transparent)]
struct LuaResult<T, E>(pub Result<T, E>);
impl<T, E> From<LuaResult<T, E>> for Result<T, E> {
    #[inline(always)]
    fn from(value: LuaResult<T, E>) -> Self {
        value.0
    }
}
impl<T: FromLuaMulti, E: FromLuaMulti> FromLuaMulti for LuaResult<T, E> {
    fn from_lua_multi(mut values: LuaMultiValue, lua: &Lua) -> mlua::Result<Self> {
        let ok = values
            .pop_front()
            .map(|value| bool::from_lua(value, lua))
            .unwrap_or(Ok(false))?;
        if ok {
            T::from_lua_multi(values, lua).map(|value| LuaResult(Ok(value)))
        } else {
            E::from_lua_multi(values, lua).map(|value| LuaResult(Err(value)))
        }
    }
}

macro_rules! trivial_lua_value {
    ($t:path) => {
        impl FromLua for $t {
            fn from_lua(value: LuaValue, lua: &Lua) -> mlua::Result<Self> {
                LuaValue::from_lua(value, lua).map(Self)
            }
        }
        impl IntoLua for $t {
            fn into_lua(self, lua: &Lua) -> mlua::Result<LuaValue> {
                self.0.into_lua(lua)
            }
        }
    };
}

#[repr(transparent)]
pub struct ConstructedSyntax(pub mlua::Value);
trivial_lua_value!(ConstructedSyntax);

#[repr(transparent)]
pub struct FlexValue(pub mlua::Value);
trivial_lua_value!(FlexValue);

#[repr(transparent)]
pub struct TypedTerm(pub mlua::Value);
trivial_lua_value!(TypedTerm);

#[repr(transparent)]
pub struct AnchoredInferrableTerm(pub mlua::Value);
trivial_lua_value!(AnchoredInferrableTerm);

/// `terms.purity`
///
/// TODO: proper Lua value conversion
#[repr(transparent)]
pub struct Purity(pub mlua::Value);
trivial_lua_value!(Purity);

/// `terms.block_purity`
pub enum BlockPurity {
    /// `terms.block_purity.effectful`
    Effectful,
    /// `terms.block_purity.pure`
    Pure,
    /// `terms.block_purity.dependent`
    Dependent {
        /// `flex_value`
        val: FlexValue,
    },
    /// `terms.block_purity.inherit`
    Inherit,
}
impl IntoLua for BlockPurity {
    fn into_lua(self, lua: &Lua) -> mlua::Result<LuaValue> {
        let block_purity = lua
            .load(r#"require("terms").block_purity"#)
            .eval::<LuaTable>()?;
        match self {
            Self::Effectful => block_purity.get("effectful"),
            Self::Pure => block_purity.get("pure"),
            Self::Dependent { val } => block_purity.get::<LuaFunction>("dependent")?.call(val),
            Self::Inherit => block_purity.get("inherit"),
        }
    }
}
impl FromLua for BlockPurity {
    fn from_lua(value: LuaValue, lua: &Lua) -> mlua::Result<Self> {
        let block_purity = lua
            .load(r#"require("terms").block_purity"#)
            .eval::<LuaTable>()?;
        if block_purity
            .get::<LuaFunction>("value_check")?
            .call(&value)?
        {
            let value = LuaTable::from_lua(value, lua)?;
            if let Ok(()) = value
                .get::<LuaFunction>("as_effectful")?
                .call::<LuaResult<_, ()>>(&value)?
                .into()
            {
                Ok(Self::Effectful)
            } else if let Ok(()) = value
                .get::<LuaFunction>("as_pure")?
                .call::<LuaResult<_, ()>>(&value)?
                .into()
            {
                Ok(Self::Pure)
            } else if let Ok(val) = value
                .get::<LuaFunction>("as_dependent")?
                .call::<LuaResult<_, ()>>(&value)?
                .into()
            {
                Ok(Self::Dependent { val })
            } else if let Ok(()) = value
                .get::<LuaFunction>("as_inherit")?
                .call::<LuaResult<_, ()>>(value)?
                .into()
            {
                Ok(Self::Inherit)
            } else {
                unreachable!()
            }
        } else {
            Err(mlua::Error::FromLuaConversionError {
                from: value.type_name(),
                to: "terms.block_purity".to_owned(),
                message: Some("expected terms.block_purity".to_owned()),
            })
        }
    }
}

impl AlicornRunner {
    pub fn enter_block(&self, purity: BlockPurity) -> Result<(), mlua::Error> {
        self.runner
            .get::<LuaFunction>("enter_block")?
            .call((&self.runner, purity))
    }
    pub fn exit_block(
        &self,
        expr: AnchoredInferrableTerm,
    ) -> Result<(AnchoredInferrableTerm, Purity), mlua::Error> {
        self.runner
            .get::<LuaFunction>("exit_block")?
            .call((&self.runner, expr))
    }
    pub fn read_format(
        &self,
        format_text: mlua::String,
        id: mlua::String,
    ) -> Result<ConstructedSyntax, mlua::Error> {
        self.runner
            .get::<LuaFunction>("read_format")?
            .call((&self.runner, format_text, id))
    }
    pub fn read_file(
        &self,
        format_file: Either<mlua::String, mlua::Value>,
        id: Option<mlua::String>,
    ) -> Result<ConstructedSyntax, mlua::Error> {
        self.runner
            .get::<LuaFunction>("read_file")?
            .call((&self.runner, format_file, id))
    }
    pub fn try_parse_syntax(
        &self,
        syntax: mlua::Table,
        id: mlua::String,
    ) -> Result<Result<AnchoredInferrableTerm, mlua::String>, mlua::Error> {
        Ok(self
            .runner
            .get::<LuaFunction>("try_parse_syntax")?
            .call::<LuaResult<_, _>>((&self.runner, syntax, id))?
            .into())
    }
    pub fn parse_syntax(
        &self,
        syntax: mlua::Table,
        id: mlua::String,
    ) -> Result<AnchoredInferrableTerm, mlua::Error> {
        self.runner
            .get::<LuaFunction>("parse_syntax")?
            .call((&self.runner, syntax, id))
    }
    pub fn try_parse_format(
        &self,
        format_text: mlua::String,
        id: mlua::String,
    ) -> Result<Result<AnchoredInferrableTerm, mlua::String>, mlua::Error> {
        Ok(self
            .runner
            .get::<LuaFunction>("try_parse_format")?
            .call::<LuaResult<_, _>>((&self.runner, format_text, id))?
            .into())
    }
    pub fn parse_format(
        &self,
        format_text: mlua::String,
        id: mlua::String,
    ) -> Result<AnchoredInferrableTerm, mlua::Error> {
        self.runner
            .get::<LuaFunction>("parse_format")?
            .call((&self.runner, format_text, id))
    }
    pub fn try_parse_file(
        &self,
        format_file: Either<mlua::String, (mlua::Value, mlua::String)>,
    ) -> Result<Result<AnchoredInferrableTerm, mlua::String>, mlua::Error> {
        Ok(self
            .runner
            .get::<LuaFunction>("try_parse_file")?
            .call::<LuaResult<_, _>>(match format_file {
                Left(path) => (&self.runner, Left(path), None),
                Right((file, id)) => (&self.runner, Right(file), Some(id)),
            })?
            .into())
    }
    pub fn parse_file(
        &self,
        format_file: Either<mlua::String, (mlua::Value, mlua::String)>,
    ) -> Result<AnchoredInferrableTerm, mlua::Error> {
        self.runner
            .get::<LuaFunction>("parse_file")?
            .call(match format_file {
                Left(path) => (&self.runner, Left(path), None),
                Right((file, id)) => (&self.runner, Right(file), Some(id)),
            })
    }
    pub fn try_infer_expr(
        &self,
        expr: AnchoredInferrableTerm,
    ) -> Result<Result<(FlexValue, mlua::Value, TypedTerm), mlua::String>, mlua::Error> {
        Ok(self
            .runner
            .get::<LuaFunction>("try_infer_expr")?
            .call::<LuaResult<_, _>>((&self.runner, expr))?
            .into())
    }
    pub fn infer_expr(
        &self,
        expr: AnchoredInferrableTerm,
    ) -> Result<(FlexValue, mlua::Value, TypedTerm), mlua::Error> {
        self.runner
            .get::<LuaFunction>("parse_file")?
            .call((&self.runner, expr))
    }
    pub fn try_typecheck_program_type(
        &self,
        r#type: FlexValue,
    ) -> Result<Result<(), mlua::String>, mlua::Error> {
        Ok(self
            .runner
            .get::<LuaFunction>("try_typecheck_program_type")?
            .call::<LuaResult<_, _>>((&self.runner, r#type))?
            .into())
    }
    pub fn typecheck_program_type(&self, r#type: FlexValue) -> Result<(), mlua::Error> {
        self.runner
            .get::<LuaFunction>("typecheck_program_type")?
            .call((&self.runner, r#type))
    }
    pub fn try_evaluate_term(
        &self,
        r#type: FlexValue,
    ) -> Result<Result<(), mlua::String>, mlua::Error> {
        Ok(self
            .runner
            .get::<LuaFunction>("try_evaluate_term")?
            .call::<LuaResult<_, _>>((&self.runner, r#type))?
            .into())
    }
    pub fn evaluate_term(&self, r#type: FlexValue) -> Result<(), mlua::Error> {
        self.runner
            .get::<LuaFunction>("evaluate_term")?
            .call((&self.runner, r#type))
    }
    pub fn try_evaluate_program_expr(
        &self,
        program_expr: AnchoredInferrableTerm,
    ) -> Result<Result<FlexValue, mlua::String>, mlua::Error> {
        Ok(self
            .runner
            .get::<LuaFunction>("try_evaluate_program_expr")?
            .call::<LuaResult<_, _>>((&self.runner, program_expr))?
            .into())
    }
    pub fn evaluate_program_expr(
        &self,
        program_expr: AnchoredInferrableTerm,
    ) -> Result<FlexValue, mlua::Error> {
        self.runner
            .get::<LuaFunction>("evaluate_program_expr")?
            .call((&self.runner, program_expr))
    }
    pub fn evaluate_program_format(
        &self,
        program_format_text: mlua::String,
        program_id: mlua::String,
    ) -> Result<FlexValue, mlua::Error> {
        self.runner
            .get::<LuaFunction>("evaluate_program_format")?
            .call((&self.runner, program_format_text, program_id))
    }
    pub fn evaluate_program_file(
        &self,
        program_format_file: Either<mlua::String, (mlua::Value, mlua::String)>,
    ) -> Result<FlexValue, mlua::Error> {
        self.runner
            .get::<LuaFunction>("evaluate_program_file")?
            .call(match program_format_file {
                Left(path) => (&self.runner, Left(path), None),
                Right((file, id)) => (&self.runner, Right(file), Some(id)),
            })
    }
    pub fn execute_program_value(
        &self,
        program_value: FlexValue,
    ) -> Result<LuaMultiValue, mlua::Error> {
        self.runner
            .get::<LuaFunction>("execute_program_value")?
            .call((&self.runner, program_value))
    }
    pub fn execute_program_format(
        &self,
        program_format_text: mlua::String,
        program_id: mlua::String,
    ) -> Result<LuaMultiValue, mlua::Error> {
        self.runner
            .get::<LuaFunction>("execute_program_format")?
            .call((&self.runner, program_format_text, program_id))
    }
    pub fn execute_program_file(
        &self,
        program_format_file: Either<mlua::String, (mlua::Value, mlua::String)>,
    ) -> Result<LuaMultiValue, mlua::Error> {
        self.runner
            .get::<LuaFunction>("execute_program_file")?
            .call(match program_format_file {
                Left(path) => (&self.runner, Left(path), None),
                Right((file, id)) => (&self.runner, Right(file), Some(id)),
            })
    }
}

impl Alicorn {
    pub fn new(lua: Option<Lua>) -> Result<Self, mlua::Error> {
        let lua = lua.unwrap_or_else(|| Lua::new());

        // Load C libraries we already linked into our rust binary using our build script. This works because we can
        // declare the C functions directly and have the linker resolve them during the link step.
        unsafe {
            let _: mlua::Value =
                lua.load_from_function("lpeg", lua.create_c_function(luaopen_lpeg)?)?;
            let _: mlua::Value =
                lua.load_from_function("lfs", lua.create_c_function(luaopen_lfs)?)?;
        }

        // Here, we load all the embedded alicorn source into the lua engine and execute it.
        lua.load(ALICORN).exec()?;

        // Then we create helper functions for compiling an alicorn source file that we can bind to mlua.
        let runner: LuaTable = lua
            .load(r#"alicorn_runner = require("alicorn-runner").Runner(); return alicorn_runner"#)
            .eval()?;

        let alicorn = Self {
            lua,
            runner: AlicornRunner { runner },
        };
        alicorn.runner.enter_block(BlockPurity::Effectful)?;

        _ = alicorn.include(PRELUDE, "prelude.alc")?;

        Ok(alicorn)
    }

    pub fn load_glsl_prelude(&self) -> Result<(), mlua::Error> {
        _ = self.include(GLSL_PRELUDE, "glsl-prelude.alc")?;

        Ok(())
    }

    pub fn include(
        &self,
        source: impl AsRef<[u8]>,
        name: impl AsRef<str>,
    ) -> Result<AnchoredInferrableTerm, mlua::Error> {
        self.runner.parse_format(
            self.lua.create_string(source.as_ref())?,
            self.lua.create_string(name.as_ref())?,
        )
    }

    pub fn include_file(
        &self,
        path: impl AsRef<std::path::Path>,
    ) -> Result<AnchoredInferrableTerm, mlua::Error> {
        self.runner
            .parse_file(Either::Left(self.lua.create_string(
                path.as_ref().as_os_str().to_string_lossy().as_bytes(),
            )?))
    }

    pub fn execute<R: FromLuaMulti>(
        &self,
        source: impl AsRef<[u8]>,
        name: impl AsRef<str>,
    ) -> Result<R, mlua::Error> {
        R::from_lua_multi(
            self.runner.execute_program_format(
                self.lua.create_string(source.as_ref())?,
                self.lua.create_string(name.as_ref())?,
            )?,
            &self.lua,
        )
    }

    pub fn execute_file<R: FromLuaMulti>(
        &self,
        path: impl AsRef<std::path::Path>,
    ) -> Result<R, mlua::Error> {
        R::from_lua_multi(
            self.runner.execute_program_file(Either::Left(
                self.lua
                    .create_string(path.as_ref().as_os_str().to_string_lossy().as_bytes())?,
            ))?,
            &self.lua,
        )
    }
}

#[test]
fn test_runtest_file() {
    // set the working directory to some random temporary folder to sabotage loading any of the files from this crate, as a crate including this one wouldn't have access to those files
    let old = std::env::current_dir().unwrap();
    let temp_dir = std::env::temp_dir();
    let root = std::path::Path::new(&temp_dir);
    std::env::set_current_dir(&root).unwrap();

    let alicorn = Alicorn::new(None).unwrap();

    // Restore working dir so we can find prelude.alc
    std::env::set_current_dir(&old).unwrap();
    let result: LuaValue = alicorn
        .execute(
            "host-tuple-of(tuple-desc-singleton(host-type, host-number))(4)",
            format!("{}:{}", file!(), line!() - 1),
        )
        .unwrap();
    assert_eq!(result, LuaValue::Integer(4));
}