能够推断出struct数组成员的类型,暂时只支持1层深度

This commit is contained in:
2021-01-25 18:15:11 +08:00
parent c8d74664b2
commit 10341ed46a

View File

@@ -106,26 +106,42 @@ export class Field {
if (this.children) {
let named_fields: {[key: string]: Field[]} = {};
for (const c of this.children) {
c.constant_array_length = this.constant_array_length;
c.build();
let arr = named_fields[c.name] || [];
arr.push(c);
named_fields[c.name] = arr;
}
for (const [name, fields] of Object.entries(named_fields)) {
if (fields.length > 1) {
let indeies = fields.map(f => Field.TYPE_ORDER.indexOf(f.type));
indeies = indeies.filter(idx => idx >= 0);
const type = Field.TYPE_ORDER[Math.min(...indeies)];
if (fields.length > 1) { // 推断数组类型
const type = this.infer_field_type(fields);
for (const f of fields) {
f._is_array = true;
f.type = type;
if (type === DataType.struct) { // 数组元素的类型
for (const keys of fields.map( f => f.children.map(cf => cf.name) )) {
for (const key of keys) {
let same_name_fileds = fields.map( f => f.children.find( cf => cf.name === key));
const ctype = this.infer_field_type(same_name_fileds);
same_name_fileds.forEach( f => { f.type = ctype });
}
}
}
}
}
}
for (const c of this.children) {
c.constant_array_length = this.constant_array_length;
c.build();
}
}
}
protected infer_field_type(fields: Field[]) {
let indeies = fields.map(f => Field.TYPE_ORDER.indexOf(f.type));
indeies = indeies.filter(idx => idx >= 0);
const type = Field.TYPE_ORDER[Math.min(...indeies)];
return type;
}
/** 解析一条数据 */
public parse_row(row: RawTableCell[]) {
if (this.type != DataType.struct) {