diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 3d2ceeb..1b908e4 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -5,8 +5,8 @@ use std::path::Path; use std::thread::sleep; use std::time::{Duration, Instant}; -use clap::{Parser, Subcommand, ValueEnum}; use clap::builder::PossibleValue; +use clap::{Parser, Subcommand, ValueEnum}; use console::style; use notify::event::ModifyKind; use notify::EventKind::Modify; @@ -17,8 +17,8 @@ pub(crate) use error::Error; use crate::compiler::{FileCompilerSource, HexoCompiler, HexoCompilerContext}; mod error; +use crate::util::logger::LogLevel; use crate::util::{defer, logger}; -use crate::util::logger::{LogLevel}; #[derive(Subcommand)] enum Commands { @@ -61,9 +61,7 @@ pub(crate) struct CliCompilerArguments { impl CliCompilerArguments { pub(crate) fn new(safe_mode: bool) -> CliCompilerArguments { - CliCompilerArguments { - safe_mode - } + CliCompilerArguments { safe_mode } } } @@ -80,8 +78,12 @@ impl Cli { let compiler_arguments = cli.cli_compiler_arguments(); let cli_result: Result<_, Error> = match cli.command { None => Err(Error::UnknownCommand), - Some(Commands::Watch { source, output }) => Self::watch(source, output, compiler_arguments), - Some(Commands::Build { source, output }) => Self::build(source, output, compiler_arguments), + Some(Commands::Watch { source, output }) => { + Self::watch(source, output, compiler_arguments) + } + Some(Commands::Build { source, output }) => { + Self::build(source, output, compiler_arguments) + } }; Self::handle_cli_error_if_required(cli_result, build_started); @@ -92,7 +94,8 @@ impl Cli { } fn log_debug_interface_arguments(&self) { - logger::debug!("initialized cli interface with arguments:\ + logger::debug!( + "initialized cli interface with arguments:\ \n --log-level = {}\ \n --safe = {}", &self.log_level, @@ -100,10 +103,7 @@ impl Cli { ); } - fn handle_cli_error_if_required( - cli_result: Result<(), Error>, - build_started: Instant, - ) { + fn handle_cli_error_if_required(cli_result: Result<(), Error>, build_started: Instant) { if let Err(e) = cli_result { Self::print_error(e.into()); } else { @@ -121,14 +121,18 @@ impl Cli { logger::error!("{error}"); } - fn watch(source: String, output: Option, compiler_arguments: CliCompilerArguments) -> Result<(), Error> { + fn watch( + source: String, + output: Option, + compiler_arguments: CliCompilerArguments, + ) -> Result<(), Error> { let source_path_clone = source.clone(); let source_path = source_path_clone.as_ref(); let mut watcher = notify::recommended_watcher(move |event: Result| { Self::watch_loop(source.clone(), output.clone(), compiler_arguments, event) }) - .map_err(Error::FileWatcher)?; + .map_err(Error::FileWatcher)?; watcher .watch(source_path, RecursiveMode::NonRecursive) @@ -151,7 +155,9 @@ impl Cli { Ok(e) => { if let Modify(ModifyKind::Data(_)) = e.kind { logger::debug!("rebuilding..."); - let _ = catch_unwind(|| Self::build(source.clone(), output.clone(), compiler_arguments)); + let _ = catch_unwind(|| { + Self::build(source.clone(), output.clone(), compiler_arguments) + }); logger::debug!(" done!"); } } @@ -164,14 +170,12 @@ impl Cli { pub(crate) fn build( source: String, output: Option, - compiler_arguments: CliCompilerArguments + compiler_arguments: CliCompilerArguments, ) -> Result<(), Error> { defer!(logger::debug!("BUILDING, done")); logger::debug!("BUILDING, source: {}, output: {:?}", source, output); - let context = HexoCompilerContext::new( - compiler_arguments.safe_mode - ); + let context = HexoCompilerContext::new(compiler_arguments.safe_mode); let compiler = HexoCompiler::new(context); let source_path = Path::new(&source); @@ -197,15 +201,13 @@ impl ValueEnum for LogLevel { fn to_possible_value(&self) -> Option { let name = match self { - LogLevel::Debug => { "debug" } - LogLevel::Info => { "info" } - LogLevel::Warn => { "warn" } - LogLevel::Error => { "error" } - LogLevel::None => { "none" } + LogLevel::Debug => "debug", + LogLevel::Info => "info", + LogLevel::Warn => "warn", + LogLevel::Error => "error", + LogLevel::None => "none", }; - Some( - PossibleValue::new(name) - ) + Some(PossibleValue::new(name)) } -} \ No newline at end of file +} diff --git a/src/compiler/ast/error.rs b/src/compiler/ast/error.rs index a2bb707..53e43a0 100644 --- a/src/compiler/ast/error.rs +++ b/src/compiler/ast/error.rs @@ -1,5 +1,5 @@ -use std::fmt::Display; use crate::compiler::ast::parser::Rule; +use std::fmt::Display; #[derive(Debug)] pub(crate) enum Error { diff --git a/src/compiler/ast/mod.rs b/src/compiler/ast/mod.rs index 1fed43d..5f6eaa3 100644 --- a/src/compiler/ast/mod.rs +++ b/src/compiler/ast/mod.rs @@ -4,4 +4,4 @@ mod parser; pub(crate) use error::Error; pub(crate) use node::{AstNode, AstNodeType}; -pub(crate) use parser::{AstParser}; \ No newline at end of file +pub(crate) use parser::AstParser; diff --git a/src/compiler/ast/node.rs b/src/compiler/ast/node.rs index 563fc9a..972731c 100644 --- a/src/compiler/ast/node.rs +++ b/src/compiler/ast/node.rs @@ -27,7 +27,6 @@ pub(crate) enum AstNodeType { } impl AstNodeType { - pub(crate) fn must_capture_value(&self) -> bool { matches!( self, @@ -52,7 +51,6 @@ pub(crate) struct AstNode { } impl AstNode { - pub(crate) fn new( node_type: AstNodeType, content: Option, diff --git a/src/compiler/ast/parser.rs b/src/compiler/ast/parser.rs index 2903cdd..18fabfa 100644 --- a/src/compiler/ast/parser.rs +++ b/src/compiler/ast/parser.rs @@ -13,10 +13,9 @@ struct AstPestParser; pub(crate) struct AstParser {} impl AstParser { - pub(crate) fn parse(&self, source: &str) -> Result { - let pairs = AstPestParser::parse(Rule::file, source) - .map_err(|e| Error::Pest(Box::new(e)))?; + let pairs = + AstPestParser::parse(Rule::file, source).map_err(|e| Error::Pest(Box::new(e)))?; let children: Result, _> = pairs .map(parse_ast_pair) diff --git a/src/compiler/compiler_context.rs b/src/compiler/compiler_context.rs index d328e05..79d598f 100644 --- a/src/compiler/compiler_context.rs +++ b/src/compiler/compiler_context.rs @@ -1,12 +1,10 @@ pub(crate) struct HexoCompilerContext { - safe_mode: bool + safe_mode: bool, } impl HexoCompilerContext { pub(crate) fn new(safe_mode: bool) -> Self { - HexoCompilerContext { - safe_mode - } + HexoCompilerContext { safe_mode } } pub(crate) fn safe_mode(&self) -> bool { diff --git a/src/compiler/compiler_source.rs b/src/compiler/compiler_source.rs index af4eb5d..49ea9fe 100644 --- a/src/compiler/compiler_source.rs +++ b/src/compiler/compiler_source.rs @@ -1,8 +1,8 @@ +use crate::util::id::HexoId; +use crate::util::logger; use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; -use crate::util::id::HexoId; -use crate::util::logger; pub(crate) trait CompilerSource { fn read(&self) -> Result; @@ -16,7 +16,9 @@ pub(crate) struct FileCompilerSource { impl FileCompilerSource { pub(crate) fn new(path: &Path) -> FileCompilerSource { - FileCompilerSource { path: path.to_path_buf() } + FileCompilerSource { + path: path.to_path_buf(), + } } } @@ -54,20 +56,17 @@ impl LiteralCompilerSource { pub(crate) fn anonymous(content: String) -> LiteralCompilerSource { LiteralCompilerSource { content: content, - path: Path::new( - format!("hexo://anonymous/{}", HexoId::next()).as_str() - ).to_path_buf(), + path: Path::new(format!("hexo://anonymous/{}", HexoId::next()).as_str()).to_path_buf(), } } } - #[cfg(test)] pub(crate) mod tests { + use crate::compiler::CompilerSource; use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; - use crate::compiler::CompilerSource; pub(crate) struct EagerCompilerSource { content: String, @@ -91,9 +90,10 @@ pub(crate) mod tests { let mut content = String::new(); source_file.read_to_string(&mut content)?; - Ok( - EagerCompilerSource { content: content, path: pat_ref.to_path_buf() } - ) + Ok(EagerCompilerSource { + content: content, + path: pat_ref.to_path_buf(), + }) } } -} \ No newline at end of file +} diff --git a/src/compiler/cst/macros.rs b/src/compiler/cst/macros.rs index c9fe7ce..5e8e875 100644 --- a/src/compiler/cst/macros.rs +++ b/src/compiler/cst/macros.rs @@ -1,4 +1,5 @@ -#[macro_export] macro_rules! match_ast { +#[macro_export] +macro_rules! match_ast { ($node:expr => $node_expected:ident, $($option:ident => $holder:ident | $transform:expr)+) => { guard_node_type($node, AstNodeType::$node_expected)?; diff --git a/src/compiler/cst/mod.rs b/src/compiler/cst/mod.rs index 336d41c..f4a86c9 100644 --- a/src/compiler/cst/mod.rs +++ b/src/compiler/cst/mod.rs @@ -2,8 +2,7 @@ pub(crate) use error::*; pub(crate) use node::*; pub(crate) use parser::*; -mod node; mod error; -mod parser; mod macros; - +mod node; +mod parser; diff --git a/src/compiler/cst/node.rs b/src/compiler/cst/node.rs index 4f1bb94..3de1252 100644 --- a/src/compiler/cst/node.rs +++ b/src/compiler/cst/node.rs @@ -8,7 +8,10 @@ pub(crate) struct CstFile { impl CstFile { pub(super) fn new(path: &Path, main: CstFunctionStatement) -> Self { - CstFile { path: path.to_path_buf(), main: main } + CstFile { + path: path.to_path_buf(), + main: main, + } } pub(crate) fn path(&self) -> &Path { @@ -62,7 +65,6 @@ pub(crate) struct CstEmitStatement { } impl CstEmitStatement { - pub(crate) fn new(atoms: CstAtomVec) -> Self { CstEmitStatement { atoms } } @@ -79,7 +81,6 @@ pub(crate) struct CstConstantStatement { } impl CstConstantStatement { - pub(crate) fn new(name: String, atoms: CstAtomVec) -> Self { CstConstantStatement { name, atoms } } @@ -102,7 +103,6 @@ pub(crate) struct CstFunctionStatement { } impl CstFunctionStatement { - pub(crate) fn new( name: String, emits: Vec, @@ -132,4 +132,4 @@ impl CstFunctionStatement { pub(crate) fn functions(&self) -> &Vec { &self.functions } -} \ No newline at end of file +} diff --git a/src/compiler/cst/parser.rs b/src/compiler/cst/parser.rs index 173c4ae..c72146c 100644 --- a/src/compiler/cst/parser.rs +++ b/src/compiler/cst/parser.rs @@ -1,8 +1,8 @@ use std::path::Path; use crate::compiler::ast::{AstNode, AstNodeType}; -use crate::compiler::cst::Error; use crate::compiler::cst::CstFile; +use crate::compiler::cst::Error; use crate::compiler::cst::{ CstActualParameter, CstAtom, CstConstantStatement, CstEmitStatement, CstFunctionStatement, }; @@ -25,17 +25,10 @@ fn parse_file(path: &Path, node: &AstNode) -> Result { guard_node_type(node, AstNodeType::File)?; let (emits, functions, constants) = parse_function_body(node)?; - Ok( - CstFile::new( - path, - CstFunctionStatement::new( - MAIN_FUNCTION_NAME.to_string(), - emits, - functions, - constants, - ), - ) - ) + Ok(CstFile::new( + path, + CstFunctionStatement::new(MAIN_FUNCTION_NAME.to_string(), emits, functions, constants), + )) } type BodyParsingResult = ( @@ -84,16 +77,13 @@ fn parse_constant(node: &AstNode) -> Result { } } - Ok( - CstConstantStatement::new( - name - .ok_or(Error::MissingContent { - node_type: AstNodeType::StatementConstName, - })? - .to_string(), - atom_buff, - ) - ) + Ok(CstConstantStatement::new( + name.ok_or(Error::MissingContent { + node_type: AstNodeType::StatementConstName, + })? + .to_string(), + atom_buff, + )) } fn parse_function(node: &AstNode) -> Result { @@ -122,16 +112,14 @@ fn parse_function(node: &AstNode) -> Result { } } - Ok( - CstFunctionStatement::new( - name.ok_or(Error::MissingContent { - node_type: AstNodeType::StatementFnName, - })?, - emits.unwrap_or(Vec::new()), - functions.unwrap_or(Vec::new()), - constants.unwrap_or(Vec::new()), - ) - ) + Ok(CstFunctionStatement::new( + name.ok_or(Error::MissingContent { + node_type: AstNodeType::StatementFnName, + })?, + emits.unwrap_or(Vec::new()), + functions.unwrap_or(Vec::new()), + constants.unwrap_or(Vec::new()), + )) } fn parse_emit_statement(node: &AstNode) -> Result { @@ -267,19 +255,17 @@ fn parse_atom_hex_into(node: &AstNode, buf: &mut Vec) -> Result<(), Err } pub(crate) fn decode_bytes_from_string(s: &str) -> Result, Error> { - (0..s.len()).step_by(2) + (0..s.len()) + .step_by(2) .map(|i| { if s.len() < 2 { - return Err( - Error::MalformedNodeValue { - message: format!("can't parse bytes {}", s), - } - ); - } - u8::from_str_radix(&s[i..i + 2], 16) - .map_err(|_| Error::MalformedNodeValue { + return Err(Error::MalformedNodeValue { message: format!("can't parse bytes {}", s), - }) + }); + } + u8::from_str_radix(&s[i..i + 2], 16).map_err(|_| Error::MalformedNodeValue { + message: format!("can't parse bytes {}", s), + }) }) .collect() } @@ -294,10 +280,9 @@ fn parse_atom_utf8_into(node: &AstNode, buf: &mut Vec) -> Result<(), Er fn parse_atom_base_num_into(node: &AstNode, buf: &mut Vec) -> Result<(), Error> { fn parse_number_base(base: String) -> Result { - let base_value = base.parse() - .map_err(|_| Error::MalformedNodeValue { - message: format!("can't parse base {}", base), - })?; + let base_value = base.parse().map_err(|_| Error::MalformedNodeValue { + message: format!("can't parse base {}", base), + })?; Ok(base_value) } @@ -307,15 +292,11 @@ fn parse_atom_base_num_into(node: &AstNode, buf: &mut Vec) -> Result<() AtomBaseNumberValue => value | Ok ); - buf.push( - CstAtom::Number( - u32::from_str_radix(value.as_str(), base).map_err(|_| { - Error::MalformedNodeValue { - message: format!("can't parse number {}", value), - } - })?, - ) - ); + buf.push(CstAtom::Number( + u32::from_str_radix(value.as_str(), base).map_err(|_| Error::MalformedNodeValue { + message: format!("can't parse number {}", value), + })?, + )); Ok(()) } @@ -328,9 +309,11 @@ fn parse_value_of(node: &AstNode) -> Result { }); } - node.content().ok_or(Error::MissingContent { - node_type: node.node_type(), - }).cloned() + node.content() + .ok_or(Error::MissingContent { + node_type: node.node_type(), + }) + .cloned() } fn guard_node_type(node: &AstNode, expected_type: AstNodeType) -> Result<(), Error> { diff --git a/src/compiler/hexo_compiler.rs b/src/compiler/hexo_compiler.rs index ba04f3d..d780370 100644 --- a/src/compiler/hexo_compiler.rs +++ b/src/compiler/hexo_compiler.rs @@ -1,8 +1,8 @@ -use crate::compiler::{Compilation, CompilerSource, HexoCompilerContext}; use crate::compiler::ast::{AstNode, AstParser}; use crate::compiler::cst::{CstFile, CstParser}; use crate::compiler::error::Error; use crate::compiler::rst::{HexoFile, RstCompiler}; +use crate::compiler::{Compilation, CompilerSource, HexoCompilerContext}; pub(crate) struct HexoCompiler { context: HexoCompilerContext, @@ -57,24 +57,16 @@ impl HexoCompiler { mod benchmarks { extern crate test; - use test::bench::*; - use crate::compiler::compiler_source::tests::EagerCompilerSource; use super::*; + use crate::compiler::compiler_source::tests::EagerCompilerSource; + use test::bench::*; #[bench] fn compilation(b: &mut Bencher) { - let compiler = HexoCompiler::new( - HexoCompilerContext::new(false) - ); + let compiler = HexoCompiler::new(HexoCompilerContext::new(false)); - let source = EagerCompilerSource::new( - "samples/java_object/input.hexo" - ).unwrap(); + let source = EagerCompilerSource::new("samples/java_object/input.hexo").unwrap(); - b.iter(|| { - black_box( - compiler.compile(&source).unwrap() - ) - }); + b.iter(|| black_box(compiler.compile(&source).unwrap())); } } diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index a4c3493..3cc2229 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -1,15 +1,15 @@ +mod ast; mod compilation_result; -mod hexo_compiler; mod compiler_context; mod compiler_source; +mod cst; mod error; +mod hexo_compiler; mod native_fn; mod rst; -mod cst; -mod ast; pub(crate) use compilation_result::Compilation; -pub(crate) use hexo_compiler::HexoCompiler; pub(crate) use compiler_context::HexoCompilerContext; pub(crate) use compiler_source::{CompilerSource, FileCompilerSource}; pub(crate) use error::Error; +pub(crate) use hexo_compiler::HexoCompiler; diff --git a/src/compiler/native_fn/arguments.rs b/src/compiler/native_fn/arguments.rs new file mode 100644 index 0000000..645768c --- /dev/null +++ b/src/compiler/native_fn/arguments.rs @@ -0,0 +1,36 @@ +use std::collections::HashMap; +use crate::compiler::native_fn::Error; +use crate::util::byte_buffer::ByteBuffer; + +pub(crate) struct NativeFunctionArguments<'a> { + args: &'a HashMap, +} + +impl NativeFunctionArguments<'_> { + pub(crate) fn new(args: &HashMap) -> NativeFunctionArguments { + NativeFunctionArguments { + args + } + } + + pub(crate) fn get_argument_at( + &self, + pos: usize, + fn_name: &str, + ) -> Result<&ByteBuffer, Error> { + let arguments = &self.args; + arguments.get(&pos.to_string()) + .ok_or_else(move || Error::MissingArgument { + name: pos.to_string(), + available_arguments: arguments.keys().cloned().collect(), + function_name: fn_name.to_string(), + }) + } + + pub(crate) fn get_named_argument( + &self, + name: &str, + ) -> Option<&ByteBuffer> { + self.args.get(name) + } +} \ No newline at end of file diff --git a/src/compiler/native_fn/implementation/eval.rs b/src/compiler/native_fn/implementation/eval.rs index a74235d..e11eb48 100644 --- a/src/compiler/native_fn/implementation/eval.rs +++ b/src/compiler/native_fn/implementation/eval.rs @@ -12,7 +12,7 @@ impl NativeFunctionDefinition for EvalNativeFunctionDef { NativeFunction::new( NativeFunctionSignature::new_unsafe("eval"), |arguments, compiler| { - let buffer = get_argument_at(arguments, 0, "eval")?.clone(); + let buffer = arguments.get_argument_at(0, "eval")?.clone(); let source = LiteralCompilerSource::anonymous( buffer .to_string() diff --git a/src/compiler/native_fn/implementations.rs b/src/compiler/native_fn/implementations.rs index 577c742..46151ed 100644 --- a/src/compiler/native_fn/implementations.rs +++ b/src/compiler/native_fn/implementations.rs @@ -1,18 +1,18 @@ -use crate::util::byte_buffer::ByteBuffer; use std::collections::HashMap; use std::fs::File; use std::io::Read; -use crate::compiler::compiler_source::LiteralCompilerSource; + use crate::compiler::native_fn::error::Error; use crate::compiler::native_fn::signature::{NativeFunction, NativeFunctionSignature}; +use crate::util::byte_buffer::ByteBuffer; pub(crate) fn create_len_native_function() -> NativeFunction { NativeFunction::new( NativeFunctionSignature::new("len"), |arguments, _| { let mut result = ByteBuffer::default(); - let arg0 = get_named_argument(arguments, "utf8") - .unwrap_or_else(|| get_argument_at(arguments, 0, "len").unwrap()); + let arg0 = arguments.get_named_argument("utf8") + .unwrap_or_else(|| arguments.get_argument_at(0, "len").unwrap()); let len = arg0.len() as u32; result.push_u32_shrunk(len); @@ -25,8 +25,8 @@ pub(crate) fn create_pad_left_native_function() -> NativeFunction { NativeFunction::new( NativeFunctionSignature::new("pad_left"), |arguments, _| { - let mut arg0 = get_argument_at(arguments, 0, "pad_left")?.clone(); - let arg1 = get_argument_at(arguments, 1, "pad_left")?; + let mut arg0 = arguments.get_argument_at(0, "pad_left")?.clone(); + let arg1 = arguments.get_argument_at(1, "pad_left")?; arg0.pad_left(arg1.as_usize_unsafe()); @@ -39,8 +39,8 @@ pub(crate) fn create_pad_right_native_function() -> NativeFunction { NativeFunction::new( NativeFunctionSignature::new("pad_right"), |arguments, _| { - let mut arg0: ByteBuffer = get_argument_at(arguments, 0, "pad_right")?.clone(); - let arg1 = get_argument_at(arguments, 1, "pad_right")?; + let mut arg0: ByteBuffer = arguments.get_argument_at(0, "pad_right")?.clone(); + let arg1 = arguments.get_argument_at(1, "pad_right")?; arg0.pad_right(arg1.as_usize_unsafe()); @@ -53,7 +53,7 @@ pub(crate) fn create_cmd_native_function() -> NativeFunction { NativeFunction::new( NativeFunctionSignature::new_unsafe("cmd"), |arguments, _| { - let command = get_argument_at(arguments, 0, "cmd")? + let command = arguments.get_argument_at(0, "cmd")? .to_string() .map_err(|e| Error::Unknown(e.to_string()))?; @@ -72,7 +72,7 @@ pub(crate) fn create_read_file_native_function() -> NativeFunction { NativeFunction::new( NativeFunctionSignature::new("read_file"), |arguments, _| { - let arg0 = get_argument_at(arguments, 0, "read_file")?; + let arg0 = arguments.get_argument_at(0, "read_file")?; let file_path = arg0 .to_string() @@ -96,11 +96,10 @@ pub(crate) fn create_pad_native_function() -> NativeFunction { NativeFunction::new( NativeFunctionSignature::new("pad"), |arguments, _| { - let mut buffer = get_argument_at(arguments, 0, "pad")?.clone(); + let mut buffer = arguments.get_argument_at(0, "pad")?.clone(); - let left_padding = get_named_argument(arguments, "left").map(|b| b.as_usize_unsafe()); - let right_padding = - get_named_argument(arguments, "right").map(|b| b.as_usize_unsafe()); + let left_padding = arguments.get_named_argument("left").map(|b| b.as_usize_unsafe()); + let right_padding = arguments.get_named_argument("right").map(|b| b.as_usize_unsafe()); if let Some(size) = left_padding { buffer.pad_left(size); diff --git a/src/compiler/native_fn/mod.rs b/src/compiler/native_fn/mod.rs index 6354405..c46e244 100644 --- a/src/compiler/native_fn/mod.rs +++ b/src/compiler/native_fn/mod.rs @@ -3,8 +3,10 @@ mod implementations; mod index; mod signature; pub(crate) mod implementation; +mod arguments; pub(crate) use error::Error; pub(crate) use implementations::*; pub(crate) use index::*; pub(crate) use signature::*; +pub(crate) use arguments::*; \ No newline at end of file diff --git a/src/compiler/native_fn/signature.rs b/src/compiler/native_fn/signature.rs index 6ff6bb3..f429a31 100644 --- a/src/compiler/native_fn/signature.rs +++ b/src/compiler/native_fn/signature.rs @@ -2,6 +2,7 @@ use crate::compiler::native_fn::error::Error; use crate::util::byte_buffer::ByteBuffer; use std::collections::HashMap; use crate::compiler::HexoCompiler; +use crate::compiler::native_fn::arguments::NativeFunctionArguments; #[derive(Clone, Debug)] pub(crate) struct NativeFunctionSignature { @@ -33,7 +34,7 @@ impl NativeFunctionSignature { } } -type NativeFunctionExecutor = fn(&HashMap, &HexoCompiler) -> Result; +type NativeFunctionExecutor = fn(NativeFunctionArguments, &HexoCompiler) -> Result; #[derive(Clone, Debug)] pub(crate) struct NativeFunction { diff --git a/src/compiler/rst/compiler.rs b/src/compiler/rst/compiler.rs index b61f08f..268e8de 100644 --- a/src/compiler/rst/compiler.rs +++ b/src/compiler/rst/compiler.rs @@ -10,6 +10,7 @@ use crate::compiler::rst::scope::{ use crate::compiler::rst::error::Error; use crate::compiler::rst::node::HexoFile; use crate::compiler::HexoCompiler; +use crate::compiler::native_fn::NativeFunctionArguments; use crate::util::id::HexoId; pub(crate) struct RstCompiler<'a> { @@ -98,7 +99,8 @@ impl RstCompiler<'_> { params_buffer.insert(param.name().to_string(), param_buffer); } - executor(¶ms_buffer, self.parent) + let arguments = NativeFunctionArguments::new(¶ms_buffer); + executor(arguments, self.parent) .map(|bb| buffer.push_byte_buffer(&bb)) .map_err(Error::NativeFunctionExecution)?;