Skip to content

Commit 8cac33e

Browse files
committed
Removed evaluation, type-checking, and symbols.
Towards the visitor mode...
1 parent 7cc6c5f commit 8cac33e

33 files changed

+474
-758
lines changed

CMakeLists.txt

+5-19
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,32 @@ BISON_TARGET(NielsParser "${SRC_DIR}/parser.yy" ${CMAKE_CURRENT_BINARY_DIR}/pars
2020
FLEX_TARGET(NielsScanner "${SRC_DIR}/scanner.ll" ${CMAKE_CURRENT_BINARY_DIR}/scanner.cc)
2121

2222
set(HDR "${HDR};${SRC_DIR}/nls/nls.hh")
23-
set(HDR "${HDR};${SRC_DIR}/nls/value.hh")
23+
set(HDR "${HDR};${SRC_DIR}/nls/variant.hh")
2424
set(HDR "${HDR};${SRC_DIR}/nls/utils.hh")
2525
set(HDR "${HDR};${SRC_DIR}/nls/driver.hh")
26-
set(HDR "${HDR};${SRC_DIR}/nls/symboltable.hh")
26+
set(HDR "${HDR};${SRC_DIR}/nls/symbol_table.hh")
2727
set(HDR "${HDR};${SRC_DIR}/nls/ast/node.hh")
28-
set(HDR "${HDR};${SRC_DIR}/nls/ast/expr.hh")
29-
set(HDR "${HDR};${SRC_DIR}/nls/ast/expr_value_auto.hh")
30-
set(HDR "${HDR};${SRC_DIR}/nls/ast/expr_unary_auto.hh")
31-
set(HDR "${HDR};${SRC_DIR}/nls/ast/expr_binary_auto.hh")
32-
set(HDR "${HDR};${SRC_DIR}/nls/ast/stmt.hh")
3328

3429
set(SRC "${SRC};${SRC_DIR}/niels.cc")
3530

3631
set(SRC "${SRC};${SRC_DIR}/nls/utils.cc")
3732
set(SRC "${SRC};${SRC_DIR}/nls/driver.cc")
38-
set(SRC "${SRC};${SRC_DIR}/nls/symboltable.cc")
33+
set(SRC "${SRC};${SRC_DIR}/nls/symbol_table.cc")
3934
set(SRC "${SRC};${SRC_DIR}/nls/ast/node.cc")
40-
set(SRC "${SRC};${SRC_DIR}/nls/ast/expr.cc")
41-
set(SRC "${SRC};${SRC_DIR}/nls/ast/expr_value_auto.cc")
42-
set(SRC "${SRC};${SRC_DIR}/nls/ast/expr_unary_auto.cc")
43-
set(SRC "${SRC};${SRC_DIR}/nls/ast/expr_binary_auto.cc")
44-
set(SRC "${SRC};${SRC_DIR}/nls/ast/stmt.cc")
4535

46-
add_custom_command(OUTPUT ${SRC_DIR}/nls/ast/expr_value_auto.hh ${SRC_DIR}/nls/ast/expr_binary_auto.hh ${SRC_DIR}/nls/ast/expr_value_auto.cc ${SRC_DIR}/nls/ast/expr_unary_auto.cc ${SRC_DIR}/nls/ast/expr_binary_auto.cc ${SRC_DIR}/nls/ast/expr_unary_auto.hh
36+
add_custom_command(OUTPUT ${SRC_DIR}/nls/ast/expr_binary_auto.hh ${SRC_DIR}/nls/ast/expr_unary_auto.cc ${SRC_DIR}/nls/ast/expr_binary_auto.cc ${SRC_DIR}/nls/ast/expr_unary_auto.hh
4737

4838
COMMAND ./niels.py > niels.json
49-
COMMAND ./gen.py --generator=expr_value_auto.hh > ${SRC_DIR}/nls/ast/expr_value_auto.hh
50-
COMMAND ./gen.py --generator=expr_value_auto.cc > ${SRC_DIR}/nls/ast/expr_value_auto.cc
5139
COMMAND ./gen.py --generator=expr_unary_auto.hh > ${SRC_DIR}/nls/ast/expr_unary_auto.hh
5240
COMMAND ./gen.py --generator=expr_unary_auto.cc > ${SRC_DIR}/nls/ast/expr_unary_auto.cc
5341
COMMAND ./gen.py --generator=expr_binary_auto.hh > ${SRC_DIR}/nls/ast/expr_binary_auto.hh
5442
COMMAND ./gen.py --generator=expr_binary_auto.cc > ${SRC_DIR}/nls/ast/expr_binary_auto.cc
5543

5644
WORKING_DIRECTORY ${GEN_DIR}
57-
DEPENDS ${GEN_DIR}/templates/expr_value_auto_hh.tpl ${GEN_DIR}/templates/expr_binary_auto_hh.tpl ${GEN_DIR}/templates/expr_unary_auto_cc.tpl ${GEN_DIR}/templates/expr_binary_auto_cc.tpl ${GEN_DIR}/templates/expr_value_auto_cc.tpl ${GEN_DIR}/templates/expr_unary_auto_hh.tpl
45+
DEPENDS ${GEN_DIR}/templates/expr_binary_auto_hh.tpl ${GEN_DIR}/templates/expr_binary_auto_cc.tpl ${GEN_DIR}/templates/expr_unary_auto_hh.tpl ${GEN_DIR}/templates/expr_unary_auto_cc.tpl
5846
COMMENT "Autogenerating based on niels.json"
5947
VERBATIM)
6048

61-
set_source_files_properties(${SRC_DIR}/nls/ast/expr_value_auto.hh PROPERTIES GENERATED TRUE)
62-
set_source_files_properties(${SRC_DIR}/nls/ast/expr_value_auto.cc PROPERTIES GENERATED TRUE)
6349
set_source_files_properties(${SRC_DIR}/nls/ast/expr_unary_auto.hh PROPERTIES GENERATED TRUE)
6450
set_source_files_properties(${SRC_DIR}/nls/ast/expr_unary_auto.cc PROPERTIES GENERATED TRUE)
6551
set_source_files_properties(${SRC_DIR}/nls/ast/expr_binary_auto.hh PROPERTIES GENERATED TRUE)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

autogen/ast.py

+125-43
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,138 @@
99
from mako.template import Template
1010
from niels import camelize
1111

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
3077
)
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
3989
)
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
4197

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+
)
44138
parser.add_argument(
45139
"--yaml",
46140
type=str,
47141
help="Path to ast.yaml",
48142
default=os.sep.join([os.path.dirname(os.path.realpath(__file__)), "ast.yaml"])
49143
)
50144

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

