private static void InsertDotMemVarMethod(Core core, ProtoCore.AST.Node root) { ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.access = ProtoCore.DSASM.AccessSpecifier.kPublic; funcDefNode.Name = ProtoCore.DSASM.Constants.kDotArgMethodName; funcDefNode.ReturnType = new ProtoCore.Type() { Name = core.TypeSystem.GetType((int)PrimitiveType.kTypeVar), UID = (int)PrimitiveType.kTypeVar }; ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode(); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kLHS), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)PrimitiveType.kTypeVar), UID = (int)PrimitiveType.kTypeVar } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kRHS), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)PrimitiveType.kTypeInt), UID = (int)PrimitiveType.kTypeInt } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, "%rhsDimExprList"), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)PrimitiveType.kTypeInt), UID = (int)PrimitiveType.kTypeInt, IsIndexable = true, rank = 1 } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, "%rhsDim"), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)PrimitiveType.kTypeInt), UID = (int)PrimitiveType.kTypeInt } }); funcDefNode.Singnature = args; ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Kw.kw_return, ProtoCore.PrimitiveType.kTypeReturn); ProtoCore.AST.AssociativeAST.DotFunctionBodyNode dotNode = new ProtoCore.AST.AssociativeAST.DotFunctionBodyNode(args.Arguments[0].NameNode, args.Arguments[1].NameNode, args.Arguments[2].NameNode, args.Arguments[3].NameNode); body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = ProtoCore.DSASM.Operator.assign, RightNode = dotNode }); funcDefNode.FunctionBody = body; (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode); }
/// Checks if the string in code block node is a literal or an identifier or has multiple lines of code. /// </summary> /// <param name="code"></param> /// <returns></returns> public static SnapshotNodeType AnalyzeString(string code) { SnapshotNodeType type = SnapshotNodeType.None; if (!code.EndsWith(";")) { code += ";"; } List <ProtoCore.AST.Node> n = new List <ProtoCore.AST.Node>(); try { ProtoCore.Options options = new ProtoCore.Options(); options.RootModulePathName = string.Empty; ProtoCore.Core core = new ProtoCore.Core(options); core.Executives.Add(ProtoCore.Language.kAssociative, new ProtoAssociative.Executive(core)); core.Executives.Add(ProtoCore.Language.kImperative, new ProtoImperative.Executive(core)); core.IsParsingPreloadedAssembly = false; core.IsParsingCodeBlockNode = true; core.ParsingMode = ProtoCore.ParseMode.AllowNonAssignment; System.IO.MemoryStream memstream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(code)); ProtoCore.DesignScriptParser.Scanner s = new ProtoCore.DesignScriptParser.Scanner(memstream); ProtoCore.DesignScriptParser.Parser p = new ProtoCore.DesignScriptParser.Parser(s, core); p.Parse(); ProtoCore.AST.AssociativeAST.CodeBlockNode codeBlockNode = p.root as ProtoCore.AST.AssociativeAST.CodeBlockNode; n = p.GetParsedASTList(codeBlockNode); } catch (Exception) { //if syntax return SnapshotNodeType as None return(SnapshotNodeType.CodeBlock); } if (n.Count > 1 || n.Count == 0) { type = SnapshotNodeType.CodeBlock; } else if (n[0] is ProtoCore.AST.AssociativeAST.BinaryExpressionNode) { type = SnapshotNodeType.CodeBlock; } else if (n[0] is ProtoCore.AST.AssociativeAST.IdentifierNode) { type = SnapshotNodeType.Identifier; } else if (n[0] is ProtoCore.AST.AssociativeAST.IntNode || n[0] is ProtoCore.AST.AssociativeAST.DoubleNode || n[0] is ProtoCore.AST.AssociativeAST.StringNode || n[0] is ProtoCore.AST.AssociativeAST.FunctionCallNode || n[0] is ProtoCore.AST.AssociativeAST.RangeExprNode) { type = SnapshotNodeType.Literal; } else { type = SnapshotNodeType.CodeBlock; } return(type); }
/// <summary> /// Parses designscript code and outputs comment nodes and returns CodeBlockNode /// </summary> /// <param name="core"></param> /// <param name="code"> Source code to parse </param> /// <param name="commentNode"> Comments in the code are preserved and stored in commentNode</param> public static ProtoCore.AST.Node Parse(Core core, string code, out ProtoCore.AST.AssociativeAST.CodeBlockNode commentNode) { commentNode = null; ProtoCore.DesignScriptParser.Parser p = ProtoCore.Utils.ParserUtils.ParseInternal(core, code); Validity.Assert(null != p); commentNode = p.commentNode; return(p.root); }
private static void InsertInlineConditionOperationMethod(Core core, ProtoCore.AST.Node root, PrimitiveType condition, PrimitiveType r) { ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.access = ProtoCore.DSASM.AccessSpecifier.kPublic; funcDefNode.Name = ProtoCore.DSASM.Constants.kInlineCondition; funcDefNode.ReturnType = new ProtoCore.Type() { Name = core.TypeSystem.GetType((int)r), UID = (int)r }; ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode(); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, "%condition"), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)condition), UID = (int)condition } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, "%trueExp"), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)r), UID = (int)r } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, "%falseExp"), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)r), UID = (int)r } }); funcDefNode.Singnature = args; ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Kw.kw_return, ProtoCore.PrimitiveType.kTypeReturn); ProtoCore.AST.AssociativeAST.IdentifierNode con = BuildAssocIdentifier(core, "%condition"); ProtoCore.AST.AssociativeAST.IdentifierNode t = BuildAssocIdentifier(core, "%trueExp"); ProtoCore.AST.AssociativeAST.IdentifierNode f = BuildAssocIdentifier(core, "%falseExp"); body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.InlineConditionalNode() { ConditionExpression = con, TrueExpression = t, FalseExpression = f } }); funcDefNode.FunctionBody = body; (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode); }
private CodeBlockNode GetCodeBlockNode(ProtoCore.AST.AssociativeAST.CodeBlockNode codeBlockNode) { Assert.IsTrue(codeBlockNode.Body.Any()); var languageBlockNode = codeBlockNode.Body[0] as ProtoCore.AST.AssociativeAST.LanguageBlockNode; Assert.IsNotNull(languageBlockNode); var imperativeCodeBlockNode = languageBlockNode.CodeBlockNode as CodeBlockNode; Assert.IsNotNull(imperativeCodeBlockNode); return(imperativeCodeBlockNode); }
public static List <ProtoCore.AST.Node> GetAstNodes(ProtoCore.AST.Node codeBlockNode) { List <ProtoCore.AST.Node> nodes = new List <ProtoCore.AST.Node>(); ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = codeBlockNode as ProtoCore.AST.AssociativeAST.CodeBlockNode; if (cbn != null) { foreach (var n in cbn.Body) { nodes.Add(n); } } return(nodes); }
private static void InsertBinaryOperationMethod(Core core, ProtoCore.AST.Node root, Operator op, PrimitiveType r, PrimitiveType op1, PrimitiveType op2, int retRank = 0, int op1rank = 0, int op2rank = 0) { ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.access = ProtoCore.DSASM.AccessSpecifier.kPublic; funcDefNode.IsAssocOperator = true; funcDefNode.IsBuiltIn = true; funcDefNode.Name = ProtoCore.DSASM.Constants.kInternalNamePrefix + op.ToString(); funcDefNode.ReturnType = new ProtoCore.Type() { Name = core.TypeSystem.GetType((int)r), UID = (int)r, rank = retRank, IsIndexable = (retRank > 0) }; ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode(); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kLHS), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)op1), UID = (int)op1, rank = op1rank, IsIndexable = (op1rank > 0) } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kRHS), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)op2), UID = (int)op2, rank = op2rank, IsIndexable = (op2rank > 0) } }); funcDefNode.Singnature = args; ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Kw.kw_return, ProtoCore.PrimitiveType.kTypeReturn); ProtoCore.AST.AssociativeAST.IdentifierNode lhs = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kLHS); ProtoCore.AST.AssociativeAST.IdentifierNode rhs = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kRHS); body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = ProtoCore.DSASM.Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = lhs, RightNode = rhs, Optr = op } }); funcDefNode.FunctionBody = body; (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode); }
public bool Compile(List <ProtoCore.AST.AssociativeAST.AssociativeNode> astList, ProtoCore.Core core, out int blockId) { bool buildSucceeded = false; blockId = ProtoCore.DSASM.Constants.kInvalidIndex; if (astList.Count <= 0) { // Nothing to compile buildSucceeded = true; } else { try { //defining the global Assoc block that wraps the entire .ds source file ProtoCore.LanguageCodeBlock globalBlock = new ProtoCore.LanguageCodeBlock(); globalBlock.language = ProtoCore.Language.kAssociative; globalBlock.body = string.Empty; //the wrapper block can be given a unique id to identify it as the global scope globalBlock.id = ProtoCore.LanguageCodeBlock.OUTERMOST_BLOCK_ID; //passing the global Assoc wrapper block to the compiler ProtoCore.CompileTime.Context context = new ProtoCore.CompileTime.Context(); context.SetData(string.Empty, new Dictionary <string, object>(), null); ProtoCore.Language id = globalBlock.language; ProtoCore.AST.AssociativeAST.CodeBlockNode codeblock = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); codeblock.Body.AddRange(astList); core.Compilers[id].Compile(out blockId, null, globalBlock, context, EventSink, codeblock); core.BuildStatus.ReportBuildResult(); buildSucceeded = core.BuildStatus.BuildSucceeded; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } return(buildSucceeded); }
private bool Compile(List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList, ProtoCore.Core core, ProtoCore.CompileTime.Context context) { bool buildSucceeded = false; if (astList.Count <= 0) { // Nothing to compile buildSucceeded = true; } else { try { //defining the global Assoc block that wraps the entire .ds source file ProtoCore.LanguageCodeBlock globalBlock = new ProtoCore.LanguageCodeBlock(); globalBlock.language = ProtoCore.Language.kAssociative; globalBlock.body = string.Empty; //the wrapper block can be given a unique id to identify it as the global scope globalBlock.id = ProtoCore.LanguageCodeBlock.OUTERMOST_BLOCK_ID; //passing the global Assoc wrapper block to the compiler context.SetData(string.Empty, new Dictionary<string, object>(), null); ProtoCore.Language id = globalBlock.language; ProtoCore.AST.AssociativeAST.CodeBlockNode codeblock = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); codeblock.Body.AddRange(astList); int blockId = ProtoCore.DSASM.Constants.kInvalidIndex; core.Compilers[id].Compile(out blockId, null, globalBlock, context, EventSink, codeblock); core.BuildStatus.ReportBuildResult(); buildSucceeded = core.BuildStatus.BuildSucceeded; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } return buildSucceeded; }
public ProtoLanguage.CompileStateTracker Compile(List <ProtoCore.AST.AssociativeAST.AssociativeNode> astList, ProtoCore.Core core, out int blockId) { ProtoLanguage.CompileStateTracker compileState = ProtoScript.CompilerUtils.BuildDefaultCompilerState(); compileState.ExecMode = ProtoCore.DSASM.InterpreterMode.kNormal; blockId = ProtoCore.DSASM.Constants.kInvalidIndex; try { //defining the global Assoc block that wraps the entire .ds source file ProtoCore.LanguageCodeBlock globalBlock = new ProtoCore.LanguageCodeBlock(); globalBlock.language = ProtoCore.Language.kAssociative; globalBlock.body = string.Empty; //the wrapper block can be given a unique id to identify it as the global scope globalBlock.id = ProtoCore.LanguageCodeBlock.OUTERMOST_BLOCK_ID; //passing the global Assoc wrapper block to the compiler ProtoCore.CompileTime.Context context = new ProtoCore.CompileTime.Context(); ProtoCore.Language id = globalBlock.language; ProtoCore.AST.AssociativeAST.CodeBlockNode codeblock = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); codeblock.Body.AddRange(astList); compileState.Executives[id].Compile(compileState, out blockId, null, globalBlock, context, EventSink, codeblock); compileState.BuildStatus.ReportBuildResult(); int errors = 0; int warnings = 0; compileState.compileSucceeded = compileState.BuildStatus.GetBuildResult(out errors, out warnings); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(compileState); }
// The following methods are used to insert methods to the bottom of the AST and convert operator to these method calls // to support replication on operators private static void InsertUnaryOperationMethod(ProtoLanguage.CompileStateTracker compileState, ProtoCore.AST.Node root, UnaryOperator op, PrimitiveType r, PrimitiveType operand) { ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.access = ProtoCore.DSASM.AccessSpecifier.kPublic; funcDefNode.IsAssocOperator = true; funcDefNode.IsBuiltIn = true; funcDefNode.Name = ProtoCore.DSASM.Constants.kInternalNamePrefix + op.ToString(); funcDefNode.ReturnType = new ProtoCore.Type() { Name = compileState.TypeSystem.GetType((int)r), UID = (int)r }; ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode(); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(compileState, "%param"), ArgumentType = new ProtoCore.Type { Name = compileState.TypeSystem.GetType((int)operand), UID = (int)operand } }); funcDefNode.Singnature = args; ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(compileState, ProtoCore.DSDefinitions.Kw.kw_return, ProtoCore.PrimitiveType.kTypeReturn); ProtoCore.AST.AssociativeAST.IdentifierNode param = BuildAssocIdentifier(compileState, "%param"); body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = ProtoCore.DSASM.Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.UnaryExpressionNode() { Expression = param, Operator = op } }); funcDefNode.FunctionBody = body; (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode); }
private static void InsertDotMemVarMethod(ProtoLanguage.CompileStateTracker compileState, ProtoCore.AST.Node root) { ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.access = ProtoCore.DSASM.AccessSpecifier.kPublic; funcDefNode.Name = ProtoCore.DSASM.Constants.kDotArgMethodName; funcDefNode.ReturnType = new ProtoCore.Type() { Name = compileState.TypeSystem.GetType((int)PrimitiveType.kTypeVar), UID = (int)PrimitiveType.kTypeVar }; ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode(); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(compileState, ProtoCore.DSASM.Constants.kLHS), ArgumentType = new ProtoCore.Type { Name = compileState.TypeSystem.GetType((int)PrimitiveType.kTypeVar), UID = (int)PrimitiveType.kTypeVar } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(compileState, ProtoCore.DSASM.Constants.kRHS), ArgumentType = new ProtoCore.Type { Name = compileState.TypeSystem.GetType((int)PrimitiveType.kTypeInt), UID = (int)PrimitiveType.kTypeInt } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(compileState, "%rhsDimExprList"), ArgumentType = new ProtoCore.Type { Name = compileState.TypeSystem.GetType((int)PrimitiveType.kTypeInt), UID = (int)PrimitiveType.kTypeInt, IsIndexable = true, rank = 1 } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(compileState, "%rhsDim"), ArgumentType = new ProtoCore.Type { Name = compileState.TypeSystem.GetType((int)PrimitiveType.kTypeInt), UID = (int)PrimitiveType.kTypeInt } }); funcDefNode.Singnature = args; ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(compileState, ProtoCore.DSDefinitions.Keyword.Return, ProtoCore.PrimitiveType.kTypeReturn); ProtoCore.AST.AssociativeAST.DotFunctionBodyNode dotNode = new ProtoCore.AST.AssociativeAST.DotFunctionBodyNode(args.Arguments[0].NameNode, args.Arguments[1].NameNode, args.Arguments[2].NameNode, args.Arguments[3].NameNode); body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = ProtoCore.DSASM.Operator.assign, RightNode = dotNode}); funcDefNode.FunctionBody = body; (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode); }
private static void InsertBinaryOperationMethod(ProtoLanguage.CompileStateTracker compileState, ProtoCore.AST.Node root, Operator op, PrimitiveType r, PrimitiveType op1, PrimitiveType op2, int retRank = 0, int op1rank = 0, int op2rank = 0) { ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.access = ProtoCore.DSASM.AccessSpecifier.kPublic; funcDefNode.IsAssocOperator = true; funcDefNode.IsBuiltIn = true; funcDefNode.Name = Op.GetOpFunction(op); funcDefNode.ReturnType = new ProtoCore.Type() { Name = compileState.TypeSystem.GetType((int)r), UID = (int)r, rank = retRank, IsIndexable = (retRank > 0)}; ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode(); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(compileState, ProtoCore.DSASM.Constants.kLHS), ArgumentType = new ProtoCore.Type { Name = compileState.TypeSystem.GetType((int)op1), UID = (int)op1, rank = op1rank, IsIndexable = (op1rank > 0)} }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(compileState, ProtoCore.DSASM.Constants.kRHS), ArgumentType = new ProtoCore.Type { Name = compileState.TypeSystem.GetType((int)op2), UID = (int)op2, rank = op2rank, IsIndexable = (op2rank > 0)} }); funcDefNode.Singnature = args; ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(compileState, ProtoCore.DSDefinitions.Keyword.Return, ProtoCore.PrimitiveType.kTypeReturn); ProtoCore.AST.AssociativeAST.IdentifierNode lhs = BuildAssocIdentifier(compileState, ProtoCore.DSASM.Constants.kLHS); ProtoCore.AST.AssociativeAST.IdentifierNode rhs = BuildAssocIdentifier(compileState, ProtoCore.DSASM.Constants.kRHS); body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = ProtoCore.DSASM.Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = lhs, RightNode = rhs, Optr = op } }); funcDefNode.FunctionBody = body; (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode); }
public override bool Compile(ProtoLanguage.CompileStateTracker compileState, out int blockId, ProtoCore.DSASM.CodeBlock parentBlock, ProtoCore.LanguageCodeBlock langBlock, ProtoCore.CompileTime.Context callContext, ProtoCore.DebugServices.EventSink sink, ProtoCore.AST.Node codeBlockNode, ProtoCore.AssociativeGraph.GraphNode graphNode = null) { Debug.Assert(langBlock != null); blockId = ProtoCore.DSASM.Constants.kInvalidIndex; bool buildSucceeded = false; bool isLangSignValid = compileState.Langverify.Verify(langBlock); if (isLangSignValid) { try { ProtoCore.CodeGen oldCodegen = compileState.assocCodegen; if (ProtoCore.DSASM.InterpreterMode.kNormal == compileState.ExecMode) { if ((compileState.IsParsingPreloadedAssembly || compileState.IsParsingCodeBlockNode) && parentBlock == null) { if (compileState.CodeBlockList.Count == 0) { compileState.assocCodegen = new ProtoAssociative.CodeGen(compileState, parentBlock); } else { // We reuse the existing toplevel CodeBlockList's for the procedureTable's // by calling this overloaded constructor - pratapa compileState.assocCodegen = new ProtoAssociative.CodeGen(compileState); } } else compileState.assocCodegen = new ProtoAssociative.CodeGen(compileState, parentBlock); } if (null != compileState.AssocNode) { ProtoCore.AST.AssociativeAST.CodeBlockNode cnode = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); cnode.Body.Add(compileState.AssocNode as ProtoCore.AST.AssociativeAST.AssociativeNode); compileState.assocCodegen.context = callContext; blockId = compileState.assocCodegen.Emit((cnode as ProtoCore.AST.AssociativeAST.CodeBlockNode), graphNode); } else { //if not null, Compile has been called from DfsTraverse. No parsing is needed. if (codeBlockNode == null) { System.IO.MemoryStream memstream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(langBlock.body)); ProtoCore.DesignScriptParser.Scanner s = new ProtoCore.DesignScriptParser.Scanner(memstream); ProtoCore.DesignScriptParser.Parser p = new ProtoCore.DesignScriptParser.Parser(s, compileState, compileState.builtInsLoaded); p.Parse(); // TODO Jun: Set this flag inside a persistent object compileState.builtInsLoaded = true; codeBlockNode = p.root; //compileState.AstNodeList = p.GetParsedASTList(codeBlockNode as ProtoCore.AST.AssociativeAST.CodeBlockNode); List<ProtoCore.AST.Node> astNodes = ProtoCore.Utils.ParserUtils.GetAstNodes(codeBlockNode); compileState.AstNodeList = astNodes; } else { if (!compileState.builtInsLoaded) { // Load the built-in methods manually ProtoCore.Utils.CoreUtils.InsertPredefinedAndBuiltinMethods(compileState, codeBlockNode, false); compileState.builtInsLoaded = true; } } compileState.assocCodegen.context = callContext; //Temporarily change the code block for code gen to the current block, in the case it is an imperative block //CodeGen for ProtoImperative is modified to passing in the core object. ProtoCore.DSASM.CodeBlock oldCodeBlock = compileState.assocCodegen.codeBlock; if (compileState.ExecMode == ProtoCore.DSASM.InterpreterMode.kExpressionInterpreter) { int tempBlockId = compileState.GetCurrentBlockId(); ProtoCore.DSASM.CodeBlock tempCodeBlock = compileState.GetCodeBlock(compileState.CodeBlockList, tempBlockId); while (null != tempCodeBlock && tempCodeBlock.blockType != ProtoCore.DSASM.CodeBlockType.kLanguage) { tempCodeBlock = tempCodeBlock.parent; } compileState.assocCodegen.codeBlock = tempCodeBlock; } compileState.assocCodegen.codeBlock.EventSink = sink; if (compileState.BuildStatus.Errors.Count == 0) //if there is syntax error, no build needed { blockId = compileState.assocCodegen.Emit((codeBlockNode as ProtoCore.AST.AssociativeAST.CodeBlockNode), graphNode); } if (compileState.ExecMode == ProtoCore.DSASM.InterpreterMode.kExpressionInterpreter) { blockId = compileState.assocCodegen.codeBlock.codeBlockId; //Restore the code block. compileState.assocCodegen.codeBlock = oldCodeBlock; } } // @keyu: we have to restore asscoCodegen here. It may be // reused later on. Suppose for an inline expression // "x = 1 == 2 ? 3 : 4", we dynamically create assocCodegen // to compile true and false expression in this inline // expression, and if we don't restore assocCodegen, the pc // is totally messed up. // // But if directly replace with old assocCodegen, will it // replace some other useful information? Need to revisit it. // // Also refer to defect IDE-2120. if (oldCodegen != null && compileState.assocCodegen != oldCodegen) { compileState.assocCodegen = oldCodegen; } } catch (ProtoCore.BuildHaltException e) { #if DEBUG //compileState.BuildStatus.LogSemanticError(e.errorMsg); #endif } int errors = 0; int warnings = 0; buildSucceeded = compileState.BuildStatus.GetBuildResult(out errors, out warnings); } return buildSucceeded; }
public void GraphILTest_FFIClassUsage_03() { /* def f() { * X = 10; * return = X; * } */ ProtoCore.AST.AssociativeAST.BinaryExpressionNode assign1 = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("X"), new ProtoCore.AST.AssociativeAST.IntNode(10), ProtoCore.DSASM.Operator.assign); ProtoCore.AST.AssociativeAST.IdentifierNode returnExpr = new ProtoCore.AST.AssociativeAST.IdentifierNode("X"); ProtoCore.AST.AssociativeAST.ReturnNode returnNode = new ProtoCore.AST.AssociativeAST.ReturnNode(); returnNode.ReturnExpr = returnExpr; ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); cbn.Body.Add(assign1); cbn.Body.Add(returnNode); ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.FunctionBody = cbn; funcDefNode.Name = "f"; funcDefNode.ReturnType = new ProtoCore.Type() { Name = "int", UID = (int)ProtoCore.PrimitiveType.kTypeInt, //IsIndexable = false, rank = 0 }; /*Class C { }*/ ProtoCore.AST.AssociativeAST.VarDeclNode varDeclNode = new ProtoCore.AST.AssociativeAST.VarDeclNode(); varDeclNode.Name = "X"; ProtoCore.AST.AssociativeAST.IdentifierNode varDeclId = new ProtoCore.AST.AssociativeAST.IdentifierNode() { Value = "X", Name = "X", datatype = new ProtoCore.Type() { Name = "int", //IsIndexable = false, rank = 0, UID = (int)ProtoCore.PrimitiveType.kTypeInt } }; varDeclNode.NameNode = varDeclId; varDeclNode.ArgumentType = new ProtoCore.Type() { Name = "int", //IsIndexable = false, rank = 0, UID = (int)ProtoCore.PrimitiveType.kTypeVar }; ProtoCore.AST.AssociativeAST.ClassDeclNode classDeclNode = new ProtoCore.AST.AssociativeAST.ClassDeclNode(); classDeclNode.className = "C"; classDeclNode.funclist.Add(funcDefNode); classDeclNode.varlist.Add(varDeclNode); // p = new C.C(); t = p.f(); val = p.X; ProtoCore.AST.AssociativeAST.FunctionCallNode funcCallP = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); funcCallP.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("C"); List<ProtoCore.AST.AssociativeAST.AssociativeNode> listArgs = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); funcCallP.FormalArguments = listArgs; ProtoCore.AST.AssociativeAST.FunctionDotCallNode funcDotCallNode = new ProtoCore.AST.AssociativeAST.FunctionDotCallNode("C", funcCallP); ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignP = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("p"), funcDotCallNode, ProtoCore.DSASM.Operator.assign ); //p = C.C() ProtoCore.AST.AssociativeAST.FunctionCallNode funcCallT = new ProtoCore.AST.AssociativeAST.FunctionCallNode(); funcCallT.Function = new ProtoCore.AST.AssociativeAST.IdentifierNode("f"); funcCallT.FormalArguments = listArgs; funcDotCallNode = new ProtoCore.AST.AssociativeAST.FunctionDotCallNode("p", funcCallT); ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignT = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("t"), funcDotCallNode, ProtoCore.DSASM.Operator.assign ); //t = p.f(); ProtoCore.AST.AssociativeAST.IdentifierListNode idListNode = new ProtoCore.AST.AssociativeAST.IdentifierListNode(); idListNode.LeftNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("p"); idListNode.Optr = ProtoCore.DSASM.Operator.dot; idListNode.RightNode = new ProtoCore.AST.AssociativeAST.IdentifierNode("X"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode assignVal = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( new ProtoCore.AST.AssociativeAST.IdentifierNode("val"), idListNode, ProtoCore.DSASM.Operator.assign); List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(classDeclNode); astList.Add(assignP); astList.Add(assignT); astList.Add(assignVal); //============================================== // emit the DS code from the AST tree // // Class C { // X : int // def f() { // x = 2; // return = X; // } // } // p = new C(); // t = p.f(); // val = p.X; //============================================== GraphToDSCompiler.GraphCompiler gc = GraphToDSCompiler.GraphCompiler.CreateInstance(); string code = gc.Emit(astList); //============================================== // Verify the results - get the value of the x property //============================================== ExecutionMirror mirror = thisTest.RunScriptSource(code); Obj o = mirror.GetValue("val"); Assert.IsTrue((Int64)o.Payload == 10); }
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] [Ignore][Category("DSDefinedClass_Ignored_DSDefinedClassSemantics")] public void TestCodeGenDS_ClassDecl_MemFunctionCall_01() { // class bar // { // f : var // def foo (b:int) // { // b = 10; // return = b + 10; // } // } // // p = bar.bar(); // a = p.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; // 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 List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>(); astList.Add(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); // 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);
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); }
public override bool Compile(out int blockId, ProtoCore.DSASM.CodeBlock parentBlock, ProtoCore.LanguageCodeBlock langBlock, ProtoCore.CompileTime.Context callContext, ProtoCore.DebugServices.EventSink sink = null, ProtoCore.AST.Node codeBlockNode = null, ProtoCore.AssociativeGraph.GraphNode graphNode = null) { Validity.Assert(langBlock != null); blockId = ProtoCore.DSASM.Constants.kInvalidIndex; bool buildSucceeded = false; bool isLangSignValid = core.Langverify.Verify(langBlock); if (isLangSignValid) { try { ProtoCore.CodeGen oldCodegen = core.assocCodegen; if (ProtoCore.DSASM.InterpreterMode.kNormal == core.ExecMode) { if ((core.IsParsingPreloadedAssembly || core.IsParsingCodeBlockNode) && parentBlock == null) { if (core.CodeBlockList.Count == 0) { core.assocCodegen = new ProtoAssociative.CodeGen(core, callContext, parentBlock); } else { // We reuse the existing toplevel CodeBlockList's for the procedureTable's // by calling this overloaded constructor - pratapa core.assocCodegen = new ProtoAssociative.CodeGen(core); } } else { core.assocCodegen = new ProtoAssociative.CodeGen(core, callContext, parentBlock); } } if (null != core.AssocNode) { ProtoCore.AST.AssociativeAST.CodeBlockNode cnode = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); cnode.Body.Add(core.AssocNode as ProtoCore.AST.AssociativeAST.AssociativeNode); core.assocCodegen.context = callContext; blockId = core.assocCodegen.Emit((cnode as ProtoCore.AST.AssociativeAST.CodeBlockNode), graphNode); } else { //if not null, Compile has been called from DfsTraverse. No parsing is needed. if (codeBlockNode == null) { var p = ParserUtils.CreateParser(langBlock.body, core); p.Parse(); // TODO Jun: Set this flag inside a persistent object core.builtInsLoaded = true; codeBlockNode = p.root; //core.AstNodeList = p.GetParsedASTList(codeBlockNode as ProtoCore.AST.AssociativeAST.CodeBlockNode); List <ProtoCore.AST.Node> astNodes = ProtoCore.Utils.ParserUtils.GetAstNodes(codeBlockNode); core.AstNodeList = astNodes; } else { if (!core.builtInsLoaded) { // Load the built-in methods manually ProtoCore.Utils.CoreUtils.InsertPredefinedAndBuiltinMethods(core, codeBlockNode, false); core.builtInsLoaded = true; } } core.assocCodegen.context = callContext; //Temporarily change the code block for code gen to the current block, in the case it is an imperative block //CodeGen for ProtoImperative is modified to passing in the core object. ProtoCore.DSASM.CodeBlock oldCodeBlock = core.assocCodegen.codeBlock; if (core.ExecMode == ProtoCore.DSASM.InterpreterMode.kExpressionInterpreter) { int tempBlockId = core.GetCurrentBlockId(); ProtoCore.DSASM.CodeBlock tempCodeBlock = core.GetCodeBlock(core.CodeBlockList, tempBlockId); while (null != tempCodeBlock && tempCodeBlock.blockType != ProtoCore.DSASM.CodeBlockType.kLanguage) { tempCodeBlock = tempCodeBlock.parent; } core.assocCodegen.codeBlock = tempCodeBlock; } core.assocCodegen.codeBlock.EventSink = sink; if (core.BuildStatus.ErrorCount == 0) //if there is syntax error, no build needed { blockId = core.assocCodegen.Emit((codeBlockNode as ProtoCore.AST.AssociativeAST.CodeBlockNode), graphNode); } if (core.ExecMode == ProtoCore.DSASM.InterpreterMode.kExpressionInterpreter) { blockId = core.assocCodegen.codeBlock.codeBlockId; //Restore the code block. core.assocCodegen.codeBlock = oldCodeBlock; } } // @keyu: we have to restore asscoCodegen here. It may be // reused later on. Suppose for an inline expression // "x = 1 == 2 ? 3 : 4", we dynamically create assocCodegen // to compile true and false expression in this inline // expression, and if we don't restore assocCodegen, the pc // is totally messed up. // // But if directly replace with old assocCodegen, will it // replace some other useful information? Need to revisit it. // // Also refer to defect IDE-2120. if (oldCodegen != null && core.assocCodegen != oldCodegen) { core.assocCodegen = oldCodegen; } } catch (ProtoCore.BuildHaltException) { #if DEBUG //core.BuildStatus.LogSemanticError(e.errorMsg); #endif } buildSucceeded = core.BuildStatus.BuildSucceeded; } return(buildSucceeded); }
public void TestProtoASTExecute_FunctionDefAndCall_01() { // 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); ExecutionMirror mirror = thisTest.RunASTSource(astList); Obj o = mirror.GetValue("x"); Assert.IsTrue((Int64)o.Payload == 20); }
public void TestProtoASTExecute_ArrayIndex_LHS_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(4), ProtoCore.DSASM.Operator.assign); astList.Add(declareNodeB); // def foo(){ // return = -2; // } 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(-2), 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); // a[b + foo()] = -1; 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(); nodeALHS.ArrayDimensions.Expr = operation1; ProtoCore.AST.AssociativeAST.BinaryExpressionNode nodeALHSAssignment = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode( nodeALHS, new ProtoCore.AST.AssociativeAST.IntNode(-1), ProtoCore.DSASM.Operator.assign); astList.Add(nodeALHSAssignment); // Verify the results ExecutionMirror mirror = thisTest.RunASTSource(astList); thisTest.Verify("a", new [] { 1, 2, -1, 4}); }
public void TestRoundTrip_FunctionDefAndCall_02() { //================================= // 1. Build AST // 2. Execute AST and verify // 3. Convert AST to source // 4. Execute source and verify //================================= int result1 = 11; 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); }
} } class B extends A { constructor B() { x = 2; } } ptrA = A.A(); ax = ptrA.x; ptrB = B.B(); bx = ptrB.x; "; ExecutionMirror mirror = thisTest.RunScriptSource(code); Assert.IsTrue((Int64)mirror.GetValue("ax").Payload == 1); Assert.IsTrue((Int64)mirror.GetValue("bx").Payload == 2); } [Test] public void TestClasses05() { String code = @" def sum : double (p : double) { return = p + 10.0; } class Obj { val : var; mx : var; my : var; mz : var; constructor Obj(xx : double, yy : double, zz : double) { mx = xx; my = yy; mz = zz; val = sum(zz); } } p = Obj.Obj(0.0, 1.0, 2.0); x = p.val; "; ExecutionMirror mirror = thisTest.RunScriptSource(code); Assert.IsTrue((double)mirror.GetValue("x").Payload == 12); } [Test] public void TestClasses06() { String code = @" class Point { mx : var; my : var; mz : var; constructor ByCoordinates(x : int, y : int, z : int) { mx = x; my = y; mz = z; } } class BSplineCurve { mpts : var[]; constructor ByPoints(ptsOnCurve : Point[]) { mpts = ptsOnCurve; } } pt1 = Point.ByCoordinates(1,2,3); pt2 = Point.ByCoordinates(4,5,6); pt3 = Point.ByCoordinates(7,8,9); pt4 = Point.ByCoordinates(10,11,12); pt5 = Point.ByCoordinates(15,16,17); pts = {pt1, pt2, pt3, pt4, pt5};
public void TestRoundTrip_FunctionDefAndCall_02() { //================================= // 1. Build AST // 2. Execute AST and verify // 3. Convert AST to source // 4. Execute source and verify //================================= int result1 = 11; 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); }
private static void InsertInlineConditionOperationMethod(ProtoLanguage.CompileStateTracker compileState, ProtoCore.AST.Node root, PrimitiveType condition, PrimitiveType r) { ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.access = ProtoCore.DSASM.AccessSpecifier.kPublic; funcDefNode.Name = ProtoCore.DSASM.Constants.kInlineCondition; funcDefNode.ReturnType = new ProtoCore.Type() { Name = compileState.TypeSystem.GetType((int)r), UID = (int)r }; ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode(); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(compileState, "%condition"), ArgumentType = new ProtoCore.Type { Name = compileState.TypeSystem.GetType((int)condition), UID = (int)condition } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(compileState, "%trueExp"), ArgumentType = new ProtoCore.Type { Name = compileState.TypeSystem.GetType((int)r), UID = (int)r } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(compileState, "%falseExp"), ArgumentType = new ProtoCore.Type { Name = compileState.TypeSystem.GetType((int)r), UID = (int)r } }); funcDefNode.Singnature = args; ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(compileState, ProtoCore.DSDefinitions.Keyword.Return, ProtoCore.PrimitiveType.kTypeReturn); ProtoCore.AST.AssociativeAST.IdentifierNode con = BuildAssocIdentifier(compileState, "%condition"); ProtoCore.AST.AssociativeAST.IdentifierNode t = BuildAssocIdentifier(compileState, "%trueExp"); ProtoCore.AST.AssociativeAST.IdentifierNode f = BuildAssocIdentifier(compileState, "%falseExp"); body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.InlineConditionalNode() { ConditionExpression = con, TrueExpression = t, FalseExpression = f } }); funcDefNode.FunctionBody = body; (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode); }
// The following methods are used to insert methods to the bottom of the AST and convert operator to these method calls // to support replication on operators private static void InsertUnaryOperationMethod(ProtoLanguage.CompileStateTracker compileState, ProtoCore.AST.Node root, UnaryOperator op, PrimitiveType r, PrimitiveType operand) { ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.access = ProtoCore.DSASM.AccessSpecifier.kPublic; funcDefNode.IsAssocOperator = true; funcDefNode.IsBuiltIn = true; funcDefNode.Name = Op.GetUnaryOpFunction(op); funcDefNode.ReturnType = new ProtoCore.Type() { Name = compileState.TypeSystem.GetType((int)r), UID = (int)r }; ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode(); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.DSASM.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(compileState, "%param"), ArgumentType = new ProtoCore.Type { Name = compileState.TypeSystem.GetType((int)operand), UID = (int)operand } }); funcDefNode.Singnature = args; ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(compileState, ProtoCore.DSDefinitions.Keyword.Return, ProtoCore.PrimitiveType.kTypeReturn); ProtoCore.AST.AssociativeAST.IdentifierNode param = BuildAssocIdentifier(compileState, "%param"); body.Body.Add(new ProtoCore.AST.AssociativeAST.BinaryExpressionNode() { LeftNode = _return, Optr = ProtoCore.DSASM.Operator.assign, RightNode = new ProtoCore.AST.AssociativeAST.UnaryExpressionNode() { Expression = param, Operator = op } }); funcDefNode.FunctionBody = body; (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(funcDefNode); }
public void TestAstToCode() { // Tracked in: http://adsk-oss.myjetbrains.com/youtrack/issue/MAGN-4393 // Convert a list of code -> AST nodes -> code -> AST nodes // Compare two AST node lists are equal List <string> statements = new List <string> { "x = {};", @"x = {1, true, false, 1.234, 41, null, ""hello world"", 'x', foo(a<1>, b<2>)[3]}[0]<1>;", "x = (a..b..c)<1>..(m<1>..n<2>..~1)<2>..(i..j..#k<1>)<3>;", "x = (a > b ? foo(1, 2)<1> : bar(2, 3)<2>) ? ding(1, 2)<1> : dong(1, 2)<2>;", "x = (a == b) ? ((a >= b) ? (a && b) : (a || b)) : (a < b);", "x = foo(a<1>, b<2>, c<3>)[0]<1>;", "x = Point.ByCoordinates({1,2,3}, {4,5,6}<1>, {7, 8, 9}<2>)<0>;", "x = this.foo(1, 2);", "x = this.X;", "return = {1, 2, 3};", "return = Math.PI;", "return = Math.Cos(1.2);", "class Foo { f0; f1 = 1; f2 = 2 + 3; f3: int = 3 + 4; f4: int[] = 5; f5: int[]..[] = {1,2,3} ; x:var; y:int[][][]; z:double[]..[]; constructor Foo(p:int[]) { x = p; } def foo:var[](p:int[]..[]) { return = 0; } static def bar() { return = null; }}", "x[0][1] = {};", }; List <ProtoCore.AST.AssociativeAST.AssociativeNode> nodes = new List <ProtoCore.AST.AssociativeAST.AssociativeNode>(); List <ProtoCore.AST.AssociativeAST.AssociativeNode> new_nodes = new List <ProtoCore.AST.AssociativeAST.AssociativeNode>(); List <string> new_statements = new List <string>(); foreach (var stmt in statements) { ProtoCore.AST.AssociativeAST.CodeBlockNode commentCode = null; var cbn = GraphToDSCompiler.GraphUtilities.Parse(stmt, out commentCode) as ProtoCore.AST.AssociativeAST.CodeBlockNode; if (cbn != null) { foreach (var item in cbn.Body) { nodes.Add(item); string code = item.ToString(); new_statements.Add(code); Console.WriteLine(code); } } } foreach (var stmt in new_statements) { ProtoCore.AST.AssociativeAST.CodeBlockNode commentCode = null; var cbn = GraphToDSCompiler.GraphUtilities.Parse(stmt, out commentCode) as ProtoCore.AST.AssociativeAST.CodeBlockNode; if (cbn != null) { foreach (var item in cbn.Body) { new_nodes.Add(item); } } } Assert.AreEqual(nodes.Count, new_nodes.Count); int count = nodes.Count; for (int i = 0; i < count; ++i) { Console.WriteLine("Compare: "); Console.WriteLine(nodes[i].ToString()); Console.WriteLine(new_nodes[i].ToString()); Assert.IsTrue(nodes[i].Equals(new_nodes[i])); } }
public void TestRoundTrip_ClassDecl_MemFunctionCall_01() { int result1 = 20; 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 // def foo (b:int) // { // b = 10; // return = b + 10; // } // } // // p = bar.bar(); // a = p.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; // 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); }
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.kTypeVar; 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); }
public void TestCodeGenDS_FunctionDefNode1() { ProtoCore.AST.AssociativeAST.CodeBlockNode cbn = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); 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); var returnIdent = new ProtoCore.AST.AssociativeAST.IdentifierNode("return"); ProtoCore.AST.AssociativeAST.BinaryExpressionNode returnNode = new ProtoCore.AST.AssociativeAST.BinaryExpressionNode(returnIdent, returnExpr); cbn.Body.Add(assignment1); cbn.Body.Add(returnNode); /// ProtoCore.AST.AssociativeAST.FunctionDefinitionNode funcDefNode = new ProtoCore.AST.AssociativeAST.FunctionDefinitionNode(); funcDefNode.Name = "foo"; funcDefNode.FunctionBody = cbn; /* def foo() * { * b = 10; * return = b + 10; * }*/ List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList = new List<ProtoCore.AST.AssociativeAST.AssociativeNode>();
public ProtoLanguage.CompileStateTracker Compile(List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList, ProtoCore.Core core, out int blockId) { ProtoLanguage.CompileStateTracker compileState = ProtoScript.CompilerUtils.BuildDefaultCompilerState(); compileState.ExecMode = ProtoCore.DSASM.InterpreterMode.kNormal; blockId = ProtoCore.DSASM.Constants.kInvalidIndex; try { //defining the global Assoc block that wraps the entire .ds source file ProtoCore.LanguageCodeBlock globalBlock = new ProtoCore.LanguageCodeBlock(); globalBlock.language = ProtoCore.Language.kAssociative; globalBlock.body = string.Empty; //the wrapper block can be given a unique id to identify it as the global scope globalBlock.id = ProtoCore.LanguageCodeBlock.OUTERMOST_BLOCK_ID; //passing the global Assoc wrapper block to the compiler ProtoCore.CompileTime.Context context = new ProtoCore.CompileTime.Context(); ProtoCore.Language id = globalBlock.language; ProtoCore.AST.AssociativeAST.CodeBlockNode codeblock = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); codeblock.Body.AddRange(astList); compileState.Executives[id].Compile(compileState, out blockId, null, globalBlock, context, EventSink, codeblock); compileState.BuildStatus.ReportBuildResult(); int errors = 0; int warnings = 0; compileState.compileSucceeded = compileState.BuildStatus.GetBuildResult(out errors, out warnings); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return compileState; }
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)); }
public override bool Compile(out int blockId, ProtoCore.DSASM.CodeBlock parentBlock, ProtoCore.LanguageCodeBlock langBlock, ProtoCore.CompileTime.Context callContext, ProtoCore.DebugServices.EventSink sink = null, ProtoCore.AST.Node codeBlockNode = null, ProtoCore.AssociativeGraph.GraphNode graphNode = null) { Validity.Assert(langBlock != null); blockId = ProtoCore.DSASM.Constants.kInvalidIndex; bool buildSucceeded = false; try { ProtoCore.CodeGen oldCodegen = core.assocCodegen; if (ProtoCore.DSASM.InterpreterMode.Normal == core.Options.RunMode) { if ((core.IsParsingPreloadedAssembly || core.IsParsingCodeBlockNode) && parentBlock == null) { if (core.CodeBlockList.Count == 0) { core.assocCodegen = new ProtoAssociative.CodeGen(core, callContext, parentBlock); } else { // We reuse the existing toplevel CodeBlockList's for the procedureTable's // by calling this overloaded constructor - pratapa core.assocCodegen = new ProtoAssociative.CodeGen(core); } } else core.assocCodegen = new ProtoAssociative.CodeGen(core, callContext, parentBlock); } if (null != core.AssocNode) { ProtoCore.AST.AssociativeAST.CodeBlockNode cnode = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); cnode.Body.Add(core.AssocNode as ProtoCore.AST.AssociativeAST.AssociativeNode); core.assocCodegen.context = callContext; blockId = core.assocCodegen.Emit((cnode as ProtoCore.AST.AssociativeAST.CodeBlockNode), graphNode); } else { //if not null, Compile has been called from DfsTraverse. No parsing is needed. if (codeBlockNode == null) { var parseResult = ParserUtils.ParseWithCore(langBlock.Code, core); // TODO Jun: Set this flag inside a persistent object core.builtInsLoaded = true; codeBlockNode = parseResult.CodeBlockNode; } else { if (!core.builtInsLoaded) { // Load the built-in methods manually CoreUtils.InsertPredefinedAndBuiltinMethods(core, codeBlockNode as ProtoCore.AST.AssociativeAST.CodeBlockNode); core.builtInsLoaded = true; } } core.assocCodegen.context = callContext; //Temporarily change the code block for code gen to the current block, in the case it is an imperative block //CodeGen for ProtoImperative is modified to passing in the core object. ProtoCore.DSASM.CodeBlock oldCodeBlock = core.assocCodegen.codeBlock; if (core.Options.RunMode == ProtoCore.DSASM.InterpreterMode.Expression) { int tempBlockId = callContext.CurrentBlockId; ProtoCore.DSASM.CodeBlock tempCodeBlock = ProtoCore.Utils.CoreUtils.GetCodeBlock(core.CodeBlockList, tempBlockId); while (null != tempCodeBlock && tempCodeBlock.blockType != ProtoCore.DSASM.CodeBlockType.Language) { tempCodeBlock = tempCodeBlock.parent; } core.assocCodegen.codeBlock = tempCodeBlock; } core.assocCodegen.codeBlock.EventSink = sink; if (core.BuildStatus.ErrorCount == 0) //if there is syntax error, no build needed { blockId = core.assocCodegen.Emit((codeBlockNode as ProtoCore.AST.AssociativeAST.CodeBlockNode), graphNode); } if (core.Options.RunMode == ProtoCore.DSASM.InterpreterMode.Expression) { blockId = core.assocCodegen.codeBlock.codeBlockId; //Restore the code block. core.assocCodegen.codeBlock = oldCodeBlock; } } // @keyu: we have to restore asscoCodegen here. It may be // reused later on. Suppose for an inline expression // "x = 1 == 2 ? 3 : 4", we dynamically create assocCodegen // to compile true and false expression in this inline // expression, and if we don't restore assocCodegen, the pc // is totally messed up. // // But if directly replace with old assocCodegen, will it // replace some other useful information? Need to revisit it. // // Also refer to defect IDE-2120. if (oldCodegen != null && core.assocCodegen != oldCodegen) { core.assocCodegen = oldCodegen; } } catch (ProtoCore.BuildHaltException) { } buildSucceeded = core.BuildStatus.BuildSucceeded; return buildSucceeded; }
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); }
public void TestRoundTrip_ClassDecl_MemFunctionCall_01() { int result1 = 20; 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 // def foo (b:int) // { // b = 10; // return = b + 10; // } // } // // p = bar.bar(); // a = p.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.kTypeVar; 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.funclist.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.kTypeInt }; classDefNode.varlist.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); }