public ErrorList SetTargetObject(
     Interpreter terp,
     ITemplateType source)
 {
     Errors.Clear();
     Interpreter = terp;
     InternalSetTargetObject((CommonTree) T.Children[0], source);
     return Errors;
 }
Exemple #2
0
 protected override void OnEval(out ITemplateType o)
 {
     o = StringType.Empty();
     foreach (var arg in T.Children)
     {
         var directive = arg.Text;
         SetDirective(directive);
     }
 }
 protected override void OnEval(
     out ITemplateType o)
 {
     o = LogicalOr(T);
     if (o == null && !Errors.ContainsError()
         && !Errors.ContainsWarning())
     {
         Errors.ErrorRuntimeError();
     }
 }
Exemple #4
0
 public static ITemplateType ApplyBinary(ITemplateType lhs, ITemplateType rhs, int id, ErrorList errors)
 {
     if (rhs == null || lhs == null)
         return null;
     var op = (Operator) id;
     try
     {
         var rt = lhs.ApplyBinary(op, rhs);
         return PrimitiveType.Create(rt);
     }
     catch (Exception e)
     {
         errors.ErrorRuntimeError(e);
     }
     return null;
 }
 public ErrorList Eval(
     Context ctx,
     Interpreter interpreter,
     out ITemplateType o)
 {
     if (T == null || T.Type == 0)
     {
         Errors.ErrorParse((CommonErrorNode) T);
         o = null;
         return Errors;
     }
     Ctx = ctx;
     Interpreter = interpreter;
     Errors.Clear();
     OnEval(out o);
     return Errors;
 }
        protected override void OnEval(out ITemplateType o)
        {
            var c = (CommonTree) T.Children[0];
            switch (c.Type)
            {
                case 0:
                {
                    Errors.ErrorParse((CommonErrorNode) c);
                    o = null;
                    break;
                }
                case TemplateLexer.MPass:
                case TemplateLexer.EPass:
                {
                    o = EvalPassThrough(c);
                    if (c.Type == TemplateLexer.MPass)
                    {
                        string obj;
                        if (!o.TryConvert(out obj))
                        {
                            Errors.ErrorMacrosOnlyAcceptStrings(c.Text);
                            return;
                        }
                        var i = new Interpreter(obj);

                        var result = i.Apply(S);
                        Errors.AddRange(result.Errors);

                        o = Errors.ContainsError()
                            ? StringType.Empty()
                            : StringType.New(result.Output);
                    }
                    break;
                }
                default:
                {
                    o = StringType.Empty();
                    break;
                }
            }
        }
 private void SetIndex(
     ITemplateType that,
     ITemplateType source,
     CommonTree argTree)
 {
     var type = that.UnderlyingType;
     var indexer = type.GetProperty("Item");
     if (indexer == null)
     {
         Errors.WarningPropertyNotFound(type, "'Index based property'");
         return;
     }
     var args = GetArgs(argTree);
     try
     {
         indexer.SetValue(that.RawValue, source.RawValue, args);
     }
     catch (Exception e)
     {
         Errors.ErrorRuntimeError(e);
     }
 }
 private IEnumerator PrimeIter(ITemplateType t)
 {
     var iter = (IEnumerable) t.RawValue;
     return iter.GetEnumerator();
 }
 private bool InvokeFunc(
     ITemplateType that,
     string name,
     CommonTree argTree,
     out ITemplateType result)
 {
     var type = that.UnderlyingType;
     var method = type.GetMethod(name);
     if (method == null)
     {
         Errors.ErrorFunctionNotFound(type, name);
         result = StringType.Empty();
         return false;
     }
     var args = GetArgs(argTree);
     if (method.ReturnType == typeof (void))
     {
         try
         {
             method.Invoke(that.RawValue, args);
             result = new VoidType();
             return true;
         }
         catch (Exception e)
         {
             Errors.ErrorRuntimeError(e);
             result = null;
             return false;
         }
     }
     try
     {
         var rt = method.Invoke(that.RawValue, args);
         result = PrimitiveType.Create(rt);
         return true;
     }
     catch (Exception e)
     {
         Errors.ErrorRuntimeError(e);
         result = null;
         return false;
     }
 }
 private void Set(
     ITemplateType that,
     string name,
     ITemplateType source)
 {
     var type = that.UnderlyingType;
     var prop = type.GetProperty(name);
     if (prop == null)
     {
         Errors.WarningPropertyNotFound(type, name);
         return;
     }
     try
     {
         prop.SetValue(that.RawValue, source.RawValue, null);
     }
     catch (Exception e)
     {
         Errors.ErrorRuntimeError(e);
     }
 }
 private ITemplateType EvalMacro(ITemplateType source)
 {
     if (source == null) return StringType.Empty();
     string obj;
     if (!source.TryConvert(out obj))
     {
         _errors.ErrorMacrosOnlyAcceptStrings(source.UnderlyingType, string.Empty);
         return StringType.Empty();
     }
     var i = new Interpreter.Interpreter(obj, Cache);
     var result = i.Apply(_state);
     _errors.AddRange(result.Errors);
     return StringType.New(result.Output);
 }
        private bool InvokeDelegate(
            ITemplateType d,
            CommonTree argTree,
            out ITemplateType result)
        {
            var func = (Delegate) d.RawValue;
            var args = GetArgs(argTree);

            if (func.Method.ReturnType == typeof (void))
            {
                try
                {
                    func.DynamicInvoke(args);
                    result = new VoidType();
                    return true;
                }
                catch (Exception e)
                {
                    Errors.ErrorRuntimeError(e);
                    result = null;
                    return false;
                }
            }
            try
            {
                var rt = func.DynamicInvoke(args);
                result = PrimitiveType.Create(rt);
                return true;
            }
            catch (Exception e)
            {
                Errors.ErrorRuntimeError(e);
                result = null;
                return false;
            }
        }
 private bool Get(
     ITemplateType that,
     string name,
     out ITemplateType result)
 {
     var type = that.UnderlyingType;
     var prop = type.GetProperty(name);
     if (prop == null)
     {
         Errors.WarningPropertyNotFound(type, name);
         result = StringType.Empty();
         return false;
     }
     try
     {
         result = PrimitiveType.Create(prop.GetValue(that.RawValue, null));
         return true;
     }
     catch (Exception e)
     {
         Errors.ErrorRuntimeError(e);
         result = null;
         return false;
     }
 }
 protected void OnChild(
     Evaluator child,
     out ITemplateType o)
 {
     var e = child.Eval(Ctx, Interpreter, out o);
     Errors.AddRange(e);
 }
 private void AppendToBuffer(ITemplateType t)
 {
     if (t == null)
         return;
     if (_buffer == null)
         return;
     t.WriteTo(_buffer);
 }
 private void SetState(
     string name,
     ITemplateType rvalue)
 {
     if (string.IsNullOrEmpty(name)
         || rvalue == null)
         return;
     _state.SetValue(name, rvalue);
 }
 private bool TryPrimary(CommonTree tree, out ITemplateType o)
 {
     switch (tree.Type)
     {
         case 0:
         {
             Errors.ErrorParse((CommonErrorNode) tree);
             o = null;
             return true;
         }
         case TemplateParser.DynamicString:
         {
             Errors.AddRange(new DynamicString(tree).Eval(Ctx, Interpreter, out o));
             return true;
         }
         case TemplateParser.Constant:
         {
             Errors.AddRange(new Constant(tree).Eval(Ctx, Interpreter, out o));
             return true;
         }
         case TemplateParser.Statement:
         {
             Errors.AddRange(new Statement(tree).Eval(Ctx, Interpreter, out o));
             return true;
         }
         case TemplateParser.Passthrough:
         {
             Errors.AddRange(new Passthrough(tree).Eval(Ctx, Interpreter, out o));
             return true;
         }
         case TemplateParser.Nested:
         {
             var expression = (CommonTree) tree.Children[0];
             o = LogicalOr(expression);
             return true;
         }
         case TemplateParser.Error:
         {
             Errors.ErrorUnknownKeyword(tree.Children[0].Text);
             o = null;
             return true;
         }
         default:
         {
             o = null;
             return false;
         }
     }
 }
        private ITemplateType InvokeFunction5(
            ITemplateType that,
            string name,
            ITemplateType arg0,
            ITemplateType arg1,
            ITemplateType arg2,
            ITemplateType arg3,
            ITemplateType arg4)
        {
            if (that == null)
                return null;
            var type = that.UnderlyingType;
            var method = type.GetMethod(name);
            if (method == null)
            {
                _errors.ErrorFunctionNotFound(type, name);
                return null;
            }
            var isVoid = method.ReturnType == typeof (void);
            FixedArgsCallSite site;
            if (!TryGetCompiledFixedArgs(type, name, out site))
            {
                site = new FixedArgsCallSite(5, method);
                SetCompiledFixedArgs(type, name, site);
            }
            try
            {
                var rt = site.Invoke(
                    that.RawValue,
                    arg0.RawValue,
                    arg1.RawValue,
                    arg2.RawValue,
                    arg3.RawValue,
                    arg4.RawValue);

                return isVoid ? new VoidType() : PrimitiveType.Create(rt);
            }
            catch (Exception e)
            {
                _errors.ErrorRuntimeError(e);
                return null;
            }
        }
 private ITemplateType InvokeFunction(
     ITemplateType that,
     string name,
     ITemplateType[] args)
 {
     if (that == null)
         return null;
     var type = that.UnderlyingType;
     var method = type.GetMethod(name);
     if (method == null)
     {
         _errors.ErrorFunctionNotFound(type, name);
         return null;
     }
     var isVoid = method.ReturnType == typeof (void);
     if ((OptimizeLevel & OptimizeLevel.Callsite) == OptimizeLevel.Callsite)
     {
         CallSite site;
         if (!TryGetCompiledFunctionCall(type, name, out site))
         {
             site = new MemberFunctionCallSite(method);
             SetCompiledFunctionCall(type, name, site);
         }
         try
         {
             var rt = site.Invoke(that.RawValue, args.ToRaw());
             return isVoid ? new VoidType() : PrimitiveType.Create(rt);
         }
         catch (Exception e)
         {
             _errors.ErrorRuntimeError(e);
             return null;
         }
     }
     try
     {
         var rt = method.Invoke(that.RawValue, args.ToRaw());
         return isVoid ? new VoidType() : PrimitiveType.Create(rt);
     }
     catch (Exception e)
     {
         _errors.ErrorRuntimeError(e);
         return null;
     }
 }
