From fe8e10ad934a3e1d753ccd96aed3bea5e2a36931 Mon Sep 17 00:00:00 2001 From: rsdexter Date: Sun, 5 Apr 2020 17:37:00 -0400 Subject: [PATCH] added initial array building and console log tests --- src/converters.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/converters.js b/src/converters.js index 021e699..e5e6705 100644 --- a/src/converters.js +++ b/src/converters.js @@ -89,11 +89,30 @@ const convertToEG = (formula) => { */ const convertToArray = (formula) => { if (typeof formula === 'string' || formula instanceof String) { - //let index = 0; - return null - - - + // hold the current index we are looking at in the string + let i = 0; + let arr = [] + // loop until parentheses found to add all elements at this level to the array + while (i < formula.length) { + // if parenthesis found, recursively call this function on the subexpression + if (formula[i] == '(') { + // find the matching closing parenthesis + let j = formula.lastIndexOf(')'); + //((({P})){Q}{R}){P} + console.log("Found parens at " + i + " and " + j) + arr.push(convertToArray(formula.substr(i+1, j-1))) + i = j + } + // if a variable is found, add it + else if(formula[i] == '{') { + arr.push(formula[++i]) + i++ + } + i++ + } + // otherwise, return the array that values this expression + console.log("array at this level: " + arr) + return arr } else return null } -- 2.51.2