From 6fd95f3cc2c487b361b76bd10878c1a0b875a732 Mon Sep 17 00:00:00 2001 From: Erin Date: Sun, 6 Jun 2021 21:09:18 +0200 Subject: [PATCH] Updated AST --- src/ast.rs | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/ast.rs diff --git a/src/ast.rs b/src/ast.rs new file mode 100644 index 00000000..434a246a --- /dev/null +++ b/src/ast.rs @@ -0,0 +1,94 @@ +use crate::variables::Value; + +type Span = std::ops::Range; + +#[derive(Debug)] +pub struct Iden(String); + +#[derive(Debug)] +pub struct Block { + pub block: Vec, +} + +/// A syntactic unit expressing an effect. +#[derive(Debug)] +pub struct Stmt { + pub kind: StmtKind, + pub span: Span, +} + +#[derive(Debug)] +pub enum StmtKind { + // Control flow + If { + cond: Expr, + body: Block, + }, + Loop { + body: Block, + }, + Break, + HopBack, + + Var { + iden: Iden, + init: Option, + }, + + Functio { + iden: Iden, + args: Vec, + body: Block, + }, + Call { + iden: Iden, + args: Vec, + }, + Print(Expr), + Melo(Iden), +} + +impl Stmt { + pub fn new(kind: StmtKind, span: Span) -> Self { + Self { kind, span } + } +} + +/// Expression is parse unit which do not cause any effect, +/// like math and logical operations or values. +#[derive(Debug)] +pub struct Expr { + kind: ExprKind, + span: Span, +} + +#[derive(Debug)] +pub enum ExprKind { + Binary { + lhs: Box, + rhs: Box, + kind: BinOpKind, + }, + Not(Box), + Literal(Value), +} + +impl Expr { + pub fn new(kind: ExprKind, span: Span) -> Self { + Self { kind, span } + } +} + +#[derive(Debug)] +pub enum BinOpKind { + Add, + Subtract, + Multiply, + Divide, + Greater, + Less, + Equal, + NotEqual, + And, + Or, +}