/// <summary> /// Reading text with text template /// Lines starting with ## are comments /// Lines starting with # are program /// Other lines are printed to output /// </summary> /// <param name="text"></param> public void readTextTemplate(String text) { Parts.Clear(); Parent = this; StringReader sr = new StringReader(text); String line = null; GSList currentList = this; while ((line = sr.ReadLine()) != null) { if (line.StartsWith("#")) { readTextListLine(line.Substring(1), ref currentList); } else { GSList list = currentList.createAndAddSublist(); list.Add(new GSToken() { Token = "println" }); list.Add(new GSString() { Value = line }); } } }
/// <summary> /// reading one line /// </summary> /// <param name="line"></param> /// <param name="currentList"></param> public void readTextListLine(string line, ref GSList currentList) { // if line starts with #, then if (line.TrimStart().StartsWith("#")) { return; } int mode = 0; StringBuilder part = new StringBuilder(); foreach (char C in line) { if (mode == 0) { if (char.IsWhiteSpace(C)) { } else if (C == '\'') { mode = 1; } else if (C == '\"') { mode = 3; } else if (C == '(') { currentList = currentList.createAndAddSublist(); } else if (C == ')') { currentList = currentList.Parent; } else { part.Append(C); mode = 2; } } else if (mode == 1) { if (char.IsWhiteSpace(C) || C == ')') { currentList.Add(new GSString() { Value = part.ToString() }); part.Clear(); mode = 0; if (C == ')') { currentList = currentList.Parent; } } else { part.Append(C); } } else if (mode == 2) { if (char.IsWhiteSpace(C) || C == ')') { double d; int i; string value = part.ToString(); if (double.TryParse(value, out d)) { currentList.Add(new GSNumber() { DoubleValue = d }); } else if (int.TryParse(value, out i)) { currentList.Add(new GSNumber() { IntegerValue = i }); } else { currentList.Add(new GSToken() { Token = part.ToString() }); } part.Clear(); mode = 0; if (C == ')') { currentList = currentList.Parent; } } else { part.Append(C); } } else if (mode == 3) { if (C == '\"') { currentList.Add(new GSString(part.ToString())); part.Clear(); mode = 0; } else if (C == '\\') { mode = 4; } else { part.Append(C); } } else if (mode == 4) { part.Append(C); mode = 3; } } if (part.Length > 0) { if (mode == 1) { currentList.Add(new GSString() { Value = part.ToString() }); } else { double d; int i; string value = part.ToString(); if (double.TryParse(value, out d)) { currentList.Add(new GSNumber() { DoubleValue = d }); } else if (int.TryParse(value, out i)) { currentList.Add(new GSNumber() { IntegerValue = i }); } else { currentList.Add(new GSToken() { Token = part.ToString() }); } } } }