| Sketch🕴️ | ad74392 | 2026-02-28 19:11:41 +0400 | [diff] [blame] | 1 | package ast |
| 2 | |
| 3 | import "matheval/token" |
| 4 | |
| 5 | // Node is the interface all AST nodes implement. |
| 6 | // The unexported marker method seals the interface — only types |
| 7 | // in this package can implement it. |
| 8 | type Node interface { |
| 9 | node() // sealed marker |
| 10 | } |
| 11 | |
| 12 | // NumberLit represents a numeric literal (e.g. 3.14). |
| 13 | type NumberLit struct { |
| 14 | Value float64 |
| 15 | } |
| 16 | |
| 17 | func (*NumberLit) node() {} |
| 18 | |
| 19 | // BinaryExpr represents a binary operation (e.g. 1 + 2). |
| 20 | type BinaryExpr struct { |
| 21 | Op token.Type // Plus, Minus, Star, Slash |
| 22 | Left Node |
| 23 | Right Node |
| 24 | } |
| 25 | |
| 26 | func (*BinaryExpr) node() {} |
| Sketch🕴️ | 5337c2b | 2026-02-28 20:37:00 +0400 | [diff] [blame] | 27 | |
| 28 | // Ident represents a variable reference (e.g. x, y). |
| 29 | type Ident struct { |
| 30 | Name string |
| 31 | } |
| 32 | |
| 33 | func (*Ident) node() {} |
| 34 | |
| 35 | // FuncCall represents a function call (e.g. f(1, 2+3)). |
| 36 | type FuncCall struct { |
| 37 | Name string |
| 38 | Args []Node |
| 39 | } |
| 40 | |
| 41 | func (*FuncCall) node() {} |
| 42 | |
| 43 | // Statement is the interface for top-level REPL input. |
| 44 | // A line is either an expression to evaluate or a function definition. |
| 45 | // The unexported marker method seals the interface. |
| 46 | type Statement interface { |
| 47 | stmt() // sealed marker |
| 48 | } |
| 49 | |
| 50 | // ExprStmt wraps an expression as a top-level statement. |
| 51 | type ExprStmt struct { |
| 52 | Expr Node |
| 53 | } |
| 54 | |
| 55 | func (*ExprStmt) stmt() {} |
| 56 | |
| 57 | // FuncDef represents a function definition (e.g. f(x, y) = x + y). |
| 58 | type FuncDef struct { |
| 59 | Name string |
| 60 | Params []string |
| 61 | Body Node |
| 62 | } |
| 63 | |
| 64 | func (*FuncDef) stmt() {} |