本文整理汇总了C#中ProtoCore.AST.AssociativeAST.BinaryExpressionNode类的典型用法代码示例。如果您正苦于以下问题:C# ProtoCore.AST.AssociativeAST.BinaryExpressionNode类的具体用法?C# ProtoCore.AST.AssociativeAST.BinaryExpressionNode怎么用?C# ProtoCore.AST.AssociativeAST.BinaryExpressionNode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ProtoCore.AST.AssociativeAST.BinaryExpressionNode类属于命名空间,在下文中一共展示了ProtoCore.AST.AssociativeAST.BinaryExpressionNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestRoundTrip_Assign02
public void TestRoundTrip_Assign02()
{
//=================================
// 1. Build AST
// 2. Execute AST and verify
// 3. Convert AST to source
// 4. Execute source and verify
//=================================
int result1 = 30;
ExecutionMirror mirror = null;
// 1. Build the AST tree
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IntNode(10),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.add),
ProtoCore.DSASM.Operator.assign);
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
astList.Add(assign);
// 2. Execute AST and verify
mirror = thisTest.RunASTSource(astList);
Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1);
// 3. Convert AST to source
ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList);
string code = codegenDS.GenerateCode();
// 4. Execute source and verify
mirror = thisTest.RunScriptSource(code);
Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1);
}
示例2: TestProtoASTExecute_Assign01
public void TestProtoASTExecute_Assign01()
{
// Build the AST trees
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.IntNode(10),
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);
thisTest.Verify("a", 10);
}
示例3: TestProtoASTExecute_Assign03
public void TestProtoASTExecute_Assign03()
{
GraphToDSCompiler.GraphCompiler gc = GraphToDSCompiler.GraphCompiler.CreateInstance();
/*b = 20;*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.assign);
/*a = (b + 50)*(b + 20)*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(50),
ProtoCore.DSASM.Operator.add),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.add),
ProtoCore.DSASM.Operator.mul),
ProtoCore.DSASM.Operator.assign);
/*c = a - 200*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign3 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("c"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.IntNode(200),
ProtoCore.DSASM.Operator.sub),
ProtoCore.DSASM.Operator.assign);
/*d = b*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign4 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("d"),
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
ProtoCore.DSASM.Operator.assign);
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
astList.Add(assign2);
astList.Add(assign1);
astList.Add(assign3);
astList.Add(assign4);
ExecutionMirror mirror = thisTest.RunASTSource(astList);
//a = 2800, c = 2600, d = b = 20
Obj o = mirror.GetValue("a");
Assert.IsTrue((Int64)o.Payload == 2800);
o = mirror.GetValue("c");
Assert.IsTrue((Int64)o.Payload == 2600);
Obj p = mirror.GetValue("b");
Assert.IsTrue((Int64)p.Payload == 20);
o = mirror.GetValue("d");
Assert.IsTrue((Int64)o.Payload == 20);
}
示例4: TestProtoASTExecute_Assign03
public void TestProtoASTExecute_Assign03()
{
/*b = 20;*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.assign);
/*a = (b + 50)*(b + 20)*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(50),
ProtoCore.DSASM.Operator.add),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.add),
ProtoCore.DSASM.Operator.mul),
ProtoCore.DSASM.Operator.assign);
/*c = a - 200*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign3 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("c"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.IntNode(200),
ProtoCore.DSASM.Operator.sub),
ProtoCore.DSASM.Operator.assign);
/*d = b*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign4 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("d"),
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
ProtoCore.DSASM.Operator.assign);
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
astList.Add(assign2);
astList.Add(assign1);
astList.Add(assign3);
astList.Add(assign4);
ExecutionMirror mirror = thisTest.RunASTSource(astList);
//a = 2800, c = 2600, d = b = 20
thisTest.Verify("a", 2800);
thisTest.Verify("c", 2600);
thisTest.Verify("b", 20);
thisTest.Verify("d", 20);
}
示例5: TestProtoASTExecute_Assign02
public void TestProtoASTExecute_Assign02()
{
// Build the AST tree
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IntNode(10),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.add),
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 == 30);
}
示例6: TestProtoASTExecute_Imperative_IfStatement02
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);
示例7: TestLocalizedStringInAST
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 == 12);
}
[Test]
public void TestLocalizedStringInAST()
{
// Build the AST tree
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
示例8: TestCodegenDS_Imperative_IfStatement01
// 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>();
示例9: TestProtoASTExecute_Imperative_Assign01
// 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);
示例10: TestProtoASTExecute_FunctionDefAndCall_03
public void TestProtoASTExecute_FunctionDefAndCall_03()
{
// def add(a : int, b : int)
// {
// return = a + b;
// }
//
// x = add(2,3);
ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
// Build the function body
ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnExpr = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
ProtoCore.DSASM.Operator.add);
ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return),
returnExpr,
ProtoCore.DSASM.Operator.assign);
cbn.Body.Add(returnNode);
// Build the function definition foo
const string functionName = "foo";
ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
funcDefNode.Name = functionName;
funcDefNode.FunctionBody = cbn;
// build the args signature
funcDefNode.Signature = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
// Build arg1
ProtoCore.AST.AssociativeAST.VarDeclNode arg1Decl = new ProtoCore.AST.AssociativeAST.VarDeclNode();
arg1Decl.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("a");
// Build the type of arg1
ProtoCore.Type arg1Type = new ProtoCore.Type();
arg1Type.Initialize();
arg1Type.UID = (int)ProtoCore.PrimitiveType.Integer;
arg1Type.Name = ProtoCore.DSDefinitions.Keyword.Int;
arg1Decl.ArgumentType = arg1Type;
funcDefNode.Signature.AddArgument(arg1Decl);
// Build arg2
ProtoCore.AST.AssociativeAST.VarDeclNode arg2Decl = new ProtoCore.AST.AssociativeAST.VarDeclNode();
arg2Decl.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("b");
// Build the type of arg2
ProtoCore.Type arg2Type = new ProtoCore.Type();
arg2Type.Initialize();
arg2Type.UID = (int)ProtoCore.PrimitiveType.Integer;
arg2Type.Name = ProtoCore.DSDefinitions.Keyword.Int;
arg2Decl.ArgumentType = arg2Type;
funcDefNode.Signature.AddArgument(arg2Decl);
// Function Return type
ProtoCore.Type returnType = new ProtoCore.Type();
returnType.Initialize();
returnType.UID = (int)ProtoCore.PrimitiveType.Var;
returnType.Name = ProtoCore.DSDefinitions.Keyword.Var;
funcDefNode.ReturnType = returnType;
// Build the statement that calls the function foo
ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode();
functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode(functionName);
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
astList.Add(funcDefNode);
// Function call
// Function args
List<ProtoCore.AST.AssociativeAST.AssociativeNode> args = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
args.Add(new ProtoCore.AST.AssociativeAST.IntNode(2));
args.Add(new ProtoCore.AST.AssociativeAST.IntNode(3));
functionCall.FormalArguments = args;
// Call the function
ProtoCore.AST.AssociativeAST.BinaryExpressionNode callstmt = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("x"),
functionCall,
ProtoCore.DSASM.Operator.assign);
astList.Add(callstmt);
ExecutionMirror mirror = thisTest.RunASTSource(astList);
Obj o = mirror.GetValue("x");
Assert.IsTrue((Int64)o.Payload == 5);
}
示例11: TestProtoASTExecute_ArrayIndex_RHS_Assign04
public void TestProtoASTExecute_ArrayIndex_RHS_Assign04()
{
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
// a = {1, 2, 3, 4};
int[] input = { 1, 2, 3, 4 };
ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeA = CreateDeclareArrayNode("a", input);
astList.Add(declareNodeA);
// b = 4;
ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(5),
ProtoCore.DSASM.Operator.assign);
astList.Add(declareNodeB);
// def foo(){
// return = -4;
// }
ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return),
new ProtoCore.AST.AssociativeAST.IntNode(-4),
ProtoCore.DSASM.Operator.assign);
cbn.Body.Add(returnNode);
// Build the function definition foo
const string functionName = "foo";
ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode() {
Name = functionName,
FunctionBody = cbn };
// Function Return Type
ProtoCore.Type returnType = new ProtoCore.Type();
returnType.Initialize();
returnType.UID = (int)ProtoCore.PrimitiveType.Var;
returnType.Name = ProtoCore.DSDefinitions.Keyword.Var;
funcDefNode.ReturnType = returnType;
astList.Add(funcDefNode);
// c = a[b + foo()];
ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode();
functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode(functionName);
ProtoCore.AST.AssociativeAST.BinaryExpressionNode operation1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
functionCall,
ProtoCore.DSASM.Operator.add);
ProtoCore.AST.AssociativeAST.IdentifierNode nodeALHS = new ProtoCore.AST.AssociativeAST.IdentifierNode("a");
nodeALHS.ArrayDimensions = new ProtoCore.AST.AssociativeAST.ArrayNode {
Expr = operation1 };
ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeALHSAssignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("c"),
nodeALHS,
ProtoCore.DSASM.Operator.assign);
astList.Add(nodeALHSAssignment);
// Verify the results
ExecutionMirror mirror = thisTest.RunASTSource(astList);
Obj o = mirror.GetValue("c");
Console.WriteLine(o.Payload);
// expected: c = 2
Assert.AreEqual(2, Convert.ToInt32(o.Payload));
}
示例12: TestRoundTrip_ClassDecl_PropertyAccess_01
public void TestRoundTrip_ClassDecl_PropertyAccess_01()
{
int result1 = 10;
ExecutionMirror mirror = null;
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
// Create an exact copy of the AST list to pass to the source conversion
// This needs to be done because the astlist to be run will be SSA'd on the AST execution run
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astListcopy= new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
// 1. Build AST
// class bar
// {
// f : var;
// }
//
// p = bar.bar();
// p.f = 10;
// a = p.f;
// Create the class node AST
ProtoCore.AST.AssociativeAST.ClassDeclNode classDefNode = new ProtoCore.AST.AssociativeAST.ClassDeclNode();
classDefNode.ClassName = "bar";
// Create the property AST
ProtoCore.AST.AssociativeAST.VarDeclNode varDeclNode = new ProtoCore.AST.AssociativeAST.VarDeclNode();
varDeclNode.Name = "f";
varDeclNode.NameNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f");
varDeclNode.ArgumentType = new ProtoCore.Type()
{
Name = "int",
rank = 0,
UID = (int)ProtoCore.PrimitiveType.Integer
};
classDefNode.Variables.Add(varDeclNode);
astList.Add(classDefNode);
astListcopy.Add(new ProtoCore.AST.AssociativeAST.ClassDeclNode(classDefNode));
// p = bar.bar();
ProtoCore.AST.AssociativeAST.FunctionCallNode constructorCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode();
constructorCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar");
ProtoCore.AST.AssociativeAST.IdentifierListNode identListConstrcctorCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode();
identListConstrcctorCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("bar");
identListConstrcctorCall.RightNode = constructorCall;
ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtInitClass = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("p"),
identListConstrcctorCall,
ProtoCore.DSASM.Operator.assign);
astList.Add(stmtInitClass);
astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtInitClass));
// p.f = 10;
ProtoCore.AST.AssociativeAST.IdentifierListNode identListPropertySet = new ProtoCore.AST.AssociativeAST.IdentifierListNode();
identListPropertySet.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p");
identListPropertySet.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f");
ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertySet = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
identListPropertySet,
new ProtoCore.AST.AssociativeAST.IntNode(10),
ProtoCore.DSASM.Operator.assign);
astList.Add(stmtPropertySet);
astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtPropertySet));
// a = p.f;
ProtoCore.AST.AssociativeAST.IdentifierListNode identListPropertyAccess = new ProtoCore.AST.AssociativeAST.IdentifierListNode();
identListPropertyAccess.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p");
identListPropertyAccess.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("f");
ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertyAccess = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
identListPropertyAccess,
ProtoCore.DSASM.Operator.assign);
astList.Add(stmtPropertyAccess);
astListcopy.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(stmtPropertyAccess));
// 2. Execute AST and verify
mirror = thisTest.RunASTSource(astList);
Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1);
// 3. Convert AST to source
ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astListcopy);
string code = codegenDS.GenerateCode();
// 4. Execute source and verify
mirror = thisTest.RunScriptSource(code);
Assert.IsTrue((Int64)mirror.GetValue("a").Payload == result1);
//.........这里部分代码省略.........
示例13: TestCodeGenDS_Assign02
public void TestCodeGenDS_Assign02()
{
// Build the AST tree
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IntNode(10),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.add),
ProtoCore.DSASM.Operator.assign);
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
astList.Add(assign);
// emit the DS code from the AST tree
ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList);
string code = codegenDS.GenerateCode();
// Verify the results
ExecutionMirror mirror = thisTest.RunScriptSource(code);
Obj o = mirror.GetValue("a");
Assert.IsTrue((Int64)o.Payload == 30);
}
示例14: TestProtoASTExecute_ArrayIndex_LHS_Assign05
public void TestProtoASTExecute_ArrayIndex_LHS_Assign05()
{
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
// a = { { 0, 1 }, { 3, 4, 5 } };
int[] input1 = { 0, 1 };
int[] input2 = { 3, 4, 5 };
List<ProtoCore.AST.AssociativeAST.AssociativeNode> arrayList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
arrayList.Add(CreateExprListNodeFromArray(input1));
arrayList.Add(CreateExprListNodeFromArray(input2));
ProtoCore.AST.AssociativeAST.ExprListNode arrayExpr = new ProtoCore.AST.AssociativeAST.ExprListNode { Exprs = arrayList };
ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeA = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
arrayExpr,
ProtoCore.DSASM.Operator.assign);
astList.Add(declareNodeA);
// b = 2;
ProtoCore.AST.AssociativeAST.BinaryExpressionNode declareNodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(2),
ProtoCore.DSASM.Operator.assign);
astList.Add(declareNodeB);
// a[0][b] = b;
ProtoCore.AST.AssociativeAST.IdentifierNode nodeALHS = new ProtoCore.AST.AssociativeAST.IdentifierNode("a");
nodeALHS.ArrayDimensions = new ProtoCore.AST.AssociativeAST.ArrayNode
{
Expr = new ProtoCore.AST.AssociativeAST.IntNode(0),
Type = new ProtoCore.AST.AssociativeAST.ArrayNode
{
Expr = new ProtoCore.AST.AssociativeAST.IdentifierNode("b")
}
};
ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeALHSAssignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
nodeALHS,
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
ProtoCore.DSASM.Operator.assign);
astList.Add(nodeALHSAssignment);
// Verify the results
ExecutionMirror mirror = thisTest.RunASTSource(astList);
Obj o = mirror.GetValue("a");
Console.WriteLine(o.Payload);
int[] output1 = { 0, 1, 2 };
int[] output2 = { 3, 4, 5 };
// Result should be = { { 0, 1, 2 }, { 3, 4, 5 } };
ProtoCore.DSASM.Mirror.DsasmArray result = o.Payload as ProtoCore.DSASM.Mirror.DsasmArray;
Assert.IsNotNull( result );
// First row of array = { 0, 1, 2 }
ProtoCore.DSASM.Mirror.DsasmArray array1 = result.members[0].Payload as ProtoCore.DSASM.Mirror.DsasmArray;
Assert.IsNotNull( array1 );
for (int i = 0; i < output1.Length; i++)
Assert.AreEqual(output1[i], Convert.ToInt32(array1.members[i].Payload));
// Second row of array = { 3, 4, 5 }
ProtoCore.DSASM.Mirror.DsasmArray array2 = (ProtoCore.DSASM.Mirror.DsasmArray)result.members[1].Payload;
Assert.IsNotNull( array2 );
for (int i = 0; i < output2.Length; i++)
Assert.AreEqual(output2[i], Convert.ToInt32(array2.members[i].Payload));
}
示例15: TestRoundTrip_FunctionDefAndCall_01
public void TestRoundTrip_FunctionDefAndCall_01()
{
//=================================
// 1. Build AST
// 2. Execute AST and verify
// 3. Convert AST to source
// 4. Execute source and verify
//=================================
int result1 = 20;
ExecutionMirror mirror = null;
// 1. Build the AST tree
// def foo()
// {
// b = 10;
// return = b + 10;
// }
//
// x = foo();
ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
// Build the function body
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(10),
ProtoCore.DSASM.Operator.assign);
ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnExpr = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(10),
ProtoCore.DSASM.Operator.add);
ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode(ProtoCore.DSDefinitions.Keyword.Return),
returnExpr,
ProtoCore.DSASM.Operator.assign);
cbn.Body.Add(assignment1);
cbn.Body.Add(returnNode);
// Build the function definition foo
const string functionName = "foo";
ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode();
funcDefNode.Name = functionName;
funcDefNode.FunctionBody = cbn;
// Function Return type
ProtoCore.Type returnType = new ProtoCore.Type();
returnType.Initialize();
returnType.UID = (int)ProtoCore.PrimitiveType.Var;
returnType.Name = ProtoCore.DSDefinitions.Keyword.Var;
funcDefNode.ReturnType = returnType;
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
astList.Add(funcDefNode);
// Build the statement that calls the function foo
ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode();
functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode(functionName);
ProtoCore.AST.AssociativeAST.BinaryExpressionNode callstmt = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("x"),
functionCall,
ProtoCore.DSASM.Operator.assign);
astList.Add(callstmt);
// 2. Execute AST and verify
mirror = thisTest.RunASTSource(astList);
Assert.IsTrue((Int64)mirror.GetValue("x").Payload == result1);
// 3. Convert AST to source
ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList);
string code = codegenDS.GenerateCode();
Console.WriteLine(code);
// 4. Execute source and verify
mirror = thisTest.RunScriptSource(code);
Assert.IsTrue((Int64)mirror.GetValue("x").Payload == result1);
}