|
9 | 9 | from mako.template import Template
|
10 | 10 | from niels import camelize
|
11 | 11 |
|
12 |
| -def gen_node(yaml): |
13 |
| - namespace_name = yaml["namespace"] |
14 |
| - namespace_guard = namespace_name.upper() |
15 |
| - |
16 |
| - for node in yaml["nodes"]: |
17 |
| - node_name = node["name"] |
18 |
| - |
19 |
| - node_class = camelize(node_name) # Construct various valuas for the template |
20 |
| - node_shape = "box" |
21 |
| - node_guard = node_name.upper() |
22 |
| - |
23 |
| - hdr = Template(filename=os.sep.join(["templates", "node.hh.tpl"])).render( |
24 |
| - namespace_name=namespace_name, |
25 |
| - namespace_guard=namespace_guard, |
26 |
| - node_name=node_name, |
27 |
| - node_class=node_class, |
28 |
| - node_shape=node_shape, |
29 |
| - node_guard=node_guard |
| 12 | +def indent(lines, spaces=4): |
| 13 | + """ |
| 14 | + Indent the given string of lines with "spaces" " " and |
| 15 | + strip trailing newline. |
| 16 | + """ |
| 17 | + indented = [" "*spaces+line.strip() for line in lines.split("\n")] |
| 18 | + return ("\n".join(indented)).rstrip() |
| 19 | + |
| 20 | + |
| 21 | +def fill_ast(ast): |
| 22 | + """ |
| 23 | + Fills out the ast-dict with default values. |
| 24 | + """ |
| 25 | + |
| 26 | + if "guard" not in ast["namespace"]: |
| 27 | + ast["namespace"]["guard"] = ast["namespace"]["name"].upper() |
| 28 | + |
| 29 | + for node in ast["nodes"]: |
| 30 | + if "guard" not in node: |
| 31 | + node["guard"] = node["name"].upper() |
| 32 | + |
| 33 | + if "shape" not in node: |
| 34 | + node["shape"] = "box" |
| 35 | + |
| 36 | + if "class" not in node: |
| 37 | + node["class"] = camelize(node["name"]) |
| 38 | + |
| 39 | + if "constr_lval" in node and node["constr_lval"]: |
| 40 | + node["constr_lval"] = indent(node["constr_lval"]) |
| 41 | + else: |
| 42 | + node["constr_lval"] = None |
| 43 | + |
| 44 | + ast["nodes"].sort() # Sorting the nodes by name |
| 45 | + # Reasoning: the default listing in filesystems |
| 46 | + # wont preserve the order as given in |
| 47 | + # in ast.yaml |
| 48 | + # Using a probably similar sort/arrangement of |
| 49 | + # names when looking in dir-listing and in |
| 50 | + # visits_auto etc. might be convenient |
| 51 | + # it might also be really annoying... who knows? |
| 52 | + |
| 53 | +def gen_nodes(ast, output_root): |
| 54 | + """ |
| 55 | + Generates code for and stores to file: |
| 56 | +
|
| 57 | + * output_root/ast/add.hh |
| 58 | + * output_root/ast/add.cc |
| 59 | + * output_root/ast/sub.hh |
| 60 | + * output_root/ast/sub.cc |
| 61 | + * ... |
| 62 | +
|
| 63 | + @returns list of generated files. |
| 64 | + """ |
| 65 | + |
| 66 | + generated = [] # Names of generated files |
| 67 | + |
| 68 | + hdr_template = os.sep.join(["templates", "nodes_hh.tpl"]) |
| 69 | + src_template = os.sep.join(["templates", "nodes_cc.tpl"]) |
| 70 | + |
| 71 | + namespace = ast["namespace"] |
| 72 | + for node in ast["nodes"]: |
| 73 | + # Fill out the HEADER template |
| 74 | + hdr = Template(filename=hdr_template).render( |
| 75 | + namespace=namespace, |
| 76 | + node=node |
30 | 77 | )
|
31 |
| - src = Template(filename=os.sep.join(["templates", "node.cc.tpl"])).render( |
32 |
| - namespace_name=namespace_name, |
33 |
| - namespace_guard=namespace_guard, |
34 |
| - |
35 |
| - node_name=node_name, |
36 |
| - node_class=node_class, |
37 |
| - node_shape=node_shape, |
38 |
| - node_guard=node_guard |
| 78 | + # Write it to file |
| 79 | + hdr_path = output_root +os.sep+ "%s.hh" % node["name"] |
| 80 | + with open(hdr_path, "w") as fd: |
| 81 | + fd.write(hdr) |
| 82 | + # Log the generated file |
| 83 | + generated.append(hdr_path) |
| 84 | + |
| 85 | + # Fill out the SOURCE template |
| 86 | + src = Template(filename=src_template).render( |
| 87 | + namespace=namespace, |
| 88 | + node=node |
39 | 89 | )
|
40 |
| - yield (node, hdr, src) |
| 90 | + src_path = output_root +os.sep+ "%s.cc" % node["name"] |
| 91 | + with open(src_path, "w") as fd: |
| 92 | + fd.write(src) |
| 93 | + # Log the generated file |
| 94 | + generated.append(src_path) |
| 95 | + |
| 96 | + return generated |
41 | 97 |
|
42 |
| -def main(): |
43 |
| - parser = argparse.ArgumentParser(description='Autogenerate code based on ast.yaml.') |
| 98 | +def gen_visitor_visit_auto_hh_inc(ast, output_root): |
| 99 | + |
| 100 | + code = Template(filename=os.sep.join([ |
| 101 | + "templates", |
| 102 | + "visitor_visit_auto_hh_inc.tpl" |
| 103 | + ])).render(namespace=ast["namespace"], nodes=ast["nodes"]) |
| 104 | + |
| 105 | + inc_path = output_root +os.sep+ "visitor" +os.sep+ "visitor_visit_auto_hh.inc" |
| 106 | + with open(inc_path, "w") as fd: |
| 107 | + fd.write(code) |
| 108 | + |
| 109 | + return [inc_path] |
| 110 | + |
| 111 | +def gen_evaluator_visit_auto_hh_inc(ast, output_root): |
| 112 | + |
| 113 | + code = Template(filename=os.sep.join([ |
| 114 | + "templates", |
| 115 | + "evaluator_visit_auto_hh_inc.tpl" |
| 116 | + ])).render(namespace=ast["namespace"], nodes=ast["nodes"]) |
| 117 | + |
| 118 | + inc_path = output_root +os.sep+ "visitor" +os.sep+ "evaluator_visit_auto_hh.inc" |
| 119 | + with open(inc_path, "w") as fd: |
| 120 | + fd.write(code) |
| 121 | + |
| 122 | + return [inc_path] |
| 123 | + |
| 124 | +def main(args): |
| 125 | + ast = yaml.load(open(args.yaml)) # Load yaml |
| 126 | + fill_ast(ast) # Fill it out with default values |
| 127 | + |
| 128 | + output_root = os.sep.join(["out", ast["namespace"]["name"], "ast"]) |
| 129 | + |
| 130 | + generated_nodes = gen_nodes(ast, output_root) |
| 131 | + gen_visitor_visit_auto_hh_inc(ast, output_root) |
| 132 | + gen_evaluator_visit_auto_hh_inc(ast, output_root) |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + parser = argparse.ArgumentParser( |
| 136 | + description='Autogenerate code based on ast.yaml.' |
| 137 | + ) |
44 | 138 | parser.add_argument(
|
45 | 139 | "--yaml",
|
46 | 140 | type=str,
|
47 | 141 | help="Path to ast.yaml",
|
48 | 142 | default=os.sep.join([os.path.dirname(os.path.realpath(__file__)), "ast.yaml"])
|
49 | 143 | )
|
50 | 144 |
|
51 |
| - args = parser.parse_args() # Parse command-line arguments |
52 |
| - ast_yaml = yaml.load(open(args.yaml)) # Load yaml |
53 |
| - |
54 |
| - for node, hdr, src in gen_node(ast_yaml): |
55 |
| - src_path = os.sep.join(["out", "nls", "ast"])+os.sep+"%s.cc" % node["name"] |
56 |
| - hdr_path = os.sep.join(["out", "nls", "ast"])+os.sep+"%s.hh" % node["name"] |
57 |
| - with open(src_path, "w") as fd: |
58 |
| - fd.write(src) |
59 |
| - with open(hdr_path, "w") as fd: |
60 |
| - fd.write(hdr) |
61 |
| - |
62 |
| - |
63 |
| -if __name__ == "__main__": |
64 |
| - main() |
| 145 | + args = parser.parse_args() # Parse command-line arguments |
| 146 | + main(args) # Run main |
0 commit comments