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)]
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<String>,
pub body: Vec<ASTPart>
}
pub struct Executor {
pub code: Vec<ASTPart>,
@ -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 {