From 4ef832510536603e7649e7ddec4f55fa11966b18 Mon Sep 17 00:00:00 2001 From: Anurag Hazru Date: Sun, 11 Nov 2018 18:53:56 +0530 Subject: [PATCH] testCommand() for refactor of test (#41) --- parser.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/parser.js b/parser.js index 6900946..ed68d27 100644 --- a/parser.js +++ b/parser.js @@ -78,6 +78,32 @@ class Parser { parse() { let cmdsExecutors = []; while (this.remainingTokens()) { + let token = this.nextToken(); + let cmd = undefined; + + // testCommand to refactor + function testCommand(value, data) { return value.test(data) } + + if (testCommand(movement, token)) { + cmd = new Command(token, parseFloat(this.nextToken())); + } else if (testCommand(noArgsCalls, token)) { + cmd = new Command(token); + } else if (testCommand(repeat, token)) { + cmd = new Command(token, parseInt(this.nextToken())); + let toRepeat = this.getRepeat(); + let parser = new Parser(toRepeat); + cmd.commands = parser.parse(); + } else if (testCommand(setxy,token)) { + cmd = new Command(token); + let argX = this.nextToken(); + let argY = this.nextToken(); + cmd.arg = [parseFloat(argX), parseFloat(argY)]; + } else if (testCommand(color, token)) { + cmd = new Command(token, this.nextToken()); + } else if (testCommand(setxySingle,token)) { + cmd = new Command(token, parseFloat(this.nextToken())); + } + const actualToken = this.nextToken(); let cmd = commandLookUp.get(actualToken);