Skip to content

Commit

Permalink
Polish code_gen eval_atom.
Browse files Browse the repository at this point in the history
  • Loading branch information
wmedrano committed Feb 10, 2024
1 parent b66b581 commit c29b617
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
16 changes: 4 additions & 12 deletions crates/steel-core/src/compiler/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SteelVal> {
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<SteelVal> {
match &t.ty {
TokenType::BooleanLiteral(b) => Some((*b).into()),
Expand All @@ -73,7 +66,6 @@ fn try_eval_atom(t: &SyntaxObject) -> Option<SteelVal> {
_what => {
// println!("getting here in the eval_atom - code_gen");
// stop!(UnexpectedToken => what; t.span)

return None;
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/steel-core/src/compiler/passes/begin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
Expand All @@ -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,
}
}
Expand Down

0 comments on commit c29b617

Please sign in to comment.