Exemple #20
0
 public static ITemplateType ApplyUnary(ITemplateType lhs, int id, ErrorList errors)
 {
     if (lhs == null)
         return null;
     var op = (Operator) id;
     try
     {
         return PrimitiveType.Create(lhs.ApplyUnary(op));
     }
     catch (Exception e)
     {
         errors.ErrorRuntimeError(e);
     }
     return null;
 }
 private bool Index(
     ITemplateType that,
     CommonTree argTree,
     out ITemplateType result)
 {
     var type = that.UnderlyingType;
     var indexer = type.GetProperty("Item");
     if (indexer == null)
     {
         Errors.WarningPropertyNotFound(type, "'Index based property'");
         result = StringType.Empty();
         return false;
     }
     var args = GetArgs(argTree);
     try
     {
         var item = indexer.GetValue(that.RawValue, args);
         result = PrimitiveType.Create(item);
         return true;
     }
     catch (Exception e)
     {
         Errors.ErrorRuntimeError(e);
         result = null;
         return false;
     }
 }
Exemple #22
0
 public static int UnwrapBooleanType(ITemplateType operand)
 {
     if (operand == null)
         return 0;
     bool flag;
     if (!operand.TryConvert(out flag))
     {
         return 0;
     }
     return flag ? 1 : 0;
 }
        private void InternalSetTargetObject(
            CommonTree s,
            ITemplateType source)
        {
            var root = s.Children[0].Text;
            if (s.Children.Count < 2)
            {
                S.SetValue(root, source);
                return;
            }
            ITemplateType r;
            if (!S.TryGetValue(root, out r))
            {
                Errors.ErrorMissingItemInChain(S.GetType(), root);
                return;
            }

            var that = r;
            for (var i = 1; i < s.Children.Count; i++)
            {
                var current = (CommonTree) s.Children[i];
                ITemplateType result;
                switch (current.Type)
                {
                    case 0:
                    {
                        Errors.ErrorParse((CommonErrorNode) current);
                        return;
                    }
                    case TemplateParser.Invoke:
                    {
                        if (i == s.Children.Count - 1)
                        {
                            Errors.ErrorUnableToSetIntoFunctionResult(
                                that.UnderlyingType.Name, current.Text);
                            return;
                        }
                        //that should be a delegate
                        if (!that.CanInvoke())
                        {
                            Errors.ErrorNonInvokableObject(that.ToString());
                            return;
                        }
                        if (!InvokeDelegate(that, current, out result))
                        {
                            return;
                        }
                        that = result;
                        break;
                    }
                    case TemplateParser.Indexer:
                    {
                        if (i == s.Children.Count - 1)
                        {
                            SetIndex(that, source, current);
                            return;
                        }

                        if (!Index(that, current, out result))
                        {
                            return;
                        }
                        that = result;
                        break;
                    }
                    case TemplateParser.Prop:
                    {
                        if (i == s.Children.Count - 1)
                        {
                            Set(that, current.Text, source);
                            return;
                        }
                        var next = (CommonTree) s.Children[i + 1];
                        switch (next.Type)
                        {
                            case TemplateParser.Invoke:
                            {
                                //consume this prop and the args
                                // then invoke
                                var funcName = current.Text;
                                var args = next;
                                if (!InvokeFunc(
                                    that,
                                    funcName,
                                    args,
                                    out result))
                                {
                                    return;
                                }
                                that = result;
                                i++; //move to args
                                break;
                            }
                            default:
                            {
                                if (!Get(
                                    that,
                                    current.Text,
                                    out result))
                                {
                                    return;
                                }
                                that = result;
                                break;
                            }
                        }
                        break;
                    }
                }
            }
            Errors.ErrorUnableToSetIntoFunctionResult(
                that.UnderlyingType.Name,
                s.Children[s.Children.Count - 2].Text);
        }
 protected abstract void OnEval(out ITemplateType o);
 private int CheckIsEnumerable(ITemplateType t)
 {
     if (t.RawValue is IEnumerable)
     {
         return 1;
     }
     _errors.ErrorIEnumerableRequired(t.UnderlyingType.Name);
     return 0;
 }