lamina 0.0.10

High-performance compiler backend for Lamina Intermediate Representation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Parser state management for Lamina IR parsing.

use crate::LaminaError;
use std::result::Result;

/// Parser state tracking position and input.
#[derive(Debug)]
pub struct ParserState<'a> {
    input: &'a str,
    bytes: &'a [u8],
    position: usize,
}

impl<'a> ParserState<'a> {
    /// Creates a new parser state for the given input.
    pub fn new(input: &'a str) -> Self {
        ParserState {
            input,
            bytes: input.as_bytes(),
            position: 0,
        }
    }

    /// Returns the current character.
    pub fn current_char(&self) -> Option<char> {
        self.peek_char(0)
    }

    pub fn peek_char(&self, offset: usize) -> Option<char> {
        self.input[self.position..].chars().nth(offset)
    }

    pub fn peek_slice(&self, len: usize) -> Option<&'a str> {
        if self.position + len <= self.input.len() {
            Some(&self.input[self.position..self.position + len])
        } else {
            None
        }
    }

    /// Advances the parser position by one character.
    pub fn advance(&mut self) {
        if let Some(c) = self.current_char() {
            self.position += c.len_utf8();
        }
    }

    /// Advances the parser position by the specified number of characters.
    pub fn advance_by(&mut self, count: usize) {
        for _ in 0..count {
            self.advance();
        }
    }

    /// Checks if the parser has reached end of input.
    pub fn is_eof(&self) -> bool {
        self.position >= self.input.len()
    }

    /// Skips whitespace and comments ('#' to end of line).
    pub fn skip_whitespace_and_comments(&mut self) {
        while !self.is_eof() {
            let byte = self.bytes[self.position];
            if byte.is_ascii_whitespace() {
                self.advance();
            } else if byte == b'#' {
                while !self.is_eof() && self.bytes[self.position] != b'\n' {
                    self.advance();
                }
                if !self.is_eof() {
                    self.advance();
                }
            } else {
                break;
            }
        }
    }

    /// Expects and consumes a specific character.
    pub fn expect_char(&mut self, expected: char) -> Result<(), LaminaError> {
        self.skip_whitespace_and_comments();
        if self.current_char() == Some(expected) {
            self.advance();
            Ok(())
        } else {
            let found = self
                .current_char()
                .map(|c| format!("'{}'", c))
                .unwrap_or_else(|| "end of input".to_string());

            let hint = match expected {
                '(' => "Did you forget an opening parenthesis?",
                ')' => "Did you forget a closing parenthesis?",
                '{' => "Did you forget an opening brace?",
                '}' => "Did you forget a closing brace?",
                '[' => "Did you forget an opening bracket?",
                ']' => "Did you forget a closing bracket?",
                ':' => "Did you forget a colon after an identifier?",
                ',' => "Did you forget a comma to separate items?",
                '=' => "Did you forget an equals sign?",
                _ => "",
            };

            let msg = if hint.is_empty() {
                format!("Expected character '{}', but found {}", expected, found)
            } else {
                format!(
                    "Expected character '{}', but found {}\n  Hint: {}",
                    expected, found, hint
                )
            };

            Err(self.error(msg))
        }
    }

    /// Consumes a keyword if present.
    pub fn consume_keyword(&mut self, keyword: &str) -> Result<(), LaminaError> {
        self.skip_whitespace_and_comments();
        if self.input[self.position..].starts_with(keyword) {
            let next_char_pos = self.position + keyword.len();
            if next_char_pos >= self.input.len()
                || !self.input.as_bytes()[next_char_pos].is_ascii_alphanumeric()
            {
                self.position += keyword.len();
                Ok(())
            } else {
                let found = self.peek_slice(keyword.len() + 10).unwrap_or("");
                Err(self.error(format!(
                    "Expected keyword '{}', but found longer identifier '{}'\n  Hint: Keywords must be followed by whitespace or punctuation, not alphanumeric characters",
                    keyword, found
                )))
            }
        } else {
            let found = self.peek_slice(keyword.len().max(20)).unwrap_or("");
            let suggestion = if found.starts_with(&keyword[..keyword.len().min(found.len())]) {
                format!("Did you mean '{}'? (check spelling)", keyword)
            } else {
                format!("Expected keyword '{}'", keyword)
            };

            Err(self.error(format!("{}, but found '{}'", suggestion, found)))
        }
    }

    /// Parses an identifier string.
    pub fn parse_identifier_str(&mut self) -> Result<&'a str, LaminaError> {
        self.skip_whitespace_and_comments();
        let start = self.position;

        let first_byte = *self
            .bytes
            .get(start)
            .ok_or_else(|| self.error("Unexpected end of input while parsing identifier\n  Hint: Identifiers must start with a letter (a-z, A-Z) or underscore (_)".to_string()))?;
        if !(first_byte.is_ascii_alphabetic() || first_byte == b'_') {
            let found_char = first_byte as char;
            let hint = if found_char.is_ascii_digit() {
                "Identifiers cannot start with a digit. Did you mean to use a numeric literal instead?"
            } else {
                "Identifiers must start with a letter (a-z, A-Z) or underscore (_)"
            };
            return Err(self.error(format!(
                "Invalid identifier start: found '{}'\n  Hint: {}",
                found_char, hint
            )));
        }
        self.advance();

        while self.position < self.bytes.len() {
            let byte = self.bytes[self.position];
            if byte.is_ascii_alphanumeric() || byte == b'_' {
                self.advance();
            } else {
                break;
            }
        }

        Ok(&self.input[start..self.position])
    }

    /// Parses a type identifier (starts with '@').
    pub fn parse_type_identifier(&mut self) -> Result<crate::Identifier<'a>, LaminaError> {
        self.expect_char('@')?;
        self.parse_identifier_str()
    }

    /// Parses a value identifier (starts with '%').
    pub fn parse_value_identifier(&mut self) -> Result<crate::Identifier<'a>, LaminaError> {
        self.expect_char('%')?;
        self.parse_identifier_str()
    }

    /// Parses a label identifier.
    pub fn parse_label_identifier(&mut self) -> Result<crate::Label<'a>, LaminaError> {
        self.parse_identifier_str()
    }

    /// Parses an integer literal.
    pub fn parse_integer(&mut self) -> Result<i64, LaminaError> {
        self.skip_whitespace_and_comments();
        let start = self.position;

        let mut negative = false;
        if !self.is_eof() && self.bytes[self.position] == b'-' {
            negative = true;
            self.advance();
        }

        while !self.is_eof() && self.bytes[self.position].is_ascii_digit() {
            self.advance();
        }

        if start == self.position || (negative && start + 1 == self.position) {
            Err(self.error("Expected an integer literal\n  Hint: Integer literals can be positive (e.g., 42) or negative (e.g., -42)".to_string()))
        } else {
            let digits = &self.input[if negative { start + 1 } else { start }..self.position];

            // Handle i64::MIN special case
            if negative && digits == "9223372036854775808" {
                return Ok(i64::MIN);
            }

            // Check for overflow before parsing
            if digits.len() > 19 {
                return Err(self.error(format!(
                    "Integer literal too large: {}{}\n  Hint: Integer literals must be between {} and {}",
                    if negative { "-" } else { "" },
                    digits,
                    i64::MIN,
                    i64::MAX
                )));
            }

            if negative {
                match digits.parse::<i64>() {
                    Ok(val) => {
                        // Check for overflow when negating
                        if val == i64::MIN {
                            Ok(i64::MIN)
                        } else {
                            Ok(-val)
                        }
                    },
                    Err(e) => Err(self.error(format!(
                        "Failed to parse integer: {}\n  Hint: Integer literals must be between {} and {}",
                        e, i64::MIN, i64::MAX
                    ))),
                }
            } else {
                digits
                    .parse::<i64>()
                    .map_err(|e| self.error(format!(
                        "Failed to parse integer: {}\n  Hint: Integer literals must be between {} and {}",
                        e, i64::MIN, i64::MAX
                    )))
            }
        }
    }

    /// Parses a float literal.
    pub fn parse_float(&mut self) -> Result<f32, LaminaError> {
        self.skip_whitespace_and_comments();
        let start = self.position;

        if !self.is_eof() && self.bytes[self.position] == b'-' {
            self.advance();
        }

        let mut has_digit = false;

        while !self.is_eof() && self.bytes[self.position].is_ascii_digit() {
            has_digit = true;
            self.advance();
        }

        if !self.is_eof() && self.bytes[self.position] == b'.' {
            self.advance();

            while !self.is_eof() && self.bytes[self.position].is_ascii_digit() {
                has_digit = true;
                self.advance();
            }
        }

        if !has_digit {
            return Err(self.error("Expected a floating-point literal\n  Hint: Float literals must contain at least one digit (e.g., 3.14, -0.5, 42.0)".to_string()));
        }

        let value_str = &self.input[start..self.position];
        value_str
            .parse::<f32>()
            .map_err(|e| self.error(format!(
                "Failed to parse float: {}\n  Hint: Float literals must be valid numbers (e.g., 3.14, -0.5, 42.0). Check for overflow or invalid format",
                e
            )))
    }

    /// Parses a string literal.
    pub fn parse_string_literal(&mut self) -> Result<&'a str, LaminaError> {
        self.expect_char('"')?;
        let start = self.position;
        let mut escaped = false;

        while !self.is_eof() {
            let byte = self.bytes[self.position];
            if escaped {
                escaped = false;
                self.advance();
                continue;
            }

            if byte == b'\\' {
                escaped = true;
                self.advance();
                continue;
            }

            if byte == b'"' {
                break;
            }

            self.advance();
        }

        if self.is_eof() {
            return Err(self.error("Unclosed string literal\n  Hint: String literals must be closed with a double quote (\")".to_string()));
        }

        let end = self.position;
        self.expect_char('"')?;
        Ok(&self.input[start..end])
    }

    /// Creates a parsing error with the given message, including line and column information.
    pub fn error(&self, message: String) -> LaminaError {
        let (line, column) = self.get_line_column();
        let context = self.get_error_context();

        let error_msg = if context.is_empty() {
            format!("{} at line {}, column {}", message, line, column)
        } else {
            format!(
                "{}\n  at line {}, column {}\n  {}\n  {}^",
                message,
                line,
                column,
                context,
                " ".repeat(column.saturating_sub(1))
            )
        };

        LaminaError::ParsingError(error_msg)
    }

    /// Gets the line and column number for the current position.
    fn get_line_column(&self) -> (usize, usize) {
        let mut line = 1;
        let mut column = 1;

        for (i, ch) in self.input.char_indices() {
            if i >= self.position {
                break;
            }
            if ch == '\n' {
                line += 1;
                column = 1;
            } else {
                column += 1;
            }
        }

        (line, column)
    }

    /// Gets a context string showing the line where the error occurred.
    fn get_error_context(&self) -> String {
        let (line_num, _) = self.get_line_column();
        let lines: Vec<&str> = self.input.lines().collect();

        if line_num > 0 && line_num <= lines.len() {
            let line = lines[line_num - 1];
            // Truncate very long lines for readability
            if line.len() > 80 {
                format!("{}...", &line[..77])
            } else {
                line.to_string()
            }
        } else {
            String::new()
        }
    }

    /// Returns the current parser position.
    pub fn position(&self) -> usize {
        self.position
    }

    /// Sets the parser position.
    pub fn set_position(&mut self, pos: usize) {
        self.position = pos;
    }

    /// Peeks at a byte at the given offset from current position.
    pub fn peek_byte(&self, offset: usize) -> Option<u8> {
        let pos = self.position + offset;
        if pos < self.bytes.len() {
            Some(self.bytes[pos])
        } else {
            None
        }
    }

    /// Returns the bytes slice for peeking.
    pub fn bytes(&self) -> &[u8] {
        self.bytes
    }
}