From 725c6aa8358803fc01e394522e292f1d3a6cd56b Mon Sep 17 00:00:00 2001 From: Seoxi Ryouko Date: Thu, 9 Jul 2026 10:29:17 -0500 Subject: [PATCH] We have parentheticals --- src/interpret.rs | 21 ++++++++++++++++++--- src/lex.rs | 1 - src/parse.rs | 3 ++- test.matcha | 8 +------- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/interpret.rs b/src/interpret.rs index ad872d2..dc42656 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -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; -fn interpret_block(ast: Vec, scopes: &mut Vec) -> Result, InterpreterError> { +fn interpret_block(ast: Vec, scopes: &mut Vec, block_type: BlockType) -> Result, InterpreterError> { let mut ast = ast.clone(); ast.reverse(); let mut stack = vec![]; @@ -74,10 +79,14 @@ fn interpret_block(ast: Vec, scopes: &mut Vec) -> Result 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, scopes: &mut Vec) -> Result { + 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, scopes: &mut Vec) -> Result { + 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, scopes: &mut Vec) -> Result) -> Result, InterpreterError> { let mut scopes = vec![HashMap::new()]; - interpret_block(ast, &mut scopes) + interpret_block(ast, &mut scopes, BlockType::Block) } diff --git a/src/lex.rs b/src/lex.rs index 28b3851..9b3c8c0 100644 --- a/src/lex.rs +++ b/src/lex.rs @@ -67,7 +67,6 @@ pub fn lex_tokens(s: &str) -> Result, (Vec, &str)> { let mut tokens: Vec = vec![]; loop { if input.is_empty() { - println!("tokens: {tokens:?}\n\n"); return Ok(tokens); } try_parse!(parse_literal(input), tokens, input); diff --git a/src/parse.rs b/src/parse.rs index eb03b51..c4b08df 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -21,6 +21,7 @@ pub enum ASTNode { UnaryMinus, StatementEnd, Block(Vec), + Parenthetical(Vec), } #[derive(Debug)] @@ -153,7 +154,7 @@ fn parse_expr(tokens: &[Token]) -> Result<(Vec, usize), ParseError> { Some(Token::Sym(Symbol::OpenParen)) => { let end = next_paren_end(&tokens[(index + 1)..])?; let (expr, count) = parse_expr(&tokens[(index + 1)..(index + end + 1)])?; - ast.push(ASTNode::Block(expr)); + ast.push(ASTNode::Parenthetical(expr)); index += count + 1; }, Some(Token::Sym(Symbol::CloseParen)) => { diff --git a/test.matcha b/test.matcha index 495a3bc..86ee907 100644 --- a/test.matcha +++ b/test.matcha @@ -1,8 +1,2 @@ -let x = 5; -let y = { - let x = -x; - x + 1 * -x -}; -x + y - +(5 + 6) * 4