fix parsing of RecordExpressions

This commit is contained in:
Lizzy Hunt 2024-03-05 14:49:46 -07:00
parent 2f77b3fb5a
commit 5e9a34e642
No known key found for this signature in database
GPG Key ID: E835BD4B08CCAF96
8 changed files with 34 additions and 15 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -10,9 +10,13 @@
},
"dependencies": {
"minimist": "^1.2.8",
"nodemon": "^3.1.0",
"peggy": "^4.0.0",
"prettier": "^3.2.5",
"prettier-plugin-pegjs": "^2.0.2",
"ts-pegjs": "^4.2.1"
},
"scripts": {
"watch-test": "nodemon -e ts,cps --exec 'bun test'"
}
}

View File

@ -172,13 +172,13 @@ PrimitiveOperationExpression
RecordExpressionTuple
= LPAREN
_?
variable:VarStatement
value:Value
_?
COMMA
_?
offset:OffsetStatement
_?
RPAREN { return { variable, offset }; }
RPAREN { return { value, offset }; }
RecordExpressionTupleList
= LBRACKET
@ -202,7 +202,7 @@ RecordExpression
_?
COMMA
_?
address:Literal
address:Identifier
_?
COMMA
_?

View File

@ -2,5 +2,6 @@ export * from './generate';
export * from './parser';
import * as peggy from './parser';
export const peggyParse = (source: string): peggy.ContinuationExpression[] =>
peggy.parse(source);
export const peggyParse = (source: string): peggy.ContinuationExpression[] => {
return peggy.parse(source);
};

View File

@ -473,8 +473,8 @@ function peg$parse(input, options) {
};
};// @ts-ignore
var peg$f11 = function(variable, offset) {// @ts-ignore
return { variable, offset }; };// @ts-ignore
var peg$f11 = function(value, offset) {// @ts-ignore
return { value, offset }; };// @ts-ignore
var peg$f12 = function(records, lastRecord) {
// @ts-ignore
@ -2588,7 +2588,7 @@ peg$parseRecordExpressionTuple() {
s2 = null;
}
// @ts-ignore
s3 = peg$parseVarStatement();
s3 = peg$parseValue();
// @ts-ignore
if (s3 !== peg$FAILED) {
// @ts-ignore
@ -2879,7 +2879,7 @@ peg$parseRecordExpression() {
s8 = null;
}
// @ts-ignore
s9 = peg$parseLiteral();
s9 = peg$parseIdentifier();
// @ts-ignore
if (s9 !== peg$FAILED) {
// @ts-ignore
@ -5372,15 +5372,12 @@ export type PrimitiveOperationExpression = {
continuations: ContinuationList;
};
};
export type RecordExpressionTuple = {
variable: VarStatement;
offset: OffsetStatement;
};
export type RecordExpressionTuple = { value: Value; offset: OffsetStatement };
export type RecordExpressionTupleList = any[];
export type RecordExpression = {
record: {
records: RecordExpressionTupleList;
address: Literal;
address: Identifier;
body: ContinuationExpression;
};
};

View File

@ -45,3 +45,10 @@ test('Application of identity function', async () => {
const result = await evaluate(ast, testingLogger);
expect(result).toEqual({ type: 'int', value: 3 });
});
test('Record construction', async () => {
const ast = peggyParse(await TestPrograms.RecordConstruction);
const result = await evaluate(ast, testingLogger);
expect(result).toEqual({ type: 'int', value: 3 });
});

View File

@ -19,4 +19,7 @@ export namespace TestPrograms {
export const Application = Bun.file(
join(import.meta.dir, 'application.cps'),
).text();
export const RecordConstruction = Bun.file(
join(import.meta.dir, 'record.cps'),
).text();
}

7
test/programs/record.cps Normal file
View File

@ -0,0 +1,7 @@
RECORD([(INT 1, OFFp 0), (INT 2, OFFp 0)], record,
SELECT(0, VAR one, record,
SELECT(1, VAR two, record,
PRIMOP(+, [VAR one, VAR two], [result], [])
)
)
)