mirror of
https://github.com/Noratrieb/dilaria.git
synced 2026-01-14 17:35:03 +01:00
more deleted
This commit is contained in:
parent
5bd2554c9f
commit
980cccb555
4 changed files with 141 additions and 228 deletions
188
src/ast.rs
188
src/ast.rs
|
|
@ -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<Expr<'ast>>, 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>]),
|
||||
}
|
||||
146
src/lib.rs
146
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<Expr<'ast>>, 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>]),
|
||||
}
|
||||
|
|
|
|||
35
src/main.rs
35
src/main.rs
|
|
@ -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: <filename>");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue