-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.h
106 lines (89 loc) · 2.32 KB
/
processor.h
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
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <map>
#include <cstdio>
#include <cassert>
enum AST_TYPE
{
#define DEFINE_AST_TYPE(type) type,
#include "ast_type.h"
#undef DEFINE_AST_TYPE
};
static const std::map<AST_TYPE, const char *> c_ast_type_strings =
{
#define DEFINE_AST_TYPE(type) { type, #type },
#include "ast_type.h"
#undef DEFINE_AST_TYPE
};
struct VskAst;
typedef std::shared_ptr<VskAst> VskAstPtr;
struct VskAst
{
AST_TYPE m_type = AST_NONE;
std::string m_str;
std::vector<VskAstPtr> m_children;
unsigned int m_lineno = -1;
VskAst(AST_TYPE type, const std::string& str)
: m_type(type)
, m_str(str)
{
}
VskAst(AST_TYPE type, const std::string& str, std::initializer_list<VskAstPtr> children)
: m_type(type)
, m_str(str)
, m_children(children)
{
}
void print()
{
auto it = c_ast_type_strings.find(m_type);
if (it != c_ast_type_strings.end())
{
printf("%s('%s'", it->second, m_str.c_str());
}
else
{
assert(0);
printf("%s('%s'", "(invalid)", m_str.c_str());
}
for (auto& child : m_children)
{
printf(", ");
child->print();
}
printf(")");
}
// is it multi-statement?
bool is_multi_stmt() const { return m_type == AST_MULTI_STMT; }
void push_back(VskAstPtr ptr) { m_children.push_back(ptr); }
size_t size() const { return m_children.size(); }
VskAstPtr& at(size_t index)
{
assert(index < size());
return data().at(index);
}
const VskAstPtr& at(size_t index) const
{
assert(index < size());
return data().at(index);
}
std::vector<VskAstPtr>& data() { return m_children; }
const std::vector<VskAstPtr>& data() const { return m_children; }
};
typedef VskAstPtr parse_result_t;
#include "parser.h"
inline VskAstPtr
vsk_ast(AST_TYPE type, const std::string& str)
{
return std::make_shared<VskAst>(type, str);
}
inline VskAstPtr
vsk_ast(AST_TYPE type, const std::string& str, std::initializer_list<VskAstPtr> children)
{
return std::make_shared<VskAst>(type, str, children);
}
int yylex(void);
void yyerror(parse_result_t& result, const char *s);
int vsk_token_from_text(const char *text);