From 980cccb555870e59f8b5b95fafc723f47a36b882 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 23 Apr 2022 22:48:23 +0200 Subject: [PATCH] more deleted --- src/ast.rs | 188 -------------------------------------------------- src/errors.rs | 0 src/lib.rs | 146 +++++++++++++++++++++++++++++++++++++-- src/main.rs | 35 ---------- 4 files changed, 141 insertions(+), 228 deletions(-) delete mode 100644 src/ast.rs delete mode 100644 src/errors.rs delete mode 100644 src/main.rs diff --git a/src/ast.rs b/src/ast.rs deleted file mode 100644 index 38fb1c1..0000000 --- a/src/ast.rs +++ /dev/null @@ -1,188 +0,0 @@ -//! -//! The AST module contains all structs and enums for the abstract syntax tree generated by the parser -//! -//! All AST nodes are bump allocated into the lifetime `'ast` - -#[derive(dbg_pls::DebugPls)] -pub struct Span { - pub start: usize, - pub end: usize, -} - - -type Symbol = usize; - -#[derive(dbg_pls::DebugPls)] -pub struct Ident { - pub sym: Symbol, - pub span: Span, -} - -pub type Program<'ast> = Block<'ast>; - -#[derive(dbg_pls::DebugPls)] -pub struct Block<'ast> { - pub stmts: &'ast [Stmt<'ast>], - pub span: Span, -} - -#[derive(dbg_pls::DebugPls)] -pub enum Stmt<'ast> { - Declaration(Declaration<'ast>), - Assignment(Assignment<'ast>), - FnDecl(FnDecl<'ast>), - If(IfStmt<'ast>), - Loop(Block<'ast>, Span), - While(WhileStmt<'ast>), - Break(Span), - Return(Option>, Span), - Block(Block<'ast>), - Expr(Expr<'ast>), - Print(Expr<'ast>, Span), -} - -#[derive(dbg_pls::DebugPls)] -pub struct Declaration<'ast> { - pub span: Span, - pub name: Ident, - pub init: Expr<'ast>, -} - -#[derive(dbg_pls::DebugPls)] -pub struct Assignment<'ast> { - pub span: Span, - pub lhs: Expr<'ast>, - pub rhs: Expr<'ast>, -} - -#[derive(dbg_pls::DebugPls)] -pub struct FnDecl<'ast> { - pub span: Span, - pub name: Ident, - pub params: &'ast [Ident], - pub body: Block<'ast>, -} - -#[derive(dbg_pls::DebugPls)] -pub struct IfStmt<'ast> { - pub span: Span, - pub cond: Expr<'ast>, - pub body: Block<'ast>, - pub else_part: Option<&'ast ElsePart<'ast>>, -} - -#[derive(dbg_pls::DebugPls)] -pub enum ElsePart<'ast> { - Else(Block<'ast>, Span), - ElseIf(IfStmt<'ast>, Span), -} - -impl ElsePart<'_> { - pub fn span(&self) -> Span { - match self { - ElsePart::Else(_, span) | ElsePart::ElseIf(_, span) => *span, - } - } -} - -#[derive(dbg_pls::DebugPls)] -pub struct WhileStmt<'ast> { - pub span: Span, - pub cond: Expr<'ast>, - pub body: Block<'ast>, -} - -#[derive(dbg_pls::DebugPls)] -pub enum Expr<'ast> { - Ident(Ident), - Literal(Literal<'ast>), - UnaryOp(&'ast UnaryOp<'ast>), - BinaryOp(&'ast BinaryOp<'ast>), - Call(&'ast Call<'ast>), -} - -impl Expr<'_> { - pub fn span(&self) -> Span { - match self { - Expr::Literal(lit) => lit.span(), - Expr::UnaryOp(unary) => unary.span, - Expr::BinaryOp(binary) => binary.span, - Expr::Ident(Ident { span, .. }) => *span, - Expr::Call(call) => call.span, - } - } -} - -#[derive(dbg_pls::DebugPls)] -pub enum Literal<'ast> { - String(Symbol, Span), - Number(f64, Span), - Array(&'ast [Expr<'ast>], Span), - Object(Span), - Boolean(bool, Span), - Null(Span), -} - -impl Literal<'_> { - pub fn span(&self) -> Span { - match self { - Literal::String(_, span) - | Literal::Number(_, span) - | Literal::Array(_, span) - | Literal::Object(span) - | Literal::Boolean(_, span) - | Literal::Null(span) => *span, - } - } -} - -#[derive(dbg_pls::DebugPls)] -pub struct UnaryOp<'ast> { - pub span: Span, - pub expr: Expr<'ast>, - pub kind: UnaryOpKind, -} - -#[derive(dbg_pls::DebugPls)] -pub enum UnaryOpKind { - Not, - Neg, -} - -#[derive(dbg_pls::DebugPls)] -pub struct BinaryOp<'ast> { - pub span: Span, - pub lhs: Expr<'ast>, - pub rhs: Expr<'ast>, - pub kind: BinaryOpKind, -} - -#[derive(dbg_pls::DebugPls)] -pub enum BinaryOpKind { - And, - Or, - Equal, - GreaterEqual, - Greater, - LessEqual, - Less, - NotEqual, - Add, - Sub, - Mul, - Div, - Mod, -} - -#[derive(dbg_pls::DebugPls)] -pub struct Call<'ast> { - pub callee: Expr<'ast>, - pub span: Span, - pub kind: CallKind<'ast>, -} - -#[derive(dbg_pls::DebugPls)] -pub enum CallKind<'ast> { - Field(Ident), - Fn(&'ast [Expr<'ast>]), -} diff --git a/src/errors.rs b/src/errors.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib.rs b/src/lib.rs index de81dd1..5f09888 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,3 @@ -mod ast; -mod errors; - -use crate::ast::Program; - pub fn process_ast(program: &str, ast: &Program) { dbg(ast); } @@ -10,3 +5,144 @@ pub fn process_ast(program: &str, ast: &Program) { pub fn dbg(x: impl dbg_pls::DebugPls) { eprintln!("{}", dbg_pls::pretty(&x)) } + +#[derive(dbg_pls::DebugPls)] +pub struct Span { + pub start: usize, + pub end: usize, +} + +type Symbol = usize; + +#[derive(dbg_pls::DebugPls)] +pub struct Ident { + pub sym: Symbol, + pub span: Span, +} + +pub type Program<'ast> = Block<'ast>; + +#[derive(dbg_pls::DebugPls)] +pub struct Block<'ast> { + pub stmts: &'ast [Stmt<'ast>], + pub span: Span, +} + +#[derive(dbg_pls::DebugPls)] +pub enum Stmt<'ast> { + FnDecl(FnDecl<'ast>), + Loop(Block<'ast>, Span), + While(WhileStmt<'ast>), + Break(Span), + Return(Option>, Span), +} + +#[derive(dbg_pls::DebugPls)] +pub struct FnDecl<'ast> { + pub span: Span, + pub name: Ident, + pub params: &'ast [Ident], + pub body: Block<'ast>, +} + +#[derive(dbg_pls::DebugPls)] +pub struct WhileStmt<'ast> { + pub span: Span, + pub cond: Expr<'ast>, + pub body: Block<'ast>, +} + +#[derive(dbg_pls::DebugPls)] +pub enum Expr<'ast> { + Ident(Ident), + Literal(Literal<'ast>), + UnaryOp(&'ast UnaryOp<'ast>), + BinaryOp(&'ast BinaryOp<'ast>), + Call(&'ast Call<'ast>), +} + +impl Expr<'_> { + pub fn span(&self) -> Span { + match self { + Expr::Literal(lit) => lit.span(), + Expr::UnaryOp(unary) => unary.span, + Expr::BinaryOp(binary) => binary.span, + Expr::Ident(Ident { span, .. }) => *span, + Expr::Call(call) => call.span, + } + } +} + +#[derive(dbg_pls::DebugPls)] +pub enum Literal<'ast> { + String(Symbol, Span), + Number(f64, Span), + Array(&'ast [Expr<'ast>], Span), + Object(Span), + Boolean(bool, Span), + Null(Span), +} + +impl Literal<'_> { + pub fn span(&self) -> Span { + match self { + Literal::String(_, span) + | Literal::Number(_, span) + | Literal::Array(_, span) + | Literal::Object(span) + | Literal::Boolean(_, span) + | Literal::Null(span) => *span, + } + } +} + +#[derive(dbg_pls::DebugPls)] +pub struct UnaryOp<'ast> { + pub span: Span, + pub expr: Expr<'ast>, + pub kind: UnaryOpKind, +} + +#[derive(dbg_pls::DebugPls)] +pub enum UnaryOpKind { + Not, + Neg, +} + +#[derive(dbg_pls::DebugPls)] +pub struct BinaryOp<'ast> { + pub span: Span, + pub lhs: Expr<'ast>, + pub rhs: Expr<'ast>, + pub kind: BinaryOpKind, +} + +#[derive(dbg_pls::DebugPls)] +pub enum BinaryOpKind { + And, + Or, + Equal, + GreaterEqual, + Greater, + LessEqual, + Less, + NotEqual, + Add, + Sub, + Mul, + Div, + Mod, +} + +#[derive(dbg_pls::DebugPls)] +pub struct Call<'ast> { + pub callee: Expr<'ast>, + pub span: Span, + pub kind: CallKind<'ast>, +} + +#[derive(dbg_pls::DebugPls)] +pub enum CallKind<'ast> { + Field(Ident), + Fn(&'ast [Expr<'ast>]), +} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index dfe7190..0000000 --- a/src/main.rs +++ /dev/null @@ -1,35 +0,0 @@ -use dilaria::Config; -use std::io; - -fn main() { - let mut args = std::env::args(); - - if let Some(filename) = args.nth(1) { - let mut stdout = io::stdout(); - - let mut cfg = Config { - debug: false, - step: false, - stdout: &mut stdout, - }; - - for arg in args { - match &*arg { - "--debug" => cfg.debug = true, - "--step" => cfg.step = true, - _ => {} - } - } - - match std::fs::read_to_string(filename) { - Ok(contents) => { - dilaria::run_program(&contents, &mut cfg); - } - Err(err) => { - eprintln!("{}", err); - } - } - } else { - eprintln!("Usage: "); - } -}