Ejemplo n.º 1
0
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntax = (ScriptSwitchRootStatement)syntaxNode;

            var condition = AstDomCompiler.Compile <CodeExpression>(syntax.Expression, prog);

            var cases = new List <CodeSwitchCase> ();

            foreach (var statement in syntax.Switch.Cases)
            {
                cases.Add(new CodeSwitchCase(
                              AstDomCompiler.Compile <CodeExpression>(statement.Condition, prog),
                              AstDomCompiler.Compile <CodeStatement>(statement.Statement, prog)));
            }

            if (syntax.Switch.DefaultCase != null)
            {
                cases.Add(new CodeSwitchCase(
                              null,
                              AstDomCompiler.Compile <CodeStatement>(syntax.Switch.DefaultCase.Statement, prog)));
            }

            var code = new CodeSwitchStatement(condition, cases);

            return(code);
        }
Ejemplo n.º 2
0
        public void AstDomCompiler_CompileBinaryOperator4()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("1>2;", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.False((bool)context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("1<2;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.True((bool)context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("2!=3;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.True((bool)context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("5 == 5;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.True((bool)context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("5>=5;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.True((bool)context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("5<=4;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.False((bool)context.Result);
        }
 public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
 {
   var syntax = (ScriptFunctionCall)syntaxNode;
   var parameters = syntax.Parameters.Select(expr => AstDomCompiler.Compile<CodeExpression>(expr, prog)).ToList();
   var code = new CodeObjectFunctionCall(parameters);
   return code;
 }
Ejemplo n.º 4
0
        public void AstDomCompiler_CompileBinaryOperator2()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x=1+2*3;", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(7, context.GetItem("x", true));
            Assert.Equal(7, context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("x=1*2+3;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(5, context.GetItem("x", true));
            Assert.Equal(5, context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("x=1+2/2+3*2;", null, false).Ast);
            CodeDomCompiler
            .Compile(domTree)
            .Execute(context);

            Assert.Equal(8, context.GetItem("x", true));
            Assert.Equal(8, context.Result);
        }
Ejemplo n.º 5
0
        public void AstDomCompiler_While()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x = 1; while (x<10) x=x+1;", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(10, context.GetItem("x", true));
            Assert.False((bool)context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("x = 1; while (x<10) x=x+1; return 3;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(3, context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("x = 2; while (x<2) return 2; return 5;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(5, context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("x = 1; while (x<2) return 2; return 5;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(2, context.Result);
        }
Ejemplo n.º 6
0
        public void AstDomCompiler_IfElse()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x = 1; if (x>1) return 2; else return 3;", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(3, context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("x = 2; if (x>1) return 2; else return 3;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(2, context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("x = 2; if (x>1) return 5;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(5, context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("x = 1; if (x>1) return 5; return 10;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(10, context.Result);
        }
Ejemplo n.º 7
0
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntax     = (ScriptFunctionCall)syntaxNode;
            var parameters = syntax.Parameters.Select(expr => AstDomCompiler.Compile <CodeExpression>(expr, prog)).ToList();
            var code       = new CodeObjectFunctionCall(parameters);

            return(code);
        }
Ejemplo n.º 8
0
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntaxAssignExpr = (ScriptAssignExpr)syntaxNode;

            if (syntaxAssignExpr.LeftExpression.IsVariable)
            {
                if (syntaxAssignExpr.Symbol == "++")
                {
                    var binary = new CodeBinaryOperator
                    {
                        Left  = new CodeVariableReference(syntaxAssignExpr.LeftExpression.Identifier),
                        Right = new CodeValueReference(1),
                        Type  = OperatorType.Plus
                    };

                    var codeAssign = new CodeAssignExpression(
                        syntaxAssignExpr.LeftExpression.Identifier,
                        binary)
                    {
                        SourceSpan = syntaxNode.Span
                    };

                    return(codeAssign);
                }
                else
                if (syntaxAssignExpr.Symbol == "--")
                {
                    var binary = new CodeBinaryOperator
                    {
                        Left  = new CodeVariableReference(syntaxAssignExpr.LeftExpression.Identifier),
                        Right = new CodeValueReference(1),
                        Type  = OperatorType.Minus
                    };

                    var codeAssign = new CodeAssignExpression(
                        syntaxAssignExpr.LeftExpression.Identifier,
                        binary)
                    {
                        SourceSpan = syntaxNode.Span
                    };

                    return(codeAssign);
                }
                else
                {
                    var codeAssign = new CodeAssignExpression(
                        syntaxAssignExpr.LeftExpression.Identifier,
                        (CodeExpression)AstDomCompiler.Compile(syntaxAssignExpr.RightExpression, prog))
                    {
                        SourceSpan = syntaxNode.Span
                    };

                    return(codeAssign);
                }
            }

            return(null);
        }
Ejemplo n.º 9
0
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntaxConstExpr = (ScriptConstExpr)syntaxNode;

            return(new CodeValueReference(syntaxConstExpr.Value)
            {
                SourceSpan = syntaxNode.Span
            });
        }
Ejemplo n.º 10
0
        public void AstDomCompiler_Return()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("return 1+1; 2+2;", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(2, context.Result);
        }
Ejemplo n.º 11
0
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntax = (ScriptWhileStatement)syntaxNode;

            var code = new CodeWhileStatement(
                AstDomCompiler.Compile <CodeExpression>(syntax.Condition.Expression, prog),
                AstDomCompiler.Compile <CodeStatement>(syntax.Statement, prog));

            return(code);
        }
Ejemplo n.º 12
0
        public void AstDomCompiler_For()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("sum=0;for(i=0; i<10; i=i+1) sum = sum + i;", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(45, context.GetItem("sum", true));
        }
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var syntax = (ScriptWhileStatement)syntaxNode;

      var code = new CodeWhileStatement(
         AstDomCompiler.Compile<CodeExpression>(syntax.Condition.Expression, prog),
         AstDomCompiler.Compile<CodeStatement>(syntax.Statement, prog));

      return code;
    }
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var syntax = (ScriptForEachStatement)syntaxNode;

      var code = new CodeForEachStatement(
         new CodeVariableReference(syntax.Id),
         AstDomCompiler.Compile<CodeExpression>(syntax.Container, prog),
         AstDomCompiler.Compile<CodeStatement>(syntax.Statement, prog));

      return code;
    }
Ejemplo n.º 15
0
        public void AstDomCompiler_CompileSimple()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x=1;", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(1, context.GetItem("x", true));
            Assert.Equal(1, context.Result);
        }
Ejemplo n.º 16
0
        public void AstDomCompiler_CompileBinaryOperator1()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x=1+1+1;", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.AreEqual(3, context.GetItem("x", true));
            Assert.AreEqual(3, context.Result);
        }
Ejemplo n.º 17
0
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntax = (ScriptForEachStatement)syntaxNode;

            var code = new CodeForEachStatement(
                new CodeVariableReference(syntax.Id),
                AstDomCompiler.Compile <CodeExpression>(syntax.Container, prog),
                AstDomCompiler.Compile <CodeStatement>(syntax.Statement, prog));

            return(code);
        }
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var syntax = (ScriptTypeConvertExpr)syntaxNode;

      if (syntax.TypeExpression == null)
        return AstDomCompiler.Compile(syntax.Expression, prog);

      var typeExpr = (CodeExpression)AstDomCompiler.Compile(syntax.TypeExpression, prog);
      var expr = (CodeExpression)AstDomCompiler.Compile(syntax.Expression, prog);

      return null;
    }
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var syntax = (ScriptCompoundStatement)syntaxNode;

      var block = new CodeBlockStatement();

      foreach (var syntaxStatement in syntax.Statements)
        block.Statements.Add(
          (CodeStatement)AstDomCompiler.Compile(syntaxStatement, prog));

      return block;
    }
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntax = (ScriptFlowControlStatement)syntaxNode;

            if (syntax.Symbol == "return")
            {
                var code = new CodeReturnStatement((CodeExpression)AstDomCompiler.Compile(syntax.Expression, prog));
                return(code);
            }

            throw new NotImplementedException();
        }
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var syntax = (ScriptFlowControlStatement)syntaxNode;

      if (syntax.Symbol == "return")
      {
        var code = new CodeReturnStatement((CodeExpression)AstDomCompiler.Compile(syntax.Expression, prog));
        return code;
      }

      throw new NotImplementedException();
    }
