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 Sequence, /* $... */
109 CurrentAddress, /* here */
110 Identifier, /* $s */
111 Parameter, /* ?$s? */
112 Integer, /* $i */
113 BaseOf, /* base of $s */
114 StartOf, /* start of $s */
115 SizeOf, /* size of $s */
116 Location, /* $1:$2 */
117 Neg, /* -$1 */
118 Not, /* ~$1 */
119 AlignTo, /* align($1, $2) */
120 Minimum, /* minimum($...) */
121 Maximum, /* maximum($...) */
122 Shl, /* $1<<$2 */
123 Shr, /* $1>>$2 */
124 Add, /* $1+$2 */
125 Sub, /* $1-$2 */
126 And, /* $1&$2 */
127 Xor, /* $1^$2 */
128 Or, /* $1|$2 */
129
130 SetCurrentAddress, /* at $1 */
131 AlignAddress, /* align $1 */
132 SetNextBase, /* base $1 */
133 Assign, /* $s = $1 */
134
135 MatchAny, /* any */
136 MatchName, /* $s */
137 MatchSuffix, /* suffix $s */
138
139 IsReadable, /* read */
140 IsWritable, /* write */
141 IsExecutable, /* execute */
142 IsMergeable, /* merge */
143 IsZeroFilled, /* zero */
144 IsFixedAddress, /* fixed */
145 IsResource, /* resource */
146 IsOptional, /* optional */
147 IsStack, /* stack */
148 IsHeap, /* heap */
149 IsCustomFlag, /* custom flags */
150 Collect, /* all $1 $2 */
151 NotPredicate, /* not $1 */
152 AndPredicate, /* $1 and $2 */
153 OrPredicate, /* $1 or $2 */
154 MaximumSections, /* $1 maximum $2 */
155
156 Segment, /* $s { $1 } $2 */
157 SegmentTemplate, /* for $1 { $2 } $3 */
158 } type;
159 Value<> * value;
160 List * list;
161
162 Node(node_type type, Value<> * value, List * list)
163 : type(type), value(value), list(list)
164 {
165 }
166
167 Node(node_type type, Value<> * value)
168 : type(type), value(value), list(new List)
169 {
170 }
171
172 Node(node_type type, List * list)
173 : type(type), value(new Value<>), list(list)
174 {
175 }
176
177 Node(node_type type)
178 : type(type), value(new Value<>), list(new List)
179 {
180 }
181
182 std::unique_ptr<Node>& at(size_t index)
183 {
184 return list->children[index];
185 }
186
187 const std::unique_ptr<Node>& at(size_t index) const
188 {
189 return list->children[index];
190 }
191};
192
193std::unique_ptr<List> parse_string(const char * buffer);
194std::unique_ptr<List> parse_file(const char * filename, bool& file_error);
195
196}
197
198extern void set_buffer(const char * buffer);
199extern void set_stream(FILE * file);
200extern int yylex(void);
201
202#endif /* __NODE_H */
Definition script.h:62
Definition script.h:104
Definition script.h:40