Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Expressions): add boolean symbols #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion lib/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@

const OPERATORS = require('./operators.js');

const BOOL = {
t: true,
nil: false,
true: true,
false: false,
};

class BooleanExpression {
type = 'boolean';

constructor(value) {
if (!(value in BOOL)) {
throw new Error(`Unknown boolean value: ${value}`);
}
this.value = BOOL[value];
}

interpret() {
return this.value;
}
}

class NumberExpression {
type = 'number';

constructor(value) {
this.value = parseFloat(value);
}
Expand All @@ -13,6 +37,8 @@ class NumberExpression {
}

class VariableExpression {
type = 'variable';

constructor(name) {
this.name = name;
}
Expand All @@ -26,6 +52,8 @@ class VariableExpression {
}

class OperationExpression {
type = 'operation';

constructor(operator, operands) {
this.operator = operator;
this.operands = operands;
Expand All @@ -41,4 +69,34 @@ class OperationExpression {
}
}

module.exports = { NumberExpression, VariableExpression, OperationExpression };
const expressions = {
number: NumberExpression,
variable: VariableExpression,
boolean: BooleanExpression,
operation: OperationExpression,
};

const expressionType = {
number: 'number',
variable: 'variable',
operation: 'operation',
boolean: 'boolean',
};

const getExpressionType = (token) => {
if (Array.isArray(token)) return expressionType.operation;
if (!isNaN(token)) return expressionType.number;
if (token in BOOL) return expressionType.boolean;
return expressionType.variable;
};

module.exports = {
NumberExpression,
VariableExpression,
OperationExpression,
BooleanExpression,
BOOL,
expressions,
expressionType,
getExpressionType,
};
14 changes: 7 additions & 7 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const {
NumberExpression,
VariableExpression,
OperationExpression,
expressions,
expressionType,
getExpressionType,
} = require('./expressions.js');

const tokenize = (source) => {
Expand Down Expand Up @@ -33,15 +33,15 @@ const tokenize = (source) => {
};

const parse = (tokens) => {
if (!Array.isArray(tokens)) {
const isVar = isNaN(tokens);
const Expression = isVar ? VariableExpression : NumberExpression;
const type = getExpressionType(tokens);
const Expression = expressions[type];
if (type !== expressionType.operation) {
return new Expression(tokens);
}
const operator = tokens[0];
const operands = tokens.slice(1);
const operandExpressions = operands.map((x) => parse(x));
return new OperationExpression(operator, operandExpressions);
return new Expression(operator, operandExpressions);
};

const evaluate = (input, context = {}) => {
Expand Down
7 changes: 6 additions & 1 deletion metalisp.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type List = Array<unknown>;
export type Value = number | string;
export type Value = number | string | boolean;
export type Result = List | Value;
export type Operator = (...list: List) => Result;
export type LispContext = Record<string, unknown>;
Expand All @@ -14,6 +14,11 @@ export interface Expression {
interpret(context: LispContext): Value;
}

export class BooleanExpression implements Expression {
constructor(value: string);
interpret(context: LispContext): boolean;
}

export class NumberExpression implements Expression {
constructor(value: number | string);
interpret(context: LispContext): number;
Expand Down
14 changes: 14 additions & 0 deletions test/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ test('Evaluate cdr (tail of list)', () => {
const expected = [3, 1];
assert.deepStrictEqual(result, expected, 'cdr (tail of list) failed');
});

test('Evaluate equality with t', () => {
const program = '(eq (eq 3 3) t)';
const result = evaluate(program, {});
const expected = true;
assert.strictEqual(result, expected, 'Equality with t failed');
});

test('Evaluate equality with nil', () => {
const program = '(eq (eq 3 4) nil)';
const result = evaluate(program, {});
const expected = true;
assert.strictEqual(result, expected, 'Equality with nil failed');
});