78 lines
1.6 KiB
Markdown
78 lines
1.6 KiB
Markdown
---
|
|
Title: 2 Lexical Structure
|
|
Prev: Overview
|
|
Next: Primitive Types
|
|
---
|
|
|
|
## 2. Lexical Structure
|
|
|
|
### 2.1 Comments
|
|
```
|
|
// Single-line comment
|
|
```
|
|
|
|
### 2.2 Identifiers
|
|
|
|
**Regular Identifiers**
|
|
- Start with letter or underscore: `[a-zA-Z_][a-zA-Z0-9_]*`
|
|
- Case-sensitive
|
|
- When encountered, identifiers are executed immediately
|
|
|
|
**Identifier Literals**
|
|
- Prefix with `::` to push the identifier itself onto the stack instead of executing it
|
|
- Syntax: `::name` pushes the identifier `name` as a value
|
|
- Example: `::Addable` pushes the identifier `Addable` onto the stack
|
|
- Example: `::Point` pushes the identifier `Point` onto the stack
|
|
|
|
> **Advanced Usage**: See [Section 11.2](./advanced_topics.html#112-identifier-literals) for complete identifier literal rules and context-dependent behavior.
|
|
|
|
### 2.3 Literals
|
|
|
|
**Integer Literals**
|
|
|
|
```
|
|
42 // i64 (default)
|
|
42:i32 // Annotate as i32
|
|
0xFF // hexadecimal
|
|
0b1010 // binary
|
|
```
|
|
|
|
**Floating Point Literals**
|
|
```
|
|
3.14 // f64 (default)
|
|
3.14:f32 // Annotate as f32
|
|
```
|
|
|
|
**String Literals**
|
|
```
|
|
"hello world"
|
|
"escape sequences: \n \t \\ \""
|
|
```
|
|
|
|
**Escape Sequences**
|
|
- `\n` - Newline (LF)
|
|
- `\r` - Carriage return (CR)
|
|
- `\t` - Tab
|
|
- `\\` - Backslash
|
|
- `\"` - Double quote
|
|
- `\'` - Single quote
|
|
- `\0` - Null character
|
|
- `\xNN` - Hexadecimal byte (e.g., `\x41` for 'A')
|
|
- `\u{NNNN}` - Unicode code point (e.g., `\u{1F600}` for 😀)
|
|
|
|
**Boolean Literals**
|
|
```
|
|
true
|
|
false
|
|
```
|
|
|
|
**Array Literals**
|
|
|
|
```
|
|
[1 2 3 4 5] // array of i32
|
|
[1.0 2.0 3.0] // array of f64
|
|
[[1 2] [3 4]] // 2D array
|
|
```
|
|
|
|
---
|