This commit is contained in:
nora 2021-10-31 18:30:57 +01:00
parent aa0a9cb0e5
commit 1824c9839f
4 changed files with 127 additions and 41 deletions

View file

@ -1,12 +1,17 @@
//!
//! The AST module contains all structs and enums for the abstract syntax tree generated by the parser
#![allow(dead_code)]
use crate::errors::Span;
/// imagine interning or something here
pub type Symbol = String;
#[derive(Debug, Clone, PartialEq)]
pub struct Ident {
pub name: Symbol,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Program(pub Vec<Stmt>);
@ -30,42 +35,25 @@ pub enum Stmt {
Expr(Expr),
}
impl Stmt {
pub fn span(&self) -> Span {
match self {
Stmt::Declaration(decl) => decl.span,
Stmt::Assignment(assign) => assign.span,
Stmt::FnDecl(decl) => decl.span,
Stmt::If(if_stmt) => if_stmt.span,
Stmt::Loop(_, span) => *span,
Stmt::While(while_stmt) => while_stmt.span,
Stmt::Break(span) => *span,
Stmt::Return(_, span) => *span,
Stmt::Block(block) => block.span,
Stmt::Expr(expr) => expr.span(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Declaration {
pub span: Span,
name: Symbol,
init: Expr,
pub name: Ident,
pub init: Expr,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Assignment {
pub span: Span,
pub lhs: Symbol,
pub lhs: Expr,
pub rhs: Expr,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FnDecl {
pub span: Span,
pub name: Symbol,
pub params: Vec<Symbol>,
pub name: Ident,
pub params: Vec<Ident>,
pub body: Block,
}
@ -101,7 +89,7 @@ pub struct WhileStmt {
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
Ident(Symbol, Span),
Ident(Ident),
Literal(Literal),
UnaryOp(Box<UnaryOp>),
BinaryOp(Box<BinaryOp>),
@ -113,7 +101,7 @@ impl Expr {
Expr::Literal(lit) => lit.span(),
Expr::UnaryOp(unary) => unary.span,
Expr::BinaryOp(binary) => binary.span,
Expr::Ident(_, span) => *span,
Expr::Ident(Ident { span, .. }) => *span,
}
}
}