From 19a0f5d4eb01664e03b5bd88b99d34740f9ba5ec Mon Sep 17 00:00:00 2001 From: Christopher Willis Date: Wed, 16 Jan 2019 01:35:51 -0600 Subject: [PATCH 1/5] basic functions mostly up --- index.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/index.js b/index.js index 8b13789..ae382cd 100644 --- a/index.js +++ b/index.js @@ -1 +1,68 @@ +'use strict'; +const readline = require('readline'); + + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +const nodePlayground = (answer) => { + const splitAnswer = answer.trim().split(" "); + if(splitAnswer.length==3){ + const firstTerm = Number(splitAnswer[0]); + const secondTerm = Number(splitAnswer[2]); + + if(splitAnswer[1]=='+'){ + return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm+secondTerm}` + } + else if(splitAnswer[1]=='-'){ + return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm-secondTerm}` + } + else if(splitAnswer[1]=='*'){ + return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm*secondTerm}` + } + else if(splitAnswer[1]=='/'){ + return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm/secondTerm}` + } + else if(splitAnswer[1]=='<'){ + return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm'){ + return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm>secondTerm}` + } + else if(splitAnswer[1]=='==='){ + return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm===secondTerm}` + } + else if(splitAnswer[1]=='!=='){ + return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm!==secondTerm}` + } + else{ + return "Syntax Error" + } + }else if (splitAnswer.length==4){ + const varType = splitAnswer[0]; + const varName = splitAnswer[1]; + const varValue = splitAnswer[3]; + if(variableSpace[varName] && variableSpace[varName].type == "const"){ + return "TypeError: Assignment to constant variable" + } + variableSpace[splitAnswer[1]] = {value:splitAnswer[3],type:splitAnswer[0]} + }else{ + return "Syntax Error" + } + return variableSpace; +} + + +const getPrompt = () => { + rl.question('Node playground, enter values ', (answer) => { + console.log(nodePlayground(answer)); + getPrompt(); + }) + } + +const variableSpace = {}; + +getPrompt(); \ No newline at end of file From 1794a9771d8f3b1b059823f8de4360ed2e01de92 Mon Sep 17 00:00:00 2001 From: Christopher Willis Date: Wed, 16 Jan 2019 02:49:35 -0600 Subject: [PATCH 2/5] varibles names can now be changed --- index.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ae382cd..4de3487 100644 --- a/index.js +++ b/index.js @@ -8,11 +8,23 @@ const rl = readline.createInterface({ output: process.stdout }); +const nameTester = (name,index) => { + if(variableSpace[index]){ + return Number(name.value); + } +} + const nodePlayground = (answer) => { const splitAnswer = answer.trim().split(" "); if(splitAnswer.length==3){ - const firstTerm = Number(splitAnswer[0]); - const secondTerm = Number(splitAnswer[2]); + const firstTerm = isNaN(Number(splitAnswer[0]))? nameTester(variableSpace[splitAnswer[0]],splitAnswer[0]) : Number(splitAnswer[0]); + const secondTerm = isNaN(Number(splitAnswer[2]))? nameTester(variableSpace[splitAnswer[2]],splitAnswer[2]) : Number(splitAnswer[2]); + if(!firstTerm){ + return `ReferenceError: ${splitAnswer[0]} is not defined` + }else if(!secondTerm){ + return `ReferenceError: ${splitAnswer[2]} is not defined` + + } if(splitAnswer[1]=='+'){ return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm+secondTerm}` @@ -38,6 +50,13 @@ const nodePlayground = (answer) => { else if(splitAnswer[1]=='!=='){ return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm!==secondTerm}` } + else if(splitAnswer[1]=='='){ + if(variableSpace[firstTerm] && variableSpace[firstTerm].type == 'const'){ + return "TypeError: Assignment to constant variable" + } + variableSpace[splitAnswer[0]] = {value: splitAnswer[2],type:variableSpace[splitAnswer[0]].type} + return variableSpace[splitAnswer[0]].value; + } else{ return "Syntax Error" } @@ -49,10 +68,15 @@ const nodePlayground = (answer) => { return "TypeError: Assignment to constant variable" } variableSpace[splitAnswer[1]] = {value:splitAnswer[3],type:splitAnswer[0]} + return varValue + }else if(splitAnswer.length==1){ + if(variableSpace[splitAnswer[0]]){ + return variableSpace[splitAnswer[0]].value + } + return `ReferenceError: ${splitAnswer[0]} is not defined` }else{ return "Syntax Error" } - return variableSpace; } From 9f388c86cbde8d666c67988c705359311165920f Mon Sep 17 00:00:00 2001 From: Christopher Willis Date: Wed, 16 Jan 2019 03:01:04 -0600 Subject: [PATCH 3/5] Different states error states defined --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4de3487..2675c41 100644 --- a/index.js +++ b/index.js @@ -51,7 +51,7 @@ const nodePlayground = (answer) => { return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm!==secondTerm}` } else if(splitAnswer[1]=='='){ - if(variableSpace[firstTerm] && variableSpace[firstTerm].type == 'const'){ + if(variableSpace[splitAnswer[0]] && variableSpace[splitAnswer[0]].type == 'const'){ return "TypeError: Assignment to constant variable" } variableSpace[splitAnswer[0]] = {value: splitAnswer[2],type:variableSpace[splitAnswer[0]].type} @@ -64,8 +64,11 @@ const nodePlayground = (answer) => { const varType = splitAnswer[0]; const varName = splitAnswer[1]; const varValue = splitAnswer[3]; + if (!(varType=="const" || varType == "let" || varType == "var") ){ + return "SyntaxError: Unexpected identifier, " + varType + } if(variableSpace[varName] && variableSpace[varName].type == "const"){ - return "TypeError: Assignment to constant variable" + return `SyntaxError: Identifier, ${varName}, has already been declared` } variableSpace[splitAnswer[1]] = {value:splitAnswer[3],type:splitAnswer[0]} return varValue From 21a174fca62b96e483bc0b83f10bdf98e0356df0 Mon Sep 17 00:00:00 2001 From: Christopher Willis Date: Wed, 16 Jan 2019 19:18:01 -0600 Subject: [PATCH 4/5] many much comments --- index.js | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 2675c41..58aee8c 100644 --- a/index.js +++ b/index.js @@ -15,17 +15,29 @@ const nameTester = (name,index) => { } const nodePlayground = (answer) => { - const splitAnswer = answer.trim().split(" "); + const splitAnswer = answer.trim().replace(/ +(?= )/g,'').split(" "); + // we are nice to the user, to nice probably. We trim leadning and trailing + // spaces and knock out any silly spaces in between since we are seperating + // on a join, interal spacing is important. If they run everything together, + // and have no spacing, we will not save the user from this if(splitAnswer.length==3){ + // if we split the string and there are 3 bits, some kind of math or logic + // was done, test for what kinda logic we need to do. const firstTerm = isNaN(Number(splitAnswer[0]))? nameTester(variableSpace[splitAnswer[0]],splitAnswer[0]) : Number(splitAnswer[0]); const secondTerm = isNaN(Number(splitAnswer[2]))? nameTester(variableSpace[splitAnswer[2]],splitAnswer[2]) : Number(splitAnswer[2]); - if(!firstTerm){ + // esoteric bit of logic. Basically, was a number written? + // if so, proceed. If not check to see if it is a variable we know about + // and replace it inline, if not logic below throws refference error. + + // can't test for falsy cause 0 is a valid entry + if(firstTerm===undefined){ return `ReferenceError: ${splitAnswer[0]} is not defined` - }else if(!secondTerm){ + }else if(secondTerm===undefined){ return `ReferenceError: ${splitAnswer[2]} is not defined` } - + // test for operand and perform operation, prob a smarter way to do this + // but went with brute force...and it hurts! if(splitAnswer[1]=='+'){ return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm+secondTerm}` } @@ -51,45 +63,67 @@ const nodePlayground = (answer) => { return `${firstTerm}${splitAnswer[1]}${secondTerm}=${firstTerm!==secondTerm}` } else if(splitAnswer[1]=='='){ + // fancy bit for reasigning varibles. if it exists in variable namespace + // see if it isn't a const, if it is, throw error if(variableSpace[splitAnswer[0]] && variableSpace[splitAnswer[0]].type == 'const'){ return "TypeError: Assignment to constant variable" } + // if not const, reasign value, keep existing type variableSpace[splitAnswer[0]] = {value: splitAnswer[2],type:variableSpace[splitAnswer[0]].type} - return variableSpace[splitAnswer[0]].value; + return splitAnswer[0] +" => "+ variableSpace[splitAnswer[0]].value; } else{ + // if we have 3 fields and one isn't listed about, its not something + // we are going to handel return "Syntax Error" } }else if (splitAnswer.length==4){ + // len 4 implies variable creation const varType = splitAnswer[0]; const varName = splitAnswer[1]; const varValue = splitAnswer[3]; + + // if it isn't a recognized type, blow up if (!(varType=="const" || varType == "let" || varType == "var") ){ return "SyntaxError: Unexpected identifier, " + varType } - if(variableSpace[varName] && variableSpace[varName].type == "const"){ + + // if variable already exists in namespace, blow up + if(variableSpace[varName]){ return `SyntaxError: Identifier, ${varName}, has already been declared` } + + // finally, if it doesn't exist and is the correct variable type + // create namespace object with name as key to data object, + // data object tells what kind of datatype and its value variableSpace[splitAnswer[1]] = {value:splitAnswer[3],type:splitAnswer[0]} - return varValue + return varName+ " => " + varValue }else if(splitAnswer.length==1){ + // look at namespace and return the value if(variableSpace[splitAnswer[0]]){ return variableSpace[splitAnswer[0]].value } + + // blow up if you don't find it return `ReferenceError: ${splitAnswer[0]} is not defined` }else{ + return "Syntax Error" } } const getPrompt = () => { + + // recursive call, we are here till end of time rl.question('Node playground, enter values ', (answer) => { console.log(nodePlayground(answer)); getPrompt(); }) } + +// container of all created varibles const variableSpace = {}; getPrompt(); \ No newline at end of file From c5f37d0aca84974ece872c4202c0e542a1faedbf Mon Sep 17 00:00:00 2001 From: Christopher Willis Date: Wed, 16 Jan 2019 20:57:49 -0600 Subject: [PATCH 5/5] works for boolean inputs --- index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.js b/index.js index 58aee8c..ea9c78f 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,9 @@ const rl = readline.createInterface({ const nameTester = (name,index) => { if(variableSpace[index]){ return Number(name.value); + }else if(index==="true"||index==="false"){ + return JSON.parse(index) + } } @@ -21,6 +24,12 @@ const nodePlayground = (answer) => { // on a join, interal spacing is important. If they run everything together, // and have no spacing, we will not save the user from this if(splitAnswer.length==3){ + + // console.log(JSON.parse(splitAnswer[0])) + // console.log(Number(JSON.parse(splitAnswer[0]))) + // console.log(isNaN(Number(JSON.parse(splitAnswer[0])))) + + // if we split the string and there are 3 bits, some kind of math or logic // was done, test for what kinda logic we need to do. const firstTerm = isNaN(Number(splitAnswer[0]))? nameTester(variableSpace[splitAnswer[0]],splitAnswer[0]) : Number(splitAnswer[0]); @@ -93,6 +102,10 @@ const nodePlayground = (answer) => { return `SyntaxError: Identifier, ${varName}, has already been declared` } + if(!isNaN(varName)){ + return `SyntaxError: Unexpected number, ${varName}, invalid variable name` + } + // finally, if it doesn't exist and is the correct variable type // create namespace object with name as key to data object, // data object tells what kind of datatype and its value @@ -126,4 +139,5 @@ const getPrompt = () => { // container of all created varibles const variableSpace = {}; + getPrompt(); \ No newline at end of file