Model ast without comments and errors

This commit is contained in:
Joscha 2022-11-17 15:21:30 +01:00
parent c593e4c872
commit 29d321334c
2 changed files with 151 additions and 0 deletions

150
src/ast.rs Normal file
View file

@ -0,0 +1,150 @@
#[derive(Debug, Clone)]
struct Ident(String);
#[derive(Debug, Clone)]
enum NumLit {
/// - `0b_0001_1011`
/// - `-0b10`
Bin(i64, String),
/// - `12_345`
/// - `-7`
Dec(i64, String),
/// - `0x_c0_f3`
/// - `-0xB`
Hex(i64, String),
}
#[derive(Debug, Clone)]
enum TableLitElem {
/// `a`
Positional(Box<Expr>),
/// `foo: a`
Named(Ident, Box<Expr>),
}
/// `'{ a, foo: b }`
#[derive(Debug, Clone)]
struct TableLit {
elems: Vec<TableLitElem>,
trailing_comma: bool,
}
#[derive(Debug, Clone)]
enum Lit {
/// `nil`
Nil,
/// - `true`
/// - `false`
Bool(bool),
/// See [`NumLit`].
Num(NumLit),
/// - `"foo"`
/// - `"Hello world!\n"`
/// - `""`
String(String),
/// See [`TableLit`].
Table(TableLit),
}
#[derive(Debug, Clone)]
enum TableConstrElem {
/// `a`
Positional(Box<Expr>),
/// `foo: a`
Named(Ident, Box<Expr>),
/// `[a]: b`
Indexed(Box<Expr>, Box<Expr>),
}
/// `{ a, b, foo: c, [d]: e }`
#[derive(Debug, Clone)]
struct TableConstr {
elems: Vec<TableConstrElem>,
trailing_comma: bool,
}
#[derive(Debug, Clone, Copy)]
enum BinOp {
/// `+`
Add,
/// `-`
Sub,
/// `*`
Mul,
/// `/`
Div,
/// `%`
Mod,
/// `==`
Eq,
/// `!=`
Neq,
/// `and`
And,
/// `or`
Or,
}
#[derive(Debug, Clone)]
enum Expr {
Lit(Lit),
/// See [`TableConstr`].
TableConstr(TableConstr),
/// `foo`
Var(Ident),
/// `[a]`
VarExpr(Box<Expr>),
/// `foo = a`
VarAssign(Ident, Box<Expr>),
/// `[a] = b`
VarExprAssign(Box<Expr>, Box<Expr>),
/// `-a`
Neg(Box<Expr>),
/// `not a`
Not(Box<Expr>),
/// `a.foo`
Field(Box<Expr>, Ident),
/// `a[b]`
FieldExpr(Box<Expr>, Box<Expr>),
/// `a.foo = b`
FieldAssign(Box<Expr>, Ident, Box<Expr>),
/// `a[b] = c`
FieldExprAssign(Box<Expr>, Box<Expr>, Box<Expr>),
/// - `a + b`
/// - `a == b`
/// - `a and b`
BinOp(Box<Expr>, BinOp, Box<Expr>),
}
#[derive(Debug, Clone)]
enum Program {
/// At the beginning of the file:
/// ```text
/// module
/// ...
/// ```
Module(TableLit),
Expr(Expr),
}

View file

@ -4,6 +4,7 @@ use std::path::PathBuf;
use chumsky::Parser as _; use chumsky::Parser as _;
use clap::Parser; use clap::Parser;
mod ast;
mod builtin; mod builtin;
mod parser; mod parser;
mod table; mod table;