Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The parser should execute lines of basic Clojure commands (defining function and some math operations).
- Also It has to print the result of the last given line of code. Each line starts with “(“and ends with “)”.
- There could be more than one whitespace between the characters. For example
- ( + 4 1 ) //Also has to return 5
- Also you can use old functions in the definitions of the new one.
- If you meet a function in a command it’ll be always defined in some of the lines before!
- There will be no more than 1 nested function in the definition of a new one.
- Example: Command can be (def func (+ 23 4 8 4 5 7)), but it won’t be (def func (+ 23 4 (* 2 2) 4 5 7)).
- There will be no nested functions in the last line. Only math operation and a sequence of numbers and functions.
- Example: Last line can be (+ 23 4 8 myFunc 5 7), but it won’t be (+ 23 4 (* 2 myFunc) 4 newFunc 7)).
- You are given an array of strings (commands). Execute all the commands and print the result
- only from the last line! */
- "use strict";
- function solve(args) {
- var funcList = {};
- for (var row = 0; row < args.length; row += 1) {
- var currentRow = args[row].trim();
- currentRow = currentRow.replace(/\s+/g, ' ');
- var funcName = '', funcValue, temp;
- var params = [];
- if (currentRow.lastIndexOf('(') && row < args.length - 1) {
- var outerStr = currentRow.substring(1, currentRow.lastIndexOf('('));
- var nestedStr = currentRow.substring((currentRow.lastIndexOf('(') + 1), currentRow.length - 2);
- var funcParams = outerStr.split(/\s/).filter(Boolean);
- if (funcParams[0] === 'def' && funcParams.length == 2) {
- funcName = funcParams[1];
- }
- params = nestedStr.split(/\s/).filter(Boolean);
- temp = calculateFuncExpression(params);
- } else {
- currentRow = currentRow.substring(1, currentRow.length - 1);
- currentRow = currentRow.split(/\s/).filter(Boolean);
- if (currentRow[0] === 'def' && currentRow.length == 3) {
- if (row < args.length - 1) {
- funcName = currentRow[1];
- }
- if (isFinite(currentRow[2])) {
- funcValue = parseInt(currentRow[2]);
- } else if (funcList.hasOwnProperty(currentRow[2])) {
- funcValue = funcList[currentRow[2]];
- }
- if (row < args.length - 1 && !(funcName in funcList) && funcName.length &&
- (typeof funcValue == 'number')) {
- funcList[funcName] = funcValue;
- } else if (row == args.length - 1 && (typeof funcValue == 'number')) {
- return console.log(funcValue);
- }
- } else if (row == args.length - 1 && (currentRow[0] == '+' || currentRow[0] == '-' ||
- currentRow[0] == '*' || currentRow[0] == '/')) {
- temp = calculateFuncExpression(currentRow);
- }
- }
- if (temp === 'Error') {
- return console.log('Division by zero! At Line:' + (row + 1));
- }
- funcValue = temp;
- if (row < args.length - 1 && funcName.length && !(funcName in funcList) &&
- (typeof funcValue == 'number')) {
- funcList[funcName] = funcValue;
- } else if (row == args.length - 1 && typeof funcValue == 'number') {
- console.log(funcValue);
- }
- }
- function calculateFuncExpression(params) {
- var operation = '';
- if (params[0] == '+' || params[0] == '-' || params[0] == '*' || params[0] == '/') {
- operation = params[0];
- }
- params = parsingFuncArguments(params);
- var i, result;
- switch (operation) {
- case '+':
- result = 0;
- for (i = 0; i < params.length; i += 1) {
- result += params[i];
- }
- return result;
- case '-':
- result = params[0];
- for (i = 1; i < params.length; i += 1) {
- result -= params[i];
- }
- return result;
- case '*':
- result = 1;
- for (i = 0; i < params.length; i += 1) {
- result *= params[i];
- }
- return result;
- case '/':
- result = params[0];
- for (i = 1; i < params.length; i += 1) {
- if (params[i] === 0) {
- return 'Error';
- }
- result /= params[i];
- }
- return parseInt(result);
- }
- }
- function parsingFuncArguments(argums) {
- var newArgs = [];
- for (var i = 1; i < argums.length; i += 1) {
- if (isFinite(argums[i])) {
- newArgs.push(parseInt(argums[i]));
- } else if (funcList.hasOwnProperty(argums[i])) {
- newArgs.push(funcList[argums[i]]);
- }
- }
- return newArgs;
- }
- }
- solve([
- '(def myFunc 5)',
- '(def myNewFunc (+ myFunc myFunc 2))',
- '(def MyFunc (* 2 myNewFunc))',
- '(/ MyFunc myFunc)'
- ]);
- solve([
- '(def func 10)',
- '(def newFunc (+ func 2))',
- '(def sumFunc (+ func func newFunc 0 0 0))',
- '(* sumFunc 2)'
- ]);
- solve([
- '(def func (+ 5 2))',
- '(def func2 (/ func 5 2 1 0))',
- '(def func3 (/ func 2))',
- '(+ func3 func)'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement