diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 5051a1d..cdaa12c 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -17,6 +17,27 @@ pub enum TypePostfix { RefMut, } +#[derive(Debug, Clone, Serialize)] +pub struct Attribute { + pub path: StaticPath, + pub args: Vec, +} + +#[derive(Debug, Clone, Serialize)] +pub enum AttributeArg { + Named(String, Literal), + Positional(Literal), +} + +#[derive(Debug, Clone, Serialize)] +pub enum Literal { + String(String), + Int(i64), + Float(f64), + Bool(bool), + Tuple(Vec), +} + #[derive(Debug, Clone, Serialize)] pub enum TypeExprKind { Path(StaticPath), @@ -137,12 +158,8 @@ pub struct ForStmt { #[derive(Debug, Clone, Serialize)] pub enum Expression { + Literal(Literal), Path(StaticPath), - IntLiteral(i64), - FloatLiteral(f64), - BoolLiteral(bool), - StringLiteral(String), - TupleLiteral(Vec), Fix { initial: Box, prefixes: Vec, diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 036e8cc..525280b 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -293,11 +293,19 @@ impl From> for Expression { } Rule::primary => Expression::from(inner.next().unwrap()), Rule::static_path => Expression::Path(StaticPath::from(pair)), - Rule::integer => Expression::IntLiteral(pair.as_str().parse::().unwrap()), - Rule::float => Expression::FloatLiteral(pair.as_str().parse::().unwrap()), - Rule::boolean => Expression::BoolLiteral(pair.as_str().parse::().unwrap()), - Rule::string_lit => Expression::StringLiteral(inner.as_str().to_string()), - Rule::tuple => Expression::TupleLiteral(inner.map(Expression::from).collect()), + Rule::integer => { + Expression::Literal(Literal::Int(pair.as_str().parse::().unwrap())) + } + Rule::float => { + Expression::Literal(Literal::Float(pair.as_str().parse::().unwrap())) + } + Rule::boolean => { + Expression::Literal(Literal::Bool(pair.as_str().parse::().unwrap())) + } + Rule::string_lit => Expression::Literal(Literal::String(inner.as_str().to_string())), + Rule::tuple => { + Expression::Literal(Literal::Tuple(inner.map(Expression::from).collect())) + } _ => unimplemented!("{rule:#?}"), } } diff --git a/src/codegen.rs b/src/codegen.rs index 45cfcdd..94cd68e 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,5 +1,5 @@ use parser::ast::{ - BinaryOp, Block, Expression, IfStmt, Postfix, Prefix, Statement, StaticPath, TopLevel, + BinaryOp, Block, Expression, IfStmt, Literal, Postfix, Prefix, Statement, StaticPath, TopLevel, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, WhileStmt, }; @@ -105,15 +105,14 @@ impl GetRust for TypeExprKind { } } -impl GetRust for Expression { +impl GetRust for Literal { fn get_rust(&self) -> String { match self { - Expression::Path(path) => path.get_rust(), - Expression::IntLiteral(n) => n.to_string(), - Expression::FloatLiteral(n) => format!("{n:?}"), - Expression::BoolLiteral(b) => b.to_string(), - Expression::StringLiteral(s) => format!("\"{s}\""), - Expression::TupleLiteral(t) => { + Self::Int(n) => n.to_string(), + Self::Float(n) => format!("{n:?}"), + Self::Bool(b) => b.to_string(), + Self::String(s) => format!("\"{s}\""), + Self::Tuple(t) => { format!( "({})", t.iter() @@ -122,6 +121,15 @@ impl GetRust for Expression { .join(", ") ) } + } + } +} + +impl GetRust for Expression { + fn get_rust(&self) -> String { + match self { + Expression::Path(path) => path.get_rust(), + Expression::Literal(literal) => literal.get_rust(), Expression::Fix { initial, prefixes,