From ebfd74ddf0ef6372624ea171e06f8460d0e1351b Mon Sep 17 00:00:00 2001 From: Cody Date: Mon, 27 Feb 2023 07:21:50 -0600 Subject: pain --- src/ast/mod.rs | 95 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 39 deletions(-) (limited to 'src/ast/mod.rs') 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), + 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, + return_type: String, + body: Vec, }, - Binary { - operation: Operation, - lhs: &'a Expression<'a>, - rhs: &'a Expression<'a>, + If { + condition: Expr, + body: Vec, + }, + For { + binding: String, + range: (Expr, Expr), + body: Vec, + }, + 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), + Binary { + operator: TokenType, + lhs: Box, + rhs: Box, + }, + Unary { + operator: TokenType, + expr: Box, + }, +} -#[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 { + fn visit_stmt(&mut self, stmt: &Stmt) -> T; + fn visit_expr(&mut self, expr: &Expr) -> T; } -- cgit v1.2.3