From c29b617de7db0ed90c42a6b2ff6439596c261fd1 Mon Sep 17 00:00:00 2001 From: wmedrano Date: Fri, 9 Feb 2024 22:47:42 -0800 Subject: [PATCH] Polish code_gen eval_atom. --- crates/steel-core/src/compiler/code_gen.rs | 16 ++++------------ crates/steel-core/src/compiler/passes/begin.rs | 4 ++-- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/crates/steel-core/src/compiler/code_gen.rs b/crates/steel-core/src/compiler/code_gen.rs index 3ef10d444..34dd8d519 100644 --- a/crates/steel-core/src/compiler/code_gen.rs +++ b/crates/steel-core/src/compiler/code_gen.rs @@ -47,20 +47,13 @@ pub struct CodeGenerator<'a> { /// Converts a syntax object's token into a `SteelVal` or returns an error if it is not a valid /// `SteelVal`. fn eval_atom(t: &SyntaxObject) -> Result { - match &t.ty { - TokenType::BooleanLiteral(b) => Ok((*b).into()), - TokenType::Number(n) => number_literal_to_steel(n), - TokenType::StringLiteral(s) => Ok(SteelVal::StringV(s.into())), - TokenType::CharacterLiteral(c) => Ok(SteelVal::CharV(*c)), - // TODO: Keywords shouldn't be misused as an expression - only in function calls are keywords allowed - TokenType::Keyword(k) => Ok(SteelVal::SymbolV(k.clone().into())), - what => { - stop!(UnexpectedToken => what; t.span) - } + match try_eval_atom(t) { + Some(v) => Ok(v), + None => stop!(UnexpectedToken => t.ty; t.span), } } -/// TODO: Consider reusing `eval_atom` as there is a lot of shared functionality. +/// This is similar to eval_atom but does not allocate a string on a failed match. fn try_eval_atom(t: &SyntaxObject) -> Option { match &t.ty { TokenType::BooleanLiteral(b) => Some((*b).into()), @@ -73,7 +66,6 @@ fn try_eval_atom(t: &SyntaxObject) -> Option { _what => { // println!("getting here in the eval_atom - code_gen"); // stop!(UnexpectedToken => what; t.span) - return None; } } diff --git a/crates/steel-core/src/compiler/passes/begin.rs b/crates/steel-core/src/compiler/passes/begin.rs index 439388434..f6326e1df 100644 --- a/crates/steel-core/src/compiler/passes/begin.rs +++ b/crates/steel-core/src/compiler/passes/begin.rs @@ -434,7 +434,7 @@ impl ExpressionType { matches!(self, ExpressionType::Expression) } - fn eval_atom(t: &SyntaxObject) -> bool { + fn is_atom(t: &SyntaxObject) -> bool { matches!( t.ty, TokenType::BooleanLiteral(_) @@ -446,7 +446,7 @@ impl ExpressionType { fn is_constant(expr: &ExprKind) -> bool { match expr { - ExprKind::Atom(Atom { syn, .. }) => Self::eval_atom(syn), + ExprKind::Atom(Atom { syn, .. }) => Self::is_atom(syn), _ => false, } }