diff options
| author | Cody <cody@codyq.dev> | 2023-02-27 07:21:50 -0600 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-02-27 07:21:50 -0600 |
| commit | ebfd74ddf0ef6372624ea171e06f8460d0e1351b (patch) | |
| tree | d59df0e9600a6f2b86b57949048cf990a3e102ae /src/ast/mod.rs | |
| parent | a653a6602fe5ae5eb4739755db7b34bc92ecdadf (diff) | |
| download | sloth-ebfd74ddf0ef6372624ea171e06f8460d0e1351b.tar.gz | |
pain
Diffstat (limited to 'src/ast/mod.rs')
| -rw-r--r-- | src/ast/mod.rs | 95 |
1 files changed, 56 insertions, 39 deletions
diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 73e73b3..04b1d26 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1,55 +1,72 @@ -#![allow(dead_code)] +use std::fmt::Display; -pub mod display; +use crate::lexer::{Literal, TokenType}; -use crate::lexer::Token; +pub mod parser; +pub mod printer; -#[derive(Clone)] -pub enum Statement<'a> { +#[derive(Debug, Eq, PartialEq)] +pub enum Stmt { + Block(Vec<Stmt>), + Expr(Expr), Val { - identifier: &'a Token<'a>, - initializer: &'a Expression<'a>, + ident: String, + value: Expr, }, Var { - identifier: &'a Token<'a>, - initializer: &'a Expression<'a>, + ident: String, + value: Expr, }, - Expression { - expr: &'a Expression<'a>, + Assignment { + ident: String, + value: Expr, }, -} - -#[derive(Clone)] -pub enum Expression<'a> { - // Basic - Literal(Value), - Unary { - operation: Operation, - expr: &'a Expression<'a>, + Function { + ident: String, + arguments: Vec<FunctionArgument>, + return_type: String, + body: Vec<Stmt>, }, - Binary { - operation: Operation, - lhs: &'a Expression<'a>, - rhs: &'a Expression<'a>, + If { + condition: Expr, + body: Vec<Stmt>, + }, + For { + binding: String, + range: (Expr, Expr), + body: Vec<Stmt>, + }, + Return { + value: Expr, + }, + Print { + value: Expr, }, - // Grouping } -#[derive(Clone)] -pub enum Operation { - Add, - Subtract, +#[derive(Debug, Eq, PartialEq)] +pub struct FunctionArgument { + name: String, + types: String, } -#[derive(Clone)] -pub struct Value(pub i32); +#[derive(Debug, Eq, PartialEq)] +pub enum Expr { + Literal(Literal), + Variable(String), + Grouping(Box<Expr>), + Binary { + operator: TokenType, + lhs: Box<Expr>, + rhs: Box<Expr>, + }, + Unary { + operator: TokenType, + expr: Box<Expr>, + }, +} -#[test] -fn test() { - let right = Expression::Literal(Value(7)); - let _ = Expression::Binary { - operation: Operation::Add, - lhs: &Expression::Literal(Value(5)), - rhs: &right, - }; +pub trait AstVisitor<T = ()> { + fn visit_stmt(&mut self, stmt: &Stmt) -> T; + fn visit_expr(&mut self, expr: &Expr) -> T; } |
