RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
script.h
1#ifndef __NODE_H
2#define __NODE_H
3
4#include <memory>
5#include <typeinfo>
6#include <vector>
7
8namespace Script
9{
10
11template <typename Type = void>
12 class Value;
13
14template <>
15 class Value<void>
16{
17public:
18 virtual ~Value() { }
19protected:
20 virtual const void * Get(const std::type_info& type) const
21 {
22 return nullptr;
23 }
24public:
25 template <typename T>
26 T * Get()
27 {
28 return reinterpret_cast<T *>(const_cast<void *>(Get(typeid(T))));
29 }
30
31 template <typename T>
32 const T * Get() const
33 {
34 return reinterpret_cast<T *>(Get(typeid(T)));
35 }
36};
37
38template <typename Type>
39 class Value : public Value<>
40{
41public:
42 Type value;
43
44 Value(const Type& value)
45 : value(value)
46 {
47 }
48
49protected:
50 const void * Get(const std::type_info& type) const override
51 {
52 if(typeid(Type) == type)
53 return &value;
54 else
55 return nullptr;
56 }
57};
58
59class Node;
60
61class List
62{
63public:
64 std::vector<std::unique_ptr<Node>> children;
65
66protected:
67 void init() { }
68
69 template <typename ... Nodes>
70 void init(Node * node, Nodes ... nodes)
71 {
72 children.push_back(std::unique_ptr<Node>(node));
73 init(nodes...);
74 }
75
76 template <typename ... Nodes>
77 void init(std::unique_ptr<Node> node, Nodes ... nodes)
78 {
79 children.push_back(std::move(node));
80 init(nodes...);
81 }
82
83public:
84 template <typename ... Nodes>
85 List(Nodes ... nodes)
86 {
87 init(nodes...);
88 }
89
90 List * Append(Node * node)
91 {
92 children.push_back(std::unique_ptr<Node>(node));
93 return this;
94 }
95
96 List * Append(std::unique_ptr<Node> node)
97 {
98 children.push_back(std::move(node));
99 return this;
100 }
101};
102
103class Node
104{
105public:
106 enum node_type
107 {
108 Empty, /* */
109 Sequence, /* $... */
110 CurrentAddress, /* here */
111 Identifier, /* $s */
112 Parameter, /* ?$s? */
113 Integer, /* $i */
114 BaseOf, /* base of $s */
115 StartOf, /* start of $s */
116 SizeOf, /* size of $s */
117 Location, /* $1:$2 */
118 Neg, /* -$1 */
119 Not, /* ~$1 */
120 AlignTo, /* align($1, $2) */
121 Minimum, /* minimum($...) */
122 Maximum, /* maximum($...) */
123 Shl, /* $1<<$2 */
124 Shr, /* $1>>$2 */
125 Add, /* $1+$2 */
126 Sub, /* $1-$2 */
127 And, /* $1&$2 */
128 Xor, /* $1^$2 */
129 Or, /* $1|$2 */
130
131 SetCurrentAddress, /* at $1 */
132 AlignAddress, /* align $1 */
133 SetNextBase, /* base $1 */
134 Assign, /* $s = $1 */
135 Call, /* call $s */
136
137 MatchAny, /* any */
138 MatchName, /* $s */
139 MatchSuffix, /* suffix $s */
140
141 IsReadable, /* read */
142 IsWritable, /* write */
143 IsExecutable, /* execute */
144 IsMergeable, /* merge */
145 IsZeroFilled, /* zero */
146 IsFixedAddress, /* fixed */
147 IsResource, /* resource */
148 IsOptional, /* optional */
149 IsStack, /* stack */
150 IsHeap, /* heap */
151 IsCustomFlag, /* custom flags */
152 Collect, /* all $1 $2 */
153 NotPredicate, /* not $1 */
154 AndPredicate, /* $1 and $2 */
155 OrPredicate, /* $1 or $2 */
156 MaximumSections, /* $1 maximum $2 */
157 UntilSection, /* $1 until $2 */
158
159 Segment, /* $s { $1 } $2 */
160 SegmentTemplate, /* for $1 { $2 } $3 */
161 } type;
162 Value<> * value;
163 List * list;
164
165 Node(node_type type, Value<> * value, List * list)
166 : type(type), value(value), list(list)
167 {
168 }
169
170 Node(node_type type, Value<> * value)
171 : type(type), value(value), list(new List)
172 {
173 }
174
175 Node(node_type type, List * list)
176 : type(type), value(new Value<>), list(list)
177 {
178 }
179
180 Node(node_type type)
181 : type(type), value(new Value<>), list(new List)
182 {
183 }
184
185 std::unique_ptr<Node>& at(size_t index)
186 {
187 return list->children[index];
188 }
189
190 const std::unique_ptr<Node>& at(size_t index) const
191 {
192 return list->children[index];
193 }
194};
195
196std::unique_ptr<List> parse_string(const char * buffer);
197std::unique_ptr<List> parse_file(const char * filename, bool& file_error);
198
199}
200
201extern void set_buffer(const char * buffer);
202extern void set_stream(FILE * file);
203extern int yylex(void);
204
205#endif /* __NODE_H */
Definition script.h:62
Definition script.h:104
Definition script.h:40