From 29d321334c6d76f69c75475a998746dbc46370e2 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 17 Nov 2022 15:21:30 +0100 Subject: [PATCH] Model ast without comments and errors --- src/ast.rs | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 2 files changed, 151 insertions(+) create mode 100644 src/ast.rs diff --git a/src/ast.rs b/src/ast.rs new file mode 100644 index 0000000..ff34f7f --- /dev/null +++ b/src/ast.rs @@ -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), + + /// `foo: a` + Named(Ident, Box), +} + +/// `'{ a, foo: b }` +#[derive(Debug, Clone)] +struct TableLit { + elems: Vec, + 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), + + /// `foo: a` + Named(Ident, Box), + + /// `[a]: b` + Indexed(Box, Box), +} + +/// `{ a, b, foo: c, [d]: e }` +#[derive(Debug, Clone)] +struct TableConstr { + elems: Vec, + 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), + + /// `foo = a` + VarAssign(Ident, Box), + + /// `[a] = b` + VarExprAssign(Box, Box), + + /// `-a` + Neg(Box), + + /// `not a` + Not(Box), + + /// `a.foo` + Field(Box, Ident), + + /// `a[b]` + FieldExpr(Box, Box), + + /// `a.foo = b` + FieldAssign(Box, Ident, Box), + + /// `a[b] = c` + FieldExprAssign(Box, Box, Box), + + /// - `a + b` + /// - `a == b` + /// - `a and b` + BinOp(Box, BinOp, Box), +} + +#[derive(Debug, Clone)] +enum Program { + /// At the beginning of the file: + /// ```text + /// module + /// ... + /// ``` + Module(TableLit), + + Expr(Expr), +} diff --git a/src/main.rs b/src/main.rs index 795507f..61535c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use std::path::PathBuf; use chumsky::Parser as _; use clap::Parser; +mod ast; mod builtin; mod parser; mod table;