added a way to create env function that have access to the executor

This commit is contained in:
afonya2 2025-05-24 19:29:11 +02:00
parent 58b1b2f94b
commit 7399a6d2ef
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
4 changed files with 14 additions and 6 deletions

View file

@ -1,4 +1,4 @@
use crate::{executor::{MemoryData, MemoryRustFunction}, parser::ASTPart}; use crate::{executor::{Executor, MemoryData, MemoryRustFunction}, parser::ASTPart};
fn to_printed(from: &ASTPart) -> String { fn to_printed(from: &ASTPart) -> String {
match from { match from {
@ -10,7 +10,7 @@ fn to_printed(from: &ASTPart) -> String {
} }
} }
fn ugass(args: Vec<ASTPart>) -> ASTPart { fn ugass(_exec: &mut Executor, args: Vec<ASTPart>) -> ASTPart {
let mut out = String::new(); let mut out = String::new();
for arg in &args { for arg in &args {
out.push_str(&to_printed(arg)); out.push_str(&to_printed(arg));
@ -20,8 +20,14 @@ fn ugass(args: Vec<ASTPart>) -> ASTPart {
return ASTPart::NOOP; return ASTPart::NOOP;
} }
fn print_memory(exec: &mut Executor, _args: Vec<ASTPart>) -> ASTPart {
println!("Memory dump: {:?}", exec.memory);
return ASTPart::NOOP;
}
pub fn generate() -> Vec<MemoryData> { pub fn generate() -> Vec<MemoryData> {
let mut out: Vec<MemoryData> = vec![]; let mut out: Vec<MemoryData> = vec![];
out.push(MemoryData::RustFunction(MemoryRustFunction { key: String::from("ugass"), func: ugass })); out.push(MemoryData::RustFunction(MemoryRustFunction { key: String::from("ugass"), func: ugass }));
out.push(MemoryData::RustFunction(MemoryRustFunction { key: String::from("printMemory"), func: print_memory }));
return out; return out;
} }

View file

@ -45,7 +45,7 @@ pub struct MemoryFunction {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct MemoryRustFunction { pub struct MemoryRustFunction {
pub key: String, pub key: String,
pub func: fn(Vec<ASTPart>) -> ASTPart pub func: fn(&mut Executor, Vec<ASTPart>) -> ASTPart
} }
pub struct Executor { pub struct Executor {
@ -458,7 +458,7 @@ impl Executor {
return ASTPart::NOOP; return ASTPart::NOOP;
}, },
ASTPart::RustFunction(func) => { ASTPart::RustFunction(func) => {
let res = (func.func)(call.args.clone()); let res = (func.func)(self, call.args.clone());
return res; return res;
}, },
_ => { _ => {

View file

@ -1,6 +1,6 @@
use core::panic; use core::panic;
use crate::lexer::{Token, TokenType}; use crate::{executor::Executor, lexer::{Token, TokenType}};
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum ASTPart { pub enum ASTPart {
@ -120,7 +120,7 @@ pub struct AstContinue {
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct AstRustFunction { pub struct AstRustFunction {
pub func: fn(Vec<ASTPart>) -> ASTPart, pub func: fn(&mut Executor, Vec<ASTPart>) -> ASTPart,
pub pos: usize pub pos: usize
} }

View file

@ -1,3 +1,5 @@
ha geny (1 == 1) { ha geny (1 == 1) {
gethelj a = 1
ugass(szaft"test"szaft) ugass(szaft"test"szaft)
printMemory()
} }