/
main.d
121 lines (96 loc) · 2 KB
/
main.d
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
module qcc.driver;
import qcc.lexer;
import qcc.parsetree;
import qcc.parser;
import std.stdio;
void main(string[] args)
{
writeln("Welcome to the QCC Compiler");
immutable string test_source = `
#include "stdio.h"
struct s {
int x;
int *y;
struct s2 {
int x2;
int y2;
} ;
} ;
int main() {
int a;
bool bl;
a = (4+4)*8+a - add2(7);
int b;
b = 12 + a + b;
int c;
c = (2 == 5);
printf("Hello World");
if (a==a) {
return a;
} else
return 1234;
}
int b;
`;
string test_parser = `
Node {
Group {
Identifier name, "{", PatternElement[] elements : ","
/ Group[] groups, "}"
}
PatternElement {
ConstantElement {
charRange {
char rangeBegin, "-", char RangeEnd
}
RangeElement {
"[", charRange[] ranges : ",", "]"
}
LookbehindElement {
"!lb", "(", string str_elm, ")"
}
}
NamedElement {
Identifier type, Identifier name
}
ArrayElement {
Identifier type, "[]", Identifier name,
? ":" : string lst_sep
}
ParenElement {
"(", PatternElement[] elements : ",", ")"
}
OptionalElement {
"?", ConstantElement ce, ":", PatternElement elem
}
}
}`;
import std.file;
// auto source = (args.length>1) ? cast(immutable(char[])) read(args[1]) : test_source;
// writeln(source);
// writeln("Tokenized Output : ");
// auto lexer = Lexer();
// auto tokens = lexer.lex(source);
// //foreach(token;tokens) {
// // writeln(token);
// //}
// writeln(lexer.intrmap);
// auto cu = Parser().parseCompilationUnit(tokens);
// foreach(decl;cu.declarations) {
// writeln (decl);
// }
// foreach(stmt;(cast(FunctionDefinition)(cu.declarations[1])).function_body.statements) {
// writeln(stmt);
// }
import pgen_parser:lex;
import pgen_parser:parse;
import pgen_parser:PTreeVisitor;
auto pres = parse(lex(test_parser));
writeln(PTreeVisitor().visit(pres.groups));
// foreach (group;pres.groups) {
// writeln(group.name.identifier);
// writeln(group.elements);
// }
// Lets the user press <Return> before program returns
stdin.readln();
}