| Sketch🕴️ | d04f362 | 2026-02-28 19:09:06 +0400 | [diff] [blame] | 1 | package token |
| 2 | |
| 3 | import "fmt" |
| 4 | |
| 5 | // Type represents the type of a lexical token. |
| 6 | type Type int |
| 7 | |
| 8 | const ( |
| 9 | Number Type = iota // numeric literal |
| 10 | Plus // + |
| 11 | Minus // - |
| 12 | Star // * |
| 13 | Slash // / |
| 14 | LParen // ( |
| 15 | RParen // ) |
| Sketch🕴️ | c3ec07d | 2026-02-28 20:35:06 +0400 | [diff] [blame] | 16 | Ident // identifier (e.g. "f", "x") |
| 17 | Comma // , |
| 18 | Equals // = |
| Sketch🕴️ | d04f362 | 2026-02-28 19:09:06 +0400 | [diff] [blame] | 19 | EOF // end of input |
| 20 | ) |
| 21 | |
| 22 | // String returns a human-readable name for the token type. |
| 23 | func (t Type) String() string { |
| 24 | switch t { |
| 25 | case Number: |
| 26 | return "Number" |
| 27 | case Plus: |
| 28 | return "+" |
| 29 | case Minus: |
| 30 | return "-" |
| 31 | case Star: |
| 32 | return "*" |
| 33 | case Slash: |
| 34 | return "/" |
| 35 | case LParen: |
| 36 | return "(" |
| 37 | case RParen: |
| 38 | return ")" |
| Sketch🕴️ | c3ec07d | 2026-02-28 20:35:06 +0400 | [diff] [blame] | 39 | case Ident: |
| 40 | return "Ident" |
| 41 | case Comma: |
| 42 | return "," |
| 43 | case Equals: |
| 44 | return "=" |
| Sketch🕴️ | d04f362 | 2026-02-28 19:09:06 +0400 | [diff] [blame] | 45 | case EOF: |
| 46 | return "EOF" |
| 47 | default: |
| 48 | return fmt.Sprintf("Unknown(%d)", int(t)) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Token represents a single lexical token. |
| 53 | type Token struct { |
| 54 | Type Type // the kind of token |
| 55 | Literal string // raw text (e.g. "3.14", "+") |
| 56 | Pos int // byte offset in input string |
| 57 | } |