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

Merged
merged 7 commits into from
Jan 16, 2025
Merged
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
48 changes: 47 additions & 1 deletion lib/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,30 @@

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

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

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

interpret() {
return this.value;
}
}

class NumberExpression {
constructor(value) {
this.type = 'number';
this.value = parseFloat(value);
}

Expand All @@ -22,6 +44,7 @@ class NumberExpression {

class VariableExpression {
constructor(name) {
this.type = 'variable';
this.name = name;
}

Expand All @@ -43,6 +66,7 @@ class VariableExpression {

class OperationExpression {
constructor(operator, operands) {
this.type = 'operation';
this.identifiers = new Set();
this.operator = operator;
this.operands = operands;
Expand Down Expand Up @@ -76,4 +100,26 @@ class OperationExpression {
}
}

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

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

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

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

const tokenize = (source) => {
const stack = [];
Expand Down Expand Up @@ -33,15 +29,14 @@ const tokenize = (source) => {
};

const parse = (tokens) => {
if (!Array.isArray(tokens)) {
const isVar = isNaN(tokens);
const Expression = isVar ? VariableExpression : NumberExpression;
const Expression = getExpressionClass(tokens);
if (Expression !== expressions.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');
});
Loading