From 25bd739a774883a9826a39f7fbf0ec3f57760d78 Mon Sep 17 00:00:00 2001 From: TheTastefulToastie <44216702+TheTastefulToastie@users.noreply.github.com> Date: Tue, 13 Nov 2018 14:06:55 +0000 Subject: [PATCH] Fix all the broken things [Issue #54] (#56) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix all the broken things... Man! This was so spaghetti'd I'm now Italian... ~ Removed a break from the switch statement in the constructor of CommandExecutor which meant that float values were pushed to the argument list twice (once as a float and then again as a command). ~ In Parser.parse() moved the call to nextToken() out of the else condition so that it is always executed. The intended functionality is to call parseExpression() when the argument type is int or float. After this change nextToken() was always executed regardless, then parseExpression was executed as well if the expression was an int or float, causing arguments to get skipped resulting in attempts to invoke numbers as if they were commands. ~ Renamed COMMAND_TYPES to ARGUMENT_TYPES but not everywhere causing ReferenceErrors ~ #30 Removed calls to parseInt() and parseFloat() in the CommandExecutor constructor because an argument may be an Expression object. #52 Added them back causing Expression objects to become NaN ~ Missing brace at end of parser.js ~ #50 also removed the calls to drawingBounds.reset() and drawingBounds.move(turtle.x, turtle.y) which obviously broke the autofit-drawing-to-canvas feature ~ The autofit-drawing-to-canvas feature was also broken because someone removed the call to the function ¯\_(ツ)_/¯ * Remove bounding box debug rect() * Switch statements should have breaks, not bugs :ant: * Changed variable name back In a previous commit changed 'str' to 'arg' when arg could be a mix of int-as-string/float-as-string/Expression #55 Changed this and now it will always be string so 'str' is more appropriate. * [Bugfix] Can't reselect default drawing Looks like selecting the 'Default' item in the dropdown used to clear the textarea and the drawing until [this](https://github.com/CodingTrain/Logo/pull/51/files#diff-9f2094443505273ff51ea1d1702e4367L101) commit removed it. So now the drawing remains unchanged when selecting the default item. It then resets the camera position and calls goTurtle() to redraw which causes the view to jump off-center. Instead it should put the default Logo in the textarea and call scaleToFitBoundingBox() to redraw _and_ center the drawing. This commit makes this change. --- command.js | 12 +++--- commandList.js | 16 -------- expression.js | 4 +- index.html | 12 ++---- parser.js | 5 --- sketch.js | 99 ++++++++++++++++++++++++-------------------------- turtle.js | 3 +- 7 files changed, 59 insertions(+), 92 deletions(-) diff --git a/command.js b/command.js index 69d8492..be8bf2e 100644 --- a/command.js +++ b/command.js @@ -47,13 +47,13 @@ class CommandArg { this.validator = (str) => { return /^[-+]?[0-9]*\.?[0-9]*$/.test(str); } - + break; case ARGUMENT_TYPES.EXPRESSION: this.validator = (str) => { let res = /^[-+]?([0-9]+\.?[0-9]?|[0-9]*\.?[0-9]+)(\s[+-/*]{1}\s[-+]?([0-9]+\.?[0-9]?|[0-9]*\.?[0-9]+))*$/.test(str); return res; } - + break; } } else this.validator = validator; @@ -117,16 +117,15 @@ class CommandExecutor { break; case ARGUMENT_TYPES.FLOAT: this.values.push(parseFloat(value)); + break; case ARGUMENT_TYPES.COMMANDS: this.values.push( new Parser(value, this.callback).parse() ); break; - case ARGUMENT_TYPES.EXPRESSION: - //console.log(this.parseExpression(value)) - this.values.push(this.parseExpression(value).eval()) - + this.values.push(this.parseExpression(value).eval()); + break; case ARGUMENT_TYPES.PARAMETERS: // Example this.values.push(value.split(" ")); break; @@ -147,7 +146,6 @@ class CommandExecutor { e = new Expression(next,new Expression('$',token), right); } else return new Expression('$', token); - //console.log(right); if (right.lvl() > e.lvl()) { let new_left = new Expression(next, new Expression('$',token), right.left); e = new Expression(right.type, new_left, right.right); diff --git a/commandList.js b/commandList.js index b15358b..73eb84a 100644 --- a/commandList.js +++ b/commandList.js @@ -9,33 +9,25 @@ const commandLookUp = new CommandLookUp(); * and then the function to execute. */ commandLookUp.add( - new Command("fd", [new CommandArg("value", ARGUMENT_TYPES.EXPRESSION)], value => { - turtle.forward(value); }) ); commandLookUp.add( - new Command("bd", [new CommandArg("value", ARGUMENT_TYPES.EXPRESSION)], value => { - turtle.forward(-value); }) ); commandLookUp.add( - new Command("rt", [new CommandArg("value", ARGUMENT_TYPES.EXPRESSION)], value => { - turtle.right(value); }) ); commandLookUp.add( - new Command("lt", [new CommandArg("value", ARGUMENT_TYPES.EXPRESSION)], value => { - turtle.right(-value); }) ); @@ -55,9 +47,7 @@ commandLookUp.add( commandLookUp.add( new Command( "pensize", - [new CommandArg("size", ARGUMENT_TYPES.EXPRESSION)], - size => { turtle.strokeWeight = size; } @@ -68,10 +58,8 @@ commandLookUp.add( new Command( "setxy", [ - new CommandArg("x", ARGUMENT_TYPES.EXPRESSION), new CommandArg("y", ARGUMENT_TYPES.EXPRESSION) - ], (x, y) => { turtle.x = x; @@ -81,17 +69,13 @@ commandLookUp.add( ); commandLookUp.add( - new Command("setx", [new CommandArg("x", ARGUMENT_TYPES.EXPRESSION)], x => { - turtle.x = x; }) ); commandLookUp.add( - new Command("sety", [new CommandArg("y", ARGUMENT_TYPES.EXPRESSION)], y => { - turtle.y = y; }) ); diff --git a/expression.js b/expression.js index ff79632..e4d2e12 100644 --- a/expression.js +++ b/expression.js @@ -7,8 +7,8 @@ class Expression { eval() { if(this.type == '$') { - return parseFloat(this.left); - } else if(this.type == '/') { + return parseFloat(this.left); + } else if(this.type == '/') { return this.left.eval() / this.right.eval(); } else if(this.type == '*') { return this.left.eval() * this.right.eval(); diff --git a/index.html b/index.html index 7483bc6..caf21c9 100644 --- a/index.html +++ b/index.html @@ -34,22 +34,18 @@ - +
- + - + Icons made by Turtle from www.flaticon.com is licensed by CC 3.0 BY - - - +