Permalink
Browse files

Change style for simple sum types to foo_e.Bar (from foo.Bar).

I'm preparing to retire util.Enum and move more types to ASDL.

All unit tests and spec tests pass.
  • Loading branch information...
Andy Chu
Andy Chu committed Nov 20, 2017
1 parent 0ab40d5 commit 0c27fabeb34fbaa488bbd20247af61a714bc466a
Showing with 165 additions and 128 deletions.
  1. +8 −8 asdl/arith_ast_test.py
  2. +3 −0 asdl/asdl_.py
  3. +13 −3 asdl/py_meta.py
  4. +7 −7 core/builtin.py
  5. +11 −11 core/cmd_exec.py
  6. +2 −2 core/completion_test.py
  7. +3 −3 core/expr_eval.py
  8. +17 −17 core/state.py
  9. +39 −40 core/state_test.py
  10. +6 −3 core/util.py
  11. +7 −0 core/util_test.py
  12. +3 −3 core/word.py
  13. +3 −3 osh/cmd_parse.py
  14. +10 −0 osh/lex_test.py
  15. +26 −0 osh/osh.asdl
  16. +1 −22 scripts/refactor.sh
  17. +6 −6 tools/osh2oil.py
View
@@ -19,7 +19,7 @@
Slice = arith_ast.Slice
arith_expr = arith_ast.arith_expr
source_location = arith_ast.source_location
op_id = arith_ast.op_id
op_id_e = arith_ast.op_id_e
class ArithAstTest(unittest.TestCase):
@@ -42,13 +42,13 @@ def testTypeCheck(self):
# Integer is not allowed
self.assertRaises(AssertionError, ArithVar, 1)
v = ArithUnary(op_id.Minus, Const(99))
v = ArithUnary(op_id_e.Minus, Const(99))
# Raw integer is not allowed
self.assertRaises(AssertionError, ArithUnary, op_id.Minus, 99)
self.assertRaises(AssertionError, ArithUnary, op_id_e.Minus, 99)
v = ArithUnary(op_id.Minus, Const(99))
v = ArithUnary(op_id_e.Minus, Const(99))
# Raw integer is not allowed
#self.assertRaises(AssertionError, ArithUnary, op_id.Minus, op_id.Plus)
#self.assertRaises(AssertionError, ArithUnary, op_id_e.Minus, op_id_e.Plus)
def testExtraFields(self):
v = ArithVar('z')
@@ -112,17 +112,17 @@ def testTypes(self):
print(Slice(Const(1), Const(5), Const(2)))
print(op_id.Plus)
print(op_id_e.Plus)
# Class for sum type
print(arith_expr)
# Invalid because only half were assigned
#print(ArithBinary(op_id.Plus, Const(5)))
#print(ArithBinary(op_id_e.Plus, Const(5)))
n = ArithBinary()
#n.CheckUnassigned()
n.op_id = op_id.Plus
n.op_id = op_id_e.Plus
n.left = Const(5)
#n.CheckUnassigned()
n.right = Const(6)
View
@@ -213,6 +213,9 @@ def visitConstructor(self, cons, name):
if conflict is None:
self.cons[key] = name
else:
# Problem: This assumes a flat namespace, which we don't want!
# This check is more evidence that a flat namespace isn't
# desirable.
print('Redefinition of constructor {}'.format(key))
print('Defined in {} and {}'.format(conflict, name))
self.errors += 1
View
@@ -279,9 +279,19 @@ def MakeTypes(module, root, app_types=None):
if asdl.is_simple(sum_type):
# An object without fields, which can be stored inline.
class_attr = {'DESCRIPTOR': sum_type} # asdl.Sum
cls = type(defn.name, (SimpleObj, ), class_attr)
#print('CLASS', cls)
setattr(root, defn.name, cls)
# Create a class called foo_e. Unlike the CompoundObj case, it doesn't
# have subtypes. Instead if has attributes foo_e.Bar, which Bar is an
# instance of foo_e.
#
# Problem: This means you have a dichotomy between:
# cflow_e.Break vs. cflow_e.Break()
# If you add a non-simple type like cflow_e.Return(5), the usage will
# change. I haven't run into this problem in practice yet.
class_name = defn.name + '_e'
cls = type(class_name, (SimpleObj, ), class_attr)
setattr(root, class_name, cls)
for i, cons in enumerate(sum_type.types):
enum_id = i + 1
View
@@ -39,8 +39,8 @@
from _devbuild import osh_help # generated file
value_e = runtime.value_e
scope = runtime.scope
var_flags = runtime.var_flags
scope_e = runtime.scope_e
var_flags_e = runtime.var_flags_e
log = util.log
e_die = util.e_die
@@ -459,7 +459,7 @@ def Cd(argv, mem):
return 1
if dest_dir == '-':
old = mem.GetVar('OLDPWD', scope.GlobalOnly)
old = mem.GetVar('OLDPWD', scope_e.GlobalOnly)
if old.tag == value_e.Undef:
log('OLDPWD not set')
return 1
@@ -539,7 +539,7 @@ def Export(argv, mem):
raise args.UsageError('export: Invalid variable name %r' % name)
# NOTE: bash does not care if it wasn't found
_ = mem.ClearFlag(name, var_flags.Exported, scope.Dynamic)
_ = mem.ClearFlag(name, var_flags_e.Exported, scope_e.Dynamic)
else:
for arg in argv[i:]:
parts = arg.split('=', 1)
@@ -552,7 +552,7 @@ def Export(argv, mem):
#log('%s %s', name, val)
mem.SetVar(
runtime.LhsName(name), val, (var_flags.Exported,), scope.Dynamic)
runtime.LhsName(name), val, (var_flags_e.Exported,), scope_e.Dynamic)
return 0
@@ -679,10 +679,10 @@ def Unset(argv, mem, funcs):
if name in funcs:
del funcs[name]
elif arg.v:
mem.Unset(runtime.LhsName(name), scope.Dynamic)
mem.Unset(runtime.LhsName(name), scope_e.Dynamic)
else:
# Try to delete var first, then func.
found = mem.Unset(runtime.LhsName(name), scope.Dynamic)
found = mem.Unset(runtime.LhsName(name), scope_e.Dynamic)
#log('%s: %s', name, found)
if not found:
if name in funcs:
View
@@ -49,11 +49,11 @@
command_e = ast.command_e
redir_e = ast.redir_e
lhs_expr_e = ast.lhs_expr_e
assign_op = ast.assign_op
assign_op_e = ast.assign_op_e
value_e = runtime.value_e
scope = runtime.scope
var_flags = runtime.var_flags
scope_e = runtime.scope_e
var_flags_e = runtime.var_flags_e
log = util.log
e_die = util.e_die
@@ -362,7 +362,7 @@ def _EvalLhs(self, node):
def _EvalRedirect(self, n):
fd = REDIR_DEFAULT_FD[n.op_id] if n.fd == -1 else n.fd
if n.tag == redir_e.Redir:
redir_type = REDIR_TYPE[n.op_id]
redir_type = REDIR_TYPE[n.op_id] # could be static in the LST?
if redir_type == RedirType.Path:
# NOTE: no globbing. You can write to a file called '*.py'.
@@ -459,7 +459,7 @@ def _EvalEnv(self, node_env, out_env):
# Set each var so the next one can reference it. Example:
# FOO=1 BAR=$FOO ls /
self.mem.SetVar(ast.LhsName(name), val, (), scope.LocalOnly)
self.mem.SetVar(ast.LhsName(name), val, (), scope_e.LocalOnly)
out_env[name] = val.s
self.mem.PopTemp()
@@ -680,27 +680,27 @@ def _Dispatch(self, node, fork_external):
check_errexit = True
pairs = []
if node.keyword == Id.Assign_Local:
lookup_mode = scope.LocalOnly
lookup_mode = scope_e.LocalOnly
flags = ()
# typeset and declare are synonyms? I see typeset -a a=() the most.
elif node.keyword in (Id.Assign_Declare, Id.Assign_Typeset):
# declare is like local, except it can also be used outside functions?
lookup_mode = scope.LocalOnly
lookup_mode = scope_e.LocalOnly
# TODO: Respect flags. -r and -x matter, but -a and -A might be
# implicit in the RHS?
flags = ()
elif node.keyword == Id.Assign_Readonly:
lookup_mode = scope.Dynamic
flags = (var_flags.ReadOnly,)
lookup_mode = scope_e.Dynamic
flags = (var_flags_e.ReadOnly,)
elif node.keyword == Id.Assign_None: # mutate existing local or global
lookup_mode = scope.Dynamic
lookup_mode = scope_e.Dynamic
flags = ()
else:
# TODO: typeset, declare, etc. Those are dynamic though.
raise NotImplementedError(node.keyword)
for pair in node.pairs:
if pair.op == assign_op.PlusEqual:
if pair.op == assign_op_e.PlusEqual:
assert pair.rhs, pair.rhs # I don't think a+= is valid?
val = self.ev.EvalWordToAny(pair.rhs)
old_val, lval = expr_eval.EvalLhs(pair.lhs, self.arith_ev, self.mem,
View
@@ -25,7 +25,7 @@
from osh import ast_ as ast
from osh import parse_lib
assign_op = ast.assign_op
assign_op_e = ast.assign_op_e
A1 = completion.WordsAction(['foo.py', 'foo', 'bar.py'])
@@ -82,7 +82,7 @@ def testShellFuncExecution(self):
w.parts.append(a)
# Set global COMPREPLY=(f1 f2)
pairs = [ast.assign_pair(ast.LhsName('COMPREPLY'), assign_op.Equal, w)]
pairs = [ast.assign_pair(ast.LhsName('COMPREPLY'), assign_op_e.Equal, w)]
body_node = ast.Assignment(Id.Assign_None, [], pairs)
func_node.name = 'myfunc'
View
@@ -35,7 +35,7 @@
part_value_e = runtime.part_value_e
value_e = runtime.value_e
lvalue_e = runtime.lvalue_e
scope = runtime.scope
scope_e = runtime.scope_e
def _StringToInteger(s, word=None):
@@ -231,7 +231,7 @@ def _EvalLhsToArith(self, node):
def _Store(self, lval, new_int):
val = runtime.Str(str(new_int))
self.mem.SetVar(lval, val, (), scope.Dynamic)
self.mem.SetVar(lval, val, (), scope_e.Dynamic)
def Eval(self, node):
"""
@@ -476,7 +476,7 @@ def Eval(self, node):
s = self._EvalCompoundWord(node.child)
# Now dispatch on arg type
arg_type = BOOL_OPS[op_id]
arg_type = BOOL_OPS[op_id] # could be static in the LST?
if arg_type == OperandType.Path:
try:
mode = os.stat(s).st_mode
View
@@ -22,8 +22,8 @@
part_value_e = runtime.part_value_e
value_e = runtime.value_e
lvalue_e = runtime.lvalue_e
scope = runtime.scope
var_flags = runtime.var_flags
scope_e = runtime.scope_e
var_flags_e = runtime.var_flags_e
log = util.log
e_die = util.e_die
@@ -254,7 +254,7 @@ def _InitEnviron(self, environ):
# variable. Dash has a loop through environ in init.c
for n, v in environ.iteritems():
self.SetVar(ast.LhsName(n), runtime.Str(v),
(var_flags.Exported,), scope.GlobalOnly)
(var_flags_e.Exported,), scope_e.GlobalOnly)
#
# Stack
@@ -347,7 +347,7 @@ def _FindCellAndNamespace(self, name, lookup_mode):
None if it's not found.
namespace: The namespace it should be set to or deleted from.
"""
if lookup_mode == scope.Dynamic:
if lookup_mode == scope_e.Dynamic:
found = False
for i in range(len(self.var_stack) - 1, -1, -1):
namespace = self.var_stack[i]
@@ -356,11 +356,11 @@ def _FindCellAndNamespace(self, name, lookup_mode):
return cell, namespace
return None, self.var_stack[0]
elif lookup_mode == scope.LocalOnly:
elif lookup_mode == scope_e.LocalOnly:
namespace = self.var_stack[-1]
return namespace.get(name), namespace
elif lookup_mode == scope.GlobalOnly:
elif lookup_mode == scope_e.GlobalOnly:
namespace = self.var_stack[0]
return namespace.get(name), namespace
@@ -404,16 +404,16 @@ def SetVar(self, lval, value, new_flags, lookup_mode):
# TODO: error context
e_die("Can't assign to readonly value %r", lval.name)
cell.val = value
if var_flags.Exported in new_flags:
if var_flags_e.Exported in new_flags:
cell.exported = True
if var_flags.ReadOnly in new_flags:
if var_flags_e.ReadOnly in new_flags:
cell.readonly = True
else:
if value is None:
value = runtime.Undef() # export foo, readonly foo
cell = runtime.cell(value,
var_flags.Exported in new_flags ,
var_flags.ReadOnly in new_flags )
var_flags_e.Exported in new_flags ,
var_flags_e.ReadOnly in new_flags )
namespace[lval.name] = cell
if (cell.val is not None and cell.val.tag == value_e.StrArray and
@@ -476,14 +476,14 @@ def SetVar(self, lval, value, new_flags, lookup_mode):
new_value = runtime.StrArray(items)
# arrays can't be exported
cell = runtime.cell(new_value, False,
var_flags.ReadOnly in new_flags)
var_flags_e.ReadOnly in new_flags)
namespace[lval.name] = cell
else:
raise AssertionError
# NOTE: Have a default for convenience
def GetVar(self, name, lookup_mode=scope.Dynamic):
def GetVar(self, name, lookup_mode=scope_e.Dynamic):
assert isinstance(name, str), name
# Do lookup of system globals before looking at user variables. Note: we
@@ -526,7 +526,7 @@ def Unset(self, lval, lookup_mode):
def ClearFlag(self, name, flag, lookup_mode):
cell, namespace = self._FindCellAndNamespace(name, lookup_mode)
if cell:
if flag == var_flags.Exported:
if flag == var_flags_e.Exported:
cell.exported = False
else:
raise AssertionError
@@ -555,22 +555,22 @@ def SetLocalString(mem, name, s):
3) read builtin
"""
assert isinstance(s, str)
mem.SetVar(ast.LhsName(name), runtime.Str(s), (), scope.LocalOnly)
mem.SetVar(ast.LhsName(name), runtime.Str(s), (), scope_e.LocalOnly)
def SetGlobalString(mem, name, s):
"""Helper for completion, $PWD, etc."""
assert isinstance(s, str)
val = runtime.Str(s)
mem.SetVar(ast.LhsName(name), val, (), scope.GlobalOnly)
mem.SetVar(ast.LhsName(name), val, (), scope_e.GlobalOnly)
def SetGlobalArray(mem, name, a):
"""Helper for completion."""
assert isinstance(a, list)
mem.SetVar(ast.LhsName(name), runtime.StrArray(a), (), scope.GlobalOnly)
mem.SetVar(ast.LhsName(name), runtime.StrArray(a), (), scope_e.GlobalOnly)
def GetGlobal(mem, name):
assert isinstance(name, str), name
return mem.GetVar(name, scope.GlobalOnly)
return mem.GetVar(name, scope_e.GlobalOnly)
Oops, something went wrong.

0 comments on commit 0c27fab

Please sign in to comment.