本文整理汇总了C#中ProtoCore.CodeGenDS类的典型用法代码示例。如果您正苦于以下问题:C# ProtoCore.CodeGenDS类的具体用法?C# ProtoCore.CodeGenDS怎么用?C# ProtoCore.CodeGenDS使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ProtoCore.CodeGenDS类属于命名空间,在下文中一共展示了ProtoCore.CodeGenDS类的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: Execute
public override void Execute()
{
NodesToCodeCompletedEventArgs args = null;
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
if (subtrees != null)
{
foreach (var tree in subtrees)
{
Validity.Assert(tree.AstNodes != null && tree.AstNodes.Count > 0);
astList.AddRange(tree.AstNodes);
}
}
lock (runner.operationsMutex)
{
try
{
string code = new ProtoCore.CodeGenDS(astList).GenerateCode();
args = new NodesToCodeCompletedEventArgs(code, EventStatus.OK, "Node to code task complete.");
}
catch (Exception exception)
{
args = new NodesToCodeCompletedEventArgs(string.Empty, EventStatus.Error, exception.Message);
}
}
// Notify the listener
if (null != runner.NodesToCodeCompleted)
{
runner.NodesToCodeCompleted(this, args);
}
}
示例3: BuildGraphDAG
public string BuildGraphDAG()
{
bool removeFromRemovedNodes = true;
this.RemoveNodes(removeFromRemovedNodes);
this.AddNodesToAST();
this.MakeConnectionsForAddedNodes();
this.UpdateModifiedNodes();
string code = null;
#if __TRANSFORM_TO_PROTOAST
GraphTransform transform = new GraphTransform();
List<ProtoCore.AST.AssociativeAST.AssociativeNode> graphIR = transform.ToGraphIR(this.Graph, this.gc);
ProtoCore.CodeGenDS codeGen = new ProtoCore.CodeGenDS(graphIR);
code = codeGen.GenerateCode();
gc.UpdateAddedNodesInModifiedNameList();
#else
code = this.gc.PrintGraph();
#endif
gc.UpdateDirtyFlags(nodesToModify);
return code;
}
示例4: ConvertNodesToCode
public static string ConvertNodesToCode(DynamoModel dynamoModel, IEnumerable<NodeModel> nodeList)
{
var astBuilder = new AstBuilder(dynamoModel, null);
var astNodes = astBuilder.CompileToAstNodes(nodeList, false);
var codeGen = new ProtoCore.CodeGenDS(astNodes);
return codeGen.GenerateCode();
}
示例5: Emit
/// <summary>
/// Emits the DS code given the list of ast nodes
/// </summary>
/// <param name="astList"></param>
/// <returns></returns>
public string Emit(List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList)
{
// Emit the DS code from the AST tree using ProtoCore code generator
ProtoCore.CodeGenDS codegenDS = new ProtoCore.CodeGenDS(astList);
string code = codegenDS.GenerateCode();
UpdateAddedNodesInModifiedNameList();
return code;
}
示例6: TestRoundTrip_ClassDecl_MemFunctionCall_01
//.........这里部分代码省略.........
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;
// Create the class node AST
ProtoCore.AST.AssociativeAST.ClassDeclNode classDefNode = new ProtoCore.AST.AssociativeAST.ClassDeclNode();
classDefNode.ClassName = "bar";
// Add the member function 'foo'
classDefNode.Procedures.Add(funcDefNode);
// 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);
// Add the constructed class AST
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));
// a = p.f;
ProtoCore.AST.AssociativeAST.FunctionCallNode functionCall = new ProtoCore.AST.AssociativeAST.FunctionCallNode();
functionCall.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("foo");
ProtoCore.AST.AssociativeAST.IdentifierListNode identListFunctionCall = new ProtoCore.AST.AssociativeAST.IdentifierListNode();
identListFunctionCall.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p");
identListFunctionCall.RightNode = functionCall;
ProtoCore.AST.AssociativeAST.BinaryExpressionNode stmtPropertyAccess = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
identListFunctionCall,
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);
}
示例7: 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);
//.........这里部分代码省略.........
示例8: ConvertNodesToCodeInternal
internal void ConvertNodesToCodeInternal(EngineController engineController, INamingProvider namingProvider)
{
var selectedNodes = DynamoSelection.Instance
.Selection
.OfType<NodeModel>()
.Where(n => n.IsConvertible);
if (!selectedNodes.Any())
return;
var cliques = NodeToCodeCompiler.GetCliques(selectedNodes).Where(c => !(c.Count == 1 && c.First() is CodeBlockNodeModel));
var codeBlockNodes = new List<CodeBlockNodeModel>();
//UndoRedo Action Group----------------------------------------------
NodeToCodeUndoHelper undoHelper = new NodeToCodeUndoHelper();
// using (UndoRecorder.BeginActionGroup())
{
foreach (var nodeList in cliques)
{
//Create two dictionarys to store the details of the external connections that have to
//be recreated after the conversion
var externalInputConnections = new Dictionary<ConnectorModel, string>();
var externalOutputConnections = new Dictionary<ConnectorModel, string>();
//Also collect the average X and Y co-ordinates of the different nodes
int nodeCount = nodeList.Count;
var nodeToCodeResult = engineController.ConvertNodesToCode(this.nodes, nodeList, namingProvider);
#region Step I. Delete all nodes and their connections
double totalX = 0, totalY = 0;
foreach (var node in nodeList)
{
#region Step I.A. Delete the connections for the node
foreach (var connector in node.AllConnectors.ToList())
{
if (!IsInternalNodeToCodeConnection(nodeList, connector))
{
//If the connector is an external connector, the save its details
//for recreation later
var startNode = connector.Start.Owner;
int index = startNode.OutPorts.IndexOf(connector.Start);
//We use the varibleName as the connection between the port of the old Node
//to the port of the new node.
var variableName = startNode.GetAstIdentifierForOutputIndex(index).Value;
//Store the data in the corresponding dictionary
if (startNode == node)
{
if (nodeToCodeResult.OutputMap.ContainsKey(variableName))
variableName = nodeToCodeResult.OutputMap[variableName];
externalOutputConnections.Add(connector, variableName);
}
else
{
if (nodeToCodeResult.InputMap.ContainsKey(variableName))
variableName = nodeToCodeResult.InputMap[variableName];
externalInputConnections.Add(connector, variableName);
}
}
//Delete the connector
undoHelper.RecordDeletion(connector);
connector.Delete();
}
#endregion
#region Step I.B. Delete the node
totalX += node.X;
totalY += node.Y;
undoHelper.RecordDeletion(node);
RemoveNode(node);
#endregion
}
#endregion
#region Step II. Create the new code block node
var outputVariables = externalOutputConnections.Values;
var newResult = NodeToCodeCompiler.ConstantPropagationForTemp(nodeToCodeResult, outputVariables);
// Rewrite the AST using the shortest unique name in case of namespace conflicts
NodeToCodeCompiler.ReplaceWithShortestQualifiedName(
engineController.LibraryServices.LibraryManagementCore.ClassTable, newResult.AstNodes, ElementResolver);
var codegen = new ProtoCore.CodeGenDS(newResult.AstNodes);
var code = codegen.GenerateCode();
var codeBlockNode = new CodeBlockNodeModel(
code,
System.Guid.NewGuid(),
totalX / nodeCount,
totalY / nodeCount, engineController.LibraryServices, ElementResolver);
undoHelper.RecordCreation(codeBlockNode);
AddAndRegisterNode(codeBlockNode, false);
codeBlockNodes.Add(codeBlockNode);
#endregion
//.........这里部分代码省略.........
示例9: TestcodegenDS_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 == 20);
}
[Test]
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>();
示例10: TestCodeGenDS_Assign04
public void TestCodeGenDS_Assign04()
{
GraphToDSCompiler.GraphCompiler gc = GraphToDSCompiler.GraphCompiler.CreateInstance();
/*b = 30*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeB = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(30),
ProtoCore.DSASM.Operator.assign);
/*a = (b - 10) * 20 + (b + 10) * (b - 20) */
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment =
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.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(10),
ProtoCore.DSASM.Operator.sub),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.mul),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(10),
ProtoCore.DSASM.Operator.add),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("b"),
new ProtoCore.AST.AssociativeAST.IntNode(20),
ProtoCore.DSASM.Operator.sub),
ProtoCore.DSASM.Operator.mul),
ProtoCore.DSASM.Operator.add),
ProtoCore.DSASM.Operator.assign);
/*c = a*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeC = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("c"),
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
ProtoCore.DSASM.Operator.assign);
/*a = a + 1000*/
ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignment2 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(
new ProtoCore.AST.AssociativeAST.IdentifierNode("a"),
new ProtoCore.AST.AssociativeAST.IntNode(1000),
ProtoCore.DSASM.Operator.add),
ProtoCore.DSASM.Operator.assign);
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
astList.Add(nodeB);
astList.Add(assignment);
astList.Add(nodeC);
astList.Add(assignment2);
ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astList);
string code = codegen.GenerateCode();
ExecutionMirror mirror = thisTest.RunScriptSource(code);
//a = 1800, c = a = 1800
Obj o = mirror.GetValue("a");
Assert.IsTrue((Int64)o.Payload == 1800);
o = mirror.GetValue("c");
Assert.IsTrue((Int64)o.Payload == 1800);
}
示例11: 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);
}
示例12: ASTListToCode
/// <summary>
/// This converts a list of ProtoASTs into ds using CodeGenDS
/// </summary>
/// <param name="astList"></param>
/// <returns></returns>
public static string ASTListToCode(List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList)
{
string code = string.Empty;
if (null != astList && astList.Count > 0)
{
ProtoCore.CodeGenDS codeGen = new ProtoCore.CodeGenDS(astList);
code = codeGen.GenerateCode();
}
return code;
}
示例13: ParseCore
private static List<string> ParseCore(string expression, ref bool parseSuccess)
{
List<string> compiled = new List<string>();
ProtoCore.AST.AssociativeAST.CodeBlockNode commentNode = null;
ProtoCore.AST.Node codeBlockNode = Parse(expression, out commentNode);
parseSuccess = true;
List<ProtoCore.AST.Node> nodes = ParserUtils.GetAstNodes(codeBlockNode);
Validity.Assert(nodes != null);
int cNodeNum = 0;
if (nodes.Count == 0)
{
InsertCommentsInCode(null, null, commentNode, ref cNodeNum, ref compiled, expression);
return compiled;
}
foreach (var node in nodes)
{
ProtoCore.AST.AssociativeAST.AssociativeNode n = node as ProtoCore.AST.AssociativeAST.AssociativeNode;
ProtoCore.Utils.Validity.Assert(n != null);
if (n is ProtoCore.AST.AssociativeAST.ModifierStackNode)
{
core.BuildStatus.LogSemanticError("Modifier Blocks are not supported currently.");
}
else if (n is ProtoCore.AST.AssociativeAST.ImportNode)
{
core.BuildStatus.LogSemanticError("Import statements are not supported in CodeBlock Nodes.");
}
else if (n is ProtoCore.AST.AssociativeAST.LanguageBlockNode)
{
core.BuildStatus.LogSemanticError("Language blocks are not supported in CodeBlock Nodes.");
}
string stmt = string.Empty;
// Append the temporaries only if it is not a function def or class decl
bool isFunctionOrClassDef = n is ProtoCore.AST.AssociativeAST.FunctionDefinitionNode || n is ProtoCore.AST.AssociativeAST.ClassDeclNode;
if (isFunctionOrClassDef)
{
ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(new List<ProtoCore.AST.AssociativeAST.AssociativeNode>{ n });
stmt = codegen.GenerateCode();
}
else
{
stmt = ProtoCore.Utils.ParserUtils.ExtractStatementFromCode(expression, node);
ProtoCore.AST.AssociativeAST.BinaryExpressionNode ben = node as ProtoCore.AST.AssociativeAST.BinaryExpressionNode;
if (ben != null && ben.Optr == ProtoCore.DSASM.Operator.assign)
{
ProtoCore.AST.AssociativeAST.IdentifierNode lNode = ben.LeftNode as ProtoCore.AST.AssociativeAST.IdentifierNode;
if (lNode != null && lNode.Value == ProtoCore.DSASM.Constants.kTempProcLeftVar)
{
stmt = "%t =" + stmt;
}
}
else
{
// These nodes are non-assignment nodes
stmt = "%t =" + stmt;
}
}
compiled.Add(stmt);
InsertCommentsInCode(stmt, node, commentNode, ref cNodeNum, ref compiled, expression);
}
InsertCommentsInCode(null, null, commentNode, ref cNodeNum, ref compiled, expression);
return compiled;
}
示例14: TestRoundTrip_FunctionDefAndCall_02
//.........这里部分代码省略.........
ExecutionMirror mirror = null;
// 1. Build the AST tree
// def foo(a : int)
// {
// b = 10;
// return = b + a;
// }
//
// x = foo(1);
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.IdentifierNode("a"),
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;
// build the args signature
funcDefNode.Signature = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode();
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);
// 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(1));
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);
// 2. Execute AST and verify
mirror = thisTest.RunASTSource(astList);
thisTest.Verify("x", 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);
thisTest.Verify("x", result1);
}
示例15: GetExpressionFromNode
private string GetExpressionFromNode(AssociativeNode astNode)
{
List<AssociativeNode> astNodes = new List<AssociativeNode>();
astNodes.Add(astNode);
ProtoCore.CodeGenDS codegen = new ProtoCore.CodeGenDS(astNodes);
return codegen.GenerateCode();
}