intuicio_frontend_assembler/
lib.rs1pub mod parser;
2
3use intuicio_core::{crate_version, script::BytesContentParser, IntuicioVersion};
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 );
66 assert!(registry
67 .find_function(FunctionQuery {
68 name: Some("main".into()),
69 module_name: Some("test".into()),
70 ..Default::default()
71 })
72 .is_some());
73 let mut host = Host::new(Context::new(10240, 10240), RegistryHandle::new(registry));
74 let (result,) = host
75 .call_function::<(usize,), _>("main", "test", None)
76 .unwrap()
77 .run(());
78 assert_eq!(result, 42);
79 }
80}