1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-14 11:36:40 +00:00

Language: enum declarations

Adds \`enum Name { V1, V2, ... }\` as a top-level declaration. Each
variant is registered as a \`u8\` constant equal to its index in the
declaration. Variant names are global and must be unique across all
enums and other symbols (E0501 on collision).

- Lexer: \`enum\` keyword
- AST: \`EnumDecl { name, variants, span }\` field on \`Program\`
- Parser: top-level \`enum\` syntax with optional trailing commas
- Analyzer: \`register_enum\` flattens variants into the symbol table
- IR lowering and AST codegen: variants resolve through the existing
  \`const_values\` path
- Tests cover parsing, duplicate detection, and usage

https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
This commit is contained in:
Claude 2026-04-12 11:36:44 +00:00
parent ae051f5909
commit dbfcb313bc
No known key found for this signature in database
12 changed files with 181 additions and 0 deletions

View file

@ -126,6 +126,13 @@ impl LoweringContext {
}
}
// Register enum variants as constants (0, 1, 2, ... per enum)
for e in &program.enums {
for (i, (variant, _)) in e.variants.iter().enumerate() {
self.const_values.insert(variant.clone(), i as u16);
}
}
// Lower globals
for var in &program.globals {
let var_id = self.get_or_create_var(&var.name);