diff options
Diffstat (limited to 'src/interpreter/interpreter.ts')
-rw-r--r-- | src/interpreter/interpreter.ts | 32 |
1 files changed, 25 insertions, 7 deletions
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'); } |