Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
89db6cf917 | ||
|
5e9a34e642 | ||
|
2f77b3fb5a |
@ -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'"
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
import type { Identifier } from '@/parser';
|
||||
import { testingLogger } from '@t/logger';
|
||||
|
||||
export type UnionDenotableType =
|
||||
| Array<DenotableType | DenotableFunctionSignature>
|
||||
@ -23,9 +22,14 @@ export type DenotableType =
|
||||
| 'real'
|
||||
| 'bool'
|
||||
| 'string'
|
||||
| 'record'
|
||||
| 'bytearray'
|
||||
| 'function'
|
||||
| 'reference';
|
||||
| 'function';
|
||||
|
||||
export type DenotableRecord = {
|
||||
length: number;
|
||||
record: Array<Denotable>;
|
||||
};
|
||||
|
||||
export type DenotableValue =
|
||||
| null
|
||||
@ -33,6 +37,7 @@ export type DenotableValue =
|
||||
| string
|
||||
| Uint8Array
|
||||
| DenotableFunction
|
||||
| DenotableRecord
|
||||
| Identifier;
|
||||
|
||||
export type Denotable = {
|
||||
|
@ -2,13 +2,17 @@ import {
|
||||
type ContinuationExpression,
|
||||
type PrimitiveOperationExpression,
|
||||
type ApplicationExpression,
|
||||
type RecordExpression,
|
||||
type Program,
|
||||
type Value,
|
||||
type RecordExpressionTuple,
|
||||
type SelectExpression,
|
||||
} from '@/parser';
|
||||
import { Environment, type Denotable } from '.';
|
||||
import { Environment, type Denotable, type DenotableRecord } from '.';
|
||||
import {
|
||||
BadArgumentError,
|
||||
InvalidStateError,
|
||||
InvalidType,
|
||||
NotImplementedError,
|
||||
type TracingLogger,
|
||||
} from '@/utils';
|
||||
@ -29,6 +33,11 @@ const evaluateValue = (
|
||||
if ('int' in value) {
|
||||
return { type: 'int', value: value.int };
|
||||
}
|
||||
|
||||
if ('bool' in value) {
|
||||
return { type: 'bool', value: value.bool };
|
||||
}
|
||||
|
||||
if ('name' in value) {
|
||||
return env.get(value.name);
|
||||
}
|
||||
@ -107,6 +116,89 @@ const evaluatePrimitiveOperation = (
|
||||
);
|
||||
};
|
||||
|
||||
export const evaluateRecordTuple = (
|
||||
tuple: RecordExpressionTuple,
|
||||
env: Environment,
|
||||
logger: TracingLogger,
|
||||
): Denotable => {
|
||||
const { value, accessPath } = tuple;
|
||||
const record = evaluateValue(value, env, logger.createChild('evaluateValue'));
|
||||
if (record.type === 'record') {
|
||||
// TODO: Implement nested records at accessPath
|
||||
logger.debug(JSON.stringify(accessPath, null, 2));
|
||||
throw new NotImplementedError('Nested records are not yet supported');
|
||||
}
|
||||
|
||||
return record;
|
||||
};
|
||||
|
||||
export const evaluateRecordExpression = (
|
||||
{ record }: RecordExpression,
|
||||
env: Environment,
|
||||
logger: TracingLogger,
|
||||
): Denotable => {
|
||||
const {
|
||||
records,
|
||||
address: { name: address },
|
||||
body,
|
||||
} = record;
|
||||
|
||||
const childEnv = env.createChild();
|
||||
const recordTuple = records.map(record =>
|
||||
evaluateRecordTuple(
|
||||
record,
|
||||
childEnv,
|
||||
logger.createChild('evaluateRecordTuple'),
|
||||
),
|
||||
);
|
||||
const length = recordTuple.length;
|
||||
|
||||
childEnv.set(address, {
|
||||
type: 'record',
|
||||
value: { length, record: recordTuple },
|
||||
});
|
||||
|
||||
return evaluteContinuationExpression(
|
||||
body,
|
||||
childEnv,
|
||||
logger.createChild('evaluteContinuationExpression'),
|
||||
);
|
||||
};
|
||||
|
||||
export const evaluateSelectExpression = (
|
||||
{ select }: SelectExpression,
|
||||
env: Environment,
|
||||
logger: TracingLogger,
|
||||
): Denotable => {
|
||||
const {
|
||||
index: { int: index },
|
||||
record,
|
||||
bind,
|
||||
continuation,
|
||||
} = select;
|
||||
const childEnv = env.createChild();
|
||||
|
||||
const recordValue = evaluateValue(
|
||||
record,
|
||||
env,
|
||||
logger.createChild('evaluateValue'),
|
||||
);
|
||||
if (recordValue.type !== 'record') {
|
||||
throw new InvalidType('Expected record type');
|
||||
}
|
||||
|
||||
const { value } = recordValue as { value: DenotableRecord };
|
||||
|
||||
const selected = value.record[index];
|
||||
childEnv.set(bind.name, selected);
|
||||
|
||||
return evaluteContinuationExpression(
|
||||
continuation,
|
||||
childEnv,
|
||||
logger.createChild('evaluteContinuationExpression'),
|
||||
);
|
||||
};
|
||||
|
||||
const evaluteContinuationExpression = (
|
||||
expr: ContinuationExpression,
|
||||
env: Environment,
|
||||
@ -131,10 +223,20 @@ const evaluteContinuationExpression = (
|
||||
}
|
||||
|
||||
if ('record' in expr) {
|
||||
throw new NotImplementedError('Continuation records are not supported yet');
|
||||
logger.debug('Evaluating record');
|
||||
return evaluateRecordExpression(
|
||||
expr,
|
||||
env,
|
||||
logger.createChild('evaluateRecordExpression'),
|
||||
);
|
||||
}
|
||||
if ('select' in expr) {
|
||||
throw new NotImplementedError('Continuation select is not supported yet');
|
||||
logger.debug('Evaluating select');
|
||||
return evaluateSelectExpression(
|
||||
expr,
|
||||
env,
|
||||
logger.createChild('evaluateSelectExpression'),
|
||||
);
|
||||
}
|
||||
if ('offset' in expr) {
|
||||
throw new NotImplementedError('Continuation offset is not supported yet');
|
||||
|
@ -17,11 +17,11 @@ SelectExpression
|
||||
_?
|
||||
LPAREN
|
||||
_?
|
||||
record:Integer
|
||||
index:Integer
|
||||
_?
|
||||
COMMA
|
||||
_?
|
||||
val:Value
|
||||
record:VarStatement
|
||||
_?
|
||||
COMMA
|
||||
_?
|
||||
@ -31,7 +31,7 @@ SelectExpression
|
||||
_?
|
||||
continuation:ContinuationExpression
|
||||
_?
|
||||
RPAREN { return { select: { record, val, bind, continuation } }; }
|
||||
RPAREN { return { select: { index, record, bind, continuation } }; }
|
||||
|
||||
OffsetExpression
|
||||
= OFFSET
|
||||
@ -86,9 +86,17 @@ SwitchExpression
|
||||
RPAREN { return { switch: { switchIndex, continuations } }; }
|
||||
|
||||
ApplicationExpression
|
||||
= APP _? LPAREN _? fn:(LabelStatement / VarStatement) _? COMMA _? args:ValueList _? RPAREN {
|
||||
return { application: { fn, args } };
|
||||
}
|
||||
= APP
|
||||
_?
|
||||
LPAREN
|
||||
_?
|
||||
fn:(LabelStatement / VarStatement)
|
||||
_?
|
||||
COMMA
|
||||
_?
|
||||
args:ValueList
|
||||
_?
|
||||
RPAREN { return { application: { fn, args } }; }
|
||||
|
||||
FixBinding
|
||||
= LPAREN
|
||||
@ -169,16 +177,29 @@ PrimitiveOperationExpression
|
||||
};
|
||||
}
|
||||
|
||||
RecordExpressionTuple
|
||||
= LPAREN
|
||||
AccessPath
|
||||
= OffsetPath
|
||||
/ SelectPath
|
||||
|
||||
OffsetPath = OFFP _ offset:Integer { return { offset: offset.int }; }
|
||||
|
||||
SelectPath
|
||||
= SELP
|
||||
_?
|
||||
variable:VarStatement
|
||||
LPAREN
|
||||
_?
|
||||
index:Integer
|
||||
_?
|
||||
COMMA
|
||||
_?
|
||||
offset:OffsetStatement
|
||||
accessPath:AccessPath
|
||||
_?
|
||||
RPAREN { return { variable, offset }; }
|
||||
RPAREN { return { select: { index, accessPath } }; }
|
||||
|
||||
RecordExpressionTuple
|
||||
= LPAREN _? value:Value _? COMMA _? accessPath:AccessPath _? RPAREN {
|
||||
return { value, accessPath };
|
||||
}
|
||||
|
||||
RecordExpressionTupleList
|
||||
= LBRACKET
|
||||
@ -202,7 +223,7 @@ RecordExpression
|
||||
_?
|
||||
COMMA
|
||||
_?
|
||||
address:Literal
|
||||
address:Identifier
|
||||
_?
|
||||
COMMA
|
||||
_?
|
||||
@ -234,18 +255,10 @@ IntStatement = INT _ int:Integer { return int; }
|
||||
|
||||
RealStatement = REAL _ real:Real { return real; }
|
||||
|
||||
BoolStatement = BOOL _ bool:Integer { return bool; }
|
||||
BoolStatement = BOOL _ bool:Integer { return { bool: bool.int }; }
|
||||
|
||||
StringStatement = STRING _ string:QuotedString { return string; }
|
||||
|
||||
AccessStatement
|
||||
= OffsetStatement
|
||||
/ SelectStatement
|
||||
|
||||
OffsetStatement = OFFP _ offset:Integer { return offset; }
|
||||
|
||||
SelectStatement = SELP _ offset:Integer { return offset; }
|
||||
|
||||
Identifier
|
||||
= name:([A-Za-z] (LETTER / DIGIT / SAFE_SYMBOL)*) {
|
||||
return { name: name[0] + name[1].join('') };
|
||||
@ -291,7 +304,8 @@ ComparisonOperation
|
||||
/ "||"
|
||||
/ "&&"
|
||||
|
||||
Integer = digits:("-"? [0-9]+) !"." { return { int: parseInt(digits.join(''), 10) }; }
|
||||
Integer
|
||||
= digits:("-"? [0-9]+) !"." { return { int: parseInt(digits.join(''), 10) }; }
|
||||
|
||||
QuotedString
|
||||
= "'" content:[^']* "'" { return content.join(''); }
|
||||
@ -299,9 +313,11 @@ QuotedString
|
||||
|
||||
Real
|
||||
= value:("-"? [0-9]+ ("." [0-9]+)?) {
|
||||
return { real: parseFloat(
|
||||
return {
|
||||
real: parseFloat(
|
||||
value.map(x => (Array.isArray(x) ? x.join('') : x)).join(''),
|
||||
) };
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
Literal
|
||||
|
@ -2,5 +2,6 @@ export * from './generate';
|
||||
export * from './parser';
|
||||
import * as peggy from './parser';
|
||||
|
||||
export const peggyParse = (source: string): peggy.FunctionDefinition[] =>
|
||||
peggy.parse(source);
|
||||
export const peggyParse = (source: string): peggy.ContinuationExpression[] => {
|
||||
return peggy.parse(source);
|
||||
};
|
||||
|
6173
src/parser/parser.ts
6173
src/parser/parser.ts
File diff suppressed because it is too large
Load Diff
@ -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 });
|
||||
});
|
||||
|
@ -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
7
test/programs/record.cps
Normal file
@ -0,0 +1,7 @@
|
||||
RECORD([(INT 1, OFFp 0), (INT 2, OFFp 0)], record,
|
||||
SELECT(0, VAR record, one,
|
||||
SELECT(1, VAR record, two,
|
||||
PRIMOP(+, [VAR one, VAR two], [result], [])
|
||||
)
|
||||
)
|
||||
)
|
Loading…
Reference in New Issue
Block a user