Skip to content

Commit

Permalink
created new NativeFunctionDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed Jul 11, 2024
1 parent c1c350f commit b96eb0c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 23 deletions.
28 changes: 28 additions & 0 deletions src/compiler/native_fn/implementation/eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::compiler::compiler_source::LiteralCompilerSource;
use crate::compiler::native_fn::{
get_argument_at, Error, NativeFunction, NativeFunctionDefinition, NativeFunctionSignature,
};
use crate::util::byte_buffer::ByteBuffer;

#[derive(Default)]
pub(crate) struct EvalNativeFunctionDef {}

impl NativeFunctionDefinition for EvalNativeFunctionDef {
fn create(&self) -> NativeFunction {
NativeFunction::new(
NativeFunctionSignature::new_unsafe("eval"),
|arguments, compiler| {
let buffer = get_argument_at(arguments, 0, "eval")?.clone();
let source = LiteralCompilerSource::anonymous(
buffer
.to_string()
.map_err(|e| Error::Unknown(e.to_string()))?,
);
let result = compiler
.compile(&source)
.map_err(|e| Error::Unknown(e.to_string()))?;
Ok(ByteBuffer::from(result.content))
},
)
}
}
2 changes: 2 additions & 0 deletions src/compiler/native_fn/implementation/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod eval;
pub(crate) use eval::EvalNativeFunctionDef;
20 changes: 2 additions & 18 deletions src/compiler/native_fn/implementations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,7 @@ pub(crate) fn create_pad_native_function() -> NativeFunction {
)
}

pub(crate) fn create_eval_native_function() -> NativeFunction {
NativeFunction::new(
NativeFunctionSignature::new_unsafe("eval"),
|arguments, compiler| {
let buffer = get_argument_at(arguments, 0, "eval")?.clone();
let source = LiteralCompilerSource::anonymous(
buffer.to_string()
.map_err(|e| Error::Unknown(e.to_string()))?,
);
let result = compiler.compile(&source)
.map_err(|e| Error::Unknown(e.to_string()))?;
Ok(ByteBuffer::from(result.content))
},
)
}

fn get_argument_at<'a>(
pub(crate) fn get_argument_at<'a>(
arguments: &'a HashMap<String, ByteBuffer>,
pos: usize,
fn_name: &str,
Expand All @@ -144,7 +128,7 @@ fn get_argument_at<'a>(
})
}

fn get_named_argument<'a>(
pub(crate) fn get_named_argument<'a>(
arguments: &'a HashMap<String, ByteBuffer>,
name: &str,
) -> Option<&'a ByteBuffer> {
Expand Down
20 changes: 16 additions & 4 deletions src/compiler/native_fn/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::compiler::native_fn::signature::NativeFunction;
use crate::compiler::native_fn::{create_cmd_native_function, create_eval_native_function, create_len_native_function, create_pad_left_native_function, create_pad_native_function, create_pad_right_native_function, create_read_file_native_function};

use crate::compiler::native_fn::{create_cmd_native_function, create_len_native_function, create_pad_left_native_function, create_pad_native_function, create_pad_right_native_function, create_read_file_native_function, NativeFunctionDefinition};
use crate::compiler::native_fn::implementation::EvalNativeFunctionDef;
#[derive(Clone, Debug)]
pub(crate) struct NativeFunctionIndex {
functions: Vec<NativeFunction>,
Expand All @@ -19,14 +19,26 @@ impl NativeFunctionIndex {
}

fn create_native_functions() -> Vec<NativeFunction> {
vec![
let definitions = Self::create_native_function_definitions();

let mut direct_fn = vec![
create_len_native_function(),
create_pad_left_native_function(),
create_pad_right_native_function(),
create_cmd_native_function(),
create_read_file_native_function(),
create_pad_native_function(),
create_eval_native_function()
];
direct_fn.append(
&mut definitions.iter().map(|d| d.create()).collect()
);

direct_fn
}

fn create_native_function_definitions() -> Vec<Box<dyn NativeFunctionDefinition>> {
return vec![
Box::new(EvalNativeFunctionDef::default()),
]
}
}
1 change: 1 addition & 0 deletions src/compiler/native_fn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod error;
mod implementations;
mod index;
mod signature;
pub(crate) mod implementation;

pub(crate) use error::Error;
pub(crate) use implementations::*;
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/native_fn/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ impl NativeFunction {
pub(crate) fn executor(&self) -> NativeFunctionExecutor {
self.executor
}
}
}

pub(crate) trait NativeFunctionDefinition {
fn create(&self) -> NativeFunction;
}

0 comments on commit b96eb0c

Please sign in to comment.