internal BlockExpression(List<Expression> body, SilverScope scope)
        {
            body.RemoveAll(e => e == null);

            Body = body;
            SetScope(scope);
        }
 public SilverPartialFunction(SilverFunction function, List<FunctionArgument> args, SilverScope scope)
     : base(function.Name, new List<FunctionArgument>(), null, null)
 {
     WrappedFunction = function;
     PartialArguments = args;
     WrappedScope = scope;
 }
 internal static dynamic CompileExpression(Expression e, SilverScope scope)
 {
     Expression newExpression = AstExpression.Convert(e, typeof (object));
     newExpression.SetScope(scope);
     dynamic l = CreateLambdaForExpression(newExpression);
     return l();
 }
Esempio n. 4
0
        private static dynamic ClassEval(object self, string eval, SilverScope scope)
        {
            SilverClass @class;
            var instance = self as SilverInstance;
            if (instance != null)
            {
                @class = instance.Class;
            }
            else
            {
                @class = self as SilverClass;
            }
            if (@class == null)
                return null;

            var xexpression = string.Format("{0};", eval);
            var lexer = new SilverLexer("source", xexpression);
            var stream = new CommonTokenStream(lexer.Queue);
            var parser = new SilverParser(stream);
            lexer.SetLines(parser);

            var res = parser.program();

            return res != null ? RuntimeOperations.DefineCategory(@class, res.ToList(), scope) : null;
        }
Esempio n. 5
0
 public SilverFunction(string name, List<FunctionArgument> arguments, BlockExpression body, SilverScope context)
 {
     Name = name;
     Arguments = arguments;
     Body = body;
     Context = context;
 }
Esempio n. 6
0
 public SilverInstance(SilverClass @class)
 {
     _class = @class;
     SingletonMethods = new Dictionary<string, SilverFunction>();
     UndefinedMethods = new List<string>();
     RemovedMethods = new List<string>();
     InstanceVariables = new SilverScope();
 }
Esempio n. 7
0
 internal SilverClass()
 {
     ClassMethods = new Dictionary<string, SilverMethodTable>();
     InstanceMethods = new Dictionary<string, SilverMethodTable>();
     UndefinedMethods = new List<string>();
     RemovedMethods = new List<string>();
     Context = new SilverScope();
 }
 private static void SetParentScopeIfBlock(this Expression expr, SilverScope scope)
 {
     if (expr is BlockExpression) {
         expr.SetParentScope(scope);
     }
     else if (expr is IScopeExpression) {
         (expr as IScopeExpression).SetScope(scope);
     }
 }
 public override void SetChildrenScopes(SilverScope scope)
 {
     LeftHandValues.ForEach(value => {
         if (value.Value is AstExpression) {
             value.Value.SetScope(scope);
         }
     });
     RightHandValues.ForEach(value => {
         if (value.Value is AstExpression) {
             value.Value.SetScope(scope);
         }
     });
 }
Esempio n. 10
0
 public override void SetChildrenScopes(SilverScope scope)
 {
     Test.SetScope(scope);
     if (DefaultBody != null)
     {
         DefaultBody.SetScope(scope);
     }
     foreach (SwitchCase @case in Cases)
     {
         foreach (Expression test in @case.TestValues)
         {
             test.SetScope(scope);
         }
         @case.Body.SetScope(scope);
     }
 }
 public static SilverBoxedInstance Box(object obj, SilverScope scope = null)
 {
     if (obj == null) {
         return null;
     }
     if (_boxCache.ContainsKey(obj)) {
         _boxCache[obj].BoxedScope.MergeWithScope(scope ?? new SilverScope());
         return _boxCache[obj];
     }
     var boxed = new SilverBoxedInstance(obj, scope ?? new SilverScope());
     _boxCache[obj] = boxed;
     if (scope != null) {
         string name;
         SilverScope _scope = scope.SearchForObject(obj, out name);
         if (_scope != null) {
             _scope[name] = boxed;
         }
     }
     return boxed;
 }
