public static NovaBoxedInstance Box(object obj, NovaScope scope = null)
        {
            if (obj == null)
            {
                return(null);
            }
            if (_boxCache.ContainsKey(obj))
            {
                _boxCache[obj].BoxedScope.MergeWithScope(scope ?? new NovaScope());
                return(_boxCache[obj]);
            }
            var boxed = new NovaBoxedInstance(obj, scope ?? new NovaScope());

            _boxCache[obj] = boxed;
            if (scope != null)
            {
                string name;
                var    _scope = scope.SearchForObject(obj, out name);
                if (_scope != null)
                {
                    _scope[name] = boxed;
                }
            }
            return(boxed);
        }
 public NovaPartialFunction(NovaFunction function, List <FunctionArgument> args, NovaScope scope)
     : base(function.Name, new List <FunctionArgument>(), null, null)
 {
     WrappedFunction  = function;
     PartialArguments = args;
     WrappedScope     = scope;
 }
Пример #3
0
 public NovaFunction(string name, List <FunctionArgument> arguments, BlockExpression body, NovaScope context)
 {
     Name      = name;
     Arguments = arguments;
     Body      = body;
     Context   = context;
 }
Пример #4
0
 internal NovaClass()
 {
     ClassMethods     = new Dictionary <string, NovaMethodTable>();
     InstanceMethods  = new Dictionary <string, NovaMethodTable>();
     UndefinedMethods = new List <string>();
     RemovedMethods   = new List <string>();
     Context          = new NovaScope();
 }
Пример #5
0
 public NovaInstance(NovaClass @class)
 {
     _class            = @class;
     SingletonMethods  = new Dictionary <string, NovaFunction>();
     UndefinedMethods  = new List <string>();
     RemovedMethods    = new List <string>();
     InstanceVariables = new NovaScope();
 }
Пример #6
0
 public NovaScope(NovaScope parent)
 {
     Variables   = new Dictionary <string, dynamic>();
     SymVars     = new Dictionary <Symbol, dynamic>();
     ParentScope = parent;
     Aliases     = new Dictionary <string, string>();
     Constants   = new List <string>();
 }
        internal static dynamic CompileExpression(Expression e, NovaScope scope)
        {
            Expression newExpression = NovaExpression.Convert(e, typeof(object));

            newExpression.SetScope(scope);
            var l = CreateLambdaForExpression(newExpression);

            return(l());
        }
