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::scope::VmScope;
41    use intuicio_core::{
42        context::Context,
43        define_function,
44        function::FunctionQuery,
45        host::Host,
46        registry::{Registry, RegistryHandle},
47        script::FileContentProvider,
48    };
49
50    #[test]
51    fn test_frontend_asm() {
52        let mut registry = Registry::default().with_basic_types();
53        registry.add_function(define_function! {
54            registry => mod intrinsics fn add(a: usize, b: usize) -> (result: usize) {
55                (a + b,)
56            }
57        });
58        let mut content_provider = FileContentProvider::new("iasm", AsmContentParser);
59        AsmPackage::new("../../resources/package.iasm", &mut content_provider)
60            .unwrap()
61            .compile()
62            .install::<VmScope<AsmExpression>>(
63                &mut registry,
64                None,
65                // Some(
66                //     PrintDebugger::full()
67                //         .basic_printables()
68                //         .stack_bytes(false)
69                //         .registers_bytes(false)
70                //         .into_handle(),
71                // ),
72            );
73        assert!(
74            registry
75                .find_function(FunctionQuery {
76                    name: Some("main".into()),
77                    module_name: Some("test".into()),
78                    ..Default::default()
79                })
80                .is_some()
81        );
82        let mut host = Host::new(Context::new(10240, 10240), RegistryHandle::new(registry));
83        let (result,) = host
84            .call_function::<(usize,), _>("main", "test", None)
85            .unwrap()
86            .run(());
87        assert_eq!(result, 42);
88    }
89}