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 | |
| parent | b856e5c7226e71e500be5eff7079c5d3b0e9e2f4 (diff) | |
| download | sloth-a653a6602fe5ae5eb4739755db7b34bc92ecdadf.tar.gz | |
More stuffs
Diffstat (limited to 'src')
| -rw-r--r-- | src/ast.rs | 0 | ||||
| -rw-r--r-- | src/ast/display.rs | 45 | ||||
| -rw-r--r-- | src/ast/evaluate.rs | 5 | ||||
| -rw-r--r-- | src/ast/mod.rs | 55 | ||||
| -rw-r--r-- | src/lexer.rs | 10 | ||||
| -rw-r--r-- | src/main.rs | 18 | 
6 files changed, 132 insertions, 1 deletions
| diff --git a/src/ast.rs b/src/ast.rs deleted file mode 100644 index e69de29..0000000 --- a/src/ast.rs +++ /dev/null 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, +    }; +} diff --git a/src/lexer.rs b/src/lexer.rs index 74a006c..27669cb 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -3,6 +3,7 @@  #[derive(Debug, Eq, PartialEq)]  pub enum TokenType {      // Utility +    DocComment(String),      Comment(String),      // Short @@ -156,6 +157,15 @@ impl<'a> Iterator for Lexer<'a> {          let tt = match character {              // Whitespace & Comments +            '#' if self.advance_if_eq(Some('#')) => { +                let mut value = String::new(); +                while self.peek() != Some('\n') { +                    value.push(self.advance().unwrap()); +                } + +                TokenType::DocComment(value) +            } +              '#' => {                  let mut value = String::new();                  while self.peek() != Some('\n') { diff --git a/src/main.rs b/src/main.rs index 8f3c001..f064d39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,13 +10,16 @@  pub mod ast;  pub mod lexer; +use ast::{Expression, Operation, Value};  use lexer::Lexer; +use crate::ast::Statement; +  const SOURCE: &str = r#"  val variable = 5; -if variable >= 7 { +if variable <= 7 {      print "Hello World";  } @@ -27,4 +30,17 @@ fn main() {      for token in lexer {          print!("{} ", token.lexeme);      } + +    println!("-------"); + +    let a = Expression::Literal(Value(7)); +    let b = Expression::Binary { +        operation: Operation::Add, +        lhs: &Expression::Literal(Value(5)), +        rhs: &a, +    }; + +    let stmt = Statement::Expression { expr: &b }; + +    println!("{stmt}");  } | 