Пример #8
0
        internal static dynamic Invoke(Type targetType, MethodBase minfo, List <FunctionArgument> args,
                                       NovaScope scope)
        {
            var 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 => {
                var _val = CompilerServices.CompileExpression(arg.Value, scope);
                if (_val is NovaString)
                {
                    _val = (string)_val;
                }
                if (_val is NovaNumber)
                {
                    _val = NovaNumber.Convert((NovaNumber)_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 NovaInstance && !(target is NovaBoxedInstance) &&
                ((NovaInstance)target).BackingObject != null)
            {
                target = ((NovaInstance)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);
        }
Пример #9
0
 private dynamic Resolve(Symbol sym, NovaScope startingScope)
 {
     if (SymVars.ContainsKey(sym))
     {
         return(SymVars[sym]);
     }
     if (ParentScope != null)
     {
         return(ParentScope.Resolve(sym, startingScope));
     }
     return(startingScope.Resolve(sym.Name));
 }
        public static NovaBoxedInstance BoxNoCache(object obj, NovaScope scope = null)
        {
            if (obj == null)
            {
                return(null);
            }
            var boxed = new NovaBoxedInstance(obj, scope ?? new NovaScope());

            if (scope != null)
            {
                string name;
                var    _scope = scope.SearchForObject(obj, out name);
                if (_scope != null)
                {
                    _scope[name] = boxed;
                }
            }
            return(boxed);
        }
Пример #11
0
        internal static dynamic DefineModule(object rawName, List <Expression> contents, object rawScope)
        {
            lock (_classDefineLock) {
                var scope       = (NovaScope)rawScope;
                var defineScope = _inClassDefine ? scope : scope.GlobalScope;

                var name = (string)rawName;

                var xScope = new NovaScope(scope);

                var module = new NovaModule {
                    Name = name, Context = scope
                };

                contents.ForEach(content => module.Contents.Add(CompilerServices.CompileExpression(content, xScope)));

                defineScope[module.Name] = module;

                return(module);
            }
        }
Пример #12
0
        internal object Run(NovaScope scope)
        {
            var body = (BlockExpression)Body;

            body.SetScope(scope);

            body.SetChildrenScopes(body.Scope);

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

            var res = block();

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

            return(res);
        }
Пример #13
0
 public NovaClass(string name, NovaClass parent, List <NovaFunction> classMethods,
                  List <NovaFunction> instanceMethods)
 {
     Name         = name;
     ClassMethods = new Dictionary <string, NovaMethodTable>();
     classMethods.ForEach(func => AddMethod(ClassMethods, func));
     if (!ClassMethods.ContainsKey("new"))
     {
         AddMethod(ClassMethods, new NovaFunction("new", new List <FunctionArgument>(),
                                                  NovaExpression.NovaBlock(
                                                      NovaExpression.Return(new List <FunctionArgument> {
             new FunctionArgument(null, NovaExpression.Variable(Expression.Constant("self")))
         }),
                                                      Expression.Label(NovaParser.ReturnTarget, Expression.Constant(null, typeof(object)))),
                                                  new NovaScope()));
     }
     InstanceMethods = new Dictionary <string, NovaMethodTable>();
     instanceMethods.ForEach(func => AddMethod(InstanceMethods, func));
     UndefinedMethods = new List <string>();
     RemovedMethods   = new List <string>();
     Context          = new NovaScope();
     Parent           = parent;
 }
Пример #14
0
        internal static dynamic DefineClass(object rawName, object rawParent, List <Expression> contents, object rawScope)
        {
            lock (_classDefineLock) {
                if (Resolve(rawName, rawScope) != null)
                {
                    return(DefineCategory(Resolve(rawName, rawScope), contents, rawScope));
                }
                var scope       = (NovaScope)rawScope;
                var defineScope = _inClassDefine ? scope : scope.GlobalScope;
                _inClassDefine = true;
                NovaClass parent;
                if (rawParent == null)
                {
                    if (scope.GlobalScope["Object"] == null)
                    {
                        scope.GlobalScope["Object"] = Nova.Box(typeof(object));
                    }
                    parent = scope.GlobalScope["Object"];
                }
                else
                {
                    var dParent = Resolve(rawParent as string, scope);
                    if (dParent == null)
                    {
                        _inClassDefine = false;
                        return(null);
                    }
                    if (dParent is Type)
                    {
                        parent = Nova.Box(dParent);
                    }
                    else
                    {
                        parent = dParent as NovaClass;
                    }
                    if (parent == null)
                    {
                        _inClassDefine = false;
                        return(null);
                    }
                }

                var name = (string)rawName;
                _className = name;

                var @class = new NovaClass {
                    Name = _className, Parent = parent
                };
                var xScope = new NovaScope(scope);
                xScope["self"]     = @class;
                xScope[_className] = @class;
                _currentClassScope = xScope;

                contents.ForEach(content => {
                    if (content is IncludeExpression)
                    {
                        // We only include modules here so make sure this include references a module
                        var names = ((IncludeExpression)content).Names;

                        dynamic module = null;

                        var index = 0;
                        names.ForEach(mname => {
                            if ((module is NovaModule))
                            {
                                module = module.Context[mname];
                            }
                            else if (index == 0)
                            {
                                module = scope[mname];
                            }
                            index = index + 1;
                        });

                        if (module != null)
                        {
                            if (module is NovaModule)
                            {
                                ((NovaModule)module).Contents.ForEach(mcon => {
                                    if (mcon is NovaFunction)
                                    {
                                        if ((mcon as NovaFunction).IsSingleton ||
                                            (mcon as NovaFunction).Name == "new")
                                        {
                                            NovaClass.AddMethod(@class.ClassMethods, mcon as NovaFunction);
                                        }
                                        else
                                        {
                                            NovaClass.AddMethod(@class.InstanceMethods, mcon as NovaFunction);
                                        }
                                        if (@class.RemovedMethods.Contains((mcon as NovaFunction).Name))
                                        {
                                            @class.RemovedMethods.Remove((mcon as NovaFunction).Name);
                                        }
                                        if (@class.UndefinedMethods.Contains((mcon as NovaFunction).Name))
                                        {
                                            @class.UndefinedMethods.Remove((mcon as NovaFunction).Name);
                                        }
                                    }
                                });

                                xScope.MergeWithScope(module.Context);
                            }
                            else if (module is NovaClass)
                            {
                                xScope[((NovaClass)module).Name] = module;
                            }
                        }
                    }
                });

                contents.ForEach(content => {
                    if (!(content is IncludeExpression))
                    {
                        var result = CompilerServices.CompileExpression(content, xScope);
                        if (result is NovaFunction)
                        {
                            if ((result as NovaFunction).IsSingleton || (result as NovaFunction).Name == "new")
                            {
                                NovaClass.AddMethod(@class.ClassMethods, result as NovaFunction);
                            }
                            else
                            {
                                NovaClass.AddMethod(@class.InstanceMethods, result as NovaFunction);
                            }
                            if (@class.RemovedMethods.Contains((result as NovaFunction).Name))
                            {
                                @class.RemovedMethods.Remove((result as NovaFunction).Name);
                            }
                            if (@class.UndefinedMethods.Contains((result as NovaFunction).Name))
                            {
                                @class.UndefinedMethods.Remove((result as NovaFunction).Name);
                            }
                        }
                    }
                });

                if ([email protected]("new"))
                {
                    NovaClass.AddMethod(@class.ClassMethods, new NovaFunction("new", new List <FunctionArgument>(),
                                                                              NovaExpression.NovaBlock(
                                                                                  NovaExpression.Return(new List <FunctionArgument> {
                        new FunctionArgument(null, NovaExpression.Variable(Expression.Constant("self")))
                    }),
                                                                                  Expression.Label(NovaParser.ReturnTarget, Expression.Constant(null, typeof(object)))),
                                                                              new NovaScope()));
                }
                @class.Context           = xScope;
                defineScope[@class.Name] = @class;
                _inClassDefine           = false;
                return(@class);
            }
        }
Пример #15
0
 public NovaModule(string name, NovaScope context, List <object> contents)
 {
     Name     = name;
     Contents = contents;
     Context  = context;
 }
        internal static dynamic String(object rawEval, object rawScope)
        {
            StringBuilder @new;
            var           eval = rawEval as String;

            var components = eval.Split(new[] { "#{" }, StringSplitOptions.None);

            if (components.Count() == 1)
            {
                return(new NovaString(eval));
            }
            @new = new StringBuilder(components[0]);
            for (var i = 1; i < components.Count(); i++)
            {
                var parts        = components[i].Split(new[] { "}" }, StringSplitOptions.None);
                var expression   = parts[0];
                var escapeString = false;
                if (expression != null && expression[0] == ':')
                {
                    escapeString = true;
                    expression   = expression.Substring(1);
                }
                if (expression != null)
                {
                    var scope       = (NovaScope)rawScope;
                    var xexpression = string.Format("{0};", expression);

                    var        res = NovaParser.Parse(xexpression);
                    Expression block;
                    if (res != null)
                    {
                        block = NovaExpression.NovaBlock(res);
                    }
                    else
                    {
                        return(null);
                    }
                    var myScope = new NovaScope();
                    var visitor = new VariableNameVisitor();
                    visitor.Visit(block);
                    visitor.VariableNames.ForEach(name => myScope[name] = scope[name]);
                    var val = CompilerServices.CompileExpression(block, myScope);
                    if (val != null)
                    {
                        string stringVal = val.ToString();
                        if (escapeString && val is string)
                        {
                            stringVal = string.Format("\"{0}\"", stringVal);
                        }
                        @new.Append(stringVal ?? "");
                    }
                    else
                    {
                        @new.Append(expression);
                    }
                }
                if (parts.Count() > 1)
                {
                    @new.Append(parts[1]);
                    var j = 2;
                    while (j < parts.Count())
                    {
                        @new.Append("}");
                        @new.Append(parts[j++]);
                    }
                }
            }

            return(new NovaString(@new.ToString()));
        }
Пример #17
0
 public void SetScope(NovaScope scope)
 {
     _scope = scope;
 }
Пример #18
0
        internal static dynamic DefineCategory(NovaClass @class, List <Expression> contents, object rawScope)
        {
            lock (_classDefineLock) {
                var scope = (NovaScope)rawScope;
                _inClassDefine = true;
                _className     = @class.Name;

                scope["self"]      = @class;
                scope[_className]  = @class;
                _currentClassScope = scope;

                contents.ForEach(content => {
                    if (content is IncludeExpression)
                    {
                        // We only include modules here so make sure this include references a module
                        var names = ((IncludeExpression)content).Names;

                        dynamic module = null;

                        var index = 0;
                        names.ForEach(mname => {
                            if (module != null && (module is NovaModule))
                            {
                                module = module.Context[mname];
                            }
                            else if (index == 0)
                            {
                                module = scope[mname];
                            }
                            index = index + 1;
                        });

                        if (module != null)
                        {
                            if (module is NovaModule)
                            {
                                ((NovaModule)module).Contents.ForEach(mcon => {
                                    if (mcon is NovaFunction)
                                    {
                                        if ((mcon as NovaFunction).IsSingleton ||
                                            (mcon as NovaFunction).Name == "new")
                                        {
                                            NovaClass.AddMethod(@class.ClassMethods, mcon as NovaFunction);
                                        }
                                        else
                                        {
                                            NovaClass.AddMethod(@class.InstanceMethods, mcon as NovaFunction);
                                        }
                                        if (@class.RemovedMethods.Contains((mcon as NovaFunction).Name))
                                        {
                                            @class.RemovedMethods.Remove((mcon as NovaFunction).Name);
                                        }
                                        if (@class.UndefinedMethods.Contains((mcon as NovaFunction).Name))
                                        {
                                            @class.UndefinedMethods.Remove((mcon as NovaFunction).Name);
                                        }
                                    }
                                });

                                scope.MergeWithScope(module.Context);
                            }
                            else if (module is NovaClass)
                            {
                                scope[((NovaClass)module).Name] = module;
                            }
                        }
                    }
                });

                contents.ForEach(content => {
                    if (!(content is IncludeExpression))
                    {
                        var result = CompilerServices.CompileExpression(content, scope);
                        if (result is NovaFunction)
                        {
                            if ((result as NovaFunction).IsSingleton)
                            {
                                NovaClass.AddMethod(@class.ClassMethods, result as NovaFunction);
                            }
                            else
                            {
                                NovaClass.AddMethod(@class.InstanceMethods, result as NovaFunction);
                            }
                            if (@class.RemovedMethods.Contains((result as NovaFunction).Name))
                            {
                                @class.RemovedMethods.Remove((result as NovaFunction).Name);
                            }
                            if (@class.UndefinedMethods.Contains((result as NovaFunction).Name))
                            {
                                @class.UndefinedMethods.Remove((result as NovaFunction).Name);
                            }
                        }
                    }
                });

                @class.Context.MergeWithScope(scope);
                return(@class);
            }
        }
Пример #19
0
 public void MergeWithScope(NovaScope other)
 {
     Variables = other.Variables.MergeLeft(Variables);
     SymVars   = other.SymVars.MergeLeft(SymVars);
 }
 internal NovaBoxedInstance(object obj, NovaScope scope, NovaClass @class) : base(@class)
 {
     BoxedObject = obj;
     BoxedScope  = scope;
 }
 protected NovaBoxedInstance(object obj, NovaScope scope) : base(GetBoxClass(obj))
 {
     BoxedObject = obj;
     BoxedScope  = scope;
 }