Skip to content

Commit

Permalink
Allow string literals in enums
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Livesey committed Sep 17, 2024
1 parent aa2a207 commit be3a27d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/en/voxel/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum ModeOfTransport {
Here, the entry `ModeOfTransport.WALKING` is explicitly assigned the value `0`. All other values will automatically be assigned an implicit, unique value when compiled.

> [!NOTE]
> Explicitly assigned values must be number literals.
> Explicitly assigned values must be number literals or string literals (for the latter, the code of the first character of the string literal will be the value).
Enumerated values can then be stored in variables:

Expand Down
6 changes: 6 additions & 0 deletions test/enums/expected.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ ImportedEnum.IMPORTED_VALUE_A = 4
ImportedEnum.IMPORTED_VALUE_B = 5
ImportedEnum.IMPORTED_VALUE_C = 6
ImportedEnum.IMPORTED_VALUE_D = -10
ImportedEnum.IMPORTED_VALUE_E = 0
ImportedEnum.IMPORTED_VALUE_F = 65
TestEnum.VALUE_A = 1
TestEnum.VALUE_B = 2
TestEnum.VALUE_C = 3
TestEnum.VALUE_D = -1
TestEnum.VALUE_E = 0
TestEnum.VALUE_F = 65
imported.ImportedEnum.IMPORTED_VALUE_A = 4
imported.ImportedEnum.IMPORTED_VALUE_B = 5
imported.ImportedEnum.IMPORTED_VALUE_C = 6
imported.ImportedEnum.IMPORTED_VALUE_D = -10
imported.ImportedEnum.IMPORTED_VALUE_E = 0
imported.ImportedEnum.IMPORTED_VALUE_F = 65
testValueA.getEnumEntry() = main:TestEnum.VALUE_A
importedValueA.getEnumEntry() = imported:ImportedEnum.IMPORTED_VALUE_A
unknown.getEnumEntry() = -99
12 changes: 11 additions & 1 deletion test/enums/imported.vxl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ enum ImportedEnum {
IMPORTED_VALUE_A,
IMPORTED_VALUE_B = 5,
IMPORTED_VALUE_C,
IMPORTED_VALUE_D = -10
IMPORTED_VALUE_D = -10,
IMPORTED_VALUE_E = "",
IMPORTED_VALUE_F = "A"
};

syscall io_out("ImportedEnum.IMPORTED_VALUE_A = ");
Expand All @@ -19,4 +21,12 @@ syscall io_out("\n");

syscall io_out("ImportedEnum.IMPORTED_VALUE_D = ");
syscall io_out(ImportedEnum.IMPORTED_VALUE_D);
syscall io_out("\n");

syscall io_out("ImportedEnum.IMPORTED_VALUE_E = ");
syscall io_out(ImportedEnum.IMPORTED_VALUE_E);
syscall io_out("\n");

syscall io_out("ImportedEnum.IMPORTED_VALUE_F = ");
syscall io_out(ImportedEnum.IMPORTED_VALUE_F);
syscall io_out("\n");
20 changes: 19 additions & 1 deletion test/enums/main.vxl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ enum TestEnum {
VALUE_A,
VALUE_B = 2,
VALUE_C,
VALUE_D = -1
VALUE_D = -1,
VALUE_E = "",
VALUE_F = "A"
}

syscall io_out("TestEnum.VALUE_A = ");
Expand All @@ -23,6 +25,14 @@ syscall io_out("TestEnum.VALUE_D = ");
syscall io_out(TestEnum.VALUE_D);
syscall io_out("\n");

syscall io_out("TestEnum.VALUE_E = ");
syscall io_out(TestEnum.VALUE_E);
syscall io_out("\n");

syscall io_out("TestEnum.VALUE_F = ");
syscall io_out(TestEnum.VALUE_F);
syscall io_out("\n");

syscall io_out("imported.ImportedEnum.IMPORTED_VALUE_A = ");
syscall io_out(imported.ImportedEnum.IMPORTED_VALUE_A);
syscall io_out("\n");
Expand All @@ -39,6 +49,14 @@ syscall io_out("imported.ImportedEnum.IMPORTED_VALUE_D = ");
syscall io_out(imported.ImportedEnum.IMPORTED_VALUE_D);
syscall io_out("\n");

syscall io_out("imported.ImportedEnum.IMPORTED_VALUE_E = ");
syscall io_out(imported.ImportedEnum.IMPORTED_VALUE_E);
syscall io_out("\n");

syscall io_out("imported.ImportedEnum.IMPORTED_VALUE_F = ");
syscall io_out(imported.ImportedEnum.IMPORTED_VALUE_F);
syscall io_out("\n");

var testValueA = TestEnum.VALUE_A;
var importedValueA = imported.ImportedEnum.IMPORTED_VALUE_A;
var unknown = -99;
Expand Down
4 changes: 2 additions & 2 deletions tools/vxbuild-js/statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,8 @@ export class EnumStatementNode extends ast.AstNode {

if (this.maybeEat(tokens, [new ast.TokenQuery(tokeniser.AssignmentOperatorToken, "=")])) {
var negate = !!this.maybeEat(tokens, [new ast.TokenQuery(tokeniser.OperatorToken, "-")]);
var entryValueToken = this.eat(tokens, [new ast.TokenQuery(tokeniser.NumberToken)]);
var entryValue = entryValueToken.value * (negate ? -1 : 1);
var entryValue = this.eat(tokens, [new ast.TokenQuery(tokeniser.NumberToken), new ast.TokenQuery(tokeniser.StringToken)]).value;
var entryValue = typeof(entryValue) == "number" ? entryValue * (negate ? -1 : 1) : (entryValue.length > 0 ? entryValue.charCodeAt(0) : 0);

enumObject[entryIdentifier.value] = entryValue;

Expand Down

0 comments on commit be3a27d

Please sign in to comment.