public bool Evaluate(string block, ScriptContext context) { var replacer = context.Get<IVariableReplacer>(); var scriptLog = context.Get<IScriptLog>(); var replaced = replacer.Replace(block, context); // replace single equals with double equals replaced = Regex.Replace(replaced, "([^=:<>])=(?!=)", "$1=="); if(context.DebugLevel > 0) { scriptLog.Log(context.Name, "if {0}".ToFormat(replaced), context.LineNumber); } try { var interpreter = new Interpreter(); var result = (bool)interpreter.Eval(replaced); if(context.DebugLevel > 0) { scriptLog.Log(context.Name, "if result {0}".ToFormat(result.ToString().ToLower()), context.LineNumber); } return result; } catch(Exception exc) { scriptLog.Log(context.Name, "if result {0}".ToFormat(exc), context.LineNumber); return false; } }
public override string RenderTag(string line, TemplateReader reader, Dictionary<string, object> parameters, int patternIndex) { Match lineMatch = Regex.Match(line.Trim(), RegularExpressionPatterns[patternIndex]); String condition = lineMatch.Groups["condition"].Value; var interpreter = new Interpreter(); foreach (var element in parameters) { interpreter.SetVariable(element.Key, element.Value, element.Value.GetType()); } var result = interpreter.Eval<bool>(condition); if (result) return renderer.RenderBlock(reader.ReadBlock()); reader.ReadBlock(); return ""; }
public string callMixin(string line, TemplateReader reader, Dictionary<string, object> parameters, int patternIndex) { Match m = Regex.Match(line.Trim(), RegularExpressionPatterns[patternIndex]); List<object> mixinParams = new List<object>(); String mixinName = m.Groups["name"].Value; Mixin mixin = mixins[mixinName]; var interpreter = new Interpreter(); Dictionary<string, object> outerParameters = new Dictionary<string, object>(); foreach (var element in parameters) { interpreter.SetVariable(element.Key, element.Value, element.Value.GetType()); } foreach (Capture cap in m.Groups["param"].Captures) { var result = interpreter.Eval(cap.Value); mixinParams.Add(result); } if (mixinParams.Count != mixin.parameters.Count) throw new TemplateException("Missmatch number of mixin arguments"); for (int i = 0; i < mixinParams.Count; i++) { if (parameters.ContainsKey(mixin.parameters[i])) { outerParameters.Add(mixin.parameters[i], parameters[mixin.parameters[i]]); parameters[mixin.parameters[i]] = mixinParams[i]; } else { parameters.Add(mixin.parameters[i], mixinParams[i]); } } String blockRendered = renderer.RenderBlock(mixin.block); foreach(var n in mixin.parameters) { parameters.Remove(n); } foreach(var p in outerParameters) { parameters.Add(p.Key, p.Value); } return blockRendered; }