diff options
| author | Cody <cody@codyq.dev> | 2023-01-29 20:58:04 -0600 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-01-29 20:58:04 -0600 |
| commit | a653a6602fe5ae5eb4739755db7b34bc92ecdadf (patch) | |
| tree | 2d99c570ccd77767e1d5fcf9b098d45c79cbe6e4 /src/ast | |
| parent | b856e5c7226e71e500be5eff7079c5d3b0e9e2f4 (diff) | |
| download | sloth-a653a6602fe5ae5eb4739755db7b34bc92ecdadf.tar.gz | |
More stuffs
Diffstat (limited to 'src/ast')
| -rw-r--r-- | src/ast/display.rs | 45 | ||||
| -rw-r--r-- | src/ast/evaluate.rs | 5 | ||||
| -rw-r--r-- | src/ast/mod.rs | 55 |
3 files changed, 105 insertions, 0 deletions
diff --git a/src/ast/display.rs b/src/ast/display.rs new file mode 100644 index 0000000..25b4d23 --- /dev/null +++ b/src/ast/display.rs @@ -0,0 +1,45 @@ +use std::fmt::Display; + +use super::{Expression, Statement, Value}; + +impl<'a> Display for Statement<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{{")?; + let value = match self { + Statement::Val { + identifier, + initializer, + } => format!("val {} {}", identifier.lexeme, initializer), + Statement::Var { + identifier, + initializer, + } => format!("var {} {}", identifier.lexeme, initializer), + Statement::Expression { expr } => expr.to_string(), + }; + write!(f, "{value}")?; + write!(f, "}}")?; + + Ok(()) + } +} + +impl<'a> Display for Expression<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "(")?; + let value = match self { + Expression::Literal(value) => value.0.to_string(), + Expression::Unary { expr, .. } => format!("+ {}", expr), + Expression::Binary { lhs, rhs, .. } => format!("+ {lhs} {rhs}"), + }; + write!(f, "{value}")?; + write!(f, ")")?; + + Ok(()) + } +} + +impl Display for Value { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} diff --git a/src/ast/evaluate.rs b/src/ast/evaluate.rs new file mode 100644 index 0000000..4d47994 --- /dev/null +++ b/src/ast/evaluate.rs @@ -0,0 +1,5 @@ +impl<'a> Expression<'a> { + fn evaluate() -> Value { + Value(5) + } +} diff --git a/src/ast/mod.rs b/src/ast/mod.rs new file mode 100644 index 0000000..73e73b3 --- /dev/null +++ b/src/ast/mod.rs @@ -0,0 +1,55 @@ +#![allow(dead_code)] + +pub mod display; + +use crate::lexer::Token; + +#[derive(Clone)] +pub enum Statement<'a> { + Val { + identifier: &'a Token<'a>, + initializer: &'a Expression<'a>, + }, + Var { + identifier: &'a Token<'a>, + initializer: &'a Expression<'a>, + }, + Expression { + expr: &'a Expression<'a>, + }, +} + +#[derive(Clone)] +pub enum Expression<'a> { + // Basic + Literal(Value), + Unary { + operation: Operation, + expr: &'a Expression<'a>, + }, + Binary { + operation: Operation, + lhs: &'a Expression<'a>, + rhs: &'a Expression<'a>, + }, + // Grouping +} + +#[derive(Clone)] +pub enum Operation { + Add, + Subtract, +} + +#[derive(Clone)] +pub struct Value(pub i32); + +#[test] +fn test() { + let right = Expression::Literal(Value(7)); + let _ = Expression::Binary { + operation: Operation::Add, + lhs: &Expression::Literal(Value(5)), + rhs: &right, + }; +} |
