From 2b4e522b12a7eb87e91cd1cdc56064ee429a5212 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 11 Feb 2025 14:00:20 +1300 Subject: Initial commit --- src/environment.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/environment.rs (limited to 'src/environment.rs') diff --git a/src/environment.rs b/src/environment.rs new file mode 100644 index 0000000..006b45b --- /dev/null +++ b/src/environment.rs @@ -0,0 +1,63 @@ +use crate::*; + +use std::collections::HashMap; + + +pub struct Environment { + pub scopes: Vec, +} + +impl Environment { + pub fn get_integer(&self, name: &str) -> Result { + for scope in self.scopes.iter().rev() { + if let Ok(value) = scope.get_integer(name, &self) { + return Ok(value); + } + } + return Err(()); + } + + pub fn get_block(&self, name: &str) -> Result { + for scope in self.scopes.iter().rev() { + if let Ok(value) = scope.get_block(name, &self) { + return Ok(value); + } + } + return Err(()); + } +} + +pub struct Scope { + pub definitions: HashMap, +} + +impl Scope { + pub fn get_integer(&self, name: &str, environment: &Environment) -> Result { + use IntegerDefinition as IntDef; + if let Some(definition) = self.definitions.get(name) { + if let Definition::Integer(integer) = definition { + match integer { + IntDef::Literal(value) => return Ok(*value), + IntDef::ConstantExpression(expr) => match expr.evaluate(environment) { + Ok(_) | Err(_) => todo!(), + }, + }; + } + } + return Err(()); + } + + pub fn get_block(&self, _name: &str, _environment: &Environment) -> Result { + todo!() + } +} + +pub enum Definition { + Integer(IntegerDefinition), + Block(BlockLiteral), +} + +pub enum IntegerDefinition { + Literal(usize), + ConstantExpression(ConstantExpression), +} -- cgit v1.2.3-70-g09d2