2 #include <msp/core/maputils.h>
3 #include <msp/strings/format.h>
4 #include "argumentstore.h"
5 #include "binaryparser.h"
9 #include "loaderaction.h"
16 BinaryParser::BinaryParser(Input &i, const string &s):
19 dict[-1] = StatementInfo("__kwd", "iss");
20 dict[-2] = StatementInfo("__str", "is");
21 dict[-3] = StatementInfo("__flt", "i");
24 Statement BinaryParser::parse()
26 const StatementKey *key;
38 key = &get_item(dict, id).key;
42 result.keyword = key->keyword;
45 for(unsigned j=0; j<key->signature.size(); ++j)
47 switch(key->signature[j])
49 case IntType::signature:
50 result.args.push_back(parse_int());
52 case FloatType::signature:
53 result.args.push_back(parse_float());
55 case StringType::signature:
56 result.args.push_back(parse_string());
58 case BoolType::signature:
59 result.args.push_back(parse_bool());
61 case SymbolType::signature:
62 result.args.push_back(Value(parse_symbol()));
67 unsigned upper_nsub = (sub_remaining.empty() ? 0 : sub_remaining.back());
69 unsigned nsub = parse_int();
70 for(unsigned j = 0; j<nsub; ++j)
71 result.sub.push_back(parse());
74 sub_remaining.back() = upper_nsub-1;
81 void BinaryParser::process_control_statement(const Statement &st)
83 if(st.keyword=="__kwd")
85 int id = st.args[0].get<int>();
87 throw bad_definition("__kwd");
89 const string &kw = st.args[1].get<const string &>();
90 const string &args = st.args[2].get<const string &>();
92 for(unsigned j=0; valid_signatures[j]!=c; ++j)
93 if(!valid_signatures[j])
94 throw bad_definition("__kwd");
96 dict[id] = StatementInfo(kw, args);
98 else if(st.keyword=="__str")
100 int id = st.args[0].get<int>();
102 throw bad_definition("__str");
104 strings[id] = st.args[1].get<const string &>();
106 else if(st.keyword=="__flt")
107 float_precision = st.args[0].get<unsigned>();
110 const StatementKey *BinaryParser::peek(unsigned level)
112 if(level>sub_remaining.size())
113 throw nesting_error("bad level");
114 while(level<sub_remaining.size())
116 // Discard any substatements that haven't been parsed yet
117 for(unsigned i=sub_remaining.back(); i-->0; )
119 sub_remaining.pop_back();
123 if(!sub_remaining.empty() && sub_remaining.back()==0)
125 // No more substatements on this level
131 return &cur_info->key;
133 int id = parse_int();
137 cur_info = &get_item(dict, id);
138 return &cur_info->key;
141 bool BinaryParser::parse_and_load(unsigned level, Loader &ldr, const LoaderAction &act)
143 if(!cur_info && !peek(level))
146 ArgumentStore args(*cur_info);
147 for(unsigned i=0; i<cur_info->key.signature.size(); ++i)
148 switch(cur_info->key.signature[i])
150 case IntType::signature:
151 args.set(i, parse_int());
153 case FloatType::signature:
154 args.set(i, parse_float());
156 case BoolType::signature:
157 args.set(i, parse_bool());
159 case StringType::signature:
160 args.set(i, parse_string());
162 case SymbolType::signature:
163 args.set(i, parse_symbol());
167 if(!sub_remaining.empty())
168 --sub_remaining.back();
169 sub_remaining.push_back(parse_int());
172 act.execute(ldr, args);
177 IntType::Store BinaryParser::parse_int()
179 IntType::Store result = 0;
186 result = (result<<7) | (c&0x7F);
193 const IntType::Store mask = 1LL<<(bits-1);
194 result = (result^mask)-mask;
199 FloatType::Store BinaryParser::parse_float()
201 uint64_t encoded = 0;
202 for(unsigned i=0; i<float_precision; i+=8)
205 encoded = (encoded<<8) | (c&0xFF);
208 BinFloat bf = BinFloat::explode(encoded, float_precision);
210 if(numeric_limits<FloatType::Store>::is_iec559)
211 return bf.compose_iec559<FloatType::Store>();
214 /* Put the float together with arithmetic since we don't know its
216 FloatType::Store f = 0;
219 if(numeric_limits<FloatType::Store>::has_infinity)
220 f = numeric_limits<FloatType::Store>::infinity();
222 f = numeric_limits<FloatType::Store>::max();
226 for(unsigned i=0; i<64; ++i)
233 for(int i=0; i<bf.exponent; ++i)
235 for(int i=0; i>bf.exponent; --i)
244 BoolType::Store BinaryParser::parse_bool()
249 StringType::Store BinaryParser::parse_string()
251 int len = parse_int();
256 for(int i = 0; i<len; ++i)
261 return get_item(strings, -len);
264 SymbolType::Store BinaryParser::parse_symbol()
266 return get_item(strings, parse_int());
269 } // namespace DataFile