full rewrite, kinda works

This commit is contained in:
Mark
2023-07-28 00:33:15 +02:00
parent 16258c7a0a
commit b81dac682e
96 changed files with 3150 additions and 1982 deletions

63
mers_lib/src/program/run/mod.rs Executable file
View File

@@ -0,0 +1,63 @@
use std::sync::Arc;
use crate::{
data::{self, Data, Type},
info,
};
pub mod assign_to;
pub mod block;
pub mod chain;
pub mod function;
pub mod r#if;
pub mod r#loop;
pub mod switch;
pub mod tuple;
pub mod value;
pub mod variable;
pub trait MersStatement: std::fmt::Debug {
fn run_custom(&self, info: &mut Info) -> Data;
/// if true, local variables etc. will be contained inside their own scope.
fn has_scope(&self) -> bool;
// fn outputs(&self) -> Type;
fn run(&self, info: &mut Info) -> Data {
if self.has_scope() {
info.create_scope();
}
let o = self.run_custom(info);
if self.has_scope() {
info.end_scope();
}
o
}
}
pub type Info = info::Info<Local>;
#[derive(Default, Clone, Debug)]
pub struct Local {
vars: Vec<Data>,
}
impl info::Local for Local {
type VariableIdentifier = usize;
type VariableData = Data;
fn init_var(&mut self, id: Self::VariableIdentifier, value: Self::VariableData) {
while self.vars.len() <= id {
self.vars.push(Data::new(data::bool::Bool(false)));
}
self.vars[id] = value;
}
fn get_var(&self, id: &Self::VariableIdentifier) -> Option<&Self::VariableData> {
match self.vars.get(*id) {
Some(v) => Some(v),
None => None,
}
}
fn get_var_mut(&mut self, id: &Self::VariableIdentifier) -> Option<&mut Self::VariableData> {
match self.vars.get_mut(*id) {
Some(v) => Some(v),
None => None,
}
}
}