Permalink
Browse files

Tests for operator precedence and exponentiation.

All shells agree that -3 ** 2 = 9, i.e. that - has higher precedence
than **.

But Python and R do the opposite: ** has higher precedence than -.

Oil wil follow Python and R, but since all shells agree we aren't
changing OSH.

Unrelated: minor refactoring in bin/oil.py.
  • Loading branch information...
Andy Chu
Andy Chu committed Sep 3, 2018
1 parent a45a5b6 commit 41c21210bb35777f063d8d56a2869d61e8f0beff
Showing with 65 additions and 41 deletions.
  1. +42 −41 bin/oil.py
  2. +23 −0 spec/arith.test.sh
View
@@ -107,7 +107,7 @@ def OshMain(argv0, argv, login_shell):
['text', 'abbrev-text', 'html', 'abbrev-html', 'oheap', 'none'],
default='abbrev-text')
spec.LongFlag('--print-status')
spec.LongFlag('--print-status') # TODO: Replace with a shell hook
spec.LongFlag('--hijack-shebang') # TODO: Implement this
# For benchmarks/*.sh
@@ -223,47 +223,48 @@ def OshMain(argv0, argv, login_shell):
status_out, ev)
return main_loop.Interactive(opts, ex, c_parser, arena)
else:
# Parse the whole thing up front
#print('Parsing file')
# Do this after parsing the entire file. There could be another option to
# do it before exiting runtime?
if opts.parser_mem_dump:
# This might be superstition, but we want to let the value stabilize
# after parsing. bash -c 'cat /proc/$$/status' gives different results
# with a sleep.
time.sleep(0.001)
input_path = '/proc/%d/status' % os.getpid()
with open(input_path) as f, open(opts.parser_mem_dump, 'w') as f2:
contents = f.read()
f2.write(contents)
log('Wrote %s to %s (--parser-mem-dump)', input_path,
opts.parser_mem_dump)
nodes_out = [] if exec_opts.noexec else None
_tlog('Execute(node)')
#status = ex.ExecuteAndRunExitTrap(node)
status = main_loop.Batch(opts, ex, c_parser, arena, nodes_out=nodes_out)
if nodes_out is not None:
ui.PrintAst(nodes_out, opts)
# NOTE: 'exit 1' is ControlFlow and gets here, but subshell/commandsub
# don't because they call sys.exit().
if opts.runtime_mem_dump:
# This might be superstition, but we want to let the value stabilize
# after parsing. bash -c 'cat /proc/$$/status' gives different results
# with a sleep.
time.sleep(0.001)
input_path = '/proc/%d/status' % os.getpid()
with open(input_path) as f, open(opts.runtime_mem_dump, 'w') as f2:
contents = f.read()
f2.write(contents)
log('Wrote %s to %s (--runtime-mem-dump)', input_path,
opts.runtime_mem_dump)
# Parse the whole thing up front
#print('Parsing file')
# Do this after parsing the entire file. There could be another option to
# do it before exiting runtime?
if opts.parser_mem_dump:
# This might be superstition, but we want to let the value stabilize
# after parsing. bash -c 'cat /proc/$$/status' gives different results
# with a sleep.
time.sleep(0.001)
input_path = '/proc/%d/status' % os.getpid()
with open(input_path) as f, open(opts.parser_mem_dump, 'w') as f2:
contents = f.read()
f2.write(contents)
log('Wrote %s to %s (--parser-mem-dump)', input_path,
opts.parser_mem_dump)
nodes_out = [] if exec_opts.noexec else None
_tlog('Execute(node)')
#status = ex.ExecuteAndRunExitTrap(node)
status = main_loop.Batch(opts, ex, c_parser, arena, nodes_out=nodes_out)
if nodes_out is not None:
ui.PrintAst(nodes_out, opts)
# NOTE: 'exit 1' is ControlFlow and gets here, but subshell/commandsub
# don't because they call sys.exit().
if opts.runtime_mem_dump:
# This might be superstition, but we want to let the value stabilize
# after parsing. bash -c 'cat /proc/$$/status' gives different results
# with a sleep.
time.sleep(0.001)
input_path = '/proc/%d/status' % os.getpid()
with open(input_path) as f, open(opts.runtime_mem_dump, 'w') as f2:
contents = f.read()
f2.write(contents)
log('Wrote %s to %s (--runtime-mem-dump)', input_path,
opts.runtime_mem_dump)
# NOTE: We haven't closed the file opened with fd_state.Open
return status
View
@@ -328,3 +328,26 @@ echo "status=$?"
## N-I dash stdout: status=127
## N-I dash status: 0
#### Operator Precedence
echo $(( 1 + 2*3 - 8/2 ))
## stdout: 3
#### Exponentiation operator has buggy precedence
# NOTE: All shells agree on this, but R and Python give -9, which is more
# mathematically correct.
echo $(( -3 ** 2 ))
## osh stdout: 9
## N-I dash stdout-json: ""
## N-I dash status: 2
## N-I mksh stdout-json: ""
## N-I mksh status: 1
#### Negative exponennt
# bash explicitly disallows negative exponents!
echo $(( 2**-1 * 5 ))
## stdout: 2.5
## status: 0
## OK bash/mksh stdout-json: ""
## OK bash/mksh status: 1
## N-I dash stdout-json: ""
## N-I dash status: 2

0 comments on commit 41c2121

Please sign in to comment.