| 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 // ) |
| 16 | EOF // end of input |
| 17 | ) |
| 18 | |
| 19 | // String returns a human-readable name for the token type. |
| 20 | func (t Type) String() string { |
| 21 | switch t { |
| 22 | case Number: |
| 23 | return "Number" |
| 24 | case Plus: |
| 25 | return "+" |
| 26 | case Minus: |
| 27 | return "-" |
| 28 | case Star: |
| 29 | return "*" |
| 30 | case Slash: |
| 31 | return "/" |
| 32 | case LParen: |
| 33 | return "(" |
| 34 | case RParen: |
| 35 | return ")" |
| 36 | case EOF: |
| 37 | return "EOF" |
| 38 | default: |
| 39 | return fmt.Sprintf("Unknown(%d)", int(t)) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Token represents a single lexical token. |
| 44 | type Token struct { |
| 45 | Type Type // the kind of token |
| 46 | Literal string // raw text (e.g. "3.14", "+") |
| 47 | Pos int // byte offset in input string |
| 48 | } |