summaryrefslogtreecommitdiff
path: root/src/interpreter/builtins.ts
diff options
context:
space:
mode:
authorLizzy Hunt <lizzy.hunt@usu.edu>2024-02-28 13:41:53 -0700
committerLizzy Hunt <lizzy.hunt@usu.edu>2024-02-28 13:41:53 -0700
commitd39cf84965dffd11cab440f5a4efa1b16932ba73 (patch)
treee0239d2047756120c51fd431999520ffbb38d29b /src/interpreter/builtins.ts
parent7cc3ef5fa1feec8087618c899441a11052f84c48 (diff)
downloadcps-interpreter-d39cf84965dffd11cab440f5a4efa1b16932ba73.tar.gz
cps-interpreter-d39cf84965dffd11cab440f5a4efa1b16932ba73.zip
branching
Diffstat (limited to 'src/interpreter/builtins.ts')
-rw-r--r--src/interpreter/builtins.ts120
1 files changed, 114 insertions, 6 deletions
diff --git a/src/interpreter/builtins.ts b/src/interpreter/builtins.ts
index 200131f..bc666e9 100644
--- a/src/interpreter/builtins.ts
+++ b/src/interpreter/builtins.ts
@@ -28,6 +28,117 @@ const addUnaryIntegerOperationsTo = (env: Environment) => {
return env;
};
+const addNumberComparisonOperationsTo = (env: Environment) => {
+ const comparisonOperationsSignatures: DenotableFunctionSignature[] = [
+ {
+ arguments: [
+ ['real', 'int'],
+ ['real', 'int'],
+ ],
+ return: 'bool',
+ },
+ ];
+
+ for (const { name, fn } of [
+ { 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',
+ value: {
+ signatures: comparisonOperationsSignatures,
+ body: ({ value: a }: Denotable, { value: b }: Denotable) =>
+ fn(a as number, b as number),
+ },
+ });
+ }
+
+ return env;
+};
+
+const addEqualityOperationsTo = (env: Environment) => {
+ const equalityOperationSignatures: DenotableFunctionSignature[] = [
+ {
+ arguments: ['int', 'int'],
+ return: 'bool',
+ },
+ {
+ arguments: ['real', 'real'],
+ return: 'bool',
+ },
+ {
+ arguments: ['bool', 'bool'],
+ return: 'bool',
+ },
+ {
+ arguments: ['string', 'string'],
+ return: 'bool',
+ },
+ ];
+
+ for (const { name, fn } of [
+ {
+ name: '==',
+ fn: (a: number | string, b: number | string) => (a === b ? 1 : 0),
+ },
+ {
+ name: '!=',
+ fn: (a: number | string, b: number | string) => (a !== b ? 1 : 0),
+ },
+ ]) {
+ env.set(name, {
+ type: 'function',
+ value: {
+ signatures: equalityOperationSignatures,
+ body: ({ value: a }: Denotable, { value: b }: Denotable) =>
+ fn(a as number | string, b as number | string),
+ },
+ });
+ }
+
+ return env;
+};
+
+const addBooleanAlgebraOperationsTo = (env: Environment) => {
+ const binaryBooleanOps: DenotableFunctionSignature[] = [
+ {
+ arguments: ['bool', 'bool'],
+ return: 'bool',
+ },
+ ];
+
+ for (const { name, fn } of [
+ { name: '||', fn: (a: number, b: number) => (a === 1 || b === 1 ? 1 : 0) },
+ { name: '&&', fn: (a: number, b: number) => (a === 1 && b === 1 ? 1 : 0) },
+ ]) {
+ env.set(name, {
+ type: 'function',
+ value: {
+ signatures: binaryBooleanOps,
+ body: ({ value: a }: Denotable, { value: b }: Denotable) =>
+ fn(a as number, b as number),
+ },
+ });
+ }
+
+ env.set('!', {
+ type: 'function',
+ value: {
+ signatures: [
+ {
+ arguments: ['bool'],
+ return: 'bool',
+ },
+ ],
+ body: ({ value }: Denotable) => (value === 1 ? 0 : 1),
+ },
+ });
+
+ return env;
+};
+
const addBinaryIntegerOperationsTo = (env: Environment) => {
const binaryIntegerOperationSignatures: DenotableFunctionSignature[] = [
{
@@ -42,12 +153,6 @@ const addBinaryIntegerOperationsTo = (env: Environment) => {
{ name: '<<', fn: (a: number, b: number) => a << b },
{ name: '|', fn: (a: number, b: number) => a | b },
{ name: '^', fn: (a: number, b: number) => a ^ b },
- { 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) },
- { 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',
@@ -102,5 +207,8 @@ export const putBuiltinsOnEnvironemtn = (env: Environment) => {
addBinaryArithmeticOperationsTo,
addBinaryIntegerOperationsTo,
addUnaryIntegerOperationsTo,
+ addNumberComparisonOperationsTo,
+ addBooleanAlgebraOperationsTo,
+ addEqualityOperationsTo,
].reduce((acc, builtinsAdder) => builtinsAdder(acc), env);
};