/// <summary> /// 获取结构数组 /// </summary> /// <param name="attributes"></param> /// <param name="type"></param> /// <param name="texts"></param> /// <param name="errors"></param> /// <returns></returns> public static object GetStructArray(AttributeTable attributes, Model.Struct type, string[] texts, List <string> errors) { var list = new List <Dictionary <string, object> >(); for (var i = 0; i < texts.Length; i++) { var value = GetStruct(attributes, type, texts[i], errors); if (value != null) { list.Add((Dictionary <string, object>)value); } } return(list); }
public override void ResolveTypes(CompileContext context, Model.Scope enclosingScope) { _struct = enclosingScope.FindType(typeName); if (_struct == null) { CachedFetchToken = Constant(0); context.ReportError(this, "Could not find type " + typeName); } else if (_struct.size == 0) { throw new InternalError("Struct size must be determined before types are resolved."); } else CachedFetchToken = Constant((ushort)_struct.size); ResultType = "word"; }
public override void ResolveTypes(CompileContext context, Model.Scope enclosingScope) { _struct = enclosingScope.FindType(typeName); if (_struct == null) { context.ReportError(this, "Could not find type " + typeName); CachedFetchToken = Constant(0); } else { var memberIndex = _struct.members.FindIndex(m => m.name == memberName); if (memberIndex < 0) { context.ReportError(this, "Member not found : " + memberName); CachedFetchToken = Constant(0); } else CachedFetchToken = Constant((ushort)memberIndex); } ResultType = "word"; }
public override void ResolveTypes(CompileContext context, Model.Scope enclosingScope) { Child(0).ResolveTypes(context, enclosingScope); _struct = enclosingScope.FindType(Child(0).ResultType); if (_struct == null) { context.ReportError(this, "Result of expression is not a struct"); ResultType = "word"; } else { foreach (var _member in _struct.members) if (_member.name == memberName) member = _member; if (member == null) { context.ReportError(this, "Member " + memberName + " not found on " + _struct.name); ResultType = "word"; } else ResultType = member.typeSpecifier; } }
/// <summary> /// 获取列表格式的结构 /// </summary> /// <param name="literal"></param> /// <param name="type"></param> /// <param name="text"></param> /// <param name="errors"></param> /// <returns></returns> private static object GetStruct(StructLiteral literal, Model.Struct type, string text, List <string> errors) { text = text.Trim(); //查找自定义的分割规则 var separator = ","; if (literal != null) { separator = literal.separator; if (text.StartsWith(literal.beginning)) { text = text.Substring(literal.beginning.Length); } if (text.EndsWith(literal.ending)) { text = text.Substring(0, text.Length - literal.ending.Length); } } //分割字符串 var texts = new string[] { }; if (!string.IsNullOrEmpty(text)) { texts = text.Split(new string[] { separator }, StringSplitOptions.None); } //解析字符串 var values = new Dictionary <string, object>(); for (int i = 0; i < texts.Length; i++) { var txt = texts[i].Trim(); if (i < type.Fields.Count) { var field = type.Fields[i]; var fieldType = field.Type; if (BaseUtil.IsInteger(fieldType) || BaseUtil.IsFloat(fieldType) || BaseUtil.IsBool(fieldType)) { object value = BaseUtil.GetScalar(fieldType, txt); if (value != null) { values.Add(field.Name, value); } else { errors.Add(string.Format("第{0}个元素{1}无法解析成{2}。", i, txt, fieldType)); } } else if (BaseUtil.IsString(fieldType)) { values.Add(field.Name, txt); } else if (field.TypeDefined is Model.Enum) { var value = BaseUtil.GetEnum(field.TypeDefined as Model.Enum, txt); if (value != null) { values.Add(field.Name, value); } else { errors.Add(string.Format("第{0}个元素{1}无法解析成{2}。", i, txt, fieldType)); } } else if (field.TypeDefined is Model.Struct) { var value = GetStruct(field.Attributes, field.TypeDefined as Model.Struct, txt, errors); if (value != null) { values.Add(field.Name, value); } } } else { errors.Add(string.Format("第{0}个元素将被忽略,因为{1}的字段数量只有{2}个。", i, type.Name, type.Fields.Count)); } } for (int i = texts.Length; i < type.Fields.Count; i++) { errors.Add(String.Format("字段{0}没有对应的数据项,因为数据只有{1}个元素。", type.Fields[i].Name, texts.Length)); } return(values); }