diff options
author | Lizzy Hunt <lizzy.hunt@usu.edu> | 2024-03-05 14:49:46 -0700 |
---|---|---|
committer | Lizzy Hunt <lizzy.hunt@usu.edu> | 2024-03-05 14:56:17 -0700 |
commit | 5e9a34e64254000b9922d69697774c430cdeca36 (patch) | |
tree | ce6c14e7ff698c668d209282720a66e6af1674d0 | |
parent | 2f77b3fb5a102224c83db2f1fa093c278db716e5 (diff) | |
download | cps-interpreter-5e9a34e64254000b9922d69697774c430cdeca36.tar.gz cps-interpreter-5e9a34e64254000b9922d69697774c430cdeca36.zip |
fix parsing of RecordExpressions
-rwxr-xr-x | bun.lockb | bin | 16198 -> 25314 bytes | |||
-rw-r--r-- | package.json | 6 | ||||
-rw-r--r-- | src/parser/grammar.pegjs | 6 | ||||
-rw-r--r-- | src/parser/index.ts | 5 | ||||
-rw-r--r-- | src/parser/parser.ts | 15 | ||||
-rw-r--r-- | test/interpreter.spec.ts | 7 | ||||
-rw-r--r-- | test/programs/index.ts | 3 | ||||
-rw-r--r-- | test/programs/record.cps | 7 |
8 files changed, 34 insertions, 15 deletions
Binary files differ diff --git a/package.json b/package.json index 163631d..11d6c63 100644 --- a/package.json +++ b/package.json @@ -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'" } -}
\ No newline at end of file +} diff --git a/src/parser/grammar.pegjs b/src/parser/grammar.pegjs index 180b87f..0017b18 100644 --- a/src/parser/grammar.pegjs +++ b/src/parser/grammar.pegjs @@ -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 _? diff --git a/src/parser/index.ts b/src/parser/index.ts index 366042c..912e6f2 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -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); +}; diff --git a/src/parser/parser.ts b/src/parser/parser.ts index 934f3ff..b73df78 100644 --- a/src/parser/parser.ts +++ b/src/parser/parser.ts @@ -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; }; }; diff --git a/test/interpreter.spec.ts b/test/interpreter.spec.ts index 6d3189a..acca7bb 100644 --- a/test/interpreter.spec.ts +++ b/test/interpreter.spec.ts @@ -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 }); +}); diff --git a/test/programs/index.ts b/test/programs/index.ts index fae3b59..71ae303 100644 --- a/test/programs/index.ts +++ b/test/programs/index.ts @@ -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(); } diff --git a/test/programs/record.cps b/test/programs/record.cps new file mode 100644 index 0000000..fe82668 --- /dev/null +++ b/test/programs/record.cps @@ -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], []) + ) + ) +)
\ No newline at end of file |