summaryrefslogtreecommitdiff
path: root/src/interpreter/interpreter.ts
diff options
context:
space:
mode:
authorLizzy Hunt <lizzy.hunt@usu.edu>2024-02-28 15:06:00 -0700
committerLizzy Hunt <lizzy.hunt@usu.edu>2024-02-28 15:06:00 -0700
commit55c00566b0c4870d4c4409ab3e93aacf74f8d081 (patch)
tree4226b7e518a3ab88987d544ea892da42952255ee /src/interpreter/interpreter.ts
parentd39cf84965dffd11cab440f5a4efa1b16932ba73 (diff)
downloadcps-interpreter-55c00566b0c4870d4c4409ab3e93aacf74f8d081.tar.gz
cps-interpreter-55c00566b0c4870d4c4409ab3e93aacf74f8d081.zip
identity function, repl upgrade
Diffstat (limited to 'src/interpreter/interpreter.ts')
-rw-r--r--src/interpreter/interpreter.ts32
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');
}