UNPKG

bkc

Version:

:dog: If there are no dogs in Heaven, then when I die I want to go where they went.

152 lines (151 loc) 5.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ast_1 = require("../types/ast"); const error_1 = require("./error"); var NODE_TYPES; (function (NODE_TYPES) { NODE_TYPES["STRING"] = "string"; NODE_TYPES["NUMBER"] = "number"; NODE_TYPES["ARRAY"] = "object"; NODE_TYPES["OBJECT"] = "object"; })(NODE_TYPES = exports.NODE_TYPES || (exports.NODE_TYPES = {})); exports.checkExpr = (expr) => { if (expr === ast_1.EXPRESSION.PLUS || expr === ast_1.EXPRESSION.MINUS || expr === '*' || expr === '/' || expr === '=' || expr === '<' || expr === '>' || expr === '<=' || expr === '>=' || expr === '==' || expr === '!=') { return expr; } else { return null; } }; exports.checkExist = (anything) => { if (anything === undefined) { return false; } return true; }; const executeExpr = (exprE, arg1, arg2) => { const expr = exports.checkExpr(exprE); if (!expr) { throw error_1.error(error_1.ERROR_CODE.INVALID_EXPRESSION); } switch (expr) { case ast_1.EXPRESSION.PLUS: if (!exports.checkExist(arg2)) { throw error_1.error(error_1.ERROR_CODE.NOT_ENOUGH_ARGUMENT); } if (typeof arg1 === 'string') { return arg1 + arg2; } else if (typeof arg2 === 'string') { return arg1 + arg2; } else { return arg1 + arg2; } case ast_1.EXPRESSION.MINUS: if (!exports.checkExist(arg2)) { throw error_1.error(error_1.ERROR_CODE.NOT_ENOUGH_ARGUMENT); } if (typeof arg1 === 'number' && typeof arg2 === 'number') { return arg1 - arg2; } else { throw error_1.error(error_1.ERROR_CODE.ILLEGAL_CALCULATION); } case '*': if (!exports.checkExist(arg2)) { throw error_1.error(error_1.ERROR_CODE.NOT_ENOUGH_ARGUMENT); } if (typeof arg1 === 'number' && typeof arg2 === 'number') { return arg1 * arg2; } else { throw error_1.error(error_1.ERROR_CODE.ILLEGAL_CALCULATION); } case '/': if (!exports.checkExist(arg2)) { throw error_1.error(error_1.ERROR_CODE.NOT_ENOUGH_ARGUMENT); } if (typeof arg1 === 'number' && typeof arg2 === 'number') { return arg1 / arg2; } else { throw error_1.error(error_1.ERROR_CODE.ILLEGAL_CALCULATION); } case '=': return arg1; case '<': if (!exports.checkExist(arg2)) { throw error_1.error(error_1.ERROR_CODE.NOT_ENOUGH_ARGUMENT); } if (typeof arg1 === 'number' && typeof arg2 === 'number') { return arg1 < arg2 ? 1 : 0; } else { throw error_1.error(error_1.ERROR_CODE.ILLEGAL_CALCULATION); } case '>': if (!exports.checkExist(arg2)) { throw error_1.error(error_1.ERROR_CODE.NOT_ENOUGH_ARGUMENT); } if (typeof arg1 === 'number' && typeof arg2 === 'number') { return arg1 > arg2 ? 1 : 0; } else { throw error_1.error(error_1.ERROR_CODE.ILLEGAL_CALCULATION); } case '<=': if (!exports.checkExist(arg2)) { throw error_1.error(error_1.ERROR_CODE.NOT_ENOUGH_ARGUMENT); } if (typeof arg1 === 'number' && typeof arg2 === 'number') { return arg1 <= arg2 ? 1 : 0; } else { throw error_1.error(error_1.ERROR_CODE.ILLEGAL_CALCULATION); } case '>=': if (!exports.checkExist(arg2)) { throw error_1.error(error_1.ERROR_CODE.NOT_ENOUGH_ARGUMENT); } if (typeof arg1 === 'number' && typeof arg2 === 'number') { return arg1 >= arg2 ? 1 : 0; } else { throw error_1.error(error_1.ERROR_CODE.ILLEGAL_CALCULATION); } case '==': if (!exports.checkExist(arg2)) { throw error_1.error(error_1.ERROR_CODE.NOT_ENOUGH_ARGUMENT); } if (typeof arg1 === 'number' && typeof arg2 === 'number') { return arg1 === arg2 ? 1 : 0; } else { throw error_1.error(error_1.ERROR_CODE.ILLEGAL_CALCULATION); } case '!=': if (!exports.checkExist(arg2)) { throw error_1.error(error_1.ERROR_CODE.NOT_ENOUGH_ARGUMENT); } if (typeof arg1 === 'number' && typeof arg2 === 'number') { return arg1 !== arg2 ? 1 : 0; } else { throw error_1.error(error_1.ERROR_CODE.ILLEGAL_CALCULATION); } default: throw error_1.error(error_1.ERROR_CODE.ILLEGAL_CALCULATION); } }; exports.default = executeExpr;