Initialize Rust implementation of Lox interpreter

This initial commit sets up the basic structure for a Rust
implementation of the Lox interpreter, including:

- Lexer for tokenizing source code - Parser for building AST - Basic
interpreter functionality - Environment for variable storage

The core components include source tracking, error handling, and basic
expression evaluation.
This commit is contained in:
Giulio Agostini
2025-10-03 19:07:12 +02:00
commit a7df45dc72
18 changed files with 2710 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
use std::collections::HashMap;
use crate::{
frontend::{source_registry::SourceSlice, tokens::LiteralValue},
result::{LoxError, LoxResult},
};
pub struct Environment<'a> {
values: HashMap<String, LiteralValue>,
parent: Option<&'a Environment<'a>>,
}
impl<'a> Environment<'a> {
pub fn new() -> Self {
Environment {
values: HashMap::new(),
parent: None,
}
}
pub fn new_with_parent(parent: &'a Environment<'a>) -> Self {
Environment {
values: HashMap::new(),
parent: Some(parent),
}
}
pub fn get(&self, name: &str) -> LoxResult<LiteralValue> {
match self.values.get(name) {
Some(value) => Ok(value.clone()),
None => match self.parent {
Some(parent) => parent.get(name),
None => Err(LoxError::RuntimeError {
source_slice: SourceSlice::default(), // todo change this to the actual source slice
message: format!("Undefined variable '{}'", name),
}),
},
}
}
pub fn set(&mut self, name: String, value: LiteralValue) {
self.values.insert(name, value);
}
}