Ejemplo n.º 22
0
        public void AstDomCompiler_ForEach()
        {
            IScriptContext context = new ScriptContext();

            context.SetItem("a", new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            CodeProgram       domTree = AstDomCompiler.Compile(Script.Compile("sum=0; foreach(i in a) sum = sum + i;", null, false).Ast);
            ExecutableMachine vm      = CodeDomCompiler.Compile(domTree);

            vm.Execute(context);

            Assert.Equal(45, context.GetItem("sum", true));
        }
Ejemplo n.º 23
0
    public void CodeValueReferenceTest()
    {
      IScriptContext context = new ScriptContext();
      CodeProgram prog1 = new CodeProgram();
      prog1.Statements.Add(
        new CodeExpressionStatement(
          new CodeAssignExpression("b",
            new CodeValueReference("Test B"))));

      CodeDomCompiler.Compile(prog1).Execute(context);

      Assert.AreEqual("Test B", context.GetItem("b", true));
    }
Ejemplo n.º 24
0
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var syntaxBinExpr = (ScriptBinExpr)syntaxNode;

      var binary = new CodeBinaryOperator
      {
        Left = (CodeExpression) AstDomCompiler.Compile(syntaxBinExpr.Left, prog),
        Right = (CodeExpression) AstDomCompiler.Compile(syntaxBinExpr.Right, prog),
        Type = Mapping[syntaxBinExpr.Symbol]
      };

      return binary;
    }
Ejemplo n.º 25
0
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntaxBinExpr = (ScriptBinExpr)syntaxNode;

            var binary = new CodeBinaryOperator
            {
                Left  = (CodeExpression)AstDomCompiler.Compile(syntaxBinExpr.Left, prog),
                Right = (CodeExpression)AstDomCompiler.Compile(syntaxBinExpr.Right, prog),
                Type  = Mapping[syntaxBinExpr.Symbol]
            };

            return(binary);
        }
Ejemplo n.º 26
0
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntax = (ScriptTypeConvertExpr)syntaxNode;

            if (syntax.TypeExpression == null)
            {
                return(AstDomCompiler.Compile(syntax.Expression, prog));
            }

            var typeExpr = (CodeExpression)AstDomCompiler.Compile(syntax.TypeExpression, prog);
            var expr     = (CodeExpression)AstDomCompiler.Compile(syntax.Expression, prog);

            return(null);
        }
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntax = (ScriptCompoundStatement)syntaxNode;

            var block = new CodeBlockStatement();

            foreach (var syntaxStatement in syntax.Statements)
            {
                block.Statements.Add(
                    (CodeStatement)AstDomCompiler.Compile(syntaxStatement, prog));
            }

            return(block);
        }
Ejemplo n.º 28
0
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var syntaxAssignExpr = (ScriptAssignExpr)syntaxNode;
      
      if (syntaxAssignExpr.LeftExpression.IsVariable)
      {
        if (syntaxAssignExpr.Symbol == "++")
        {
          var binary = new CodeBinaryOperator
          {
            Left = new CodeVariableReference(syntaxAssignExpr.LeftExpression.Identifier),
            Right = new CodeValueReference(1),
            Type = OperatorType.Plus
          };

          var codeAssign = new CodeAssignExpression(
            syntaxAssignExpr.LeftExpression.Identifier,
            binary) {SourceSpan = syntaxNode.Span};

          return codeAssign;
        }
        else
          if (syntaxAssignExpr.Symbol == "--")
          {
            var binary = new CodeBinaryOperator
            {
              Left = new CodeVariableReference(syntaxAssignExpr.LeftExpression.Identifier),
              Right = new CodeValueReference(1),
              Type = OperatorType.Minus
            };

            var codeAssign = new CodeAssignExpression(
              syntaxAssignExpr.LeftExpression.Identifier,
              binary) {SourceSpan = syntaxNode.Span};

            return codeAssign;
          }
          else
          {
            var codeAssign = new CodeAssignExpression(
              syntaxAssignExpr.LeftExpression.Identifier,
              (CodeExpression)AstDomCompiler.Compile(syntaxAssignExpr.RightExpression, prog))
            {SourceSpan = syntaxNode.Span};

            return codeAssign;
          }
      }

      return null;
    }
Ejemplo n.º 29
0
        public void CodeValueReferenceTest()
        {
            IScriptContext context = new ScriptContext();
            CodeProgram    prog1   = new CodeProgram();

            prog1.Statements.Add(
                new CodeExpressionStatement(
                    new CodeAssignExpression("b",
                                             new CodeValueReference("Test B"))));

            CodeDomCompiler.Compile(prog1).Execute(context);

            Assert.AreEqual("Test B", context.GetItem("b", true));
        }
Ejemplo n.º 30
0
        public void AstDomCompiler_InvokeMember3()
        {
            VM_Test1 vt1 = new VM_Test1();

            IScriptContext context = new ScriptContext();

            context.SetItem("v", vt1);

            CodeProgram       domTree = AstDomCompiler.Compile(Script.Compile("return v.Next.Next;", null, false).Ast);
            ExecutableMachine vm      = CodeDomCompiler.Compile(domTree);

            vm.Execute(context);

            Assert.Equal(2, ((VM_Test1)context.Result).Level);
        }
Ejemplo n.º 31
0
        public void AstDomCompiler_PlusPlus_MinusMinus()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("sum=0;for(i=0; i<10; i++) sum = sum + i;", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.AreEqual(45, context.GetItem("sum", true));

            domTree = AstDomCompiler.Compile(Script.Compile("sum=0;for(i=9; i>=0; i--) sum = sum + i;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.AreEqual(45, context.GetItem("sum", true));
        }
Ejemplo n.º 32
0
    public void CodeVariableReferenceTest()
    {
      object testObject = new object();

      IScriptContext context = new ScriptContext();
      context.SetItem("a", testObject);

      CodeProgram prog = new CodeProgram();
      prog.Statements.Add(
        new CodeExpressionStatement(
          new CodeVariableReference("a")));

      CodeDomCompiler.Compile(prog).Execute(context);

      Assert.AreEqual(testObject, context.Result);
    }
Ejemplo n.º 33
0
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var statement = (ScriptStatement)syntaxNode;

      foreach (var code in statement.Select(subStatement => AstDomCompiler.Compile(subStatement, prog)))
      {
        if (code is CodeExpression)
        {
          return new CodeExpressionStatement((CodeExpression)code) { SourceSpan = syntaxNode.Span };
        }

        if (code is CodeStatement)
          return code;
      }

      return null;
    }
Ejemplo n.º 34
0
        public void AstDomCompiler_InvokeMember()
        {
            int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[] b = new int[9];

            IScriptContext context = new ScriptContext();

            context.SetItem("a", a);
            context.SetItem("b", b);

            CodeProgram       domTree = AstDomCompiler.Compile(Script.Compile("return a.CopyTo(b, 0);", null, false).Ast);
            ExecutableMachine vm      = CodeDomCompiler.Compile(domTree);

            vm.Execute(context);

            Assert.Equal(a[4], b[4]);
        }
Ejemplo n.º 35
0
        public void AstDomCompiler_Block()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("{x = 1; y=2;}", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(1, context.GetItem("x", true));
            Assert.Equal(2, context.GetItem("y", true));

            domTree = AstDomCompiler.Compile(Script.Compile("x = 10; y = 0; while (x>0) {x=x-1; y = y+1;}", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(0, context.GetItem("x", true));
            Assert.Equal(10, context.GetItem("y", true));
        }
Ejemplo n.º 36
0
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntax = (ScriptQualifiedName)syntaxNode;

            if (syntax.IsVariable)
            {
                var code = new CodeVariableReference(syntax.Identifier)
                {
                    SourceSpan = syntaxNode.Span
                };
                return(code);
            }

            var modifiers = syntax.Modifiers.Select(modifier => AstDomCompiler.Compile(modifier, prog)).ToList();

            CodeObjectReference objectReference;

            if (syntax.NextFirst)
            {
                var o        = AstDomCompiler.Compile(syntax.NextPart, prog);
                var variable = o as CodeVariableReference;

                if (variable != null)
                {
                    objectReference = new CodeObjectReference(
                        variable.Id,
                        new CodeObjectReference(syntax.Identifier, null, modifiers),
                        new CodeObject[0]);
                }
                else
                {
                    objectReference = (CodeObjectReference)o;
                    ((CodeObjectReference)objectReference.Next).Next = new CodeObjectReference(syntax.Identifier, null, modifiers);
                }
            }
            else
            {
                objectReference = new CodeObjectReference(
                    syntax.Identifier,
                    AstDomCompiler.Compile(syntax.NextPart, prog),
                    modifiers);
            }

            return(objectReference);
        }
Ejemplo n.º 37
0
        public void AstDomCompiler_SwitchDefault()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile(@"
        a = 15;
        switch(a){
          case 1: return 1;
          case 2: return 3;
          case 3: return 5;
          default: return 4;
        }
      ", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(4, context.Result);
        }
Ejemplo n.º 38
0
        public void CodeVariableReferenceTest()
        {
            object testObject = new object();

            IScriptContext context = new ScriptContext();

            context.SetItem("a", testObject);

            CodeProgram prog = new CodeProgram();

            prog.Statements.Add(
                new CodeExpressionStatement(
                    new CodeVariableReference("a")));

            CodeDomCompiler.Compile(prog).Execute(context);

            Assert.AreEqual(testObject, context.Result);
        }
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var syntax = (ScriptQualifiedName)syntaxNode;

      if (syntax.IsVariable)
      {
        var code = new CodeVariableReference(syntax.Identifier) {SourceSpan = syntaxNode.Span};
        return code;
      }

      var modifiers = syntax.Modifiers.Select(modifier => AstDomCompiler.Compile(modifier, prog)).ToList();

      CodeObjectReference objectReference;

      if (syntax.NextFirst)
      {
        var o = AstDomCompiler.Compile(syntax.NextPart, prog);
        var variable = o as CodeVariableReference;

        if (variable != null)
        {
          objectReference = new CodeObjectReference(
            variable.Id,
            new CodeObjectReference(syntax.Identifier, null, modifiers),
            new CodeObject[0]);

        }
        else
        {
          objectReference = (CodeObjectReference)o;
          ((CodeObjectReference)objectReference.Next).Next = new CodeObjectReference(syntax.Identifier, null, modifiers);
        }

      }
      else
      {
        objectReference = new CodeObjectReference(
          syntax.Identifier,
          AstDomCompiler.Compile(syntax.NextPart, prog),
          modifiers);
      }

      return objectReference;
    }
Ejemplo n.º 40
0
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var statement = (ScriptStatement)syntaxNode;

            foreach (var code in statement.Select(subStatement => AstDomCompiler.Compile(subStatement, prog)))
            {
                if (code is CodeExpression)
                {
                    return(new CodeExpressionStatement((CodeExpression)code)
                    {
                        SourceSpan = syntaxNode.Span
                    });
                }

                if (code is CodeStatement)
                {
                    return(code);
                }
            }

            return(null);
        }
Ejemplo n.º 41
0
        public void AstDomCompiler_CompileBinaryOperator3()
        {
            IScriptContext context = new ScriptContext();

            CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x=(1+(1+3))*2-1;", null, false).Ast);

            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(9, context.GetItem("x", true));
            Assert.Equal(9, context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("x=(3+2)-1;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal(4, context.GetItem("x", true));
            Assert.Equal(4, context.Result);

            domTree = AstDomCompiler.Compile(Script.Compile("x=(String)-1;", null, false).Ast);
            CodeDomCompiler.Compile(domTree).Execute(context);

            Assert.Equal("-1", context.GetItem("x", true));
        }
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var syntax = (ScriptSwitchRootStatement)syntaxNode;

      var condition = AstDomCompiler.Compile<CodeExpression>(syntax.Expression, prog);

      var cases = new List<CodeSwitchCase> ();
      foreach (var statement in syntax.Switch.Cases)
      {
        cases.Add(new CodeSwitchCase(
          AstDomCompiler.Compile<CodeExpression>(statement.Condition, prog),
          AstDomCompiler.Compile<CodeStatement>(statement.Statement, prog)));
      }
      
      if (syntax.Switch.DefaultCase != null)
      {
        cases.Add(new CodeSwitchCase(
            null,
            AstDomCompiler.Compile<CodeStatement>(syntax.Switch.DefaultCase.Statement, prog)));
      }

      var code = new CodeSwitchStatement(condition, cases);
      return code;
    }
Ejemplo n.º 43
0
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var syntaxConstExpr = (ScriptConstExpr)syntaxNode;

      return new CodeValueReference(syntaxConstExpr.Value) { SourceSpan = syntaxNode.Span };
    }