From 3bd47fbe62e64da2ea18ca87c0699a3bb307b0a2 Mon Sep 17 00:00:00 2001 From: DrMaxNix Date: Tue, 6 Sep 2022 22:29:40 +0200 Subject: [PATCH] :sparkles: add support for negative numbers --- src/lexer.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/lexer.js b/src/lexer.js index 4428b63..0240a58 100644 --- a/src/lexer.js +++ b/src/lexer.js @@ -155,15 +155,33 @@ class Juicescript_lexer { break; + // NEGATIVE NUMBERS // + case "-": + // only if there's a valid digit after it + if(this.is_digit(this.peek())){ + // ignore minus sign + this.start += 1; + this.next(); + + // scan like a normal number + this.scan_number(true); + break; + } + + // ignore with warning + this.warning("unexpected character '" + char + "'"); + break; + + // EVERYTHING ELSE // default: - // number + // numbers if(this.is_digit(char)){ this.scan_number(); break; } - // identifier + // identifiers if(this.is_alpha(char)){ this.scan_identifier(); break; @@ -334,7 +352,7 @@ class Juicescript_lexer { /* SCANNER: Handle number */ - scan_number(){ + scan_number(negative = false){ // DEFAULT VALUES FOR BASE 10 // var base = null; var is_valid_char = this.is_digit; @@ -405,6 +423,9 @@ class Juicescript_lexer { var number = parseFloat(number_string); } + // maybe negate + if(negative) number *= -1; + // add token this.token_add({type: Juicescript.token_type.NUMBER, value: number}); }