Permalink
Browse files

Add 'oshc translate', 'oshc arena', 'oshc spans'.

These subcommands take the place of osh --fix, --parse-and-print, and
--debug-spans.

test/osh2oil.sh now passes.

Also, get rid of --show-ast.  -n is enough.
  • Loading branch information...
Andy Chu
Andy Chu committed Sep 3, 2018
1 parent 038bce8 commit a45a5b6124b1fb39892af168a6b4e31d0ab0e0bb
Showing with 29 additions and 16 deletions.
  1. +16 −15 bin/oil.py
  2. +3 −0 core/main_loop.py
  3. +1 −1 test/osh2oil.sh
  4. +9 −0 tools/osh2oil.py
View
@@ -102,15 +102,13 @@ def OshMain(argv0, argv, login_shell):
# TODO: -h too
spec.LongFlag('--help')
spec.LongFlag('--version')
# the output format when passing -n
spec.LongFlag('--ast-format',
['text', 'abbrev-text', 'html', 'abbrev-html', 'oheap', 'none'],
default='abbrev-text')
spec.LongFlag('--show-ast') # execute and show
spec.LongFlag('--fix') # oshc translate
spec.LongFlag('--debug-spans') # For oshc translate
spec.LongFlag('--parse-and-print-arena') # Invariant for translation
spec.LongFlag('--print-status')
spec.LongFlag('--hijack-shebang') # TODO:
spec.LongFlag('--hijack-shebang') # TODO: Implement this
# For benchmarks/*.sh
spec.LongFlag('--parser-mem-dump', args.Str)
@@ -229,13 +227,6 @@ def OshMain(argv0, argv, login_shell):
# Parse the whole thing up front
#print('Parsing file')
if opts.fix:
#osh2oil.PrintAsOil(arena, node, opts.debug_spans)
raise AssertionError
if opts.parse_and_print_arena:
osh2oil.PrintArena(arena)
raise AssertionError
# Do this after parsing the entire file. There could be another option to
# do it before exiting runtime?
if opts.parser_mem_dump:
@@ -311,7 +302,9 @@ def BoilMain(main_argv):
# TODO: Hook up to completion.
SUBCOMMANDS = ['translate', 'format', 'deps', 'undefined-vars']
SUBCOMMANDS = [
'translate', 'arena', 'spans', 'format', 'deps', 'undefined-vars'
]
def OshCommandMain(argv):
"""Run an 'oshc' tool.
@@ -353,8 +346,10 @@ def OshCommandMain(argv):
arena.PushSource(script_name)
line_reader = reader.FileLineReader(f, arena)
_, c_parser = parse_lib.MakeParser(line_reader, arena)
aliases = {} # Dummy value; not respecting aliases!
_, c_parser = parse_lib.MakeParser(line_reader, arena, aliases)
# TODO: Should I use main_loop.Batch() here?
try:
node = c_parser.ParseWholeFile()
except util.ParseError as e:
@@ -383,6 +378,12 @@ def OshCommandMain(argv):
debug_spans = False
osh2oil.PrintAsOil(arena, node, debug_spans)
elif action == 'arena': # for debugging
osh2oil.PrintArena(arena)
elif action == 'spans': # for debugging
osh2oil.PrintSpans(arena)
elif action == 'format':
# TODO: autoformat code
raise NotImplementedError(action)
@@ -391,7 +392,7 @@ def OshCommandMain(argv):
deps.Deps(node)
elif action == 'undefined-vars': # could be environment variables
pass
raise NotImplementedError
else:
raise AssertionError # Checked above
View
@@ -54,6 +54,9 @@ def Interactive(opts, ex, c_parser, arena):
if is_fatal: # e.g. divide by zero
continue
# TODO: Replace this with a shell hook? with 'trap', or it could be just
# like command_not_found. The hook can be 'echo $?' or something more
# complicated, i.e. with timetamps.
if opts.print_status:
print('STATUS', repr(status))
View
@@ -10,7 +10,7 @@ set -o errexit
source test/common.sh
osh-to-oil() {
$OSH --fix "$@"
bin/oshc translate "$@"
}
# Compare osh code on stdin (fd 0) and expected oil code on fd 3.
View
@@ -68,6 +68,15 @@ def PrintArena(arena):
cursor.PrintUntil(arena.LastSpanId())
def PrintSpans(arena):
"""Just to see spans."""
for i, span in enumerate(arena.spans):
line = arena.GetLine(span.line_id)
piece = line[span.col : span.col + span.length]
print('%5d %r' % (i, piece), file=sys.stderr)
print('(%d spans)' % len(arena.spans), file=sys.stderr)
def PrintAsOil(arena, node, debug_spans):
#print node
#print(spans)

0 comments on commit a45a5b6

Please sign in to comment.