Example #1
0
        private static dynamic InstanceEval(object self, string eval, NovaScope scope)
        {
            if (!(self is NovaInstance instance))
            {
                return(null);
            }

            var        xexpression = string.Format("{0};", eval);
            var        res         = NovaParser.Parse(xexpression);
            Expression block;

            if (res != null)
            {
                scope["self"] = scope["super"] = instance;
                scope["<nova_context_invokemember>"] = true;
                string selfName;
                var    selfScope = scope.SearchForObject(instance, out selfName);
                if (selfScope != null && selfName != null)
                {
                    scope["<nova_context_selfscope>"] = selfScope;
                    scope["<nova_context_selfname>"]  = selfName;
                }
                block = NovaExpression.NovaBlock(res);
                // We want eval'd expressions to execute in the current scope, not its own child scopes.  This ensures assignment evals work properly.
                ((BlockExpression)block).Scope = scope;
                ((BlockExpression)block).SetChildrenScopes(scope);
            }
            else
            {
                return(null);
            }
            var val = CompilerServices.CreateLambdaForExpression(block)();

            return(val);
        }
Example #2
0
 public NovaTokenQueue(NovaParser parser, NovaLexer lexer, string sourceName)
 {
     Parser     = parser;
     _lexer     = lexer;
     SourceName = sourceName;
     _factory   = new NovaTokenFactory {
         Parser = parser
     };
     _tokens = new Queue <CommonToken>();
 }
Example #3
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            var res = NovaParser.Parse(sourceUnit.GetCode(), sourceUnit);

            if (res != null)
            {
                Expression mainBlock = NovaExpression.NovaBlock(res);
                return(new NovaScriptCode(mainBlock, sourceUnit));
            }
            else
            {
                throw new SyntaxErrorException("Syntax error", sourceUnit, SourceSpan.None, 0, Severity.Error);
            }
        }
        public override Expression Reduce()
        {
            var ci = 0;

            Arguments.ForEach(arg => arg.Index = ci++);
            var realBody = new List <Expression>(((BlockExpression)Body).Body);

            if (Name == "new")
            {
                realBody.Add(Return(new List <FunctionArgument> {
                    new FunctionArgument(null, Variable(Constant("self")))
                }));
            }
            realBody.Add(Label(NovaParser.ReturnTarget, Constant(null, typeof(object))));

            return(Operation.Define(typeof(NovaFunction), Constant(Name),
                                    Constant(Arguments),
                                    Constant(NovaParser.CreateBlock(realBody)),
                                    Constant(Scope)));
        }
Example #5
0
        private static dynamic Eval(string eval, NovaScope scope)
        {
            var xexpression = string.Format("{0};", eval);

            var        res = NovaParser.Parse(xexpression);
            Expression block;

            if (res != null)
            {
                block = NovaExpression.NovaBlock(res);
                // We want eval'd expressions to execute in the current scope, not its own child scopes.  This ensures assignment evals work properly.
                ((BlockExpression)block).Scope = scope;
                ((BlockExpression)block).SetChildrenScopes(scope);
            }
            else
            {
                return(null);
            }
            var val = CompilerServices.CreateLambdaForExpression(block)();

            return(val);
        }
Example #6
0
        public bool Read()
        {
            if (!File.Exists(Filepath))
            {
                Logger.Write("File not found : " + Filepath, LogType.Error);
                return(false);
            }
            string text = File.ReadAllText(Filepath);

            NovaParsingErrorHandler parsingErrorHandler = new NovaParsingErrorHandler();

            var inputStream = new AntlrInputStream(text);
            var lexer       = new NovaLexer(inputStream);

            lexer.RemoveErrorListener(ConsoleErrorListener <int> .Instance);

            var commonTokenStream = new CommonTokenStream(lexer);
            var parser            = new NovaParser(commonTokenStream);

            parser.RemoveErrorListener(ConsoleErrorListener <IToken> .Instance);
            parser.AddErrorListener(parsingErrorHandler);

            NovaParser.CompilationUnitContext ectx = parser.compilationUnit();

            foreach (var importDeclaration in ectx.importDeclaration())
            {
                Usings.Add(new Using(UsingType.Ref, importDeclaration.fileName().GetText()));
            }
            ClassListener classListener = new ClassListener(this);

            foreach (var typeDeclaration in ectx.typeDeclaration())
            {
                typeDeclaration.EnterRule(classListener);
            }

            //Console.WriteLine(ectx.ToStringTree(parser));

            return(parsingErrorHandler.ErrorsCount == 0);
        }
Example #7
0
        private static dynamic ClassEval(object self, string eval, NovaScope scope)
        {
            NovaClass @class;
            var       instance = self as NovaInstance;

            if (instance != null)
            {
                @class = instance.Class;
            }
            else
            {
                @class = self as NovaClass;
            }
            if (@class == null)
            {
                return(null);
            }

            var xexpression = string.Format("{0};", eval);

            var res = NovaParser.Parse(xexpression);

            return(res != null?RuntimeOperations.DefineCategory(@class, res.ToList(), scope) : null);
        }
 public void SetLines(NovaParser parser)
 {
     parser.Lines = _lines;
 }
Example #9
0
        public Expression Compile(string rawsource)
        {
            var res = NovaParser.Parse(rawsource);

            return(res != null?NovaExpression.NovaBlock(res) : null);
        }
        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()));
        }