Esempio n. 12
0
 public SilverClass(string name, SilverClass parent, List<SilverFunction> classMethods,
     List<SilverFunction> instanceMethods)
 {
     Name = name;
     ClassMethods = new Dictionary<string, SilverMethodTable>();
     classMethods.ForEach(func => AddMethod(ClassMethods, func));
     if (!ClassMethods.ContainsKey("new")) {
         AddMethod(ClassMethods, new SilverFunction("new", new List<FunctionArgument>(),
             AstExpression.Block(
                 AstExpression.Return(new List<FunctionArgument> {
                     new FunctionArgument(null, AstExpression.Variable(Expression.Constant("self")))
                 }),
                 Expression.Label(SilverParser.ReturnTarget, Expression.Constant(null, typeof (object)))),
             new SilverScope()));
     }
     InstanceMethods = new Dictionary<string, SilverMethodTable>();
     instanceMethods.ForEach(func => AddMethod(InstanceMethods, func));
     UndefinedMethods = new List<string>();
     RemovedMethods = new List<string>();
     Context = new SilverScope();
     Parent = parent;
 }
 public override void SetChildrenScopes(SilverScope scope)
 {
     Contents.ForEach(content => content.SetScope(scope));
 }
        internal static dynamic Invoke(Type targetType, MethodBase minfo, List<FunctionArgument> args,
            SilverScope scope)
        {
            bool isClassMethod = minfo.IsStatic;

            object target = scope.Variables.ContainsKey("self")
                ? scope["self"]
                : isClassMethod ? targetType : targetType.GetConstructor(new Type[] {}).Invoke(null);

            var arguments = new List<object>();
            args.ForEach(arg => {
                dynamic _val = CompilerServices.CompileExpression(arg.Value, scope);
                if (_val is SilverString) {
                    _val = (string) _val;
                }
                if (_val is SilverNumber)
                {
                    _val = SilverNumber.Convert((SilverNumber) _val);
                }
                arguments.Add(_val);
            });

            while (arguments.Count < minfo.GetParameters().Count()) {
                arguments.Add(null);
            }

            if (minfo.IsConstructor) {
                var ctor = (ConstructorInfo) minfo;
                return ctor.Invoke(arguments.ToArray());
            }

            if (target is SilverInstance && !(target is SilverBoxedInstance) &&
                ((SilverInstance) target).BackingObject != null) {
                target = ((SilverInstance) target).BackingObject;
            }

            dynamic val = null;

            if (((MethodInfo) minfo).ReturnType != typeof (void)) {
                val = minfo.Invoke(target, arguments.ToArray());
            }
            else {
                minfo.Invoke(target, arguments.ToArray());
            }

            return val;
        }
 public override void SetChildrenScopes(SilverScope scope)
 {
     foreach (Expression value in Values) {
         value.SetScope(scope);
     }
 }
Esempio n. 16
0
 /// <summary>
 ///     Called by SetScope to set the children scope on expressions.  Should be overridden to tell the runtime which
 ///     children should have scopes set.
 /// </summary>
 /// <param name="scope"></param>
 public virtual void SetChildrenScopes(SilverScope scope)
 {
 }
Esempio n. 17
0
 public override void SetChildrenScopes(SilverScope scope)
 {
     Test.SetScope(scope);
     IfTrue.SetScope(scope);
     IfFalse.SetScope(scope);
 }
Esempio n. 18
0
 public override void SetChildrenScopes(SilverScope scope)
 {
     foreach (FunctionArgument arg in Arguments) {
         arg.Value.SetScope(scope);
     }
 }
 public override void SetChildrenScopes(SilverScope scope)
 {
     Key.SetScope(scope);
     Value.SetScope(scope);
 }
Esempio n. 20
0
 /// <summary>
 ///     Sets the scope used by this expression.
 /// </summary>
 /// <param name="scope">Scope for this expression</param>
 public void SetScope(SilverScope scope)
 {
     _scope = scope;
     SetChildrenScopes(scope);
 }
Esempio n. 21
0
 public override void SetChildrenScopes(SilverScope scope)
 {
     Body.SetScope(scope);
     Init.SetScope(((AstExpression) Body).Scope);
     Test.SetScope(((AstExpression) Body).Scope);
     Step.SetScope(((AstExpression) Body).Scope);
 }
Esempio n. 22
0
 private static void SetParentScope(this Expression expr, SilverScope scope)
 {
     if (expr is IScopeExpression) {
         (expr as IScopeExpression).Scope.ParentScope = scope;
     }
 }
Esempio n. 23
0
 public override void SetChildrenScopes(SilverScope scope)
 {
     Expression.SetScope(scope);
 }
Esempio n. 24
0
        internal object Run(SilverScope scope)
        {
            var body = (BlockExpression) Body;

            body.SetScope(scope);

            body.SetChildrenScopes(body.Scope);

            dynamic block = CompilerServices.CreateLambdaForExpression(Expression.Block(body));

            dynamic res = block();

            if (res is Symbol) {
                var symval = new BlockExpression(new List<Expression> {new VariableExpression(res)}, body.Scope);
                res = CompilerServices.CreateLambdaForExpression(symval)();
            }

            return res;
        }
 public override void SetChildrenScopes(SilverScope scope)
 {
     Function.SetScope(scope);
 }
 public override void SetChildrenScopes(SilverScope scope)
 {
     Left.SetScope(scope);
     Right.SetScope(scope);
 }
Esempio n. 27
0
 public override void SetChildrenScopes(SilverScope scope)
 {
     TargetType.SetScope(scope);
     Method.SetScope(scope);
 }
Esempio n. 28
0
 public override void SetChildrenScopes(SilverScope scope)
 {
     Enumerator.SetScope(scope);
     Body.SetScope(scope);
 }
 public override void SetChildrenScopes(SilverScope scope)
 {
     Body.SetScope(scope);
 }
Esempio n. 30
0
 public override void SetChildrenScopes(SilverScope scope)
 {
     ExceptionObject.SetScope(scope);
 }