From 8327a0871884f945aee38a96557d2644e6db75b4 Mon Sep 17 00:00:00 2001 From: satyr Date: Sun, 21 Oct 2012 22:31:46 +0900 Subject: [PATCH] fixed `n&-n` --- lib/lexer.js | 15 +++++++++------ src/lexer.co | 11 +++++++---- test/operator.co | 2 ++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/lexer.js b/lib/lexer.js index b3b52a495..5064254b2 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -1160,9 +1160,7 @@ function addImplicitParentheses(tokens){ } tag = token[0]; prev = tokens[i - 1]; - if (tag === '[') { - brackets.push(prev[0] === 'DOT'); - } + tag === '[' && brackets.push(prev[0] === 'DOT'); if (prev[0] === ']') { if (brackets.pop()) { prev.index = true; @@ -1170,7 +1168,7 @@ function addImplicitParentheses(tokens){ continue; } } - if (tag === 'BITWISE' && token[1] === '&' && !of$(tokens[i + 1][0], ARG)) { + if (tag === 'BITWISE' && token[1] === '&' && !exp(tokens[i + 1])) { tag = token[0] = 'LITERAL'; } if (!(((ref$ = prev[0]) === 'FUNCTION' || ref$ === 'LET') || prev.spaced && able(tokens, i, true))) { @@ -1181,11 +1179,11 @@ function addImplicitParentheses(tokens){ tpair[0] = ')CALL'; continue; } - if (!(of$(tag, ARG) || !token.spaced && (tag === '+-' || tag === '^'))) { + if (!exp(token)) { continue; } if (tag === 'CREMENT') { - if (token.spaced || !of$((ref$ = tokens[i + 1]) != null ? ref$[0] : void 8, CHAIN)) { + if (token.spaced || !of$(tokens[i + 1][0], CHAIN)) { continue; } } @@ -1193,6 +1191,11 @@ function addImplicitParentheses(tokens){ tokens.splice(i++, 0, ['CALL(', '', token[2]]); detectEnd(tokens, i, ok, go); } + function exp(token){ + var tag; + tag = token[0]; + return of$(tag, ARG) || !token.spaced && (tag === '+-' || tag === '^'); + } function ok(token, i){ var tag, ref$, pre; tag = token[0]; diff --git a/src/lexer.co b/src/lexer.co index 60a0b9932..377a76121 100644 --- a/src/lexer.co +++ b/src/lexer.co @@ -796,10 +796,10 @@ character = if JSON!? then uxxxx else -> token.doblock = true tokens.splice i, 1 [tag] = token; prev = tokens[i-1] - brackets.push prev.0 is \DOT if tag is \[ + tag is \[ and brackets.push prev.0 is \DOT if prev.0 is \] if brackets.pop! then prev.index = true else continue - if tag is \BITWISE and token.1 is \& and tokens[i+1]0 not of ARG + if tag is \BITWISE and token.1 is \& and not exp tokens[i+1] tag = token.0 = \LITERAL continue unless prev.0 of <[ FUNCTION LET ]> or prev.spaced and able tokens, i, true @@ -807,12 +807,15 @@ character = if JSON!? then uxxxx else -> token.0 = \CALL( tpair.0 = \)CALL continue - continue unless tag of ARG or not token.spaced and tag of <[ +- ^ ]> + continue unless exp token if tag is \CREMENT - continue if token.spaced or tokens[i+1]?0 not of CHAIN + continue if token.spaced or tokens[i+1]0 not of CHAIN skipBlock = seenSwitch = false tokens.splice i++, 0 [\CALL( '' token.2] detectEnd tokens, i, ok, go + # Does the token start an expression? + function exp [tag]:token + tag of ARG or not token.spaced and tag of <[ +- ^ ]> function ok token, i tag = token.0 return true if tag of <[ |> POST_IF ]> diff --git a/test/operator.co b/test/operator.co index 7cb729e1a..6b5f64309 100644 --- a/test/operator.co +++ b/test/operator.co @@ -446,6 +446,8 @@ n = 1 eq 1, n&n eq 1, n& n eq 1, n & n +eq 1, n&-n +eq 1, n & -n compileThrows 'stray reference' 2 '\n&'