diff options
author | Lizzy Hunt <lizzy.hunt@usu.edu> | 2024-02-28 15:06:00 -0700 |
---|---|---|
committer | Lizzy Hunt <lizzy.hunt@usu.edu> | 2024-02-28 15:06:00 -0700 |
commit | 55c00566b0c4870d4c4409ab3e93aacf74f8d081 (patch) | |
tree | 4226b7e518a3ab88987d544ea892da42952255ee /src/interpreter | |
parent | d39cf84965dffd11cab440f5a4efa1b16932ba73 (diff) | |
download | cps-interpreter-55c00566b0c4870d4c4409ab3e93aacf74f8d081.tar.gz cps-interpreter-55c00566b0c4870d4c4409ab3e93aacf74f8d081.zip |
identity function, repl upgrade
Diffstat (limited to 'src/interpreter')
-rw-r--r-- | src/interpreter/builtins.ts | 46 | ||||
-rw-r--r-- | src/interpreter/interpreter.ts | 32 |
2 files changed, 71 insertions, 7 deletions
diff --git a/src/interpreter/builtins.ts b/src/interpreter/builtins.ts index bc666e9..16322f1 100644 --- a/src/interpreter/builtins.ts +++ b/src/interpreter/builtins.ts @@ -202,6 +202,51 @@ const addBinaryArithmeticOperationsTo = (env: Environment) => { return env; }; +const addIdentityFunctionTo = (env: Environment) => { + env.set('id', { + type: 'function', + value: { + signatures: [ + { + arguments: ['null'], + return: 'null', + }, + { + arguments: ['int'], + return: 'int', + }, + { + arguments: ['real'], + return: 'real', + }, + { + arguments: ['bool'], + return: 'bool', + }, + { + arguments: ['string'], + return: 'string', + }, + { + arguments: ['bytearray'], + return: 'bytearray', + }, + { + arguments: ['function'], + return: 'function', + }, + { + arguments: ['reference'], + return: 'reference', + }, + ], + body: ({ value }: Denotable) => value, + }, + }); + + return env; +}; + export const putBuiltinsOnEnvironemtn = (env: Environment) => { return [ addBinaryArithmeticOperationsTo, @@ -210,5 +255,6 @@ export const putBuiltinsOnEnvironemtn = (env: Environment) => { addNumberComparisonOperationsTo, addBooleanAlgebraOperationsTo, addEqualityOperationsTo, + addIdentityFunctionTo, ].reduce((acc, builtinsAdder) => builtinsAdder(acc), env); }; diff --git a/src/interpreter/interpreter.ts b/src/interpreter/interpreter.ts index 94e263c..278e027 100644 --- a/src/interpreter/interpreter.ts +++ b/src/interpreter/interpreter.ts @@ -1,6 +1,7 @@ import { type ContinuationExpression, type PrimitiveOperationExpression, + type ApplicationExpression, type Program, type Value, } from '@/parser'; @@ -16,7 +17,7 @@ import { putBuiltinsOnEnvironemtn } from './builtins'; const evaluateValue = ( value: Value, env: Environment, - logger: TracingLogger, + _logger: TracingLogger, ): Denotable => { if (typeof value === 'string') { return { type: 'string', value }; @@ -35,6 +36,18 @@ const evaluateValue = ( throw new InvalidStateError(`Invalid value: ${value}`); }; +const evaluateApplicationExpression = ( + { application }: ApplicationExpression, + env: Environment, + logger: TracingLogger, +): Denotable => { + const { fn, args } = application; + const argValues = args.map(arg => + evaluateValue(arg, env, logger.createChild('evaluateValue')), + ); + return env.apply(fn.name, argValues); +}; + const evaluatePrimitiveOperation = ( { primitiveOperation }: PrimitiveOperationExpression, env: Environment, @@ -61,6 +74,7 @@ const evaluatePrimitiveOperation = ( logger.warn( `Expected 2 continuations for boolean result, got ContinuationLength=(${continuations.length})`, ); + return result; } const [trueContinuation, falseContinuation] = continuations; @@ -79,7 +93,7 @@ const evaluatePrimitiveOperation = ( ); } else if (continuations.length === 0) { logger.warn( - `!! Expected 1 continuation in continuation list... implicitly returning result but PLEASE NOTE this is technically undefined behavior !!`, + "Expected 1 continuation for non-boolean result, but there wasn't any. Implicitly returning the result", // technically undefined behavior ); return result; } @@ -107,6 +121,15 @@ const evaluteContinuationExpression = ( ); } + if ('application' in expr) { + logger.debug('Evaluating function application'); + return evaluateApplicationExpression( + expr, + env, + logger.createChild('evaluateApplicationExpression'), + ); + } + if ('record' in expr) { throw new NotImplementedError('Continuation records are not supported yet'); } @@ -116,11 +139,6 @@ const evaluteContinuationExpression = ( if ('offset' in expr) { throw new NotImplementedError('Continuation offset is not supported yet'); } - if ('application' in expr) { - throw new NotImplementedError( - 'Continuation application is not supported yet', - ); - } if ('switch' in expr) { throw new NotImplementedError('Continuation switch is not supported yet'); } |