We have parentheticals

This commit is contained in:
2026-07-09 10:29:17 -05:00
parent a34bbba4b1
commit 725c6aa835
4 changed files with 21 additions and 12 deletions
+18 -3
View File
@@ -9,6 +9,11 @@ enum VarType {
//Mut,
}
enum BlockType {
Block,
Parenthetical,
}
#[derive(Debug)]
pub struct InterpreterError {
description: String,
@@ -64,7 +69,7 @@ macro_rules! apply_op {
type Scope = HashMap<String, VarType>;
fn interpret_block(ast: Vec<ASTNode>, scopes: &mut Vec<Scope>) -> Result<Vec<Value>, InterpreterError> {
fn interpret_block(ast: Vec<ASTNode>, scopes: &mut Vec<Scope>, block_type: BlockType) -> Result<Vec<Value>, InterpreterError> {
let mut ast = ast.clone();
ast.reverse();
let mut stack = vec![];
@@ -74,10 +79,14 @@ fn interpret_block(ast: Vec<ASTNode>, scopes: &mut Vec<Scope>) -> Result<Vec<Val
None => return Ok(stack),
Some(ASTNode::Block(tokens)) => {
scopes.push(HashMap::new());
let mut result = interpret_block(tokens, scopes)?;
let mut result = interpret_block(tokens, scopes, BlockType::Block)?;
scopes.pop();
stack.append(&mut result);
},
Some(ASTNode::Parenthetical(tokens)) => {
let mut result = interpret_block(tokens, scopes, BlockType::Parenthetical)?;
stack.append(&mut result);
},
Some(ASTNode::Val(v)) => stack.push(v),
Some(ASTNode::BinOp(op)) => {
let b = some_or_error!(stack.pop(), InterpreterError::new("right value needed"));
@@ -110,6 +119,9 @@ fn interpret_block(ast: Vec<ASTNode>, scopes: &mut Vec<Scope>) -> Result<Vec<Val
}));
},
Some(ASTNode::ConstDecl) => {
if let BlockType::Parenthetical = block_type {
return Err(InterpreterError::new("let bindings not allowed in parenthetical"));
}
let rhs = some_or_error!(stack.pop(), InterpreterError::new("rhs value needed"));
let lhs = some_or_error!(stack.pop(), InterpreterError::new("lhs value needed"));
let rhs: TeaValue = get_value(rhs, scopes)?;
@@ -126,6 +138,9 @@ fn interpret_block(ast: Vec<ASTNode>, scopes: &mut Vec<Scope>) -> Result<Vec<Val
}
},
Some(ASTNode::StatementEnd) => {
if let BlockType::Parenthetical = block_type {
return Err(InterpreterError::new("statements not allowed in parenthetical"));
}
if !stack.is_empty() {
return Err(InterpreterError::new("stack not empty"));
}
@@ -136,5 +151,5 @@ fn interpret_block(ast: Vec<ASTNode>, scopes: &mut Vec<Scope>) -> Result<Vec<Val
pub fn interpret(ast: Vec<ASTNode>) -> Result<Vec<Value>, InterpreterError> {
let mut scopes = vec![HashMap::new()];
interpret_block(ast, &mut scopes)
interpret_block(ast, &mut scopes, BlockType::Block)
}