public static string Read(List <StructDefine> list, byte[] binary) { StringBuilder sb = new StringBuilder(); MemoryStream m = new MemoryStream(binary); m.Seek(0, 0); StructDefine start = null; foreach (StructDefine def in list) { if (def.name == "main") { start = def; break; } } if (start == null) { return(""); } try { sb.Append(ReadStruct("", start, list, m, 0)); } catch { } return(sb.ToString()); }
private static bool Check(StructDefine def, List <StructDefine> list) { def.marked = true; if (baseTypes.Contains(def.name)) { ErrorOut = "unknown type found '" + def.name + "'"; return(false); } foreach (TypeDefine t in def.types) { if (baseTypes.Contains(t.type)) { continue; } else { StructDefine sub = null; foreach (StructDefine t2 in list) { if (t2.name == t.type) { sub = t2; break; } } if (sub == null) { ErrorOut = "couldnt find definition for struct type '" + t.type + "'"; return(false); } if (sub.marked) { ErrorOut = "recursive reference found"; return(false); } return(Check(sub, list)); } } return(true); }
public static bool Verify(List <StructDefine> list) { ErrorOut = ""; StructDefine start = null; foreach (StructDefine def in list) { if (def.name == "main") { start = def; break; } } if (start == null) { ErrorOut = "cant find start struct named 'main'"; return(false); } for (int i = 0; i < list.Count; i++) { list[i].marked = false; } return(Check(start, list)); }
public static List <StructDefine> Parse(List <Token> input) { ErrorOut = ""; List <StructDefine> result = new List <StructDefine>(); int pos = 0; try { while (pos < input.Count) { if (input[pos].text == "struct") { pos++; if (input[pos] is TokenAlpha && input[pos + 1].text == "{") { StructDefine def = new StructDefine(); def.name = input[pos].text; def.types = new List <TypeDefine>(); pos += 2; while (true) { if (input[pos] is TokenAlpha && input[pos + 1] is TokenAlpha) { TypeDefine tdef = new TypeDefine(); tdef.type = input[pos].text; tdef.name = input[pos + 1].text; pos += 2; if (input[pos].text == ";") { tdef.isArray = false; tdef.size = 0; def.types.Add(tdef); pos++; } else if (input[pos].text == "[") { pos++; if (input[pos] is TokenNumeric && input[pos + 1].text == "]" && input[pos + 2].text == ";") { tdef.isArray = true; tdef.size = Convert.ToInt32(input[pos].text); def.types.Add(tdef); pos += 3; } else if (input[pos] is TokenNumeric && input[pos].text == "0" && input[pos + 1] is TokenAlpha && input[pos + 1].text.ToLower().StartsWith("x") && input[pos + 2].text == "]" && input[pos + 3].text == ";") { tdef.isArray = true; tdef.size = Convert.ToInt32(input[pos + 1].text.Substring(1), 16); def.types.Add(tdef); pos += 4; } else { throw new Exception("Expected array definition"); } } else { throw new Exception("Expected array define or end of define ; semicolon"); } } else if (input[pos].text == "}") { break; } else { throw new Exception("Expected new type definition or end of structure definition"); } } pos++; result.Add(def); } else { throw new Exception("Expected 'struct NAME {' next"); } } else { throw new Exception("Expected 'struct' next, got instead '" + input[pos].text + "'"); } } } catch (Exception ex) { ErrorOut = ex.Message; } return(result); }
public static List <StructDefine> Parse(List <Token> input) { ErrorOut = ""; List <StructDefine> result = new List <StructDefine>(); int pos = 0; try { while (pos < input.Count) { if (input[pos].text == "struct") { pos++; if (input[pos] is TokenAlpha && input[pos + 1].text == "{") { StructDefine def = new StructDefine(); def.name = input[pos].text; def.types = new List <TypeDefine>(); pos += 2; while (true) { if (input[pos] is TokenAlpha && input[pos + 1] is TokenAlpha) { TypeDefine tdef = new TypeDefine(); tdef.type = input[pos].text; tdef.name = input[pos + 1].text; pos += 2; if (input[pos].text == ";") { tdef.isArray = false; tdef.size = 0; def.types.Add(tdef); pos++; } else if (input[pos].text == "[") { pos++; if (input[pos] is TokenNumeric && input[pos + 1] is TokenAlpha) { pos++; if (input[pos - 1].text == "0" && input[pos].text.ToLower().StartsWith("x")) { input[pos - 1].isSpecial = true; input[pos].isHex = true; string hextranslate = input[pos].text.ToLower().TrimStart('x'); int newtext = int.Parse(hextranslate, System.Globalization.NumberStyles.HexNumber); input[pos].text = String.Format("{0}", newtext); } } if ((input[pos] is TokenNumeric || input[pos].isHex) && input[pos + 1].text == "]" && input[pos + 2].text == ";") { tdef.isArray = true; tdef.size = Convert.ToInt32(input[pos].text); def.types.Add(tdef); pos += 3; } else { throw new Exception("Expected array definition"); } } else { throw new Exception("Expected array define or end of define ; semicolon"); } } else if (input[pos].text == "}") { break; } else { throw new Exception("Expected new type definition or end of structure definition"); } } pos++; result.Add(def); } else { throw new Exception("Expected 'struct NAME {' next"); } } else { throw new Exception("Expected 'struct' next, got instead '" + input[pos].text + "'"); } } } catch (Exception ex) { ErrorOut = ex.Message; } return(result); }
private static string ReadStruct(string parent, StructDefine def, List <StructDefine> list, Stream s, int tab) { StringBuilder sb = new StringBuilder(); string tabs = ""; for (int i = 0; i < tab; i++) { tabs += "\t"; } sb.AppendLine(tabs + def.name + " " + parent + " {"); foreach (TypeDefine t in def.types) { if (Verifier.baseTypes.Contains(t.type)) { int tsize = Verifier.GetTypeSize(t.type); int ttype = 0; if (t.type == "float") { ttype = 1; } if (t.type == "double") { ttype = 2; } sb.Append(tabs + "\t" + t.name + " = "); if (!t.isArray) { sb.AppendLine(ReadBinary(s, tsize, ttype)); } else { sb.Append("{"); for (int i = 0; i < t.size - 1; i++) { sb.Append(ReadBinary(s, tsize, ttype) + ", "); } sb.AppendLine(ReadBinary(s, tsize, ttype) + "}"); } } else { StructDefine def2 = null; foreach (StructDefine t2 in list) { if (t2.name == t.type) { def2 = t2; break; } } if (!t.isArray) { sb.Append(ReadStruct(t.name, def2, list, s, tab + 1)); } else { sb.AppendLine(tabs + "\t" + t.name + " = "); sb.AppendLine(tabs + "\t{"); for (int i = 0; i < t.size; i++) { sb.Append(ReadStruct("[" + i + "]", def2, list, s, tab + 2)); } sb.AppendLine(tabs + "\t}"); } } } sb.AppendLine(tabs + "}"); return(sb.ToString()); }