From 186eb71a505e4f6309b3daf91354714c199ed3b5 Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Wed, 20 Oct 2021 19:57:25 +0200 Subject: [PATCH] add break to lex and create first ast --- README.md | 1 + src/ast.rs | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lex.rs | 9 +++-- src/lib.rs | 1 + 4 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/ast.rs diff --git a/README.md b/README.md index 78d61bf..25f0680 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ _ is dynamically and *strongly* typed `loop` `while` `for` +`break` #### Values `true` diff --git a/src/ast.rs b/src/ast.rs new file mode 100644 index 0000000..5087dd6 --- /dev/null +++ b/src/ast.rs @@ -0,0 +1,101 @@ +pub struct Block(pub Vec); + +pub enum Stmt { + VariableDecl(VariableDecl), + Assignment(Assignment), + FnDecl(FnDecl), + Break, + Return(Option), + Conditional(Conditional), + Loop(Block), + WhileLoop(WhileLoop), + ForLoop(Box), + Expr(Expr), +} + +pub struct VariableDecl { + name: String, + init: Option, +} + +pub struct Assignment { + pub lhs: Expr, + pub rhs: Expr, +} + +pub struct FnDecl { + pub name: String, + pub params: Vec, + pub body: Block, +} + +pub struct Conditional { + pub condition: Expr, + pub body: Block, + pub else_block: Option, +} + +pub struct WhileLoop { + pub cond: Expr, + pub body: Block, +} + +pub struct ForLoop { + pub init: Stmt, + pub cond: Expr, + pub post: Stmt, + pub body: Block, +} + +pub enum Expr { + Literal(Literal), + UnaryOp, + BinaryOp, + Call, +} + +pub enum Literal { + String(String), + Number(f64), + Array(Vec), + Object, // todo + Boolean(bool), + Null, +} + +pub struct UnaryOp { + pub expr: Expr, + pub kind: UnaryOpKind, +} + +pub enum UnaryOpKind { + Not, + Neg, +} + +pub struct BinaryOp { + pub lhs: Expr, + pub rhs: Expr, + pub kind: BinaryOpKind, +} + +pub enum BinaryOpKind { + And, + Or, + Equal, + GreaterEqual, + Greater, + LessEqual, + Less, + NotEqual, + Add, + Sub, + Mul, + Div, + Mod, +} + +pub enum Call { + Function(Expr, Vec), + Field(Expr, Vec), +} diff --git a/src/lex.rs b/src/lex.rs index 5e83902..1d82fb8 100644 --- a/src/lex.rs +++ b/src/lex.rs @@ -35,6 +35,7 @@ pub enum TokenType<'code> { Loop, While, For, + Break, True, False, Null, @@ -322,6 +323,8 @@ fn keyword_or_ident(name: &str) -> TokenType { b'e' if len == 4 && bs[1..4] == *b"lse" => TokenType::Else, // while b'w' if len == 5 && bs[1..5] == *b"hile" => TokenType::While, + // break + b'b' if len == 5 && bs[1..5] == *b"reak" => TokenType::Break, // true b't' if len == 4 && bs[1..4] == *b"rue" => TokenType::True, // null && not @@ -568,9 +571,9 @@ mod test { #[test] fn keywords() { lex_test( - "let fn if else loop while for true false null and not or", + "let fn if else loop while break for true false null and not or", vec![ - Let, Fn, If, Else, Loop, While, For, True, False, Null, And, Not, Or, + Let, Fn, If, Else, Loop, While, Break, For, True, False, Null, And, Not, Or, ], ) } @@ -611,6 +614,8 @@ mod test { "nor", "andnowQuestionMark", "notOrAnd", + "breakMe", + "Ibreak", ]; let sentences = words .iter() diff --git a/src/lib.rs b/src/lib.rs index 623b275..ca2be1e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ mod alloc; +mod ast; mod errors; mod lex; mod parse;