added functions to memory

This commit is contained in:
afonya2 2025-05-24 18:36:01 +02:00
parent 724bec6eb2
commit 8679172c42
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
2 changed files with 31 additions and 8 deletions

View file

@ -1,4 +1,4 @@
use crate::parser::{ASTPart, AstBool, AstNull, AstNumber, AstString}; use crate::parser::{ASTPart, AstBool, AstFunction, AstNull, AstNumber, AstString};
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum State { pub enum State {
@ -13,7 +13,8 @@ pub enum MemoryData {
Number(MemoryNumber), Number(MemoryNumber),
String(MemoryString), String(MemoryString),
Boolean(MemoryBoolean), Boolean(MemoryBoolean),
Null(MemoryNull) Null(MemoryNull),
Function(MemoryFunction)
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct MemoryNumber { pub struct MemoryNumber {
@ -34,6 +35,12 @@ pub struct MemoryBoolean {
pub struct MemoryNull { pub struct MemoryNull {
pub key: String, pub key: String,
} }
#[derive(Debug, Clone, PartialEq)]
pub struct MemoryFunction {
pub key: String,
pub args: Vec<String>,
pub body: Vec<ASTPart>
}
pub struct Executor { pub struct Executor {
pub code: Vec<ASTPart>, pub code: Vec<ASTPart>,
@ -81,6 +88,11 @@ impl Executor {
if null_data.key == key { if null_data.key == key {
return ASTPart::Null(AstNull { pos: 0 }); 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); self.memory[current_scope].remove(data);
return true; 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 { match value {
ASTPart::Number(num) => { ASTPart::Number(num) => {
let current_scope = self.memory.len() - 1;
self.memory[current_scope].push(MemoryData::Number(MemoryNumber { key: key, value: num.value })); self.memory[current_scope].push(MemoryData::Number(MemoryNumber { key: key, value: num.value }));
}, },
ASTPart::String(str) => { ASTPart::String(str) => {
let current_scope = self.memory.len() - 1;
self.memory[current_scope].push(MemoryData::String(MemoryString { key: key, value: str.value })); self.memory[current_scope].push(MemoryData::String(MemoryString { key: key, value: str.value }));
}, },
ASTPart::Boolean(bool_data) => { 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 })); self.memory[current_scope].push(MemoryData::Boolean(MemoryBoolean { key: key, value: bool_data.value }));
}, },
ASTPart::Null(_) => { ASTPart::Null(_) => {
let current_scope = self.memory.len() - 1;
self.delete_memory(key.clone());
self.memory[current_scope].push(MemoryData::Null(MemoryNull { key: key })); 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)); self.state = State::Error(format!("Unable to write to memory: {:?}", value));
return; return;
@ -184,6 +201,9 @@ impl Executor {
ASTPart::Boolean(_) => { ASTPart::Boolean(_) => {
return code.clone(); return code.clone();
}, },
ASTPart::Function(_) => {
return code.clone();
},
ASTPart::Assigment(assign) => { ASTPart::Assigment(assign) => {
let value = self.process_code(&assign.value, op_count); let value = self.process_code(&assign.value, op_count);
if self.state != State::Running { if self.state != State::Running {

View file

@ -1,3 +1,6 @@
gethelj a = 1 gethelj a = 1
gethelj b = (a + 2) * 3 gethelj b = (a + 2) * 3
a = 3 a = 3
gethelj d = lőcsve(e,f) {
}