mirror of
https://github.com/Noratrieb/dilaria.git
synced 2026-01-15 01:45:02 +01:00
add spans to AST
This commit is contained in:
parent
d848818824
commit
8f99a1d630
4 changed files with 155 additions and 61 deletions
62
src/ast.rs
62
src/ast.rs
|
|
@ -1,8 +1,9 @@
|
|||
//!
|
||||
//! 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;
|
||||
|
||||
|
|
@ -20,25 +21,28 @@ pub enum Stmt {
|
|||
If(IfStmt),
|
||||
Loop(Block),
|
||||
While(WhileStmt),
|
||||
Break,
|
||||
Break(Break),
|
||||
Return(Option<Expr>),
|
||||
Expr(Expr),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Declaration {
|
||||
pub span: Span,
|
||||
name: Symbol,
|
||||
init: Expr,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Assignment {
|
||||
pub span: Span,
|
||||
pub lhs: Symbol,
|
||||
pub rhs: Expr,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct FnDecl {
|
||||
pub span: Span,
|
||||
pub name: Symbol,
|
||||
pub params: Vec<Symbol>,
|
||||
pub body: Block,
|
||||
|
|
@ -46,6 +50,7 @@ pub struct FnDecl {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct IfStmt {
|
||||
pub span: Span,
|
||||
pub condition: Expr,
|
||||
pub body: Block,
|
||||
pub else_part: Box<ElsePart>,
|
||||
|
|
@ -59,29 +64,59 @@ pub enum ElsePart {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct WhileStmt {
|
||||
pub span: Span,
|
||||
pub cond: Expr,
|
||||
pub body: Block,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Break {
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Expr {
|
||||
Literal(Literal),
|
||||
UnaryOp,
|
||||
BinaryOp,
|
||||
UnaryOp(Box<UnaryOp>),
|
||||
BinaryOp(Box<BinaryOp>),
|
||||
}
|
||||
|
||||
impl Expr {
|
||||
pub fn span(&self) -> Span {
|
||||
match self {
|
||||
Expr::Literal(lit) => lit.span(),
|
||||
Expr::UnaryOp(unary) => unary.span,
|
||||
Expr::BinaryOp(binary) => binary.span,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Literal {
|
||||
String(String),
|
||||
Number(f64),
|
||||
Array(Vec<Expr>),
|
||||
Object,
|
||||
Boolean(bool),
|
||||
Null,
|
||||
String(String, Span),
|
||||
Number(f64, Span),
|
||||
Array(Vec<Expr>, Span),
|
||||
Object(Span),
|
||||
Boolean(bool, Span),
|
||||
Null(Span),
|
||||
}
|
||||
|
||||
impl Literal {
|
||||
pub fn span(&self) -> Span {
|
||||
match self {
|
||||
Literal::String(_, span) => *span,
|
||||
Literal::Number(_, span) => *span,
|
||||
Literal::Array(_, span) => *span,
|
||||
Literal::Object(span) => *span,
|
||||
Literal::Boolean(_, span) => *span,
|
||||
Literal::Null(span) => *span,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct UnaryOp {
|
||||
pub span: Span,
|
||||
pub expr: Expr,
|
||||
pub kind: UnaryOpKind,
|
||||
}
|
||||
|
|
@ -94,6 +129,7 @@ pub enum UnaryOpKind {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct BinaryOp {
|
||||
pub span: Span,
|
||||
pub lhs: Expr,
|
||||
pub rhs: Expr,
|
||||
pub kind: BinaryOpKind,
|
||||
|
|
@ -115,9 +151,3 @@ pub enum BinaryOpKind {
|
|||
Div,
|
||||
Mod,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Call {
|
||||
Function(Expr, Vec<Expr>),
|
||||
Field(Expr, Vec<Expr>),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue