List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
            astList.Add(assign);

            // Verify the results
            ExecutionMirror mirror = thisTest.RunASTSource(astList);
            Obj o = mirror.GetValue("a");
            Assert.IsTrue((Int64)o.Payload == 11);
        }

        [Test]
        public void TestProtoASTExecute_Imperative_IfStatement02()
        {
            //
            //  a = [Imperative]
            //  {
            //      b = 10;
            //      if (b > 10)
            //      {
            //          b = 11;
            //      }
            //      else
            //      {
            //          b = 12
            //      }
            //      return = b;
            //  }
            //

            List<ProtoCore.AST.ImperativeAST.ImperativeNode> imperativeList = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>();

            // b = 10
            ProtoCore.AST.ImperativeAST.BinaryExpressionNode imperativeAssign = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode(
                new ProtoCore.AST.ImperativeAST.IdentifierNode("b"),
                new ProtoCore.AST.ImperativeAST.IntNode(10),
                ProtoCore.DSASM.Operator.assign);
            imperativeList.Add(imperativeAssign);

            // if (b > 10)
            ProtoCore.AST.ImperativeAST.BinaryExpressionNode equality = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode(
                new ProtoCore.AST.ImperativeAST.IdentifierNode("b"),
                new ProtoCore.AST.ImperativeAST.IntNode(10),
                ProtoCore.DSASM.Operator.gt);

            ProtoCore.AST.ImperativeAST.IfStmtNode ifNode = new ProtoCore.AST.ImperativeAST.IfStmtNode();
            ifNode.IfExprNode = equality;

            // if body
            // b = 11
            ProtoCore.AST.ImperativeAST.BinaryExpressionNode ifCodeBlockStmt = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode(
                new ProtoCore.AST.ImperativeAST.IdentifierNode("b"),
                new ProtoCore.AST.ImperativeAST.IntNode(11),
                ProtoCore.DSASM.Operator.assign);
            List<ProtoCore.AST.ImperativeAST.ImperativeNode> ifCodeBlock = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>();
            ifCodeBlock.Add(ifCodeBlockStmt);
            ifNode.IfBody = ifCodeBlock;

            // else body
            // b = 12
            ProtoCore.AST.ImperativeAST.BinaryExpressionNode elseCodeBlockStmt = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode(
                new ProtoCore.AST.ImperativeAST.IdentifierNode("b"),
                new ProtoCore.AST.ImperativeAST.IntNode(12),
                ProtoCore.DSASM.Operator.assign);
            List<ProtoCore.AST.ImperativeAST.ImperativeNode> elseCodeBlock = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>();
            elseCodeBlock.Add(elseCodeBlockStmt);
            ifNode.ElseBody = elseCodeBlock;

            imperativeList.Add(ifNode);

            // return = b
            ProtoCore.AST.ImperativeAST.BinaryExpressionNode returnStmt = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode(
                new ProtoCore.AST.ImperativeAST.IdentifierNode("return"),
                new ProtoCore.AST.ImperativeAST.IdentifierNode("b"),
                ProtoCore.DSASM.Operator.assign);
            imperativeList.Add(returnStmt);


            // Build the language block
            ProtoCore.AST.ImperativeAST.CodeBlockNode imperativeCodeBlock = new ProtoCore.AST.ImperativeAST.CodeBlockNode();
            imperativeCodeBlock.Body = imperativeList;

            ProtoCore.AST.AssociativeAST.LanguageBlockNode langblock = new ProtoCore.AST.AssociativeAST.LanguageBlockNode();
            langblock.codeblock = new ProtoCore.LanguageCodeBlock(ProtoCore.Language.Imperative);
            langblock.CodeBlockNode = imperativeCodeBlock;


            // Build an assignment where the rhs is the imperative block
            ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
                new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
                langblock,
                ProtoCore.DSASM.Operator.assign);
            // Generate the script
            ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astList);
            string code = codegen.GenerateCode();


            ExecutionMirror mirror = thisTest.RunScriptSource(code);
            Assert.IsTrue((Int64)mirror.GetValue("a").Payload == 10);
        }

        [Test]
        public void TestCodegenDS_Imperative_IfStatement01()
        {
            //
            //  a = [Imperative]
            //  {
            //      b = 10;
            //      if (b == 10)
            //      {
            //          b = 11;
            //      }
            //      return = b;
            //  }
            //

            List<ProtoCore.AST.ImperativeAST.ImperativeNode> imperativeList = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>();

            // b = 10
            ProtoCore.AST.ImperativeAST.BinaryExpressionNode imperativeAssign = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode(
                new ProtoCore.AST.ImperativeAST.IdentifierNode("b"),
                new ProtoCore.AST.ImperativeAST.IntNode(10),
                ProtoCore.DSASM.Operator.assign);
            imperativeList.Add(imperativeAssign);

            // if (b == 10)
            ProtoCore.AST.ImperativeAST.BinaryExpressionNode equality = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode(
                new ProtoCore.AST.ImperativeAST.IdentifierNode("b"),
                new ProtoCore.AST.ImperativeAST.IntNode(10),
                ProtoCore.DSASM.Operator.eq);

            ProtoCore.AST.ImperativeAST.IfStmtNode ifNode = new ProtoCore.AST.ImperativeAST.IfStmtNode();
            ifNode.IfExprNode = equality;

            // b = 11
            ProtoCore.AST.ImperativeAST.BinaryExpressionNode ifCodeBlockStmt = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode(
                new ProtoCore.AST.ImperativeAST.IdentifierNode("b"),
                new ProtoCore.AST.ImperativeAST.IntNode(11),
                ProtoCore.DSASM.Operator.assign);
            List<ProtoCore.AST.ImperativeAST.ImperativeNode> ifCodeBlock = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>();
            ifCodeBlock.Add(ifCodeBlockStmt);
            ifNode.IfBody = ifCodeBlock;

            imperativeList.Add(ifNode);

            // return = b
            ProtoCore.AST.ImperativeAST.BinaryExpressionNode returnStmt = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode(
                new ProtoCore.AST.ImperativeAST.IdentifierNode("return"),
                new ProtoCore.AST.ImperativeAST.IdentifierNode("b"),
                ProtoCore.DSASM.Operator.assign);
            imperativeList.Add(returnStmt);


            // Build the language block
            ProtoCore.AST.ImperativeAST.CodeBlockNode imperativeCodeBlock = new ProtoCore.AST.ImperativeAST.CodeBlockNode();
            imperativeCodeBlock.Body = imperativeList;

            ProtoCore.AST.AssociativeAST.LanguageBlockNode langblock = new ProtoCore.AST.AssociativeAST.LanguageBlockNode();
            langblock.codeblock = new ProtoCore.LanguageCodeBlock(ProtoCore.Language.Imperative);
            langblock.CodeBlockNode = imperativeCodeBlock;


            // Build an assignment where the rhs is the imperative block
            ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
                new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
                langblock,
                ProtoCore.DSASM.Operator.assign);


            List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
Beispiel #3
0
        public override IEnumerable<AssociativeNode> BuildOutputAstInScope(List<AssociativeNode> inputAstNodes, bool verboseLogging, AstBuilder builder)
        {
            // This function will compile IF node to the following format:
            //
            //     cond = ...;
            //     v = [Imperative]
            //     {
            //         if (cond) {
            //             return = [Associative] {
            //                 ...
            //             }
            //         }
            //         else {
            //             return = [Associative] {
            //                 ...
            //             }
            //         }
            //     }
            //

            var astsInTrueBranch = GetAstsForBranch(1, inputAstNodes, verboseLogging, builder);
            var astsInFalseBranch = GetAstsForBranch(2, inputAstNodes, verboseLogging, builder);

            // if (cond) {
            //     return = [Associative] {...}
            // }
            var ifBlock = new LanguageBlockNode
            {
                codeblock = new LanguageCodeBlock(Language.kAssociative),
                CodeBlockNode = new CodeBlockNode { Body = astsInTrueBranch }
            };
            var ifBranch = AstFactory.BuildReturnStatement(ifBlock).ToImperativeAST();

            // else {
            //     return = [Associative] { ... }
            // }
            var elseBlock = new LanguageBlockNode
            {
                codeblock = new LanguageCodeBlock(Language.kAssociative),
                CodeBlockNode = new CodeBlockNode { Body = astsInFalseBranch }
            };
            var elseBranch = AstFactory.BuildReturnStatement(elseBlock).ToImperativeAST();

            var ifelseStatement = new ProtoCore.AST.ImperativeAST.IfStmtNode()
            {
                IfExprNode = inputAstNodes[0].ToImperativeAST(),
                IfBody = new List<ProtoCore.AST.ImperativeAST.ImperativeNode> { ifBranch },
                ElseBody = new List<ProtoCore.AST.ImperativeAST.ImperativeNode> { elseBranch }
            };

            // thisVariable = [Imperative]
            // {
            //     ...
            // }
            var outerBlock = new LanguageBlockNode
            {
                codeblock = new LanguageCodeBlock(Language.kImperative),
                CodeBlockNode = new ProtoCore.AST.ImperativeAST.CodeBlockNode
                {
                    Body = new List<ProtoCore.AST.ImperativeAST.ImperativeNode> { ifelseStatement }
                }
            };

            var thisVariable = GetAstIdentifierForOutputIndex(0);
            var assignment = AstFactory.BuildAssignment(thisVariable, outerBlock);

            return new AssociativeNode[] 
            {
                assignment
            };
        }
Beispiel #4
0
        public override IEnumerable <AssociativeNode> BuildOutputAstInScope(List <AssociativeNode> inputAstNodes, bool verboseLogging, AstBuilder builder)
        {
            // This function will compile IF node to the following format:
            //
            //     cond = ...;
            //     v = [Imperative]
            //     {
            //         if (cond) {
            //             return = [Associative] {
            //                 ...
            //             }
            //         }
            //         else {
            //             return = [Associative] {
            //                 ...
            //             }
            //         }
            //     }
            //

            var astsInTrueBranch  = GetAstsForBranch(1, inputAstNodes, verboseLogging, builder);
            var astsInFalseBranch = GetAstsForBranch(2, inputAstNodes, verboseLogging, builder);

            // if (cond) {
            //     return = [Associative] {...}
            // }
            var ifBlock = new LanguageBlockNode
            {
                codeblock     = new LanguageCodeBlock(Language.kAssociative),
                CodeBlockNode = new CodeBlockNode {
                    Body = astsInTrueBranch
                }
            };
            var ifBranch = AstFactory.BuildReturnStatement(ifBlock).ToImperativeAST();

            // else {
            //     return = [Associative] { ... }
            // }
            var elseBlock = new LanguageBlockNode
            {
                codeblock     = new LanguageCodeBlock(Language.kAssociative),
                CodeBlockNode = new CodeBlockNode {
                    Body = astsInFalseBranch
                }
            };
            var elseBranch = AstFactory.BuildReturnStatement(elseBlock).ToImperativeAST();

            var ifelseStatement = new ProtoCore.AST.ImperativeAST.IfStmtNode()
            {
                IfExprNode = inputAstNodes[0].ToImperativeAST(),
                IfBody     = new List <ProtoCore.AST.ImperativeAST.ImperativeNode> {
                    ifBranch
                },
                ElseBody = new List <ProtoCore.AST.ImperativeAST.ImperativeNode> {
                    elseBranch
                }
            };

            // thisVariable = [Imperative]
            // {
            //     ...
            // }
            var outerBlock = new LanguageBlockNode
            {
                codeblock     = new LanguageCodeBlock(Language.kImperative),
                CodeBlockNode = new ProtoCore.AST.ImperativeAST.CodeBlockNode
                {
                    Body = new List <ProtoCore.AST.ImperativeAST.ImperativeNode> {
                        ifelseStatement
                    }
                }
            };

            var thisVariable = GetAstIdentifierForOutputIndex(0);
            var assignment   = AstFactory.BuildAssignment(thisVariable, outerBlock);

            return(new AssociativeNode[]
            {
                assignment
            });
        }
Beispiel #5
0
	void Imperative_ifstmt(out ProtoCore.AST.ImperativeAST.ImperativeNode node) {
		ProtoCore.AST.ImperativeAST.IfStmtNode ifStmtNode = new ProtoCore.AST.ImperativeAST.IfStmtNode(); 
		List<ProtoCore.AST.ImperativeAST.ImperativeNode> body = null; 
		Expect(29);
		NodeUtils.SetNodeLocation(ifStmtNode, t); 
		Expect(12);
		Imperative_expr(out node);
		ifStmtNode.IfExprNode = node; 
		Expect(13);
		NodeUtils.SetNodeStartLocation(ifStmtNode.IfExprNode, ifStmtNode);
		NodeUtils.SetNodeEndLocation(ifStmtNode.IfExprNode, t);
		NodeUtils.SetNodeStartLocation(ifStmtNode.IfBodyPosition, la);
		
		if (la.kind == 45) {
			Get();
			Imperative_stmtlist(out body);
			ifStmtNode.IfBody = body; 
			Expect(46);
		} else if (StartOf(21)) {
			ProtoCore.AST.ImperativeAST.ImperativeNode singleStmt; 
			Imperative_stmt(out singleStmt);
			ifStmtNode.IfBody.Add(singleStmt); 
		} else SynErr(107);
		NodeUtils.SetNodeEndLocation(ifStmtNode.IfBodyPosition, t); 
		while (la.kind == 30) {
			ProtoCore.AST.ImperativeAST.ElseIfBlock elseifBlock = new ProtoCore.AST.ImperativeAST.ElseIfBlock(); 
			Get();
			int line = t.line; int col = t.col; 
			Expect(12);
			Imperative_expr(out node);
			Expect(13);
			elseifBlock.Expr = node;
			elseifBlock.Expr.line = line;
			elseifBlock.Expr.col = col;
			NodeUtils.SetNodeEndLocation(elseifBlock.Expr, t);
			NodeUtils.SetNodeStartLocation(elseifBlock.ElseIfBodyPosition, la);
			
			if (la.kind == 45) {
				Get();
				Imperative_stmtlist(out body);
				elseifBlock.Body = body; 
				Expect(46);
			} else if (StartOf(21)) {
				ProtoCore.AST.ImperativeAST.ImperativeNode singleStmt = null; 
				Imperative_stmt(out singleStmt);
				elseifBlock.Body.Add(singleStmt); 
			} else SynErr(108);
			NodeUtils.SetNodeEndLocation(elseifBlock.ElseIfBodyPosition, t); 
			ifStmtNode.ElseIfList.Add(elseifBlock); 
		}
		if (la.kind == 31) {
			Get();
			NodeUtils.SetNodeStartLocation(ifStmtNode.ElseBodyPosition, la); 
			if (la.kind == 45) {
				Get();
				Imperative_stmtlist(out body);
				ifStmtNode.ElseBody = body; 
				Expect(46);
			} else if (StartOf(21)) {
				ProtoCore.AST.ImperativeAST.ImperativeNode singleStmt = null; 
				Imperative_stmt(out singleStmt);
				ifStmtNode.ElseBody.Add(singleStmt); 
			} else SynErr(109);
			NodeUtils.SetNodeEndLocation(ifStmtNode.ElseBodyPosition, t); 
		}
		node = ifStmtNode; 
	}
Beispiel #6
0
	void Imperative_forloop(out ProtoCore.AST.ImperativeAST.ImperativeNode forloop) {
		ProtoCore.AST.ImperativeAST.IfStmtNode dummyIfNode = new ProtoCore.AST.ImperativeAST.IfStmtNode();
		ProtoCore.AST.ImperativeAST.ImperativeNode node;
		ProtoCore.AST.ImperativeAST.ForLoopNode loopNode = new ProtoCore.AST.ImperativeAST.ForLoopNode();
		List<ProtoCore.AST.ImperativeAST.ImperativeNode> body = null;   
		
		Expect(34);
		NodeUtils.SetNodeLocation(loopNode, t); loopNode.KwForLine = t.line; loopNode.KwForCol = t.col; 
		Expect(10);
		int idLine = la.line; int idCol = la.col; 
		Imperative_Ident(out node);
		loopNode.loopVar = node; loopNode.loopVar.line = idLine; loopNode.loopVar.col = idCol; 
		Expect(68);
		loopNode.KwInLine = t.line; loopNode.KwInCol = t.col; int exprLine = la.line; int exprCol = la.col; 
		Imperative_expr(out node);
		loopNode.expression = node; if (loopNode.expression != null) {  loopNode.expression.line = exprLine; loopNode.expression.col = exprCol; } 
		Expect(11);
		if (la.kind == 46) {
			Get();
			Imperative_stmtlist(out body);
			loopNode.body = body; 
			Expect(47);
			NodeUtils.SetNodeEndLocation(loopNode, t); 
		} else if (StartOf(22)) {
			ProtoCore.AST.ImperativeAST.ImperativeNode singleStmt = null; 
			Imperative_stmt(out singleStmt);
			loopNode.body.Add(singleStmt); 
		} else SynErr(116);
		dummyIfNode.IfExprNode
		= new ProtoCore.AST.ImperativeAST.BooleanNode(true);
		dummyIfNode.IfBody.Add(loopNode);
		dummyIfNode.line = loopNode.line;
		dummyIfNode.col = loopNode.col;
		dummyIfNode.endLine = loopNode.endLine;
		dummyIfNode.endCol = loopNode.endCol;
		forloop = dummyIfNode;
		
	}