blob: 627a6234277254b8dbaf70a5143fb0c5ecdf3bd4 [file] [log] [blame]
Sketch🕴️ad743922026-02-28 19:11:41 +04001package ast
2
3import "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.
8type Node interface {
9 node() // sealed marker
10}
11
12// NumberLit represents a numeric literal (e.g. 3.14).
13type NumberLit struct {
14 Value float64
15}
16
17func (*NumberLit) node() {}
18
19// BinaryExpr represents a binary operation (e.g. 1 + 2).
20type BinaryExpr struct {
21 Op token.Type // Plus, Minus, Star, Slash
22 Left Node
23 Right Node
24}
25
26func (*BinaryExpr) node() {}
Sketch🕴️5337c2b2026-02-28 20:37:00 +040027
28// Ident represents a variable reference (e.g. x, y).
29type Ident struct {
30 Name string
31}
32
33func (*Ident) node() {}
34
35// FuncCall represents a function call (e.g. f(1, 2+3)).
36type FuncCall struct {
37 Name string
38 Args []Node
39}
40
41func (*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.
46type Statement interface {
47 stmt() // sealed marker
48}
49
50// ExprStmt wraps an expression as a top-level statement.
51type ExprStmt struct {
52 Expr Node
53}
54
55func (*ExprStmt) stmt() {}
56
57// FuncDef represents a function definition (e.g. f(x, y) = x + y).
58type FuncDef struct {
59 Name string
60 Params []string
61 Body Node
62}
63
64func (*FuncDef) stmt() {}