Skip to content

Commit

Permalink
Add collection for expressions
Browse files Browse the repository at this point in the history
Additionly type field was added to all Expression classes
Refs: metarhia#5
  • Loading branch information
zpwebbear committed Dec 9, 2024
1 parent cb65abe commit 900cee1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
38 changes: 36 additions & 2 deletions lib/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ 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 = value;
this.value = BOOL[value];
}

interpret() {
return BOOL[this.value];
return this.value;
}
}

class NumberExpression {
type = 'number';

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

class VariableExpression {
type = 'variable';

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

class OperationExpression {
type = 'operation';

constructor(operator, operands) {
this.operator = operator;
this.operands = operands;
Expand All @@ -59,10 +69,34 @@ class 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,
};
24 changes: 8 additions & 16 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';

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

const tokenize = (source) => {
Expand Down Expand Up @@ -34,22 +32,16 @@ const tokenize = (source) => {
return stack[0];
};

const expressionFactory = (token) => {
if (!isNaN(token)) return new NumberExpression(token);
if (token in BOOL) {
return new BooleanExpression(token);
}
return new VariableExpression(token);
};

const parse = (tokens) => {
if (!Array.isArray(tokens)) {
return expressionFactory(tokens);
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

0 comments on commit 900cee1

Please sign in to comment.