Skip to content

Commit

Permalink
Remove some dead import code
Browse files Browse the repository at this point in the history
  • Loading branch information
trotterdylan committed Aug 22, 2017
1 parent 205bbd3 commit 50cbc8b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 46 deletions.
16 changes: 1 addition & 15 deletions compiler/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ def _resolve_global(self, writer, name):


class ModuleBlock(Block):
"""Python block for a module.
Attributes:
imports: A dict mapping fully qualified Go package names to Package objects.
"""
"""Python block for a module."""

def __init__(self, importer, full_package_name,
filename, src, future_features):
Expand All @@ -156,7 +152,6 @@ def __init__(self, importer, full_package_name,
self.filename = filename
self.buffer = source.Buffer(src)
self.strings = set()
self.imports = {}
self.future_features = future_features

def bind_var(self, writer, name, value):
Expand All @@ -171,15 +166,6 @@ def del_var(self, writer, name):
def resolve_name(self, writer, name):
return self._resolve_global(writer, name)

def add_native_import(self, name, alias=None):
if name == 'grumpy':
alias = 'πg'
if name in self.imports:
return self.imports[name]
package = Package(name, alias)
self.imports[name] = package
return package

def intern(self, s):
if len(s) > 64 or _non_word_re.search(s):
return 'πg.NewStr({})'.format(util.go_str(s))
Expand Down
9 changes: 0 additions & 9 deletions compiler/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@ def write_block(self, block_, body):
self.write(body)
self.write('}')

def write_import_block(self, imports):
if not imports:
return
self.write('import (')
with self.indent_block():
for name in sorted(imports):
self.write('{} "{}"'.format(imports[name].alias, name))
self.write(')')

def write_label(self, label):
with self.indent_block(-1):
self.write('Label{}:'.format(label))
Expand Down
12 changes: 0 additions & 12 deletions compiler/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ def testWriteBlock(self):
dispatch = 'switch πF.State() {\n\tcase 0:\n\tdefault: panic'
self.assertIn(dispatch, output)

def testWriteImportBlockEmptyImports(self):
writer = util.Writer()
writer.write_import_block({})
self.assertEqual(writer.getvalue(), '')

def testWriteImportBlockImportsSorted(self):
writer = util.Writer()
imports = {name: block.Package(name) for name in ('a', 'b', 'c')}
writer.write_import_block(imports)
self.assertEqual(writer.getvalue(),
'import (\n\tπ_a "a"\n\tπ_b "b"\n\tπ_c "c"\n)\n')

def testWriteMultiline(self):
writer = util.Writer()
writer.indent(2)
Expand Down
20 changes: 10 additions & 10 deletions tools/grumpc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def main(args):
full_package_name = args.modname.replace('.', '/')
mod_block = block.ModuleBlock(importer, full_package_name, args.script,
py_contents, future_features)
mod_block.add_native_import('grumpy')

visitor = stmt.StatementVisitor(mod_block, future_node)
# Indent so that the module body is aligned with the goto labels.
Expand All @@ -80,15 +79,16 @@ def main(args):
return 2

writer = util.Writer(sys.stdout)
package_name = args.modname.split('.')[-1]
writer.write('package {}'.format(package_name))
writer.write_import_block(mod_block.imports)
writer.write_tmpl(textwrap.dedent("""\
var Code *πg.Code
func init() {
\tCode = πg.NewCode("<module>", $script, nil, 0, func(πF *πg.Frame, _ []*πg.Object) (*πg.Object, *πg.BaseException) {
\t\tvar πR *πg.Object; _ = πR
\t\tvar πE *πg.BaseException; _ = πE"""), script=util.go_str(args.script))
tmpl = textwrap.dedent("""\
package $package
import πg "grumpy"
var Code *πg.Code
func init() {
\tCode = πg.NewCode("<module>", $script, nil, 0, func(πF *πg.Frame, _ []*πg.Object) (*πg.Object, *πg.BaseException) {
\t\tvar πR *πg.Object; _ = πR
\t\tvar πE *πg.BaseException; _ = πE""")
writer.write_tmpl(tmpl, package=args.modname.split('.')[-1],
script=util.go_str(args.script))
with writer.indent_block(2):
for s in sorted(mod_block.strings):
writer.write('ß{} := πg.InternStr({})'.format(s, util.go_str(s)))
Expand Down

0 comments on commit 50cbc8b

Please sign in to comment.