-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (61 loc) · 1.31 KB
/
Copy pathmain.cpp
File metadata and controls
80 lines (61 loc) · 1.31 KB
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
/*
* main.c file
*/
#include "dast.h"
#include "dlang_grm.hpp"
#include "dlang_lex.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cerrno>
void getFileContents(std::string& contents, const char *filename)
{
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in)
{
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
in.close();
return;
}
throw(errno);
}
int yyparse(ModuleNode **expression, yyscan_t scanner);
extern int yydebug;
ModuleNode *getAST(const char *expr)
{
ModuleNode *expression;
yyscan_t scanner;
YY_BUFFER_STATE state;
if (yylex_init(&scanner)) {
// couldn't initialize
return NULL;
}
state = yy_scan_string(expr, scanner);
yydebug = 0;
if (yyparse(&expression, scanner)) {
// error parsing
return NULL;
}
yy_delete_buffer(state, scanner);
yylex_destroy(scanner);
return expression;
}
int main(void)
{
std::string str;
getFileContents(str, "test.d");
// yydebug = 1;
ModuleNode* e;
e = getAST(str.c_str());
if (!e)
{
std::cerr << "MainError" << std::endl;
exit(1);
}
std::cout << *e << std::endl;
return 0;
}