From 8679172c429b0c96a9923e07d32841e5479439f8 Mon Sep 17 00:00:00 2001 From: afonya2 Date: Sat, 24 May 2025 18:36:01 +0200 Subject: [PATCH] added functions to memory --- src/executor.rs | 34 +++++++++++++++++++++++++++------- test.as | 5 ++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/executor.rs b/src/executor.rs index 6ac3e74..2a3e23c 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -1,4 +1,4 @@ -use crate::parser::{ASTPart, AstBool, AstNull, AstNumber, AstString}; +use crate::parser::{ASTPart, AstBool, AstFunction, AstNull, AstNumber, AstString}; #[derive(Debug, PartialEq)] pub enum State { @@ -13,7 +13,8 @@ pub enum MemoryData { Number(MemoryNumber), String(MemoryString), Boolean(MemoryBoolean), - Null(MemoryNull) + Null(MemoryNull), + Function(MemoryFunction) } #[derive(Debug, Clone, PartialEq)] pub struct MemoryNumber { @@ -34,6 +35,12 @@ pub struct MemoryBoolean { pub struct MemoryNull { pub key: String, } +#[derive(Debug, Clone, PartialEq)] +pub struct MemoryFunction { + pub key: String, + pub args: Vec, + pub body: Vec +} pub struct Executor { pub code: Vec, @@ -81,6 +88,11 @@ impl Executor { if null_data.key == key { return ASTPart::Null(AstNull { pos: 0 }); } + }, + MemoryData::Function(func) => { + if func.key == key { + return ASTPart::Function(AstFunction { args: func.args.clone(), body: func.body.clone(), pos: 0 }); + } } } } @@ -118,6 +130,12 @@ impl Executor { self.memory[current_scope].remove(data); return true; } + }, + MemoryData::Function(func) => { + if func.key == key { + self.memory[current_scope].remove(data); + return true; + } } } } @@ -139,24 +157,23 @@ impl Executor { } } } + let current_scope = self.memory.len() - 1; match value { ASTPart::Number(num) => { - let current_scope = self.memory.len() - 1; self.memory[current_scope].push(MemoryData::Number(MemoryNumber { key: key, value: num.value })); }, ASTPart::String(str) => { - let current_scope = self.memory.len() - 1; self.memory[current_scope].push(MemoryData::String(MemoryString { key: key, value: str.value })); }, ASTPart::Boolean(bool_data) => { - let current_scope = self.memory.len() - 1; self.memory[current_scope].push(MemoryData::Boolean(MemoryBoolean { key: key, value: bool_data.value })); }, ASTPart::Null(_) => { - let current_scope = self.memory.len() - 1; - self.delete_memory(key.clone()); self.memory[current_scope].push(MemoryData::Null(MemoryNull { key: key })); }, + ASTPart::Function(func) => { + self.memory[current_scope].push(MemoryData::Function(MemoryFunction { key: key, args: func.args.clone(), body: func.body.clone() })); + } _ => { self.state = State::Error(format!("Unable to write to memory: {:?}", value)); return; @@ -184,6 +201,9 @@ impl Executor { ASTPart::Boolean(_) => { return code.clone(); }, + ASTPart::Function(_) => { + return code.clone(); + }, ASTPart::Assigment(assign) => { let value = self.process_code(&assign.value, op_count); if self.state != State::Running { diff --git a/test.as b/test.as index 7e549f0..f458550 100644 --- a/test.as +++ b/test.as @@ -1,3 +1,6 @@ gethelj a = 1 gethelj b = (a + 2) * 3 -a = 3 \ No newline at end of file +a = 3 +gethelj d = lőcsve(e,f) { + +} \ No newline at end of file