Skip to content

Commit

Permalink
Intense Speeds
Browse files Browse the repository at this point in the history
  • Loading branch information
ahqsoftwares committed Jan 28, 2025
1 parent 44a30ec commit 9c25fb8
Show file tree
Hide file tree
Showing 17 changed files with 288 additions and 148 deletions.
104 changes: 77 additions & 27 deletions interpreter/src/ipreter.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
use crate::{
error, runtime::_root_syntax::insert_into_application, types::{call_runtime_val, mkbuf, set_runtime_val, BufValue, Heap, HeapWrapper, Options, RawRTValue}, Application
error,
runtime::_root_syntax::insert_into_application,
types::{
call_runtime_val, mkbuf, set_runtime_val, BufValue, Heap, HeapWrapper, Options, RawRTValue,
},
Application,
};

pub fn interpret(file: &str, mut app: &mut Application) {
let file_name = if file == ":entry" { app.entry } else { file };

let app_ptr = app as *mut Application;
let app_ptr = app as *mut Application;

let file = app.code.get(file).unwrap_or_else(move || {
let app = unsafe { &mut *app_ptr };
let data = app.module_resolver.call_mut((&format!("./{file}.pb"),));

app.code.insert(file.into(), String::from_utf8(data).unwrap());
app
.code
.insert(file.into(), String::from_utf8(data).unwrap());

app.code.get(file).expect("Impossible")
});
Expand All @@ -29,60 +36,80 @@ pub fn interpret(file: &str, mut app: &mut Application) {
let content = &file[line];

if !content.starts_with("#") {
tok_parse(format!("{}:{}", &file_name, line), content, &mut app, &mut app2.heap, &mut line);
unsafe {tok_parse(
format!("{}:{}", &file_name, line),
content,
&mut app,
&mut app2.heap,
&mut line,
);}
}

line += 1;
}
}

