mirror of
https://github.com/imjasonh/nescript
synced 2026-07-12 10:39:31 +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:
parent
ae051f5909
commit
dbfcb313bc
12 changed files with 181 additions and 0 deletions
|
|
@ -298,6 +298,42 @@ fn analyze_return_wrong_type() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn analyze_enum_variants_as_constants() {
|
||||
let result = analyze_ok(
|
||||
r#"
|
||||
game "Test" { mapper: NROM }
|
||||
enum Color { Red, Green, Blue }
|
||||
var c: u8 = Red
|
||||
on frame {
|
||||
if c == Blue { c = Green }
|
||||
}
|
||||
start Main
|
||||
"#,
|
||||
);
|
||||
// Variants should be registered as constant symbols.
|
||||
assert!(result.symbols.get("Red").is_some_and(|s| s.is_const));
|
||||
assert!(result.symbols.get("Green").is_some_and(|s| s.is_const));
|
||||
assert!(result.symbols.get("Blue").is_some_and(|s| s.is_const));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn analyze_duplicate_enum_variant_errors() {
|
||||
let errors = analyze_errors(
|
||||
r#"
|
||||
game "Test" { mapper: NROM }
|
||||
enum A { Foo, Bar }
|
||||
enum B { Baz, Bar }
|
||||
on frame { wait_frame }
|
||||
start Main
|
||||
"#,
|
||||
);
|
||||
assert!(
|
||||
errors.contains(&ErrorCode::E0501),
|
||||
"duplicate variant should emit E0501, got: {errors:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn analyze_dead_code_after_break() {
|
||||
let src = r#"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue