// 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 == 12); } [Test] public void TestProtoASTExecute_Imperative_Assign01() { // // a = [Imperative] // { // return = 10; // } // List<ProtoCore.AST.ImperativeAST.ImperativeNode> imperativeList = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>(); // return = 10 ProtoCore.AST.ImperativeAST.BinaryExpressionNode imperativeAssign = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("return"), new ProtoCore.AST.ImperativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); imperativeList.Add(imperativeAssign); // 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>(); 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>();
void Imperative_languageblock(out ProtoCore.AST.ImperativeAST.ImperativeNode node) { node = null; ProtoCore.AST.ImperativeAST.LanguageBlockNode langblock = new ProtoCore.AST.ImperativeAST.LanguageBlockNode(); Expect(10); NodeUtils.SetNodeLocation(langblock, t); Expect(1); if( 0 == t.val.CompareTo(ProtoCore.DSASM.kw.imperative)) { langblock.codeblock.Language = ProtoCore.Language.Imperative; } else if( 0 == t.val.CompareTo(ProtoCore.DSASM.kw.associative)) { langblock.codeblock.Language = ProtoCore.Language.Associative; } else { langblock.codeblock.Language = ProtoCore.Language.NotSpecified; errors.SemErr(t.line, t.col, String.Format(Resources.InvalidLanguageBlockIdentifier, t.val)); } Expect(11); Expect(45); Node codeBlockNode = null; if (langblock.codeblock.Language == ProtoCore.Language.Associative || langblock.codeblock.Language == ProtoCore.Language.NotSpecified) { Hydrogen(out codeBlockNode); } else if (langblock.codeblock.Language == ProtoCore.Language.Imperative ) { Imperative(out codeBlockNode); } else SynErr(104); if (langblock.codeblock.Language == ProtoCore.Language.NotSpecified) { int openCurlyBraceCount = 0, closeCurlyBraceCount = 0; ProtoCore.AST.ImperativeAST.CodeBlockNode codeBlockInvalid = new ProtoCore.AST.ImperativeAST.CodeBlockNode(); ProtoCore.AST.ImperativeAST.ImperativeNode validBlockInInvalid = null; while (closeCurlyBraceCount <= openCurlyBraceCount) { if (la.kind == 10) { Imperative_languageblock(out validBlockInInvalid); codeBlockInvalid.Body.Add(validBlockInInvalid); } else if (la.kind == 45) { Get(); openCurlyBraceCount++; } else if (la.kind == 46) { Get(); closeCurlyBraceCount++; } else if (la.kind == 0) { Get(); Expect(46); break; } else if (StartOf(13)) { Get(); } else SynErr(105); } codeBlockNode = codeBlockInvalid; } else if (la.kind == 46) { Get(); } else SynErr(106); langblock.CodeBlockNode = codeBlockNode; node = langblock; }
void Imperative(out Node codeBlockNode) { ProtoCore.AST.ImperativeAST.ImperativeNode node = null; ProtoCore.AST.ImperativeAST.CodeBlockNode codeblock = new ProtoCore.AST.ImperativeAST.CodeBlockNode(); NodeUtils.SetNodeStartLocation(codeblock, t); while (StartOf(15)) { if (IsNotAttributeFunction()) { Imperative_stmt(out node); } else { List<ProtoCore.AST.ImperativeAST.ImperativeNode> attrs = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>(); if (la.kind == 10) { Imperative_AttributeDeclaration(out attrs); } Imperative_functiondecl(out node, attrs); } if (null != node) codeblock.Body.Add(node); } codeBlockNode = codeblock; // We look ahead (la) here instead of looking at the current token (t) // because when we get here at the end of a language block, "t" would // have been pointing to the ending token of the last statement in the // language block. What we really need here is the closing bracket '}' // character, and that's conveniently residing in the look ahead token. // NodeUtils.SetNodeEndLocation(codeblock, la); }
void Imperative_languageblock(out ProtoCore.AST.ImperativeAST.ImperativeNode node) { node = null; ProtoCore.AST.ImperativeAST.LanguageBlockNode langblock = new ProtoCore.AST.ImperativeAST.LanguageBlockNode(); Expect(8); NodeUtils.SetNodeLocation(langblock, t); Expect(1); if( 0 == t.val.CompareTo(ProtoCore.DSASM.kw.imperative)) { langblock.codeblock.language = ProtoCore.Language.kImperative; } else if( 0 == t.val.CompareTo(ProtoCore.DSASM.kw.associative)) { langblock.codeblock.language = ProtoCore.Language.kAssociative; } else { langblock.codeblock.language = ProtoCore.Language.kInvalid; errors.SemErr(t.line, t.col, String.Format(Resources.InvalidLanguageBlockIdentifier, t.val)); } while (WeakSeparator(48,5,6) ) { if (IsLanguageBlockProperty()) { Expect(1); string key = t.val; Expect(49); Expect(4); if ("fingerprint" == key) { langblock.codeblock.fingerprint = t.val; langblock.codeblock.fingerprint = langblock.codeblock.fingerprint.Remove(0,1); langblock.codeblock.fingerprint = langblock.codeblock.fingerprint.Remove(langblock.codeblock.fingerprint.Length-1,1); } else if ("version" == key) { langblock.codeblock.version = t.val; langblock.codeblock.version = langblock.codeblock.version.Remove(0,1); langblock.codeblock.version = langblock.codeblock.version.Remove(langblock.codeblock.version.Length-1,1); } } else if (la.kind == 1) { ProtoCore.AST.ImperativeAST.ImperativeNode attr = null; Imperative_Attribute(out attr); if (attr != null) langblock.Attributes.Add(attr); } else SynErr(106); } Expect(9); Expect(46); Node codeBlockNode = null; if (langblock.codeblock.language == ProtoCore.Language.kAssociative || langblock.codeblock.language == ProtoCore.Language.kInvalid) { Hydrogen(out codeBlockNode); } else if (langblock.codeblock.language == ProtoCore.Language.kImperative ) { Imperative(out codeBlockNode); } else SynErr(107); if (langblock.codeblock.language == ProtoCore.Language.kInvalid ) { int openCurlyBraceCount = 0, closeCurlyBraceCount = 0; ProtoCore.AST.ImperativeAST.CodeBlockNode codeBlockInvalid = new ProtoCore.AST.ImperativeAST.CodeBlockNode(); ProtoCore.AST.ImperativeAST.ImperativeNode validBlockInInvalid = null; while (closeCurlyBraceCount <= openCurlyBraceCount) { if (la.kind == 8) { Imperative_languageblock(out validBlockInInvalid); codeBlockInvalid.Body.Add(validBlockInInvalid); } else if (la.kind == 46) { Get(); openCurlyBraceCount++; } else if (la.kind == 47) { Get(); closeCurlyBraceCount++; } else if (la.kind == 0) { Get(); Expect(47); break; } else if (StartOf(13)) { Get(); } else SynErr(108); } codeBlockNode = codeBlockInvalid; } else if (la.kind == 47) { Get(); } else SynErr(109); langblock.CodeBlockNode = codeBlockNode; node = langblock; }
public void TestcodegenDS_Imperative_Assign01() { // // a = [Imperative] // { // return = 10; // } // List<ProtoCore.AST.ImperativeAST.ImperativeNode> imperativeList = new List<ProtoCore.AST.ImperativeAST.ImperativeNode>(); // return = 10 ProtoCore.AST.ImperativeAST.BinaryExpressionNode imperativeAssign = new ProtoCore.AST.ImperativeAST.BinaryExpressionNode( new ProtoCore.AST.ImperativeAST.IdentifierNode("return"), new ProtoCore.AST.ImperativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); imperativeList.Add(imperativeAssign); // 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>(); astList.Add(assign); // Generate the script ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astList); string code = codegen.GenerateCode(); ExecutionMirror mirror = thisTest.RunScriptSource(code); thisTest.Verify("a", 10); }