autogen/ast.yaml

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,55 @@
1-
namespace: nls
1+
namespace:
2+
name: nls
23
nodes:
34
# Literals
45
- name: bul
6+
constr_lval: |
7+
_variant = new Variant;
8+
_variant->value.bul = lval[0] == 't';
9+
_variant->value_type = NLS_BUL;
10+
511
- name: i32
12+
constr_lval: |
13+
_variant = new Variant;
14+
_variant->value.i32 = atoi(lval);
15+
_variant->value_type = NLS_I32;
16+
617
- name: i64
18+
constr_lval: |
19+
_variant = new Variant;
20+
_variant->value.i64 = atol(lval);
21+
_variant->value_type = NLS_I64;
22+
723
- name: r32
24+
constr_lval: |
25+
_variant = new Variant;
26+
_variant->value.r32 = atof(lval);
27+
_variant->value_type = NLS_R32;
28+
829
- name: r64
30+
constr_lval: |
31+
_variant = new Variant;
32+
_variant->value.r64 = atof(lval);
33+
_variant->value_type = NLS_R64;
34+
935
- name: str
36+
constr_lval: |
37+
_variant = new Variant;
38+
_variant->value.str = new string(lval+1, strlen(lval)-2);
39+
_variant->value_type = NLS_STR;
40+
# Comment
41+
- name: comment
42+
constr_lval: |
43+
_variant = new Variant;
44+
_variant->value.str = new string(val);
45+
_variant->value_type = NLS_STR;
46+
1047
# Identifier
1148
- name: ident
49+
constr_lval: |
50+
_variant = new Variant;
51+
_variant->value.str = new string(lval);
52+
_variant->value_type = NLS_STR;
1253
# Expressions - Arithmetic
1354
- name: add
1455
- name: sub
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
NOTE: This file is generated by:
3+
4+
* Declaration: autogen/ast.yaml
5+
* Logic: autogen/ast.py
6+
* Template: autogen/templates/evaluator_visit_auto_hh_inc.tpl
7+
8+
Modify the above files to persist changes.
9+
10+
Locally changing this file is possible, however, be aware that cmake
11+
picks up changes to ANY file in autogen/* and thereby
12+
overwrites ALL local changes to ANY generated file.
13+
*/
14+
% for node in nodes:
15+
void visit(${node["class"]}& &node);
16+
% endfor
17+

autogen/templates/expr_binary_auto_cc.tpl renamed to autogen/templates/evaluator_visit_binary_auto_cc.tpl

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <nls/ast/expr_binary_auto.hh>
22
#include <nls/utils.hh>
3+
#include <nls/ast/evaluator.hh>
34

45
using namespace std;
56
namespace nls {
@@ -37,6 +38,10 @@ ${op2node[op]}::${op2node[op]}(Node* left, Node* right) : Node(left, right)
3738
%endif
3839
stype(EXPR);
3940
}
41+
void ${op2node[op]}::visit(Evaluator& visitor)
42+
{
43+
visitor.visit(*this);
44+
}
4045
void ${op2node[op]}::eval(Driver& env)
4146
{
4247
Node* res = this;

autogen/templates/expr_unary_auto_cc.tpl renamed to autogen/templates/evaluator_visit_unary_auto_cc.tpl

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
#include <nls/ast/expr_unary_auto.hh>
2+
#include <nls/ast/evaluator.hh>
23

34
using namespace std;
45
namespace nls {
56
namespace ast {
67
7-
%for k, op, ninput, exprs in operators:
8-
9-
${op2node[op]}::${op2node[op]}(Node* left) : Node(left)
8+
% for node in ast["nodes"]:
9+
void Evaluator::visit(${node["class"]& node)
1010
{
11-
%if k == "comparison" or k == "logical":
12-
vtype(NLS_BUL);
13-
%elif k == "arithmetic" or k == "bitwise":
14-
_vtype = left->vtype();
15-
%else:
16-
FORGOT SOMETHING
17-
%endif
11+
walk()
12+
Variant in1 = pop();
13+
Variant res;
1814
19-
stype(EXPR);
20-
}
21-
void ${op2node[op]}::eval(Driver& env)
22-
{
23-
Node* res = this;
24-
Node* in1 = left();
15+
// TODO: Derive type
2516
26-
VType res_t = res->vtype(); // Evaluate *this
17+
VType res_t = res->vtype();
2718
VType in1_t = in1->vtype();
2819
uint64_t mask = (res_t << 16) + in1_t;
2920
switch(mask) {
@@ -51,7 +42,6 @@ void ${op2node[op]}::eval(Driver& env)
5142
break;
5243
}
5344
}
54-
string ${op2node[op]}::dot_label(void) { return "${op2node[op]}"; }
5545
%endfor
5646

5747
}}

autogen/templates/expr_binary_auto_hh.tpl

-22
This file was deleted.

0 commit comments

Comments
 (0)