dash_lang/ast.rs
1use std::collections::HashMap;
2
3/// Stores the runtime context for the interpreter, including variables and user-defined functions.
4#[derive(Default)]
5pub struct Context {
6 /// A map of variable names to their string values.
7 pub variables: HashMap<String, String>,
8 /// A map of function names to their parameter list and body.
9 pub functions: HashMap<String, (Vec<String>, Vec<Stmt>)>,
10}
11
12/// Represents an expression in the language.
13#[derive(Debug, Clone)]
14pub enum Expr {
15 /// An integer literal.
16 Int(i64),
17 /// A string literal.
18 Str(String),
19 /// A variable reference.
20 Var(String),
21 /// A function call with arguments.
22 Call(String, Vec<Expr>),
23 /// A binary operation (e.g., addition, comparison).
24 Binary(Box<Expr>, Op, Box<Expr>),
25}
26
27/// Represents a statement in the language.
28#[derive(Debug, Clone)]
29pub enum Stmt {
30 /// Prints the result of an expression.
31 Print(Expr),
32 /// Declares or updates a variable.
33 Let(String, Expr),
34 /// Conditional execution.
35 If {
36 condition: Expr,
37 then_branch: Vec<Stmt>,
38 else_branch: Option<Vec<Stmt>>,
39 },
40 /// Looping construct.
41 While {
42 condition: Expr,
43 body: Vec<Stmt>,
44 },
45 /// Exits a loop early.
46 Break,
47 /// Skips to the next loop iteration.
48 Continue,
49 /// Defines a function.
50 Fn {
51 name: String,
52 params: Vec<String>,
53 body: Vec<Stmt>,
54 },
55 /// Calls a function as a statement.
56 Call(String, Vec<Expr>),
57 /// Returns a value from a function.
58 Return(Expr),
59}
60
61/// Supported binary operators.
62#[derive(Debug, Clone)]
63pub enum Op {
64 Add,
65 Sub,
66 Mul,
67 Div,
68 Greater,
69 Less,
70 GreaterEq,
71 LessEq,
72 Equal,
73 NotEqual,
74}
75
76/// Internal control flow used during execution.
77pub enum LoopControl {
78 None,
79 Break,
80 Continue,
81 Return(String),
82}