Skip to content

Commit

Permalink
Force initializer in variable declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
arnav-ag committed Apr 10, 2024
1 parent 663b738 commit e16c242
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
31 changes: 16 additions & 15 deletions src/parser/ooga.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,14 +635,10 @@ function peg$parse(input, options) {
var peg$f60 = function(type, init) {
return {
type: type || null,
init: init || null
init: init
};
};
var peg$f61 = function(id, typeInit) {
if (!typeInit || (typeInit.type === null && typeInit.init === null)) {
throw new Error("Either type or initializer must be provided.");
}

return {
tag: "VariableDeclaration",
id: id,
Expand Down Expand Up @@ -5556,22 +5552,22 @@ function peg$parse(input, options) {
return s0;
}

function peg$parseTypeOrInit() {
function peg$parseTypeWithInit() {
var s0, s1, s2, s3, s4;

s0 = peg$currPos;
s1 = peg$parseInitType();
s1 = peg$parseStructIdentifier();
if (s1 === peg$FAILED) {
s1 = peg$parseStructIdentifier();
s1 = peg$parseInitType();
}
if (s1 === peg$FAILED) {
s1 = null;
}
s2 = peg$currPos;
s3 = peg$parse__();
s4 = peg$parseInitialiser();
s4 = peg$parseStructInitializer();
if (s4 === peg$FAILED) {
s4 = peg$parseStructInitializer();
s4 = peg$parseInitialiser();
}
if (s4 !== peg$FAILED) {
s3 = [s3, s4];
Expand All @@ -5580,11 +5576,13 @@ function peg$parse(input, options) {
peg$currPos = s2;
s2 = peg$FAILED;
}
if (s2 === peg$FAILED) {
s2 = null;
if (s2 !== peg$FAILED) {
peg$savedPos = s0;
s0 = peg$f60(s1, s2);
} else {
peg$currPos = s0;
s0 = peg$FAILED;
}
peg$savedPos = s0;
s0 = peg$f60(s1, s2);

return s0;
}
Expand All @@ -5599,7 +5597,10 @@ function peg$parse(input, options) {
s3 = peg$parseIdentifier();
if (s3 !== peg$FAILED) {
s4 = peg$parse__();
s5 = peg$parseTypeOrInit();
s5 = peg$parseTypeWithInit();
if (s5 === peg$FAILED) {
s5 = null;
}
s6 = peg$parseEOS();
if (s6 !== peg$FAILED) {
peg$savedPos = s0;
Expand Down
13 changes: 5 additions & 8 deletions src/parser/ooga.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -620,20 +620,17 @@ SequenceStatement
body: buildList(head, tail, 1)
};
}
TypeOrInit
= type:(InitType / StructIdentifier)? init:(__ (Initialiser / StructInitializer))? {

TypeWithInit
= type:(StructIdentifier / InitType)? init:(__ (StructInitializer / Initialiser)) {
return {
type: type || null,
init: init || null
init: init
};
}

VariableStatement
= VarToken __ id:Identifier __ typeInit:(TypeOrInit)? EOS {
if (!typeInit || (typeInit.type === null && typeInit.init === null)) {
throw new Error("Either type or initializer must be provided.");
}

= VarToken __ id:Identifier __ typeInit:(TypeWithInit)? EOS {
return {
tag: "VariableDeclaration",
id: id,
Expand Down
1 change: 0 additions & 1 deletion src/vm/oogavm-typechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,6 @@ const type_comp = {
return fun_type.ret;
},
ConstantDeclaration: (comp, te, struct_te) => {
// TODO: Check this properly and make sure constants can't be changed
log('ConstantDeclaration');
log(unparse(comp));
const expected_type = lookup_type(comp.id.name, te);
Expand Down

0 comments on commit e16c242

Please sign in to comment.