Skip to content

Commit

Permalink
syntax: parse "not not x" (google#129)
Browse files Browse the repository at this point in the history
+Test
  • Loading branch information
adonovan authored Oct 4, 2018
1 parent ea6a6cb commit c7df045
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
25 changes: 12 additions & 13 deletions syntax/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ func (p *parser) parseTestPrec(prec int) Expr {
// expr = NOT expr
if p.tok == NOT && prec == int(precedence[NOT]) {
pos := p.nextToken()
x := p.parseTestPrec(prec + 1)
x := p.parseTestPrec(prec)
return &UnaryExpr{
OpPos: pos,
Op: NOT,
Expand Down Expand Up @@ -591,16 +591,16 @@ var precedence [maxToken]int8
// Unary MINUS, unary PLUS, and TILDE have higher precedence so are handled in parsePrimary.
// See https://github.com/google/skylark/blob/master/doc/spec.md#binary-operators
var preclevels = [...][]Token{
{OR}, // or
{AND}, // and
{NOT}, // not (unary)
{OR}, // or
{AND}, // and
{NOT}, // not (unary)
{EQL, NEQ, LT, GT, LE, GE, IN, NOT_IN}, // == != < > <= >= in not in
{PIPE}, // |
{CIRCUMFLEX}, // ^
{AMP}, // &
{LTLT, GTGT}, // << >>
{MINUS, PLUS}, // -
{STAR, PERCENT, SLASH, SLASHSLASH}, // * % / //
{PIPE}, // |
{CIRCUMFLEX}, // ^
{AMP}, // &
{LTLT, GTGT}, // << >>
{MINUS, PLUS}, // -
{STAR, PERCENT, SLASH, SLASHSLASH}, // * % / //
}

func init() {
Expand Down Expand Up @@ -759,7 +759,7 @@ func (p *parser) parseArgs() []Expr {
// | '[' ... // list literal or comprehension
// | '{' ... // dict literal or comprehension
// | '(' ... // tuple or parenthesized expression
// | ('-'|'+') primary_with_suffix
// | ('-'|'+'|'~') primary_with_suffix
func (p *parser) parsePrimary() Expr {
switch p.tok {
case IDENT:
Expand Down Expand Up @@ -805,8 +805,7 @@ func (p *parser) parsePrimary() Expr {
Rparen: rparen,
}

case MINUS, PLUS, TILDE:
// unary minus/plus/tilde:
case MINUS, PLUS, TILDE: // unary
tok := p.tok
pos := p.nextToken()
x := p.parsePrimaryWithSuffix()
Expand Down
24 changes: 14 additions & 10 deletions testdata/bool.sky
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ load("assert.sky", "assert")
# truth
assert.true(True)
assert.true(not False)
assert.true(not not True)

# bool conversion
assert.eq([bool(), bool(1), bool(0), bool("hello"), bool("")],
[False, True, False, True, False])
assert.eq(
[bool(), bool(1), bool(0), bool("hello"), bool("")],
[False, True, False, True, False],
)

# comparison
assert.true(None == None)
assert.true(None != False)
assert.true(None != True)
assert.eq(1==1, True)
assert.eq(1==2, False)
assert.eq(1 == 1, True)
assert.eq(1 == 2, False)
assert.true(False == False)
assert.true(True == True)

Expand All @@ -34,10 +37,11 @@ assert.eq(1 if "" else 0, 0)

# short-circuit evaluation of 'and' and 'or':
# 'or' yields the first true operand, or the last if all are false.
assert.eq(0 or "" or [] or 0, 0)
assert.eq(0 or "" or [] or 123 or 1/0, 123)
assert.fails(lambda: 0 or "" or [] or 0 or 1/0, "division by zero")
assert.eq(0 or "" or [] or 0, 0)
assert.eq(0 or "" or [] or 123 or 1 / 0, 123)
assert.fails(lambda : 0 or "" or [] or 0 or 1 / 0, "division by zero")

# 'and' yields the first false operand, or the last if all are true.
assert.eq(1 and "a" and [1] and 123, 123)
assert.eq(1 and "a" and [1] and 0 and 1/0, 0)
assert.fails(lambda: 1 and "a" and [1] and 123 and 1/0, "division by zero")
assert.eq(1 and "a" and [1] and 123, 123)
assert.eq(1 and "a" and [1] and 0 and 1 / 0, 0)
assert.fails(lambda : 1 and "a" and [1] and 123 and 1 / 0, "division by zero")

0 comments on commit c7df045

Please sign in to comment.