19 typedef std::string String;
20 typedef offset_t Integer;
22 typedef std::vector<Value> List;
23 typedef std::map<std::string, Value> Table;
35 Value(
const Empty& content) : content(Empty {}) { }
36 Value(
const String& content) : content(content) { }
37 Value(
const char * content) : content(String(content)) { }
38 Value(Integer content) : content(content) { }
39 explicit Value(Logical content) : content(content) { }
40 Value(
const List& content) : content(content) { }
41 Value(
const Table& content) : content(content) { }
43 static Value MakeList()
48 static Value MakeTable()
50 return Value(Table{});
55 return std::holds_alternative<Empty>(content);
60 return std::get_if<String>(&content);
63 const String * GetString()
const
65 return std::get_if<String>(&content);
68 Integer * GetInteger()
70 return std::get_if<Integer>(&content);
73 const Integer * GetInteger()
const
75 return std::get_if<Integer>(&content);
78 Logical * GetLogical()
80 return std::get_if<Logical>(&content);
83 const Logical * GetLogical()
const
85 return std::get_if<Logical>(&content);
90 return std::get_if<List>(&content);
93 const List * GetList()
const
95 return std::get_if<List>(&content);
100 return std::get_if<Table>(&content);
103 const Table * GetTable()
const
105 return std::get_if<Table>(&content);
114 else if(String *
string = GetString())
118 else if(Integer * integer = GetInteger())
120 std::ostringstream oss;
124 else if(Logical * logical = GetLogical())
126 return *logical ?
"true" :
"false";
130 Linker::FatalError(
"Fatal error: unable to convert value to string");
136 if(String *
string = GetString())
140 if(starts_with(*
string,
"0x"))
142 text =
string->c_str() + 2;
145 else if(starts_with(*
string,
"0o"))
147 text =
string->c_str() + 2;
150 else if(starts_with(*
string,
"0b"))
152 text =
string->c_str() + 2;
155 else if(starts_with(*
string,
"0"))
157 text =
string->c_str() + 1;
162 text =
string->c_str();
170 offset_t result = strtoll(text, &tail, base);
171 if(tail[0] ==
'\0' && errno == 0)
177 else if(Integer * integer = GetInteger())
182 Linker::FatalError(
"Fatal error: unable to convert value to integer");
185 Value& operator[](Value value)
187 if(Table * table = GetTable())
189 return (*table)[value.ToString()];
191 else if(List * list = GetList())
193 return (*list)[value.ToInteger()];
197 Linker::FatalError(
"Fatal error: not an aggregate type, cannot call [] operator");
201 const Value& operator[](Value value)
const
203 if(
const Table * table = GetTable())
205 return table->find(value.ToString())->second;
207 else if(
const List * list = GetList())
209 return (*list)[value.ToInteger()];
213 Linker::FatalError(
"Fatal error: not an aggregate type, cannot call [] operator");