diff options
Diffstat (limited to 'sloth/src/parser')
| -rw-r--r-- | sloth/src/parser/ast.rs | 56 | ||||
| -rw-r--r-- | sloth/src/parser/expr.rs | 6 | ||||
| -rw-r--r-- | sloth/src/parser/graph.rs | 40 | ||||
| -rw-r--r-- | sloth/src/parser/mod.rs | 4 | ||||
| -rw-r--r-- | sloth/src/parser/stmt.rs | 42 |
5 files changed, 67 insertions, 81 deletions
diff --git a/sloth/src/parser/ast.rs b/sloth/src/parser/ast.rs index 2385443..2c70156 100644 --- a/sloth/src/parser/ast.rs +++ b/sloth/src/parser/ast.rs @@ -1,32 +1,6 @@ use crate::lexer::{self, TokenType}; use crate::parser::ParsingError; -// #[derive(PartialEq, Clone, Debug)] -// pub struct Node { -// id: i32, -// kind: NodeKind, -// } -// -// impl Node { -// pub fn new(id: i32, kind: NodeKind) -> Self { -// Self { id, kind } -// } -// } -// -// #[derive(PartialEq, Clone, Debug)] -// pub enum NodeKind { -// Expr(Expr), -// Stmt(Stmt), -// } -// -// impl From<Expr> for NodeKind { -// fn from(value: Expr) -> Self { -// todo!() -// } -// } -// -// impl From<Stmt> for NodeKind {} - #[derive(PartialEq, Clone, Debug)] pub struct Expr { pub id: i32, @@ -60,25 +34,31 @@ pub enum ExprKind { } #[derive(PartialEq, Clone, Debug)] -pub struct FunctionInput { - pub identifier: String, - pub typ: String, +pub struct Stmt { + pub id: i32, + pub kind: StmtKind, +} + +impl Stmt { + pub fn new(id: i32, kind: StmtKind) -> Self { + Self { id, kind } + } } // TODO: For loops // TODO: Values & Constants #[derive(PartialEq, Clone, Debug)] -pub enum Stmt { - Block(Vec<Stmt>), +pub enum StmtKind { + Block(Vec<StmtKind>), ExprStmt(Expr), IfStmt { condition: Expr, - if_then: Box<Stmt>, - else_then: Option<Box<Stmt>>, + if_then: Box<StmtKind>, + else_then: Option<Box<StmtKind>>, }, WhileStmt { condition: Expr, - body: Box<Stmt>, + body: Box<StmtKind>, }, DefineVariable { identifier: String, @@ -96,11 +76,17 @@ pub enum Stmt { identifier: String, inputs: Vec<FunctionInput>, output: Option<String>, - body: Box<Stmt>, + body: Box<StmtKind>, }, Return(Expr), } +#[derive(PartialEq, Clone, Debug)] +pub struct FunctionInput { + pub identifier: String, + pub typ: String, +} + #[derive(Debug, Clone, PartialEq)] pub enum Literal { Integer(i64), diff --git a/sloth/src/parser/expr.rs b/sloth/src/parser/expr.rs index 4783fc3..ff662cc 100644 --- a/sloth/src/parser/expr.rs +++ b/sloth/src/parser/expr.rs @@ -1,4 +1,4 @@ -use super::ast::{Expr, Literal, UnaryOp}; +use super::ast::{Expr, UnaryOp}; use super::AstParser; use crate::lexer::TokenType; use crate::parser::ast::{BinaryOp, ExprKind}; @@ -88,9 +88,9 @@ impl<'a> AstParser<'a> { mod tests { use itertools::Itertools; - use super::{AstParser, BinaryOp, ExprKind, Literal}; use crate::lexer::Lexer; - use crate::parser::ast::Expr; + use crate::parser::ast::{BinaryOp, Expr, ExprKind, Literal}; + use crate::AstParser; #[test] fn basic_expression() { diff --git a/sloth/src/parser/graph.rs b/sloth/src/parser/graph.rs index b35be05..b2a9324 100644 --- a/sloth/src/parser/graph.rs +++ b/sloth/src/parser/graph.rs @@ -1,6 +1,6 @@ use std::fmt::{Error, Write}; -use super::ast::{ExprKind, Stmt}; +use super::ast::{ExprKind, StmtKind}; pub struct GraphBuilder { i: i32, @@ -8,7 +8,7 @@ pub struct GraphBuilder { } impl GraphBuilder { - pub fn generate(ast: &[Stmt]) -> Result<String, Error> { + pub fn generate(ast: &[StmtKind]) -> Result<String, Error> { let mut this = Self { i: 0, graph: String::new(), @@ -27,17 +27,17 @@ impl GraphBuilder { Ok(this.graph) } - fn traverse_stmt0(&mut self, stmt: &Stmt) -> Result<(), Error> { + fn traverse_stmt0(&mut self, stmt: &StmtKind) -> Result<(), Error> { self.i += 1; match stmt { - Stmt::Block(body) => { + StmtKind::Block(body) => { writeln!(&mut self.graph, "N{} [shape=box label=\"Block\"];", self.i)?; for stmt in body.iter() { self.traverse_stmt0(stmt)?; } } - Stmt::ExprStmt(expr) => { + StmtKind::ExprStmt(expr) => { writeln!( &mut self.graph, "N{} [shape=box label=\"ExprStmt\"];", @@ -45,7 +45,7 @@ impl GraphBuilder { )?; // self.traverse_expr0(expr); } - Stmt::IfStmt { + StmtKind::IfStmt { condition, if_then, else_then, @@ -57,7 +57,7 @@ impl GraphBuilder { self.traverse_stmt0(else_then)?; } } - Stmt::WhileStmt { condition, body } => { + StmtKind::WhileStmt { condition, body } => { writeln!( &mut self.graph, "N{} [shape=box label=\"WhileStmt\"];", @@ -66,7 +66,7 @@ impl GraphBuilder { // self.traverse_expr0(condition); self.traverse_stmt0(body)?; } - Stmt::DefineVariable { + StmtKind::DefineVariable { identifier, value, typ, @@ -78,7 +78,7 @@ impl GraphBuilder { )?; // self.traverse_expr0(value); } - Stmt::AssignVariable { identifier, value } => { + StmtKind::AssignVariable { identifier, value } => { writeln!( &mut self.graph, "N{} [shape=box label=\"AssignVariable\\n\\nIdentifier={}\"];", @@ -86,7 +86,7 @@ impl GraphBuilder { )?; // self.traverse_expr0(value); } - Stmt::DefineFunction { + StmtKind::DefineFunction { identifier, inputs, output, @@ -103,7 +103,7 @@ impl GraphBuilder { )?; self.traverse_stmt0(body)?; } - Stmt::Return(expr) => { + StmtKind::Return(expr) => { writeln!(&mut self.graph, "N{} [shape=box label=\"Return\"];", self.i)?; // self.traverse_expr0(expr); } @@ -124,31 +124,31 @@ impl GraphBuilder { // } } - fn traverse_stmt(&mut self, stmt: &Stmt) -> Result<(), Error> { + fn traverse_stmt(&mut self, stmt: &StmtKind) -> Result<(), Error> { self.i += 1; match stmt { - Stmt::Block(_) => todo!(), - Stmt::ExprStmt(_) => todo!(), - Stmt::IfStmt { + StmtKind::Block(_) => todo!(), + StmtKind::ExprStmt(_) => todo!(), + StmtKind::IfStmt { condition, if_then, else_then, } => todo!(), - Stmt::WhileStmt { condition, body } => todo!(), - Stmt::DefineVariable { + StmtKind::WhileStmt { condition, body } => todo!(), + StmtKind::DefineVariable { identifier, value, typ, } => todo!(), - Stmt::AssignVariable { identifier, value } => todo!(), - Stmt::DefineFunction { + StmtKind::AssignVariable { identifier, value } => todo!(), + StmtKind::DefineFunction { identifier, inputs, output, body, } => todo!(), - Stmt::Return(_) => todo!(), + StmtKind::Return(_) => todo!(), } Ok(()) diff --git a/sloth/src/parser/mod.rs b/sloth/src/parser/mod.rs index bc47ddd..958166a 100644 --- a/sloth/src/parser/mod.rs +++ b/sloth/src/parser/mod.rs @@ -3,7 +3,7 @@ pub mod expr; pub mod graph; pub mod stmt; -use self::ast::{Literal, Stmt}; +use self::ast::{Literal, StmtKind}; use crate::lexer::{Token, TokenType}; #[derive(thiserror::Error, Debug, PartialEq)] @@ -22,7 +22,7 @@ pub struct AstParser<'a> { } impl<'a> AstParser<'a> { - pub fn parse(tokens: Vec<Token<'a>>) -> Result<Vec<Stmt>, ParsingError> { + pub fn parse(tokens: Vec<Token<'a>>) -> Result<Vec<StmtKind>, ParsingError> { let mut parser = Self::new(tokens); let mut statements = Vec::new(); diff --git a/sloth/src/parser/stmt.rs b/sloth/src/parser/stmt.rs index aaa65ff..f9980e3 100644 --- a/sloth/src/parser/stmt.rs +++ b/sloth/src/parser/stmt.rs @@ -1,9 +1,9 @@ -use super::ast::{FunctionInput, Stmt}; +use super::ast::{FunctionInput, StmtKind}; use super::{AstParser, ParsingError}; use crate::lexer::TokenType; impl<'a> AstParser<'a> { - pub(super) fn statement(&mut self) -> Result<Stmt, ParsingError> { + pub(super) fn statement(&mut self) -> Result<StmtKind, ParsingError> { match self.peek().tt { TokenType::OpeningBrace => self.block(), @@ -18,7 +18,7 @@ impl<'a> AstParser<'a> { } } - fn if_stmt(&mut self) -> Result<Stmt, ParsingError> { + fn if_stmt(&mut self) -> Result<StmtKind, ParsingError> { // Consume the if token self.consume(TokenType::If, "Expected if")?; @@ -36,28 +36,28 @@ impl<'a> AstParser<'a> { } } - Ok(Stmt::IfStmt { + Ok(StmtKind::IfStmt { condition, if_then: if_then.into(), else_then: else_then.map(|it| it.into()), }) } - fn while_stmt(&mut self) -> Result<Stmt, ParsingError> { + fn while_stmt(&mut self) -> Result<StmtKind, ParsingError> { // Consume the while token self.consume(TokenType::While, "Expected while")?; let condition = self.expression()?; let body = self.block()?; - Ok(Stmt::WhileStmt { + Ok(StmtKind::WhileStmt { condition, body: body.into(), }) } // TODO: Make variable types optional - fn define_variable(&mut self) -> Result<Stmt, ParsingError> { + fn define_variable(&mut self) -> Result<StmtKind, ParsingError> { // Consume the var token self.consume(TokenType::Var, "Expected var")?; @@ -72,7 +72,7 @@ impl<'a> AstParser<'a> { self.consume(TokenType::SemiColon, "Expected ';' at end of statement")?; - Ok(Stmt::DefineVariable { + Ok(StmtKind::DefineVariable { identifier, value, typ, @@ -80,7 +80,7 @@ impl<'a> AstParser<'a> { } // TODO: Make argument types optional - fn define_function(&mut self) -> Result<Stmt, ParsingError> { + fn define_function(&mut self) -> Result<StmtKind, ParsingError> { // Consume the fn token self.consume(TokenType::Fn, "Expected fn")?; @@ -113,7 +113,7 @@ impl<'a> AstParser<'a> { // Get the function body let body = self.block()?; - Ok(Stmt::DefineFunction { + Ok(StmtKind::DefineFunction { identifier, inputs, output, @@ -121,28 +121,28 @@ impl<'a> AstParser<'a> { }) } - fn return_stmt(&mut self) -> Result<Stmt, ParsingError> { + fn return_stmt(&mut self) -> Result<StmtKind, ParsingError> { self.consume(TokenType::Return, "Expected return")?; let value = self.expression()?; self.consume(TokenType::SemiColon, "Expected ';' at end of statement")?; - Ok(Stmt::Return(value)) + Ok(StmtKind::Return(value)) } - fn assign_variable(&mut self) -> Result<Stmt, ParsingError> { + fn assign_variable(&mut self) -> Result<StmtKind, ParsingError> { let identifier = self.consume_identifier()?; self.consume(TokenType::Eq, "Expected '='")?; let value = self.expression()?; self.consume(TokenType::SemiColon, "Expected ';' at end of statement")?; - Ok(Stmt::AssignVariable { identifier, value }) + Ok(StmtKind::AssignVariable { identifier, value }) } - fn expression_stmt(&mut self) -> Result<Stmt, ParsingError> { + fn expression_stmt(&mut self) -> Result<StmtKind, ParsingError> { let expr = self.expression()?; self.consume(TokenType::SemiColon, "Expected ';' at end of statement")?; - Ok(Stmt::ExprStmt(expr)) + Ok(StmtKind::ExprStmt(expr)) } - fn block(&mut self) -> Result<Stmt, ParsingError> { + fn block(&mut self) -> Result<StmtKind, ParsingError> { // Consume the opening brace self.consume(TokenType::OpeningBrace, "Expected '{'")?; @@ -155,7 +155,7 @@ impl<'a> AstParser<'a> { // Consume the closing brace self.consume(TokenType::ClosingBrace, "Expected '}'")?; - Ok(Stmt::Block(body)) + Ok(StmtKind::Block(body)) } } @@ -163,7 +163,7 @@ impl<'a> AstParser<'a> { mod tests { use itertools::Itertools; - use super::{AstParser, Stmt}; + use super::{AstParser, StmtKind}; use crate::lexer::Lexer; use crate::parser::ast::{BinaryOp, ExprKind, FunctionInput, Literal}; @@ -234,7 +234,7 @@ mod tests { // value: ExprKind::BinaryOp { // op: BinaryOp::Add, // lhs: - // Box::new(ExprKind::Identifier("bar".to_owned())), + // Box::new(ExprKind::Identifier("bar".to_owned())), // rhs: Box::new(Literal::Integer(1).into()), }, // typ: "Int".to_owned(), // }, @@ -243,7 +243,7 @@ mod tests { // value: ExprKind::BinaryOp { // op: BinaryOp::Add, // lhs: - // Box::new(ExprKind::Identifier("baz".to_owned())), + // Box::new(ExprKind::Identifier("baz".to_owned())), // rhs: Box::new(Literal::Integer(1).into()), }, // }, // Stmt::Return(ExprKind::Identifier("baz".to_owned())), |
