builtin_match_signatures #1

Merged
simponic merged 4 commits from builtin_match_signatures into main 2024-02-28 14:59:29 -05:00
7 changed files with 51 additions and 4 deletions
Showing only changes of commit c9dc50cb97 - Show all commits

View File

@ -48,7 +48,6 @@ const addBinaryIntegerOperationsTo = (env: Environment) => {
{ name: '>', fn: (a: number, b: number) => (a > b ? 1 : 0) },
{ name: '>=', fn: (a: number, b: number) => (a >= b ? 1 : 0) },
{ name: '||', fn: (a: number, b: number) => (a || b ? 1 : 0) },
{ name: '==', fn: (a: number, b: number) => (a == b ? 1 : 0) },
]) {
env.set(name, {
type: 'function',

View File

@ -52,6 +52,9 @@ const evaluatePrimitiveOperation = (
const result = env.apply(opr, operandValues);
const continuationEnvironment = env.createChild();
for (const { name } of resultBindings) {
continuationEnvironment.set(name, result);
}
// return the result of the last continuation
return continuations.reduce((_, continuation, i) => {
@ -107,7 +110,7 @@ export const evaluate = async (
logger: TracingLogger,
): Promise<Denotable> => {
const globalEnvironment = putBuiltinsOnEnvironemtn(
new Environment(logger.createChild('Root')),
new Environment(logger.createChild('RootEnv')),
);
return ast.reduce((_, continuation, i) => {

View File

@ -11,6 +11,13 @@ test('Add (1 real) and (3 int) => (4 real)', async () => {
expect(result).toEqual({ type: 'real', value: 4 });
});
test('Add (1 real) and (3 int) -> result => (real 1 - result) = -3 done with correct lexical scope', async () => {
const ast = peggyParse(await TestPrograms.PrimopScope);
const result = await evaluate(ast, testingLogger);
expect(result).toEqual({ type: 'real', value: -3 });
});
/*
test('String equality', async () => {
const ast = peggyParse(await TestPrograms.StringEquality);

View File

@ -2,7 +2,7 @@ import { expect, test } from 'bun:test';
import { TestPrograms } from './programs';
import { peggyParse } from '@/parser';
test('Primitive Operations', async () => {
test('primitive operation', async () => {
const [operation] = peggyParse(await TestPrograms.AddOneThree);
const { primitiveOperation } = operation;
@ -13,3 +13,33 @@ test('Primitive Operations', async () => {
continuations: [],
});
});
test('primitive operation with continuation', async () => {
const [operation] = peggyParse(await TestPrograms.PrimopScope);
const { primitiveOperation } = operation;
const continuation = {
primitiveOperation: {
opr: '-',
operands: [{ real: 1 }, { name: 'result' }],
resultBindings: [{ name: 'result' }],
continuations: [
{
primitiveOperation: {
opr: '+',
operands: [{ name: 'result' }, { real: 0 }],
resultBindings: [],
continuations: [],
},
},
],
},
};
expect(primitiveOperation).toEqual({
opr: '+',
operands: [{ real: 1 }, { int: 3 }],
resultBindings: [{ name: 'result' }],
continuations: [continuation],
});
});

View File

@ -1 +1 @@
PRIMOP(+, [REAL 1, INT 3], [result], [])
PRIMOP(+, [REAL 1.0, INT 3], [result], [])

View File

@ -4,6 +4,9 @@ export namespace TestPrograms {
export const AddOneThree = Bun.file(
join(import.meta.dir + '/add-1-3.cps'),
).text();
export const PrimopScope = Bun.file(
join(import.meta.dir + '/primop-scope.cps'),
).text();
export const StringEquality = Bun.file(
join(import.meta.dir + '/string-equal.cps'),
).text();

View File

@ -0,0 +1,5 @@
PRIMOP(+, [REAL 1.0, INT 3], [result], [
PRIMOP(-, [REAL 1.0, VAR result], [result], [
PRIMOP(+, [VAR result, REAL 0], [], [])
])
])