/// <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 }); } } }
public GSList createAndAddSublist() { GSList list = new GSList() { Parent = this }; Parts.Add(list); return(list); }
private GSCore execForeach(GSCoreCollection args) { if (args.Count < 4) { Debugger.Log(0, "", "Insufficient arguments for (FOREACH varName : list commands ) "); return(null); } GSCore t1 = args.getSafe(0); GSCore l1 = ExecuteElement(args.getSafe(2)); if (!(t1 is GSToken)) { Debugger.Log(0, "", "Token shoudl be second argument in FOREACH "); return(null); } if (!(l1 is GSList)) { Debugger.Log(0, "", "List should be fourth argument in FOREACH "); return(null); } GSToken tk = (GSToken)t1; GSList lst = (GSList)l1; GSCore r = null; int ik = 0; foreach (GSCore item in lst.Parts) { SetVariable(tk.Token, item); for (int i = 3; i < args.Count; i++) { r = ExecuteElement(args.getSafe(i)); if (r is GSReturn) { break; } } ik++; if (r is GSReturn) { GSReturn ret = r as GSReturn; if (ret.Type == GSReturn.TYPE_BREAK) { break; } if (ret.Type == GSReturn.TYPE_RETURN) { return(ret); } } } return(new GSInt64(ik)); }
/// <summary> /// Executing element. For most of the elements in the program it is element itself, /// but for the list it is result of executing operation that is mentioned in the head /// of the list. /// </summary> /// <param name="E"></param> /// <returns></returns> public GSCore ExecuteElement(GSCore E) { if (E is GSList) { GSList L = (GSList)E; if (L.Count == 0) { return(GSVoid.Void); } else if (L.Count == 1) { return(ExecuteElement(L[0])); } else { GSCore result = null; GSCore target = ExecuteElement(L.Parts[0]); string message = (L.Parts[1] is GSToken ? (L.Parts[1] as GSToken).Token : L.Parts[1].getStringValue()); if (target != null) { result = target.ExecuteMessage(message, L.Parts.getEvaluatedSublist(this, 2)); } if (result == null) { return(new GSString()); } return(result); } } else if (E is GSToken) { GSToken t = E as GSToken; if (t.Token.StartsWith("#")) { return(new GSString(t.Token)); } return(EvaluateProperty(((GSToken)E).Token)); } else { return(E); } }
/// <summary> /// Reading text with script /// Lines starting with # are comments /// Other lines are program /// </summary> /// <param name="text"></param> public void readList(String text) { Parts.Clear(); Parent = this; StringReader sr = new StringReader(text); String line = null; GSList currentList = this; while ((line = sr.ReadLine()) != null) { readTextListLine(line, ref currentList); } if (Parts.Count == 1 && (Parts[0] is GSList)) { Parts = (Parts[0] as GSList).Parts; } else if (Parts.Count > 1) { Parts.Insert(0, new GSToken("do")); } }
/// <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 GSDouble() { DoubleValue = d }); } else if (int.TryParse(value, out i)) { currentList.Add(new GSInt64() { Int64Value = 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 GSDouble() { DoubleValue = d }); } else if (int.TryParse(value, out i)) { currentList.Add(new GSInt64() { Int64Value = i }); } else { currentList.Add(new GSToken() { Token = part.ToString() }); } } } }