pub(crate) fn tok_parse(file: String, piece: &str, app: &mut Application, heap: &mut Heap, line: &mut usize) {
let mut tokens: Vec<String> = piece.split(" ").map(|x| x.to_string()).collect();
pub(crate) unsafe fn tok_parse(
file: String,
piece: &str,
app: &mut Application,
heap: &mut Heap,
line: &mut usize,
) {
let mut tokens: Vec<*const str> = piece.split(" ").map(|x| x as *const str).collect();

let mut caller = tokens[0].as_str();
let mut caller = unsafe { &*tokens[0] };
let mut val_type = "<-none->";

let mut to_set = String::new();

if tokens[0].ends_with(":") && (tokens[0].starts_with("*") || tokens[0].starts_with("$")) {
if tokens[0].starts_with("*") {
if caller.ends_with(":") && (caller.starts_with("*") || caller.starts_with("$")) {
if caller.starts_with("*") {
val_type = "*";
} else {
val_type = "$";
}

to_set = tokens.remove(0);
to_set = to_set.split_at(to_set.len() - 1).0.into();
let l = unsafe { &*tokens.remove(0) };
to_set = l.split_at(l.len() - 1).0.into();

caller = tokens[0].as_str();
caller = unsafe { &*tokens[0] };
}

let mut opt = Options::new();

if caller.starts_with("*if$") {
let caller = tokens.remove(0);
let caller = unsafe { &*tokens.remove(0) };

let caller = caller.replacen("*if", "", 1);

let BufValue::Bool(x) = heap.get(&caller).expect("Unable to get the value") else {
panic!("Invalid type, expected boolean in *if");
};

let piece = tokens.join(" ");
let piece = tokens
.into_iter()
.map(|x| unsafe { &*x })
.collect::<Vec<_>>()
.join(" ");

if *x {
tok_parse(file, &piece, app, heap, line);
}
} else if caller.starts_with("*else$") {
let caller = tokens.remove(0);
let caller = unsafe { &*tokens.remove(0) };

let caller = caller.replacen("*else", "", 1);

let BufValue::Bool(x) = heap.get(&caller).expect("Unable to get the value") else {
panic!("Invalid type, expected boolean in *if");
};

let piece = tokens.join(" ");
let piece = tokens
.into_iter()
.map(|x| unsafe { &*x })
.collect::<Vec<_>>()
.join(" ");

if !*x {
tok_parse(file, &piece, app, heap, line);
Expand All @@ -107,9 +134,11 @@ pub(crate) fn tok_parse(file: String, piece: &str, app: &mut Application, heap:
};

match call_runtime_val(heap, caller, &tokens, wrap, &file, &mut opt, &file) {
None => if &caller != &"" {
error(&format!("Unexpected `{}`", &caller), &file);
},
None => {
if &caller != &"" {
error(&format!("Unexpected `{}`", &caller), &file);
}
}
Some(v) => {
opt.pre = v.to_string();

Expand Down Expand Up @@ -157,26 +186,47 @@ pub(crate) fn tok_parse(file: String, piece: &str, app: &mut Application, heap:
}
_ => {
let app2 = app as *mut Application;

match app.modules.get_mut(caller) {
Some(v) => {
let tkns = tokens.drain(2..).collect::<Vec<_>>();
let token0 = tokens.remove(1);
let token0 = &*tokens.remove(1);

v.run_method(app2, &token0, &file, move |fn_heap, app_heap, args| {
if tkns.len() != args.len() {
error("Not all arguments provided", ":interpreter:loadmodule:heap:check");
error(
"Not all arguments provided",
":interpreter:loadmodule:heap:check",
);
}

tkns.into_iter().zip(args.iter()).for_each(|(token, arg)| {
let from = app_heap.remove(&token).unwrap_or_else(|| error(format!("Unable to get {token} from Heap"), ":interpreter:loadmodule")).unwrap_or_else(|| error(format!("Unable to get {token} from Heap"), ":interpreter:loadmodule"));

fn_heap.set((*arg as &str).replacen("->$", "$", 1), from).unwrap();
let token = unsafe { &*token };
let from = app_heap
.remove(token)
.unwrap_or_else(|| {
error(
format!("Unable to get {token} from Heap"),
":interpreter:loadmodule",
)
})
.unwrap_or_else(|| {
error(
format!("Unable to get {token} from Heap"),
":interpreter:loadmodule",
)
});

fn_heap
.set((*arg as &str).replacen("->$", "$", 1), from)
.unwrap();
});
});
}
_ => if &caller != &"" {
error(&format!("Unexpected `{}`", &caller), &file);
_ => {
if &caller != &"" {
error(&format!("Unexpected `{}`", &caller), &file);
}
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,26 @@ pub struct Application<'a> {
entry: &'a str,
heap: Heap,
markers: HashMap<String, usize>,

// Resolve files
module_resolver: Box<dyn FnMut(&str) -> Vec<u8>>,
// Resolve path from mod name
pkg_resolver: Box<dyn FnMut(&str) -> RespPackage>,
// Log in case of full access request
log_info: Box<dyn FnMut(&str) -> ()>
log_info: Box<dyn FnMut(&str) -> ()>,
}

impl<'a> Application<'a> {
pub fn new<T: FnMut(&str) -> Vec<u8> + 'static, F: FnMut(&str) -> RespPackage + 'static, R: FnMut(&str) -> () + 'static>(file: &'a str, mut fs_resolver: T, dll_resolver: F, requested_perm: R) -> Self {
pub fn new<
T: FnMut(&str) -> Vec<u8> + 'static,
F: FnMut(&str) -> RespPackage + 'static,
R: FnMut(&str) -> () + 'static,
>(
file: &'a str,
mut fs_resolver: T,
dll_resolver: F,
requested_perm: R,
) -> Self {
let main = String::from_utf8(fs_resolver(file)).expect("Invalid utf8");

let mut code = HashMap::new();
Expand All @@ -79,7 +88,7 @@ impl<'a> Application<'a> {
module_resolver: Box::new(fs_resolver),
markers: HashMap::new(),
pkg_resolver: Box::new(dll_resolver),
log_info: Box::new(requested_perm)
log_info: Box::new(requested_perm),
}
}

Expand Down Expand Up @@ -107,7 +116,7 @@ impl<'a> Application<'a> {
let pkg = ImplPackage {
name,
methods,
dyn_methods
dyn_methods,
};

self.pkg.import(pkg);
Expand Down
4 changes: 4 additions & 0 deletions interpreter/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ macro_rules! parse {
interpreter::error("Invalid Format!", $file);
};

$(
let $y = unsafe { &**$y };
)*

$(interpreter::modify!($file + $heap: $x $y);)*;
};
}
Expand Down
16 changes: 7 additions & 9 deletions interpreter/src/package/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{types::{DynMethodRes, MethodRes, PackageCallback}, Package};

use crate::{
types::{DynMethodRes, MethodRes, PackageCallback},
Package,
};

#[derive(Default)]
/// ImplPackage is not meant to create a package out of
Expand Down Expand Up @@ -44,7 +46,7 @@ macro_rules! generate {
pub fn ver() -> u16 {
interpreter::VERSION_INT
}

#[no_mangle]
pub fn modules() -> Vec<Box<dyn interpreter::Package>> {
use interpreter::Package;
Expand Down Expand Up @@ -79,12 +81,8 @@ impl ImplPackage {
self
}

pub fn add_method(
mut self,
name: &'static str,
callback: PackageCallback,
) -> Self {
pub fn add_method(mut self, name: &'static str, callback: PackageCallback) -> Self {
self.dyn_methods.push((name, callback));
self
}
}
}
Loading

0 comments on commit 9c25fb8

Please sign in to comment.