✨ add support for negative numbers
This commit is contained in:
parent
b4774f2405
commit
3bd47fbe62
27
src/lexer.js
27
src/lexer.js
@ -155,15 +155,33 @@ class Juicescript_lexer {
|
|||||||
break;
|
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 //
|
// EVERYTHING ELSE //
|
||||||
default:
|
default:
|
||||||
// number
|
// numbers
|
||||||
if(this.is_digit(char)){
|
if(this.is_digit(char)){
|
||||||
this.scan_number();
|
this.scan_number();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// identifier
|
// identifiers
|
||||||
if(this.is_alpha(char)){
|
if(this.is_alpha(char)){
|
||||||
this.scan_identifier();
|
this.scan_identifier();
|
||||||
break;
|
break;
|
||||||
@ -334,7 +352,7 @@ class Juicescript_lexer {
|
|||||||
/*
|
/*
|
||||||
SCANNER: Handle number
|
SCANNER: Handle number
|
||||||
*/
|
*/
|
||||||
scan_number(){
|
scan_number(negative = false){
|
||||||
// DEFAULT VALUES FOR BASE 10 //
|
// DEFAULT VALUES FOR BASE 10 //
|
||||||
var base = null;
|
var base = null;
|
||||||
var is_valid_char = this.is_digit;
|
var is_valid_char = this.is_digit;
|
||||||
@ -405,6 +423,9 @@ class Juicescript_lexer {
|
|||||||
var number = parseFloat(number_string);
|
var number = parseFloat(number_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// maybe negate
|
||||||
|
if(negative) number *= -1;
|
||||||
|
|
||||||
// add token
|
// add token
|
||||||
this.token_add({type: Juicescript.token_type.NUMBER, value: number});
|
this.token_add({type: Juicescript.token_type.NUMBER, value: number});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user