From c42623223ce5dd04d1a5d3b71166dc65d39a2705 Mon Sep 17 00:00:00 2001 From: numzero Date: Sun, 20 Apr 2025 13:49:06 +0300 Subject: [PATCH] Extract ast::Constant as scalar::Scalar --- src/ast.rs | 49 ++++--------------------------------------------- src/lib.rs | 1 + src/scalar.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/twopass.rs | 17 +++++++++-------- 4 files changed, 55 insertions(+), 53 deletions(-) create mode 100644 src/scalar.rs diff --git a/src/ast.rs b/src/ast.rs index a6fa7ce..9cd7807 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,3 +1,5 @@ +use crate::scalar::Scalar; + pub type Ident = String; #[derive(Debug, Clone)] @@ -26,7 +28,7 @@ pub enum Location { #[derive(Debug, Clone)] pub enum Expression { - Constant(Constant), + Constant(Scalar), Variable(Location), Call(Call), Function(Function), @@ -58,15 +60,6 @@ pub struct Function { pub ret: Option>, } -#[derive(Debug, Clone, Default)] -pub enum Constant { - #[default] - Nil, - Boolean(bool), - Number(i64), - String(String), -} - impl Location { pub fn new_variable(name: impl Into) -> Self { Self::Variable { name: name.into() } @@ -81,7 +74,7 @@ impl Location { } impl Expression { - pub fn new_constant(value: impl Into) -> Self { + pub fn new_constant(value: impl Into) -> Self { Self::Constant(value.into()) } @@ -101,40 +94,6 @@ impl Expression { } } -impl From<()> for Constant { - fn from(_value: ()) -> Self { - Self::Nil - } -} - -macro_rules! impl_from { - ($as: ident, $from: ty) => { - impl From<$from> for Constant { - fn from(value: $from) -> Self { - Self::$as(value.into()) - } - } - }; - (try $as: ident, $from: ty) => { - impl TryFrom<$from> for Constant { - type Error = ::std::num::TryFromIntError; - fn try_from(value: $from) -> Result { - Ok(Self::$as(value.try_into()?)) - } - } - }; -} - -impl_from!(Boolean, bool); -impl_from!(try Number, isize); -impl_from!(try Number, usize); -impl_from!(try Number, i64); -impl_from!(try Number, u64); -impl_from!(Number, u32); -impl_from!(Number, i32); -impl_from!(String, String); -impl_from!(String, &str); - #[cfg(test)] mod tests { use super::*; diff --git a/src/lib.rs b/src/lib.rs index 5b7061a..5ecfa93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod ast; +pub mod scalar; pub mod twopass; pub mod types; diff --git a/src/scalar.rs b/src/scalar.rs new file mode 100644 index 0000000..90a30f0 --- /dev/null +++ b/src/scalar.rs @@ -0,0 +1,41 @@ +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub enum Scalar { + #[default] + Nil, + Boolean(bool), + Number(i64), + String(String), +} +macro_rules! impl_from { + ($as: ident, $from: ty) => { + impl From<$from> for Scalar { + fn from(value: $from) -> Self { + Self::$as(value.into()) + } + } + }; + (try $as: ident, $from: ty) => { + impl TryFrom<$from> for Scalar { + type Error = ::std::num::TryFromIntError; + fn try_from(value: $from) -> Result { + Ok(Self::$as(value.try_into()?)) + } + } + }; +} + +impl From<()> for Scalar { + fn from(_value: ()) -> Self { + Self::Nil + } +} + +impl_from!(Boolean, bool); +impl_from!(try Number, isize); +impl_from!(try Number, usize); +impl_from!(try Number, i64); +impl_from!(try Number, u64); +impl_from!(Number, u32); +impl_from!(Number, i32); +impl_from!(String, String); +impl_from!(String, &str); diff --git a/src/twopass.rs b/src/twopass.rs index b019793..e378154 100644 --- a/src/twopass.rs +++ b/src/twopass.rs @@ -2,6 +2,7 @@ use std::{cell::RefCell, rc::Rc}; use crate::{ ast, + scalar::Scalar, types::{self, Nil, Value}, }; @@ -47,7 +48,7 @@ enum Location { #[derive(Debug, Clone)] enum Expression { - Constant(ast::Constant), + Constant(Scalar), Variable(Location), Call(Call), Function(Function), @@ -172,7 +173,7 @@ fn lookup(scope: &BuildContext<'_>, loc: &ast::Location) -> Location { .expect("_ENV must be always available"); Location::Field { table: Box::new(Expression::Variable(Location::Variable { id: env })), - index: Box::new(Expression::Constant(ast::Constant::String(name.clone()))), + index: Box::new(Expression::Constant(Scalar::String(name.clone()))), } } ast::Location::Field { table, index } => { @@ -258,7 +259,7 @@ fn build_function(parent: &BuildContext<'_>, code: &ast::Function) -> Function { let ret = Box::new(if let Some(ret) = &code.ret { build_expression(&scope, &ret) } else { - Expression::Constant(ast::Constant::Nil) + Expression::Constant(Scalar::Nil) }); let scope = scope.scope.into_inner().finish(); Function { @@ -313,13 +314,13 @@ trait Eval { fn eval(&self, ctx: &RunContext) -> Value; } -impl Eval for ast::Constant { +impl Eval for Scalar { fn eval(&self, _ctx: &RunContext) -> Value { match self { - ast::Constant::Nil => Nil, - ast::Constant::Boolean(value) => Value::Boolean(*value), - ast::Constant::Number(value) => Value::Number(*value), - ast::Constant::String(value) => Value::String(value.clone()), + Self::Nil => Nil, + Self::Boolean(value) => Value::Boolean(*value), + Self::Number(value) => Value::Number(*value), + Self::String(value) => Value::String(value.clone()), } } }