예제 #1
0
        public FunctionContext(ExpressionCompiler compiler, string name)
        {
            _compiler = compiler;
            _instructions = new List<Instruction>();
            _loopLabels = new IndexedStack<Tuple<LabelOperand, LabelOperand>>();

            Name = name != null ? _compiler.Identifier(name) : null;
            Label = _compiler.MakeLabel("function");

            IdentifierCount = 0;
        }
예제 #2
0
        /// <summary>
        /// Compile a Mond program from a string.
        /// </summary>
        /// <param name="source">Source code to compile</param>
        /// <param name="fileName">Optional file name to use in errors</param>
        /// <param name="generateDebugInfo">Enable generating debug info</param>
        public static MondProgram Compile(string source, string fileName = null, bool generateDebugInfo = true)
        {
            var lexer = new Lexer(source, fileName);
            var parser = new Parser(lexer);
            var expression = parser.ParseAll();
            expression.SetParent(null);
            expression.Simplify();
            //expression.Print(0);

            var compiler = new ExpressionCompiler(generateDebugInfo);
            return compiler.Compile(expression);
        }
예제 #3
0
        public FunctionContext(ExpressionCompiler compiler, int frameIndex, Scope prevScope, string parentName, string name)
        {
            _instructions = new List <Instruction>();
            _loopLabels   = new IndexedStack <Tuple <LabelOperand, LabelOperand> >();

            Compiler   = compiler;
            FrameIndex = frameIndex;

            Scope = new Scope(frameIndex, prevScope);

            ParentName = parentName;
            Name       = name;
            FullName   = string.Format("{0}{1}{2}", parentName, string.IsNullOrEmpty(parentName) ? "" : ".", Name ?? "");

            AssignedName = name != null?prevScope.Get(name) : null;

            Label = Compiler.MakeLabel("function");

            IdentifierCount = 0;
        }
예제 #4
0
        public FunctionContext(ExpressionCompiler compiler, int argIndex, int localIndex, Scope prevScope, string parentName, string name)
        {
            _instructions = new List<Instruction>();
            _loopLabels = new IndexedStack<Tuple<LabelOperand, LabelOperand>>();

            Compiler = compiler;
            ArgIndex = argIndex;
            LocalIndex = localIndex;

            Scope = prevScope;

            ParentName = parentName;
            Name = name;
            FullName = string.Format("{0}{1}{2}", parentName, string.IsNullOrEmpty(parentName) ? "" : ".", Name ?? "");

            AssignedName = name != null ? prevScope.Get(name) : null;
            Label = Compiler.MakeLabel("function");

            IdentifierCount = 0;
        }
예제 #5
0
        public MondValue Evaluate(string expression)
        {
            var options = new MondCompilerOptions();

            var lexer = new Lexer("return " + expression, "debug", options);
            var parser = new Parser(lexer);

            var expr = parser.ParseStatement(false);

            var rewriter = new DebugExpressionRewriter(LocalObjectName);
            expr = expr.Accept(rewriter).Simplify();
            expr.SetParent(null);

            var oldLocal = _state[LocalObjectName];
            _state[LocalObjectName] = _localObject;

            var program = new ExpressionCompiler(options).Compile(expr);
            var result = _state.Load(program);

            _state[LocalObjectName] = oldLocal;

            return result;
        }