-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathformatter.n
98 lines (89 loc) · 2.59 KB
/
formatter.n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Xml;
using Nemerle.Utility;
using Nemerle.Collections;
using Nemerle.IO;
variant Xml {
| Elt {
name : string;
attrs : Hashtable [string, string];
mutable children : list [Xml];
}
public override ToString () : string
{
match (this) {
| Elt (name, _, ch) =>
$ "($name $ch)"
}
}
}
module Formatter
{
read_xml () : Xml
{
def reader = XmlTextReader (System.Console.OpenStandardInput ());
def loop (acc) {
if (reader.Read ())
match (reader.NodeType) {
| XmlNodeType.Element =>
def attrs = Hashtable ();
def name = reader.Name;
def attrcnt = reader.AttributeCount;
for (mutable i = 0; i < attrcnt; ++i) {
reader.MoveToAttribute (i);
attrs [reader.Name] = reader.Value;
}
_ = reader.MoveToElement ();
def content =
if (reader.IsEmptyElement) []
else loop ([]);
def elt = Xml.Elt (name, attrs, content);
loop (elt :: acc)
| XmlNodeType.EndElement => NList.Rev (acc)
// ignore text
| _ => loop (acc)
}
else
NList.Rev (acc)
}
NList.Head (loop ([]))
}
handle_ty (e : Xml) : string
{
| Xml.Elt ("tapp", a, []) => a["ref"]
| Xml.Elt ("tapp", a, args) =>
a["ref"] + "<" + NString.Concat (", ", NList.Map (args, handle_ty)) + ">"
| Xml.Elt ("tprod", _, args) =>
NString.Concat (" * ", NList.Map (args, handle_ty))
| Xml.Elt ("tfun", _, [Xml.Elt ("from", _, [from]), Xml.Elt ("to", _, [to])]) =>
handle_ty (from) + " -> " + handle_ty (to)
| Xml.Elt ("tvoid", _, []) => "void"
| Xml.Elt ("tref", _, [t]) =>
"ref " + handle_ty (t)
| Xml.Elt ("tout", _, [t]) =>
"out " + handle_ty (t)
| Xml.Elt ("tarray", a, [t]) =>
"array" + (if (a["rank"] == "1") "" else "." + a["rank"]) +
" <" + handle_ty (t) + ">"
| Xml.Elt ("ttypelist", _, _) => "..."
| Xml.Elt (name, _, _) => $ "[unknown: $name]"
}
handle_decl (e : Xml) : void
{
| Xml.Elt ("alias", a, [Xml.Elt ("modifiers", _, _),
Xml.Elt ("aliased", _, [ty])]) =>
def name = a["name"];
print ("type $name = $(handle_ty (ty));\n")
| Xml.Elt (name, _, _) =>
print ("unknown $name\n")
}
public Main () : void
{
match (read_xml ()) {
| (Xml.Elt ("unit", _, lst)) as e =>
print ("$e\n");
NList.Iter (lst, handle_decl)
| Xml.Elt =>
print ("invalid toplevel")
}
}
}