Skip to content

Commit

Permalink
allow enum values to be set
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 24, 2024
1 parent 97f3511 commit 29f9703
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
Binary file modified bun.lockb
Binary file not shown.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
"run:test": "bun run ./src/test.ts"
},
"devDependencies": {
"@types/node": "^20.9.4",
"@types/node": "^20.14.12",
"@types/treeify": "^1.0.3",
"@types/yargs": "^17.0.32",
"blessed": "^0.1.81",
"bun-types": "^1.0.14",
"prettier": "^3.1.1",
"tinybench": "^2.5.1",
"bun-types": "^1.1.20",
"prettier": "^3.3.3",
"tinybench": "^2.8.0",
"treeify": "^1.1.0",
"typescript": "^5.3.2"
"typescript": "^5.5.4"
},
"dependencies": {
"binaryen": "^116.0.0",
"binaryen": "^118.0.0",
"chalk": "^5.3.0",
"wasmati": "^0.2.0",
"wazum": "latest",
"wazum": "^0.0.13",
"yargs": "^17.7.2"
},
"repository": {
Expand Down
24 changes: 20 additions & 4 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,24 +240,40 @@ export class Parser {
const elements: EnumElement[] = [];

let index = 0;
let elementValue: NumberLiteral | null = null;
let elementName: TokenData;
while (true) {
const name = this.tokenizer.getToken();
if (name.token !== Token.Identifier) return null;
if (!elementValue) {
elementName = this.tokenizer.getToken();
if (elementName.token !== Token.Identifier) return null;
}
const trailing = this.tokenizer.getToken();
if (trailing.text === "}" || trailing.text === ",") {
const element = new EnumElement(
new Identifier(
name.text,
name.range
),
new NumberLiteral(
elementValue || new NumberLiteral(
index.toString()
)
);

console.log("Element: ", element)
if (elementValue) elementValue = null;
index++;
elements.push(element);
if (trailing.text === "}") break;
} else if (trailing.text === "=") {
elementValue = this.parseNumberLiteral(scope);
console.log("Value: ", elementValue);
if (!elementValue) {
new CompileTimeError(
"Value of an enum element must be of type number or string!",
ErrorTypes.TypeError,
12
);
break;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Transpile } from "./transpiler/transpiler";
const start = Date.now();
const tokenizer = new Tokenizer(`
enum Axis {
X,
X = 5,
Y,
Z
}
Expand All @@ -22,7 +22,7 @@ console.log(tokenizer.getAll());
const parser = new Parser(tokenizer, "test.zp");
const program = parser.parseProgram();
//console.dir(program, { depth: null });
console.log(program);
console.dir(program, { depth: 10 });

const transpiled = Transpile.from(program);
console.log("Transpiled:\n" + transpiled);
Expand Down
6 changes: 3 additions & 3 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
enum Axis {
X = 0,
Y = 1,
Z = 2
Axis = 5,
Axis = 1,
Axis = 2
}
export function add(a: i32, b: i32) {
let c = a + b
Expand Down

0 comments on commit 29f9703

Please sign in to comment.