Skip to content

Commit

Permalink
resolve: enable bitwise (& | ~ ^ << >>) operators always (google#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
adonovan committed Jan 30, 2019
1 parent 1258e4d commit 2f3bb7c
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 28 deletions.
1 change: 0 additions & 1 deletion cmd/starlark/starlark.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func init() {
flag.BoolVar(&resolve.AllowSet, "set", resolve.AllowSet, "allow set data type")
flag.BoolVar(&resolve.AllowLambda, "lambda", resolve.AllowLambda, "allow lambda expressions")
flag.BoolVar(&resolve.AllowNestedDef, "nesteddef", resolve.AllowNestedDef, "allow nested def statements")
flag.BoolVar(&resolve.AllowBitwise, "bitwise", resolve.AllowBitwise, "allow bitwise operations (&, |, ^, ~, <<, and >>)")
flag.BoolVar(&resolve.AllowRecursion, "recursion", resolve.AllowRecursion, "allow while statements and recursive functions")
flag.BoolVar(&resolve.AllowGlobalReassign, "globalreassign", resolve.AllowGlobalReassign, "allow reassignment of globals, and if/for/while statements at top level")
}
Expand Down
7 changes: 1 addition & 6 deletions doc/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,6 @@ arithmetic is exact, motivated by the need for lossless manipulation
of protocol messages which may contain signed and unsigned 64-bit
integers.
The Java implementation currently supports only signed 32-bit integers.

The Go implementation of Starlark requires the `-bitwise` flag to
enable support for `&`, `|`, `^`, `~`, `<<`, and `>>` operations.
The Java implementation does not support `^`, `~`, `<<`, and `>>` operations.


Expand Down Expand Up @@ -837,8 +834,7 @@ A set used in a Boolean context is considered true if it is non-empty.

<b>Implementation note:</b>
The Go implementation of Starlark requires the `-set` flag to
enable support for sets and the `-bitwise` flag to enable support for
the `&`, `|`, and `^` operators.
enable support for sets.
The Java implementation does not support sets.


Expand Down Expand Up @@ -4025,7 +4021,6 @@ See [Starlark spec issue 20](https://github.com/bazelbuild/starlark/issues/20).
* Integers are represented with infinite precision.
* Integer arithmetic is exact.
* Integers support bitwise operators `&`, `|`, `<<`, `>>`, `^`, `~`, and their assignment forms.
* Floating-point literals are supported (option: `-float`).
* The `float` built-in function is provided (option: `-float`).
* Real division using `float / float` is supported (option: `-float`).
Expand Down
18 changes: 2 additions & 16 deletions resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ var (
AllowFloat = false // allow floating point literals, the 'float' built-in, and x / y
AllowSet = false // allow the 'set' built-in
AllowGlobalReassign = false // allow reassignment to globals declared in same file (deprecated)
AllowBitwise = false // allow bitwise operations (&, |, ^, ~, <<, and >>)
AllowRecursion = false // allow while statements and recursive functions

AllowBitwise = true // obsolete; bitwise operations (&, |, ^, ~, <<, and >>) are always enabled
)

// File resolves the specified file.
Expand Down Expand Up @@ -435,12 +436,6 @@ func (r *resolver) stmt(stmt syntax.Stmt) {
r.stmts(stmt.False)

case *syntax.AssignStmt:
if !AllowBitwise {
switch stmt.Op {
case syntax.AMP_EQ, syntax.PIPE_EQ, syntax.CIRCUMFLEX_EQ, syntax.LTLT_EQ, syntax.GTGT_EQ:
r.errorf(stmt.OpPos, doesnt+"support bitwise operations")
}
}
r.expr(stmt.RHS)
isAugmented := stmt.Op != syntax.EQ
r.assign(stmt.LHS, isAugmented)
Expand Down Expand Up @@ -632,21 +627,12 @@ func (r *resolver) expr(e syntax.Expr) {
}

case *syntax.UnaryExpr:
if !AllowBitwise && e.Op == syntax.TILDE {
r.errorf(e.OpPos, doesnt+"support bitwise operations")
}
r.expr(e.X)

case *syntax.BinaryExpr:
if !AllowFloat && e.Op == syntax.SLASH {
r.errorf(e.OpPos, doesnt+"support floating point (use //)")
}
if !AllowBitwise {
switch e.Op {
case syntax.AMP, syntax.PIPE, syntax.CIRCUMFLEX, syntax.LTLT, syntax.GTGT:
r.errorf(e.OpPos, doesnt+"support bitwise operations")
}
}
r.expr(e.X)
r.expr(e.Y)

Expand Down
1 change: 0 additions & 1 deletion resolve/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
)

func setOptions(src string) {
resolve.AllowBitwise = option(src, "bitwise")
resolve.AllowFloat = option(src, "float")
resolve.AllowGlobalReassign = option(src, "globalreassign")
resolve.AllowLambda = option(src, "lambda")
Expand Down
1 change: 0 additions & 1 deletion starlark/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

// A test may enable non-standard options by containing (e.g.) "option:recursion".
func setOptions(src string) {
resolve.AllowBitwise = option(src, "bitwise")
resolve.AllowFloat = option(src, "float")
resolve.AllowGlobalReassign = option(src, "globalreassign")
resolve.AllowLambda = option(src, "lambda")
Expand Down
2 changes: 1 addition & 1 deletion starlark/testdata/int.star
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Tests of Starlark 'int'
# option:bitwise option:float
# option:float

load("assert.star", "assert")

Expand Down
2 changes: 1 addition & 1 deletion starlark/testdata/set.star
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Tests of Starlark 'set'
# option:set option:bitwise
# option:set

# Sets are not a standard part of Starlark, so the features
# tested in this file must be enabled in the application by setting
Expand Down
2 changes: 1 addition & 1 deletion starlark/testdata/string.star
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Tests of Starlark 'string'
# option:float option:bitwise option:set
# option:float option:set

load("assert.star", "assert")

Expand Down

0 comments on commit 2f3bb7c

Please sign in to comment.