From 7399a6d2efddcdf6c89b373a8c7bbd5ff2828104 Mon Sep 17 00:00:00 2001 From: afonya2 Date: Sat, 24 May 2025 19:29:11 +0200 Subject: [PATCH] added a way to create env function that have access to the executor --- src/enviroment.rs | 10 ++++++++-- src/executor.rs | 4 ++-- src/parser.rs | 4 ++-- test.as | 2 ++ 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/enviroment.rs b/src/enviroment.rs index fc72ef7..3ac50e6 100644 --- a/src/enviroment.rs +++ b/src/enviroment.rs @@ -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 { match from { @@ -10,7 +10,7 @@ fn to_printed(from: &ASTPart) -> String { } } -fn ugass(args: Vec) -> ASTPart { +fn ugass(_exec: &mut Executor, args: Vec) -> ASTPart { let mut out = String::new(); for arg in &args { out.push_str(&to_printed(arg)); @@ -20,8 +20,14 @@ fn ugass(args: Vec) -> ASTPart { return ASTPart::NOOP; } +fn print_memory(exec: &mut Executor, _args: Vec) -> ASTPart { + println!("Memory dump: {:?}", exec.memory); + return ASTPart::NOOP; +} + pub fn generate() -> Vec { let mut out: Vec = vec![]; 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; } \ No newline at end of file diff --git a/src/executor.rs b/src/executor.rs index 1f122d0..cde6755 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -45,7 +45,7 @@ pub struct MemoryFunction { #[derive(Debug, Clone, PartialEq)] pub struct MemoryRustFunction { pub key: String, - pub func: fn(Vec) -> ASTPart + pub func: fn(&mut Executor, Vec) -> ASTPart } pub struct Executor { @@ -458,7 +458,7 @@ impl Executor { return ASTPart::NOOP; }, ASTPart::RustFunction(func) => { - let res = (func.func)(call.args.clone()); + let res = (func.func)(self, call.args.clone()); return res; }, _ => { diff --git a/src/parser.rs b/src/parser.rs index b747d96..cff50b9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,6 +1,6 @@ use core::panic; -use crate::lexer::{Token, TokenType}; +use crate::{executor::Executor, lexer::{Token, TokenType}}; #[derive(Debug, Clone, PartialEq)] pub enum ASTPart { @@ -120,7 +120,7 @@ pub struct AstContinue { } #[derive(Debug, Clone, PartialEq)] pub struct AstRustFunction { - pub func: fn(Vec) -> ASTPart, + pub func: fn(&mut Executor, Vec) -> ASTPart, pub pos: usize } diff --git a/test.as b/test.as index 62dba72..491d8a3 100644 --- a/test.as +++ b/test.as @@ -1,3 +1,5 @@ ha geny (1 == 1) { + gethelj a = 1 ugass(szaft"test"szaft) + printMemory() } \ No newline at end of file