OILS / yaks / yaks_main.py View on Github | oilshell.org

134 lines, 49 significant
1#!/usr/bin/env python2
2"""
3yaks_main.py - Generate C++ from Yaks IR.
4
5Uses yaks.asdl. Will this be rewritten as yaks_main.yaks?
6"""
7from __future__ import print_function
8
9import sys
10
11from _devbuild.gen import yaks_asdl
12from asdl import format as fmt
13from data_lang import j8
14from mycpp import mylib
15from yaks import transform
16from yaks import gen_cpp
17
18from typing import List
19"""
20def Options():
21 # type: () -> optparse.OptionParser
22 "Returns an option parser instance."
23
24 p = optparse.OptionParser()
25 p.add_option('--no-pretty-print-methods',
26 dest='pretty_print_methods',
27 action='store_false',
28 default=True,
29 help='Whether to generate pretty printing methods')
30
31 # Control Python constructors
32
33 # for hnode.asdl
34 p.add_option('--py-init-N',
35 dest='py_init_n',
36 action='store_true',
37 default=False,
38 help='Generate Python __init__ that requires every field')
39
40 # The default, which matches C++
41 p.add_option(
42 '--init-zero-N',
43 dest='init_zero_n',
44 action='store_true',
45 default=True,
46 help='Generate 0 arg and N arg constructors, in Python and C++')
47
48 return p
49"""
50
51
52def main(argv):
53 # type: (List[str]) -> int
54
55 #o = Options()
56 #opts, argv = o.parse_args(argv)
57
58 stderr_ = mylib.Stderr()
59 try:
60 action = argv[1]
61 except IndexError:
62 raise RuntimeError('Action required')
63
64 if action == 'cpp':
65 # Usage:
66 # - Specify a root file - each file contains a module
67 # - this module may or may not contain main()
68 # - then it will walk imports, and create a big list of modules
69 # - then does it make a SINGLE translation unit for all modules?
70 # - and then maybe generate a unit test that links the translation unit
71 # and calls a function?
72 # - I suppose we could add main() to each module, like core/ and osh/
73
74 path = argv[2]
75 #with open(path) as f:
76 # contents = f.read()
77
78 # TODO: could implement mylib.SlurpFile()
79 lines = [] # type: List[str]
80 f = mylib.open(path)
81 while True:
82 line = f.readline()
83 if len(line) == 0:
84 break
85 lines.append(line)
86
87 #contents = '(print "hi")'
88 contents = ''.join(lines)
89
90 p = j8.Nil8Parser(contents, True)
91 nval = p.ParseNil8()
92
93 #print(obj)
94
95 # Dump ASDL representation
96 # We could also have a NIL8 printer
97 pretty_f = fmt.DetectConsoleOutput(stderr_)
98 fmt.PrintTree(nval.PrettyTree(), pretty_f)
99 stderr_.write('\n')
100
101 prog = transform.Transform(nval)
102
103 fmt.PrintTree(prog.PrettyTree(), pretty_f)
104 stderr_.write('\n')
105
106 # TODO: a few mycpp passes over this representation
107 # - not sure if we'll need any more IRs
108 gen_cpp.GenCpp(prog, mylib.Stdout())
109
110 elif action == 'check':
111 # Only do type checking?
112
113 path = argv[2]
114
115 m = yaks_asdl.Module('hi', [])
116
117 pretty_f = fmt.DetectConsoleOutput(stderr_)
118 fmt.PrintTree(m.PrettyTree(), pretty_f)
119 stderr_.write('\n')
120
121 else:
122 raise RuntimeError('Invalid action %r' % action)
123
124 return 0
125
126
127if __name__ == '__main__':
128 try:
129 main(sys.argv)
130 except RuntimeError as e:
131 print('%s: FATAL: %s' % (sys.argv[0], e), file=sys.stderr)
132 sys.exit(1)
133
134# vim: sw=4