public override dynamic Run(IList<Token> tokens, Context context) { /* 1: content to write 2: file object 3: item separator 4: line breack 3: file mode -> w: write, a: append */ var arguments = ExtractArguments(tokens.Skip(1).ToArray(), context); FileObject fileObject; string separator = ",", lineBreack = "\r\n"; var rhs = recurse.Run(tokens.Skip(2).Take(1).ToArray(), context); fileObject = recurse.Run(tokens.Skip(4).Take(1).ToArray(), context); if (arguments.Count() >= 6) separator = arguments[5].Value.ExtractStringValue(); if (arguments.Count() >= 7) lineBreack = arguments[6].Value.ExtractStringValue(); context.Core.WriteToFile(rhs, fileObject, separator, lineBreack); return null; }
public override dynamic Run(IList<Token> tokens, Context context) { ExpectTokens(tokens, 4); if (tokens[1].Type != TokenType.OpenPar || tokens[3].Type != TokenType.Comma || tokens[5].Type != TokenType.ClosedPar) throw new Exception("Invalid syntax."); var value = recurse.Run(tokens.Skip(2).Take(1).ToArray(), context); var xpath = recurse.Run(tokens.Skip(4).Take(1).ToArray(), context); return context.Core.Find(value, xpath); }
public override dynamic Run(IList<Token> tokens, Context context) { ExpectTokens(tokens, 1); var rhs = recurse.Run(tokens.Skip(2).ToArray(), context); var separator = recurse.Run(tokens.Skip(4).ToArray(), context); string lineEnd = ""; if (tokens.Count >= 6) lineEnd = recurse.Run(tokens.Skip(6).ToArray(), context); context.Core.PrintList(rhs, separator, lineEnd); return null; }
protected IList<Token> ExtractArguments(IList<Token> tokens, Context context) { if (tokens.Count < 3) throw new Exception("Invalid subexpression."); if (tokens[0].Type != TokenType.OpenPar) throw new Exception("Syntax error."); // start depth at zero; for each token, increment the depth for "(" and decrement for ")" // when the depth reaches zero again, stop and return the explored sublist var depth = 0; var index = 0; while (index < tokens.Count) { if (tokens[index].Type == TokenType.OpenPar) depth++; else if (tokens[index].Type == TokenType.ClosedPar) depth--; if (depth == 0) break; index++; } // if the loop has ended and the depth is not zero, the parentheses are unbalanced if (depth > 0) throw new Exception("Too many open parentheses."); // this normally can't happen but just in case if (depth < 0) throw new Exception("Too many closed parentheses."); return tokens.Skip(1).Take(index - 1).ToArray(); }
public dynamic Run(IList<Token> tokens, Context context) { if (!tokens.Any()) return null; System.Diagnostics.Debug.WriteLine("Executing: " + string.Join(" ", tokens.Select(t => t.Value).ToArray())); var name = tokens[0].Value; // precedence: accessor, string constant, statement/function, variable if (tokens.Count > 1 && tokens[1].Type == TokenType.Access && tokens[0].Type == TokenType.Identifier) return accessor.Run(tokens, context); if (tokens[0].Type == TokenType.String) return stringConstant.Run(tokens, context); if (executors.ContainsKey(name)) { var executor = executors[name]; return executor.Run(tokens, context); } if (context.Symbols.Exists(name)) return variable.Run(tokens, context); // if all failed, assume it's a mispelled statement name throw new Exception("Unknown statement: " + name); }
public override dynamic Run(IList<Token> tokens, Context context) { var arguments = ExtractArguments(tokens.Skip(1).ToArray(), context); string trimValue = ""; if (arguments.Count >= 4) trimValue = arguments[arguments.Count - 1].Value.ExtractStringValue(); object expression = null; if (arguments[0].Type == TokenType.Identifier && arguments[0].Value.ToLower() == "it") { var expr = arguments.Take(3).ToArray(); var argName = arguments[0].Value; // do not evaluate this symbol //context.Symbols.Set(argName, item); expression = recurse.Run(expr, context); } else expression = recurse.Run(arguments, context); if (expression is string) return TrimString(expression, trimValue); else if (expression is IEnumerable<string>) { var list = ((IEnumerable<string>)expression).ToArray(); for (int index = 0; index < list.Count(); index++) list[index] = TrimString(list[index], trimValue); return list; } throw new Exception("Trim expected string values but not foud"); }
public override dynamic Run(IList<Token> tokens, Context context) { var arguments = ExtractArguments(tokens.Skip(1).ToArray(), context); var listName = arguments[0].Value; var list = context.Symbols.Get(listName) as IEnumerable; if (list == null) throw new Exception("Map: first argument is not a list."); var lambda = arguments.Skip(2).ToArray(); var argName = lambda[0].Value; // do not evaluate this symbol context.Symbols.Declare(argName); var expr = lambda.Skip(2).ToArray(); var result = new List<object>(); foreach (var item in list) { context.Symbols.Set(argName, item); result.Add(recurse.Run(expr, context)); } context.Symbols.Undeclare(argName); return result; }
public void SetUp() { sut = new StatementRunner(); symbols = new SymbolsTable(); core = A.Fake<WebCoreRepository>(); context = new Context(symbols, core); }
public override dynamic Run(IList<Token> tokens, Context context) { ExpectTokens(tokens, 1); var rhs = recurse.Run(tokens.Skip(1).ToArray(), context); context.Core.Print(rhs); return null; }
public override dynamic Run(IList<Token> tokens, Context context) { ExpectTokens(tokens, 1); var identifiers = tokens.Skip(1).Where(it => it.Type == TokenType.Identifier); foreach (var token in identifiers) context.Symbols.Declare(token.Value); return null; }
public void Run(string script, WebCoreRepository core) { var context = new Context(symbols, core); var tokenizedLines = script .Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries) .Select(line => tokenizer.Parse(line)); foreach (var tokens in tokenizedLines) executor.Run(tokens.ToArray(), context); }
public override dynamic Run(IList<Token> tokens, Context context) { ExpectTokens(tokens, 3); if (tokens[2].Type != TokenType.Assignment) throw new Exception("Invalid syntax."); var lhs = tokens[1].Value; var rhs = recurse.Run(tokens.Skip(3).ToArray(), context); context.Symbols.Set(lhs, rhs); return null; }
public override dynamic Run(IList<Token> tokens, Context context) { ExpectTokens(tokens, 2); var it = context.Symbols.Get(tokens[0].Value); var name = tokens[2].Value; var property = it.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance); if (property == null) throw new Exception("Unknown property " + name); return property.GetValue(it); }
public override dynamic Run(IList<Token> tokens, Context context) { ExpectTokens(tokens, 3); if (tokens[1].Type != TokenType.OpenPar || tokens[3].Type != TokenType.ClosedPar) throw new Exception("Invalid syntax."); var rhs = recurse.Run(tokens.Skip(2).ToArray(), context); bool fileExists = File.Exists(rhs); if (rhs.ToString().StartsWith("http") || !fileExists) return context.Core.OpenPage(rhs.ToString()); else return context.Core.OpenFile(rhs.ToString()); }
public override dynamic Run(IList<Token> tokens, Context context) { var arguments = ExtractArguments(tokens.Skip(1).ToArray(), context); var listName = arguments[0].Value; var list = context.Symbols.Get(listName) as IEnumerable; if (list == null) throw new Exception("Map: first argument is not a list."); string result = ""; foreach (var item in list) { result += item.ToString(); } return result; }
public override dynamic Run(IList<Token> tokens, Context context) { var arguments = ExtractArguments(tokens.Skip(1).ToArray(), context); var value = recurse.Run(arguments, context); var list = value as IEnumerable<object>; if (list == null) throw new Exception("Flatten: argument is not a list."); var enumerables = list .Select(item => item as IEnumerable) .Where(it => it != null); var result = new List<object>(); foreach (var sublist in enumerables) result.AddRange(sublist.Cast<object>()); return result; }
public override dynamic Run(IList<Token> tokens, Context context) { var arguments = ExtractArguments(tokens.Skip(1).ToArray(), context); object result = new string[0]; object stringValue = null; string separator = ","; if (tokens.Count >= 6 && tokens[6].Type == TokenType.String) { separator = tokens[6].Value.ExtractStringValue(); if (separator == "\\r\\n") separator = Environment.NewLine; } if (arguments[0].Type == TokenType.Identifier && arguments[0].Value.ToLower() == "it") { var expr = arguments.Take(3).ToArray(); var argName = arguments[0].Value; // do not evaluate this symbol stringValue = recurse.Run(expr, context); } else stringValue = recurse.Run(arguments, context); if (stringValue != null) { if (stringValue is string) result = ((string)stringValue).Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries); else throw new Exception("ToArray: Expected string value but " + stringValue.GetType() + " found"); } else throw new Exception("ToArray: Expected string value but no value found"); return result; }
public override dynamic Run(IList<Token> tokens, Context context) { return context.Symbols.Get(tokens[0].Value); }
public override dynamic Run(IList<Token> tokens, Context context) { return Unquote(tokens[0].Value); }
public abstract dynamic Run(IList<Token> tokens, Context context);