Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/array' into array
Browse files Browse the repository at this point in the history
  • Loading branch information
JothamWong committed Apr 10, 2024
2 parents 646b8b5 + 7b4331a commit 4092e84
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 48 deletions.
124 changes: 92 additions & 32 deletions src/parser/ooga.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,25 +503,33 @@ function peg$parse(input, options) {
var peg$f33 = function(callee, args) { // Handle function calls on member expressions
return { tag: "CallExpression", callee: callee, arguments: args };
};
var peg$f34 = function(head, args) {
return { tag: "CallExpression", arguments: args };
};
var peg$f35 = function(head, property) { // Support for chaining dot syntax
var peg$f34 = function(head, property) { // Support for chaining dot syntax
return {
tag: "MemberExpression",
object: head,
operation: "propertyAccess",
property: property
};
};
var peg$f35 = function(head, index) { // Allow for immediate index access
return {
operation: "indexAccess",
index: index
};
};
var peg$f36 = function(head, tail) {
return tail.reduce(function(result, element) {
// Depending on the type of element (call or member access), adjust the target of the call or property access
if (element.tag === "CallExpression") {
element.callee = result;
} else { // For member expressions
element.object = result;
if (element.operation === "propertyAccess") {
return {
tag: "MemberExpression",
object: result,
property: element.property
};
} else if (element.operation === "indexAccess") {
return {
tag: "ArraySliceIndex",
arrayExpression: result,
index: element.index
};
}
return element;
}, head);
};
var peg$f37 = function(callee, args) { // Handles function/method calls
Expand Down Expand Up @@ -902,7 +910,7 @@ function peg$parse(input, options) {
type: {
tag:'Array',
elementType: {type: type},
length: -1,
length: elements.length,
is_bound: false
},
elements: elements,
Expand Down Expand Up @@ -3383,7 +3391,7 @@ function peg$parse(input, options) {
}

function peg$parseCallExpression() {
var s0, s1, s2, s3, s4, s5, s6, s7;
var s0, s1, s2, s3, s4, s5, s6, s7, s8, s9;

s0 = peg$currPos;
s1 = peg$currPos;
Expand All @@ -3409,30 +3417,56 @@ function peg$parse(input, options) {
s2 = [];
s3 = peg$currPos;
s4 = peg$parse__();
s5 = peg$parseArguments();
if (input.charCodeAt(peg$currPos) === 46) {
s5 = peg$c6;
peg$currPos++;
} else {
s5 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e16); }
}
if (s5 !== peg$FAILED) {
peg$savedPos = s3;
s3 = peg$f34(s1, s5);
s6 = peg$parse__();
s7 = peg$parseIdentifier();
if (s7 !== peg$FAILED) {
peg$savedPos = s3;
s3 = peg$f34(s1, s7);
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
if (s3 === peg$FAILED) {
s3 = peg$currPos;
s4 = peg$parse__();
if (input.charCodeAt(peg$currPos) === 46) {
s5 = peg$c6;
if (input.charCodeAt(peg$currPos) === 91) {
s5 = peg$c4;
peg$currPos++;
} else {
s5 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e16); }
if (peg$silentFails === 0) { peg$fail(peg$e10); }
}
if (s5 !== peg$FAILED) {
s6 = peg$parse__();
s7 = peg$parseIdentifier();
s7 = peg$parseExpression();
if (s7 !== peg$FAILED) {
peg$savedPos = s3;
s3 = peg$f35(s1, s7);
s8 = peg$parse__();
if (input.charCodeAt(peg$currPos) === 93) {
s9 = peg$c5;
peg$currPos++;
} else {
s9 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e11); }
}
if (s9 !== peg$FAILED) {
peg$savedPos = s3;
s3 = peg$f35(s1, s7);
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
} else {
peg$currPos = s3;
s3 = peg$FAILED;
Expand All @@ -3446,30 +3480,56 @@ function peg$parse(input, options) {
s2.push(s3);
s3 = peg$currPos;
s4 = peg$parse__();
s5 = peg$parseArguments();
if (input.charCodeAt(peg$currPos) === 46) {
s5 = peg$c6;
peg$currPos++;
} else {
s5 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e16); }
}
if (s5 !== peg$FAILED) {
peg$savedPos = s3;
s3 = peg$f34(s1, s5);
s6 = peg$parse__();
s7 = peg$parseIdentifier();
if (s7 !== peg$FAILED) {
peg$savedPos = s3;
s3 = peg$f34(s1, s7);
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
if (s3 === peg$FAILED) {
s3 = peg$currPos;
s4 = peg$parse__();
if (input.charCodeAt(peg$currPos) === 46) {
s5 = peg$c6;
if (input.charCodeAt(peg$currPos) === 91) {
s5 = peg$c4;
peg$currPos++;
} else {
s5 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e16); }
if (peg$silentFails === 0) { peg$fail(peg$e10); }
}
if (s5 !== peg$FAILED) {
s6 = peg$parse__();
s7 = peg$parseIdentifier();
s7 = peg$parseExpression();
if (s7 !== peg$FAILED) {
peg$savedPos = s3;
s3 = peg$f35(s1, s7);
s8 = peg$parse__();
if (input.charCodeAt(peg$currPos) === 93) {
s9 = peg$c5;
peg$currPos++;
} else {
s9 = peg$FAILED;
if (peg$silentFails === 0) { peg$fail(peg$e11); }
}
if (s9 !== peg$FAILED) {
peg$savedPos = s3;
s3 = peg$f35(s1, s7);
} else {
peg$currPos = s3;
s3 = peg$FAILED;
}
} else {
peg$currPos = s3;
s3 = peg$FAILED;
Expand Down
37 changes: 22 additions & 15 deletions src/parser/ooga.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -324,32 +324,39 @@ NewExpression

CallExpression
= head:(
callee:(MemberExpression/LambdaDeclaration) __ args:Arguments { // Handle function calls on member expressions
callee:(MemberExpression / LambdaDeclaration) __ args:Arguments { // Handle function calls on member expressions
return { tag: "CallExpression", callee: callee, arguments: args };
}
)
tail:(
// The tail part remains the same, handling further call expressions and property accesses
__ args:Arguments {
return { tag: "CallExpression", arguments: args };
}
/ __ "." __ property:Identifier { // Support for chaining dot syntax
__ "." __ property:Identifier { // Support for chaining dot syntax
return {
tag: "MemberExpression",
object: head,
operation: "propertyAccess",
property: property
};
}
/ __ "[" __ index:Expression __ "]" { // Allow for immediate index access
return {
operation: "indexAccess",
index: index
};
}
)*
{
return tail.reduce(function(result, element) {
// Depending on the type of element (call or member access), adjust the target of the call or property access
if (element.tag === "CallExpression") {
element.callee = result;
} else { // For member expressions
element.object = result;
if (element.operation === "propertyAccess") {
return {
tag: "MemberExpression",
object: result,
property: element.property
};
} else if (element.operation === "indexAccess") {
return {
tag: "ArraySliceIndex",
arrayExpression: result,
index: element.index
};
}
return element;
}, head);
}

Expand Down Expand Up @@ -1032,7 +1039,7 @@ ArraySliceLiteral
type: {
tag:'Array',
elementType: {type: type},
length: -1,
length: elements.length,
is_bound: false
},
elements: elements,
Expand Down
3 changes: 2 additions & 1 deletion src/vm/oogavm-typechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ function getType(t, struct_te): Type {
return t.type;
} else if (t.type.tag === 'Struct') {
// Ensure that the struct is defined
log('GetType StructType:', t.type.name, struct_te);
const structType = lookup_type(t.type.name, struct_te);
t.type = structType;
log('Exiting getType, returning StructType, ', structType);
Expand Down Expand Up @@ -334,7 +335,7 @@ const type_comp = {

// We now can set the fields of the struct. We do this now to allow for recursive struct definitions
const fields = comp.fields.map(
f => new StructField(f.name.name, getType(f, struct_te))
f => new StructField(f.name.name, getType(f, extended_struct_te))
);
// log('Fields for struct', structName);
// log(fields);
Expand Down

0 comments on commit 4092e84

Please sign in to comment.