intuicio_frontend_assembler/
lib.rs

1pub mod parser;
2
3use intuicio_core::{IntuicioVersion, crate_version, script::BytesContentParser};
4use intuicio_frontend_serde::*;
5use std::error::Error;
6
7pub type AsmScript = SerdeScript;
8pub type AsmLiteral = SerdeLiteral;
9pub type AsmExpression = SerdeExpression;
10pub type AsmOperation = SerdeOperation;
11pub type AsmFunctionParameter = SerdeFunctionParameter;
12pub type AsmFunction = SerdeFunction;
13pub type AsmStructField = SerdeStructField;
14pub type AsmStruct = SerdeStruct;
15pub type AsmEnumVariant = SerdeEnumVariant;
16pub type AsmEnum = SerdeEnum;
17pub type AsmModule = SerdeModule;
18pub type AsmFile = SerdeFile;
19pub type AsmPackage = SerdePackage;
20pub type AsmNodeTypeInfo = SerdeNodeTypeInfo;
21pub type AsmNodes = SerdeNodes;
22pub type CompileAsmNodeGraphVisitor = CompileSerdeNodeGraphVisitor;
23
24pub fn frontend_assembly_version() -> IntuicioVersion {
25    crate_version!()
26}
27
28pub struct AsmContentParser;
29
30impl BytesContentParser<SerdeFile> for AsmContentParser {
31    fn parse(&self, bytes: Vec<u8>) -> Result<SerdeFile, Box<dyn Error>> {
32        let content = String::from_utf8(bytes)?;
33        Ok(parser::parse(&content)?)
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use crate::*;
40    use intuicio_backend_vm::prelude::*;
41    use intuicio_core::prelude::*;
42
43    #[test]
44    fn test_frontend_asm() {
45        let mut registry = Registry::default().with_basic_types();
46        registry.add_function(define_function! {
47            registry => mod intrinsics fn add(a: usize, b: usize) -> (result: usize) {
48                (a + b,)
49            }
50        });
51        let mut content_provider = FileContentProvider::new("iasm", AsmContentParser);
52        AsmPackage::new("../../resources/package.iasm", &mut content_provider)
53            .unwrap()
54            .compile()
55            .install::<VmScope<AsmExpression>>(
56                &mut registry,
57                None,
58                // Some(
59                //     PrintDebugger::full()
60                //         .basic_printables()
61                //         .stack_bytes(false)
62                //         .registers_bytes(false)
63                //         .into_handle(),
64                // ),
65            );
66        assert!(
67            registry
68                .find_function(FunctionQuery {
69                    name: Some("main".into()),
70                    module_name: Some("test".into()),
71                    ..Default::default()
72                })
73                .is_some()
74        );
75        let mut host = Host::new(Context::new(10240, 10240), RegistryHandle::new(registry));
76        let (result,) = host
77            .call_function::<(usize,), _>("main", "test", None)
78            .unwrap()
79            .run(());
80        assert_eq!(result, 42);
81    }
82}