Skip to content

Commit

Permalink
Change how command-line arguments are parsed. Add --version switch.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Feb 26, 2022
1 parent 1731b43 commit 1985ccf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 56 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Castile 0.5
* Established an abstract base class for compiler backends.
* Fixed a bug where tagged values were being tagged again
during a cast from a union type to another union type.
* ArgumentParser is used instead of OptionParser to parse
command-line arguments. `--version` added, `--test` (and
remaining doctests in source modules) removed.

Castile 0.4
-----------
Expand Down
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Test framework: collect the backend-independent tests into a single
file, and only test it once. Run all the *other* tests on every
backend.

Also make sure that doctests are run by test script.

### Design ###

Don't output final value. Command-line arguments passed to `main`. (`sysmain`?)
Expand Down
24 changes: 0 additions & 24 deletions src/castile/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@ class CastileContextError(ValueError):


class ScopedContext(object):
"""
>>> d = ScopedContext({ 'a': 2, 'b': 3 })
>>> e = ScopedContext({ 'c': 4 }, parent=d)
>>> e['c']
4
>>> e['b']
3
>>> 'a' in e
True
>>> 'e' in e
False
"""
def __init__(self, dict, parent=None, level=None):
self._dict = dict
self.parent = parent
Expand Down Expand Up @@ -47,14 +34,3 @@ def __contains__(self, key):

def __repr__(self):
return 'ScopedContext(%r,parent=%r)' % (self._dict, self.parent)


if __name__ == "__main__":
import sys
import doctest
(fails, something) = doctest.testmod()
if fails == 0:
print("All tests passed.")
sys.exit(0)
else:
sys.exit(1)
60 changes: 30 additions & 30 deletions src/castile/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import sys

from optparse import OptionParser
from argparse import ArgumentParser

from castile.parser import Parser
from castile.eval import Program
Expand All @@ -16,35 +16,35 @@


def main(argv):
optparser = OptionParser(__doc__.strip())
optparser.add_option("-a", "--show-ast",
action="store_true", dest="show_ast", default=False,
help="show parsed AST instead of evaluating")
optparser.add_option("-c", "--compile-to", metavar='BACKEND',
dest="compile_to", default=None,
help="compile to given backend code instead "
"of evaluating directly (available backends: "
"c, javascript, ruby, stackmac)")
optparser.add_option("-p", "--parse-only",
action="store_true", dest="parse_only",
default=False,
help="parse the input program only and exit")
optparser.add_option("-t", "--test",
action="store_true", dest="test", default=False,
help="run test cases and exit")
optparser.add_option("-Y", "--no-typecheck",
action="store_false", dest="typecheck", default=True,
help="do not typecheck the program")
(options, args) = optparser.parse_args(argv[1:])
if options.test:
import doctest
(fails, something) = doctest.testmod()
if fails == 0:
print("All tests passed.")
sys.exit(0)
else:
sys.exit(1)
with open(args[0], 'r') as f:
argparser = ArgumentParser()

argparser.add_argument('input_files', nargs='+', metavar='FILENAME', type=str,
help='Source files containing the Castile program'
)
argparser.add_argument("-a", "--show-ast",
action="store_true", dest="show_ast", default=False,
help="show parsed AST instead of evaluating"
)
argparser.add_argument("-c", "--compile-to", metavar='BACKEND',
dest="compile_to", default=None,
help="compile to given backend code instead "
"of evaluating directly (available backends: "
"c, javascript, ruby, stackmac)"
)
argparser.add_argument("-p", "--parse-only",
action="store_true", dest="parse_only",
default=False,
help="parse the input program only and exit"
)
argparser.add_argument("-Y", "--no-typecheck",
action="store_false", dest="typecheck", default=True,
help="do not typecheck the program"
)
argparser.add_argument('--version', action='version', version="%(prog)s 0.5")

options = argparser.parse_args(argv[1:])

with open(options.input_files[0], 'r') as f:
p = Parser(f.read())
ast = p.program()
if options.show_ast:
Expand Down

0 comments on commit 1985ccf

Please sign in to comment.