예제 #1
0
파일: util_test.py 프로젝트: ip4368/grumpy
 def testIndentBlock(self):
     writer = util.Writer()
     writer.write('foo')
     with writer.indent_block(n=2):
         writer.write('bar')
     writer.write('baz')
     self.assertEqual(writer.getvalue(), 'foo\n\t\tbar\nbaz\n')
예제 #2
0
 def testWriteBlock(self):
     writer = util.Writer()
     mod_block = block.ModuleBlock(None, '__main__', '<test>', '',
                                   imputil.FutureFeatures())
     writer.write_block(mod_block, 'BODY')
     output = writer.getvalue()
     dispatch = 'switch πF.State() {\n\tcase 0:\n\tdefault: panic'
     self.assertIn(dispatch, output)
예제 #3
0
 def testWriteBlock(self):
   writer = util.Writer()
   writer.write_block(block.ModuleBlock(
       '__main__', 'grumpy', 'grumpy/lib', '<test>', []), 'BODY')
   output = writer.out.getvalue()
   dispatch = 'switch πF.State() {\n\tcase 0:\n\tdefault: panic'
   self.assertIn(dispatch, output)
   self.assertIn('return nil, nil\n}', output)
예제 #4
0
파일: util_test.py 프로젝트: ip4368/grumpy
 def testWriteBlock(self):
     writer = util.Writer()
     mod_block = block.ModuleBlock(imputil_test.MockPath(), '__main__',
                                   '<test>', '', stmt.FutureFeatures())
     writer.write_block(mod_block, 'BODY')
     output = writer.getvalue()
     dispatch = 'switch πF.State() {\n\tcase 0:\n\tdefault: panic'
     self.assertIn(dispatch, output)
     self.assertIn('return nil, nil\n}', output)
예제 #5
0
파일: util_test.py 프로젝트: ip4368/grumpy
 def testDedent(self):
     writer = util.Writer()
     writer.indent(4)
     writer.dedent(3)
     writer.write('foo')
     self.assertEqual(writer.getvalue(), '\tfoo\n')
예제 #6
0
파일: util_test.py 프로젝트: ip4368/grumpy
 def testWriteTmpl(self):
     writer = util.Writer()
     writer.write_tmpl('$foo, $bar\n$baz', foo=1, bar=2, baz=3)
     self.assertEqual(writer.getvalue(), '1, 2\n3\n')
예제 #7
0
파일: util_test.py 프로젝트: ip4368/grumpy
 def testWriteSkipBlankLine(self):
     writer = util.Writer()
     writer.write('foo\n\nbar')
     self.assertEqual(writer.getvalue(), 'foo\nbar\n')
예제 #8
0
파일: util_test.py 프로젝트: ip4368/grumpy
 def testWritePyContext(self):
     writer = util.Writer()
     writer.write_py_context(12, 'print "foo"')
     self.assertEqual(writer.getvalue(), '// line 12: print "foo"\n')
예제 #9
0
파일: util_test.py 프로젝트: ip4368/grumpy
 def testWriteMultiline(self):
     writer = util.Writer()
     writer.indent(2)
     writer.write('foo\nbar\nbaz\n')
     self.assertEqual(writer.getvalue(), '\t\tfoo\n\t\tbar\n\t\tbaz\n')
예제 #10
0
파일: util_test.py 프로젝트: ip4368/grumpy
 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')
예제 #11
0
 def testIndent(self):
   writer = util.Writer()
   writer.indent(2)
   writer.write('foo')
   self.assertEqual(writer.out.getvalue(), '\t\tfoo\n')
예제 #12
0
 def __init__(self, block_):
   self.block = block_
   self.future_features = self.block.root.future_features or FutureFeatures()
   self.writer = util.Writer()
   self.expr_visitor = expr_visitor.ExprVisitor(self.block, self.writer)
예제 #13
0
 def __init__(self, block_):
     self.block = block_
     self.writer = util.Writer()
     self.expr_visitor = expr_visitor.ExprVisitor(self.block, self.writer)
예제 #14
0
def main(script=None, modname=None):
    assert script and modname, 'Script "%s" or Modname "%s" is empty' % (
        script, modname)

    gopath = os.getenv('GOPATH', None)
    if not gopath:
        print >> sys.stderr, 'GOPATH not set'
        return 1

    with open(script) as py_file:
        py_contents = py_file.read()
    try:
        mod = pythonparser.parse(py_contents)
    except SyntaxError as e:
        print >> sys.stderr, '{}: line {}: invalid syntax: {}'.format(
            e.filename, e.lineno, e.text)
        return 2

    # Do a pass for compiler directives from `from __future__ import *` statements
    try:
        future_node, future_features = imputil.parse_future_features(mod)
    except util.CompileError as e:
        print >> sys.stderr, str(e)
        return 2

    importer = imputil.Importer(gopath, modname, script,
                                future_features.absolute_import)
    full_package_name = modname.replace('.', '/')
    mod_block = block.ModuleBlock(importer, full_package_name, script,
                                  py_contents, future_features)

    visitor = stmt.StatementVisitor(mod_block, future_node)
    # Indent so that the module body is aligned with the goto labels.
    with visitor.writer.indent_block():
        try:
            visitor.visit(mod)
        except util.ParseError as e:
            print >> sys.stderr, str(e)
            return 2

    writer = util.Writer(sys.stdout)
    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=modname.split('.')[-1],
                      script=util.go_str(script))
    with writer.indent_block(2):
        for s in sorted(mod_block.strings):
            writer.write('ß{} := πg.InternStr({})'.format(s, util.go_str(s)))
        writer.write_temp_decls(mod_block)
        writer.write_block(mod_block, visitor.writer.getvalue())
    writer.write_tmpl(textwrap.dedent("""\
    \t\treturn nil, πE
    \t})
    \tπg.RegisterModule($modname, Code)
    }"""),
                      modname=util.go_str(modname))
    return 0
예제 #15
0
파일: stmt.py 프로젝트: liangfeixu/grumpy
 def __init__(self, block_, future_node=None):
   self.block = block_
   self.future_node = future_node
   self.writer = util.Writer()
   self.expr_visitor = expr_visitor.ExprVisitor(self)
예제 #16
0
def _ParseAndVisitExpr(expr):
    writer = util.Writer()
    visitor = expr_visitor.ExprVisitor(_MakeModuleBlock(), writer)
    visitor.visit(_ParseExpr(expr))
    return writer.out.getvalue()
예제 #17
0
파일: block_test.py 프로젝트: zksfyz/grumpy
 def _ResolveName(self, b, name):
   writer = util.Writer()
   b.resolve_name(writer, name)
   return writer.getvalue()
예제 #18
0
파일: util_test.py 프로젝트: ip4368/grumpy
 def testWriteImportBlockEmptyImports(self):
     writer = util.Writer()
     writer.write_import_block({})
     self.assertEqual(writer.getvalue(), '')