Permalink
Browse files

Cleanup after the osh/meta.py change.

- Fix REDIR_TYPE now that dependencies are untangled.
- Make functions in id_kind public.
- Move LoadSchema into the ASDL library.
  • Loading branch information...
Andy Chu
Andy Chu committed Feb 7, 2018
1 parent 056e493 commit 42fa8d71f9c564d63b5c44d372af5beba34c47a6
Showing with 43 additions and 59 deletions.
  1. +11 −1 asdl/asdl_.py
  2. +1 −2 core/cmd_exec.py
  3. +4 −4 core/id_kind.py
  4. +0 −19 osh/ast_.py
  5. +3 −2 osh/ast_gen.py
  6. +24 −31 osh/meta.py
View
@@ -146,7 +146,6 @@ def _CheckFieldsAndWire(typ, type_lookup):
typ.type_lookup = type_lookup # wire it for lookup
def ResolveTypes(module, app_types=None):
# Walk the module, checking types, and adding the type_lookup attribute
# everywhere.
@@ -596,3 +595,14 @@ def _match(self, kind):
def _at_keyword(self, keyword):
return (self.cur_token.kind == TokenKind.TypeId and
self.cur_token.value == keyword)
def LoadSchema(f, app_types):
"""Parse an ASDL schema. Used for code gen and metaprogramming."""
asdl_module = parse(f)
if not check(asdl_module, app_types):
raise AssertionError('ASDL file is invalid')
type_lookup = ResolveTypes(asdl_module, app_types)
return asdl_module, type_lookup
View
@@ -34,13 +34,12 @@
from core import ui
from core import util
from core import builtin
from osh.meta import Id, REDIR_TYPE, REDIR_DEFAULT_FD, _InitRedirType
from core import process
from core import runtime
from core import state
from core import word_compile
from osh.meta import ast
from osh.meta import ast, Id, REDIR_TYPE, REDIR_DEFAULT_FD
from osh import parse_lib
try:
View
@@ -130,7 +130,7 @@ def AddBoolOp(self, id_, arg_type):
self.bool_ops[id_] = arg_type
def _AddKinds(spec):
def AddKinds(spec):
# TODO: Unknown_Tok is OK, but Undefined_Id is better
spec.AddKind('Undefined', ['Tok']) # for initial state
spec.AddKind('Unknown', ['Tok']) # for when nothing matches
@@ -379,7 +379,7 @@ def _Dash(strs):
return [(s, '-' + s) for s in strs]
def _AddBoolKinds(spec, Id):
def AddBoolKinds(spec, Id):
spec.AddBoolKind('BoolUnary', {
OperandType.Str: _Dash(list(_UNARY_STR_CHARS)),
OperandType.Other: _Dash(list(_UNARY_OTHER_CHARS)),
@@ -404,8 +404,8 @@ def _AddBoolKinds(spec, Id):
spec.AddBoolOp(Id.Redir_Great, OperandType.Str)
def _SetupTestBuiltin(Id, Kind, id_spec,
unary_lookup, binary_lookup, other_lookup):
def SetupTestBuiltin(Id, Kind, id_spec,
unary_lookup, binary_lookup, other_lookup):
"""Setup tokens for test/[.
Similar to _AddBoolKinds above. Differences:
View

This file was deleted.

Oops, something went wrong.
View
@@ -6,7 +6,7 @@
import sys
from osh.meta import ast, Id
from osh import ast_
from asdl import asdl_ as asdl
from asdl import gen_cpp
lex_mode_e = ast.lex_mode_e
@@ -16,8 +16,9 @@ def main(argv):
#print lex_mode_e
#print dir(lex_mode_e)
app_types = {'id': asdl.UserType(Id)}
with open('osh/osh.asdl') as f:
asdl_module, _ = ast_.LoadSchema(Id, f)
asdl_module, _ = asdl.LoadSchema(f, app_types)
# TODO: Generate C files for lex_mode_e, id_e, etc.
View
@@ -11,14 +11,14 @@
from osh.meta import Id, Kind, ast, ID_SPEC
"""
import sys
from asdl import py_meta
from asdl import asdl_ as asdl
# These are metaprogramming libraries. Everything can happen at compile time.
# Could move these to a dir like meta? From meta import id_kind? From meta
# import asdl?
from core import id_kind
from osh import ast_
from asdl import py_meta
from core import util
@@ -67,7 +67,7 @@ def IdName(id_):
# OBJECT IDENTITY.
# Do NOT create any any more instances of them! Always used IdInstance().
# TODO: Fold this into ASDL, which will enforce this?
# TODO: Fold Id into ASDL, which will enforce uniqueness?
_ID_INSTANCES = {} # int -> Id
@@ -84,21 +84,19 @@ def IdInstance(i):
TEST_OTHER_LOOKUP = {}
#
# Instantiate the spec
#
ID_SPEC = id_kind.IdSpec(Id, Kind,
_ID_NAMES, _ID_INSTANCES, _ID_TO_KIND,
BOOL_OPS)
id_kind._AddKinds(ID_SPEC)
id_kind._AddBoolKinds(ID_SPEC, Id) # must come second
id_kind._SetupTestBuiltin(Id, Kind, ID_SPEC,
TEST_UNARY_LOOKUP, TEST_BINARY_LOOKUP,
TEST_OTHER_LOOKUP)
id_kind.AddKinds(ID_SPEC)
id_kind.AddBoolKinds(ID_SPEC, Id) # must come second
id_kind.SetupTestBuiltin(Id, Kind, ID_SPEC,
TEST_UNARY_LOOKUP, TEST_BINARY_LOOKUP,
TEST_OTHER_LOOKUP)
# Debug
_kind_sizes = ID_SPEC.kind_sizes
@@ -109,7 +107,8 @@ def IdInstance(i):
#
f = util.GetResourceLoader().open('osh/osh.asdl')
_asdl_module, _type_lookup = ast_.LoadSchema(Id, f)
app_types = {'id': asdl.UserType(Id)}
_asdl_module, _type_lookup = asdl.LoadSchema(f, app_types)
ast = _AsdlModule()
if 0:
@@ -150,26 +149,20 @@ def IdInstance(i):
Id.Redir_DLessDash: 0,
}
redir_type_e = ast.redir_type_e
def _InitRedirType():
# To break circular import. TODO: Id should really be metaprogrammed in the
# same module!
redir_type_e = ast.redir_type_e
return {
# filename
Id.Redir_Less: redir_type_e.Path,
Id.Redir_Great: redir_type_e.Path,
Id.Redir_DGreat: redir_type_e.Path,
Id.Redir_Clobber: redir_type_e.Path,
Id.Redir_LessGreat: redir_type_e.Path,
# descriptor
Id.Redir_GreatAnd: redir_type_e.Desc,
Id.Redir_LessAnd: redir_type_e.Desc,
REDIR_TYPE = {
# filename
Id.Redir_Less: redir_type_e.Path,
Id.Redir_Great: redir_type_e.Path,
Id.Redir_DGreat: redir_type_e.Path,
Id.Redir_Clobber: redir_type_e.Path,
Id.Redir_LessGreat: redir_type_e.Path,
Id.Redir_TLess: redir_type_e.Here, # here word
# note: here docs aren't included
}
# descriptor
Id.Redir_GreatAnd: redir_type_e.Desc,
Id.Redir_LessAnd: redir_type_e.Desc,
REDIR_TYPE = _InitRedirType()
Id.Redir_TLess: redir_type_e.Here, # here word
# note: here docs aren't included
}

0 comments on commit 42fa8d7

Please sign in to comment.