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); bool buildSucceeded = true; blockId = 0; core.assocCodegen = new ProtoVHDL.CodeGen(core, callContext, parentBlock); 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, core, core.builtInsLoaded); p.Parse(); core.builtInsLoaded = true; codeBlockNode = p.root; List <ProtoCore.AST.Node> astNodes = ProtoCore.Utils.ParserUtils.GetAstNodes(codeBlockNode); core.AstNodeList = astNodes; core.assocCodegen.Emit(codeBlockNode as ProtoCore.AST.AssociativeAST.CodeBlockNode); buildSucceeded = core.BuildStatus.BuildSucceeded; return(buildSucceeded); }
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); }
public static void SetNodeEndLocation(ProtoCore.AST.Node node, ProtoCore.AST.Node other) { if (null == node || (null == other) || (node == other)) return; node.endLine = other.endLine; node.endCol = other.endCol; }
public static void SetNodeStartLocation(ProtoCore.AST.Node node, int line, int col) { if (null == node) return; node.line = line; node.col = col; }
public static void SetNodeStartLocation(ProtoCore.AST.Node node, ProtoCore.AST.Node other) { if (null == node || (null == other) || (node == other)) return; node.line = other.line; node.col = other.col; }
public static void SetNodeEndLocation(ProtoCore.AST.Node node, ProtoCore.DesignScriptParser.Token token) { if (null == node || (null == token)) return; node.endLine = token.line; node.endCol = token.col + ((null == token.val) ? 0 : token.val.Length); }
public static void SetNodeStartLocation(ProtoCore.AST.Node node, ProtoCore.DesignScriptParser.Token token) { if (null == node || (null == token)) return; node.line = token.line; node.col = token.col; }
public static Statement CreateInstance(Node parsedNode) { if (parsedNode == null) { throw new ArgumentNullException(); } return(new Statement(parsedNode)); }
public static void SetNodeLocation(ProtoCore.AST.Node node, ProtoCore.AST.Node startNode, ProtoCore.AST.Node endNode) { if (null == node || (null == startNode) || (null == endNode)) return; node.line = startNode.line; node.col = startNode.col; node.endLine = endNode.endLine; node.endCol = endNode.endCol; }
public static void CopyDebugData(ProtoCore.AST.Node nodeTo, ProtoCore.AST.Node nodeFrom) { if (null != nodeTo && null != nodeFrom) { nodeTo.col = nodeFrom.col; nodeTo.endCol = nodeFrom.endCol; nodeTo.endLine = nodeFrom.endLine; nodeTo.line = nodeFrom.line; } }
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.Compiler.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.Compiler.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.Compiler.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.Compiler.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, "%falseExp"), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)r), UID = (int)r } }); funcDefNode.Signature = args; ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Keyword.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); }
/// <summary> /// Parse simple RHS expression /// </summary> /// <param name="expression"></param> /// <param name="core"></param> /// <returns></returns> public static ProtoCore.AST.AssociativeAST.AssociativeNode ParseRHSExpression(string expression, ProtoCore.Core core) { if (string.IsNullOrEmpty(expression)) { throw new ArgumentException("expression"); } if (core == null) { throw new ArgumentException("core"); } var currentParsingMode = core.ParsingMode; var currentParsingFlag = core.IsParsingCodeBlockNode; core.ParsingMode = ProtoCore.ParseMode.AllowNonAssignment; core.IsParsingCodeBlockNode = true; ProtoCore.AST.Node astNode = null; try { expression = expression.Trim(); if (!expression.EndsWith(";")) { expression += ";"; } expression = "__dummy = " + expression; astNode = ParserUtils.ParseWithCore(expression, core); } catch (ProtoCore.BuildHaltException ex) { } core.ParsingMode = currentParsingMode; core.IsParsingCodeBlockNode = currentParsingFlag; if (astNode == null) { return(null); } var cbn = astNode as ProtoCore.AST.AssociativeAST.CodeBlockNode; if (cbn != null && cbn.Body.Any()) { var expr = cbn.Body[0] as ProtoCore.AST.AssociativeAST.BinaryExpressionNode; if (expr != null) { return(expr.RightNode); } } return(null); }
private static void InsertBuiltInMethods(Core core, ProtoCore.AST.Node root, bool builtinMethodsLoaded) { if (!builtinMethodsLoaded) { ProtoCore.Lang.BuiltInMethods builtInMethods = new Lang.BuiltInMethods(core); foreach (ProtoCore.Lang.BuiltInMethods.BuiltInMethod method in builtInMethods.Methods) { (root as ProtoCore.AST.AssociativeAST.CodeBlockNode).Body.Add(GenerateBuiltInMethodSignatureNode(method)); } } }
public static void InsertPredefinedAndBuiltinMethods(Core core, ProtoCore.AST.Node root, bool builtinMethodsLoaded) { if (DSASM.InterpreterMode.kNormal == core.Options.RunMode) { if (core.Options.AssocOperatorAsMethod) { ProtoCore.Utils.CoreUtils.InsertPredefinedMethod(core, root, builtinMethodsLoaded); } ProtoCore.Utils.CoreUtils.InsertBuiltInMethods(core, root, builtinMethodsLoaded); } }
public static void SetNodeEndLocation(ProtoCore.AST.Node node, ProtoCore.DesignScriptParser.Token token) { if (null == node || (null == token)) { return; } node.endLine = token.line; node.endCol = token.col + (token.val?.Length ?? 0); node.endCharPos = token.charPos + (token.val?.Length ?? 0); }
public static void InsertPredefinedAndBuiltinMethods(Core core, ProtoCore.AST.Node root, bool builtinMethodsLoaded) { if (DSASM.InterpreterMode.kNormal == core.ExecMode /*&& core.Options.CompilationTarget == DSDefinitions.CompileTarget.JIL*/) { if (core.Options.AssocOperatorAsMethod) { ProtoCore.Utils.CoreUtils.InsertPredefinedMethod(core, root, builtinMethodsLoaded); } ProtoCore.Utils.CoreUtils.InsertBuiltInMethods(core, root, builtinMethodsLoaded); } }
/// <summary> /// Try get default argument expression from DefaultArgumentAttribute, /// and parse into Associaitve AST node. /// </summary> /// <param name="arg"></param> /// <param name="defaultArgumentNode"></param> /// <returns></returns> private bool TryGetDefaultArgumentFromAttribute(ArgumentInfo arg, out AssociativeNode defaultArgumentNode) { defaultArgumentNode = null; if (arg.Attributes == null) { return(false); } object o; if (!arg.Attributes.TryGetAttribute("DefaultArgumentAttribute", out o)) { return(false); } var defaultExpression = o as string; if (string.IsNullOrEmpty(defaultExpression)) { return(false); } var currentParsingmode = LibraryManagementCore.ParsingMode; var currentParsingFlag = LibraryManagementCore.IsParsingCodeBlockNode; LibraryManagementCore.ParsingMode = ProtoCore.ParseMode.AllowNonAssignment; LibraryManagementCore.IsParsingCodeBlockNode = true; ProtoCore.AST.Node astNode = null; try { astNode = ParserUtils.ParseWithCore(defaultExpression + ";", LibraryManagementCore); } catch (ProtoCore.BuildHaltException ex) { Log("Failed to parse default argument attribute \"" + defaultExpression + "\" for parameter: " + arg.Name); } if (astNode != null) { var cbn = astNode as CodeBlockNode; if (cbn != null && cbn.Body.Any()) { defaultArgumentNode = (cbn.Body[0] as BinaryExpressionNode).RightNode; } } LibraryManagementCore.ParsingMode = currentParsingmode; LibraryManagementCore.IsParsingCodeBlockNode = currentParsingFlag; return(defaultArgumentNode != null); }
public static void CopyNodeLocation(ProtoCore.AST.Node node, ProtoCore.AST.Node other) { if (null == node || (null == other) || (node == other)) { return; } node.line = other.line; node.col = other.col; node.endLine = other.endLine; node.endCol = other.endCol; }
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); }
public static ProtoCore.AST.Node Clone(ProtoCore.AST.Node rhsNode) { if (rhsNode is ProtoCore.AST.AssociativeAST.AssociativeNode) { return(Clone(rhsNode as ProtoCore.AST.AssociativeAST.AssociativeNode)); } else if (rhsNode is ProtoCore.AST.ImperativeAST.ImperativeNode) { return(Clone(rhsNode as ProtoCore.AST.ImperativeAST.ImperativeNode)); } else { Validity.Assert(false); } return(null); }
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.Compiler.AccessSpecifier.kPublic; funcDefNode.IsAssocOperator = true; funcDefNode.IsBuiltIn = true; funcDefNode.Name = Op.GetOpFunction(op); funcDefNode.ReturnType = new ProtoCore.Type() { Name = core.TypeSystem.GetType((int)r), UID = (int)r, rank = retRank }; ProtoCore.AST.AssociativeAST.ArgumentSignatureNode args = new ProtoCore.AST.AssociativeAST.ArgumentSignatureNode(); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.Compiler.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kLHS), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)op1), UID = (int)op1, rank = op1rank } }); args.AddArgument(new ProtoCore.AST.AssociativeAST.VarDeclNode() { memregion = ProtoCore.DSASM.MemoryRegion.kMemStack, access = ProtoCore.Compiler.AccessSpecifier.kPublic, NameNode = BuildAssocIdentifier(core, ProtoCore.DSASM.Constants.kRHS), ArgumentType = new ProtoCore.Type { Name = core.TypeSystem.GetType((int)op2), UID = (int)op2, rank = op2rank } }); funcDefNode.Signature = args; ProtoCore.AST.AssociativeAST.CodeBlockNode body = new ProtoCore.AST.AssociativeAST.CodeBlockNode(); ProtoCore.AST.AssociativeAST.IdentifierNode _return = BuildAssocIdentifier(core, ProtoCore.DSDefinitions.Keyword.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 static bool Compare(string s1, string s2, Core core) { System.IO.MemoryStream memstream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(s1)); ProtoCore.DesignScriptParser.Scanner s = new ProtoCore.DesignScriptParser.Scanner(memstream); ProtoCore.DesignScriptParser.Parser p = new ProtoCore.DesignScriptParser.Parser(s, core); p.Parse(); ProtoCore.AST.Node s1Root = p.root; memstream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(s2)); s = new ProtoCore.DesignScriptParser.Scanner(s2); p = new ProtoCore.DesignScriptParser.Parser(s, core); p.Parse(); ProtoCore.AST.Node s2Root = p.root; bool areEqual = s1Root.Equals(s2Root); return(areEqual); }
public static string ExtractStatementFromCode(string code, ProtoCore.AST.Node node) { int line = node.line; int col = node.col; int endLine = node.endLine; int endCol = node.endCol; string stmt = ""; if (node is ProtoCore.AST.AssociativeAST.IdentifierNode || node is ProtoCore.AST.AssociativeAST.IdentifierListNode || node is ProtoCore.AST.AssociativeAST.FunctionCallNode || node is ProtoCore.AST.AssociativeAST.RangeExprNode || node is ProtoCore.AST.AssociativeAST.StringNode) { string[] lines = code.Split('\n'); int lastLine = lines.Length; int lastCol = lines[lastLine - 1].Length; string stmts = ExtractStatementHelper(code, line, col, lastLine, lastCol + 1); stmt = stmts.Split(';')[0]; return(stmt + ";"); } if (node is ProtoCore.AST.AssociativeAST.IntNode) { return((node as ProtoCore.AST.AssociativeAST.IntNode).value + ";"); } else if (node is ProtoCore.AST.AssociativeAST.DoubleNode) { return((node as ProtoCore.AST.AssociativeAST.DoubleNode).value + ";"); } else if (node is ProtoCore.AST.AssociativeAST.BooleanNode) { return((node as ProtoCore.AST.AssociativeAST.BooleanNode).value + ";"); } stmt = ExtractStatementHelper(code, node.line, node.col, node.endLine, node.endCol); if (node is ProtoCore.AST.AssociativeAST.ExprListNode) { return(stmt + ";"); } return(stmt); }
public static IdentifierNode GetDefinedIdentifier(Node leftNode) { if (leftNode is IdentifierNode) { return(leftNode as IdentifierNode); } else if (leftNode is IdentifierListNode) { return(GetDefinedIdentifier((leftNode as IdentifierListNode).LeftNode)); } else if (leftNode is FunctionCallNode) { return(null); } else { throw new ArgumentException("Left node type incorrect"); } }
public static string ExtractStatementFromCode(string code, ProtoCore.AST.Node node) { int line = node.line; int col = node.col; int endLine = node.endLine; int endCol = node.endCol; string stmt = ""; if (node is ProtoCore.AST.AssociativeAST.IdentifierNode || node is ProtoCore.AST.AssociativeAST.IdentifierListNode || node is ProtoCore.AST.AssociativeAST.FunctionCallNode || node is ProtoCore.AST.AssociativeAST.RangeExprNode || node is ProtoCore.AST.AssociativeAST.StringNode || node is ProtoCore.AST.AssociativeAST.IntNode || node is ProtoCore.AST.AssociativeAST.DoubleNode || node is ProtoCore.AST.AssociativeAST.BooleanNode || node is ProtoCore.AST.AssociativeAST.ExprListNode) { string[] lines = code.Split('\n'); int lastLine = lines.Length; int lastCol = lines[lastLine - 1].Length; string stmts = ExtractStatementHelper(code, line, col, lastLine, lastCol + 1); string[] induvidualStmts = stmts.Split(';'); stmt = induvidualStmts[0] + ";"; if (induvidualStmts.Length > 1) { foreach (char ch in induvidualStmts[1]) { if (ch == ' ' || ch == '\n' || ch == '\t') { stmt += ch.ToString(); } else { break; } } } return(stmt); } stmt = ExtractStatementHelper(code, line, col, node.endLine, node.endCol); return(stmt); }
private static void InsertPredefinedMethod(Core core, ProtoCore.AST.Node root, bool builtinMethodsLoaded) { if (!builtinMethodsLoaded) { InsertBinaryOperationMethod(core, root, Operator.add, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); InsertBinaryOperationMethod(core, root, Operator.sub, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); InsertBinaryOperationMethod(core, root, Operator.div, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); InsertBinaryOperationMethod(core, root, Operator.mul, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); InsertBinaryOperationMethod(core, root, Operator.mod, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); InsertBinaryOperationMethod(core, root, Operator.eq, PrimitiveType.kTypeBool, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); InsertBinaryOperationMethod(core, root, Operator.nq, PrimitiveType.kTypeBool, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); InsertBinaryOperationMethod(core, root, Operator.ge, PrimitiveType.kTypeBool, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); InsertBinaryOperationMethod(core, root, Operator.gt, PrimitiveType.kTypeBool, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); InsertBinaryOperationMethod(core, root, Operator.le, PrimitiveType.kTypeBool, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); InsertBinaryOperationMethod(core, root, Operator.lt, PrimitiveType.kTypeBool, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); InsertBinaryOperationMethod(core, root, Operator.and, PrimitiveType.kTypeBool, PrimitiveType.kTypeBool, PrimitiveType.kTypeBool); InsertBinaryOperationMethod(core, root, Operator.or, PrimitiveType.kTypeBool, PrimitiveType.kTypeBool, PrimitiveType.kTypeBool); InsertUnaryOperationMethod(core, root, UnaryOperator.Not, PrimitiveType.kTypeBool, PrimitiveType.kTypeBool); InsertUnaryOperationMethod(core, root, UnaryOperator.Neg, PrimitiveType.kTypeVar, PrimitiveType.kTypeVar); } }
internal Statement(ProtoCore.AST.Node astNode) { if (astNode is BinaryExpressionNode) { Initialize((BinaryExpressionNode)astNode); } else if (astNode is ClassDeclNode) { Initialize((ClassDeclNode)astNode); } else if (astNode is FunctionDefinitionNode) { Initialize((FunctionDefinitionNode)astNode); } else { string type = (astNode == null ? "null" : astNode.GetType().ToString()); string message = string.Format("AST Node '{0}' is not supported (C71AB50DA47C)", type); throw new NotImplementedException(message); // @TODO(Done): GUID? } }
/// <summary> /// A node is a single identifier if its type is Identifer and is not array indexed /// /// i.e. identifer: a, foo, _x /// non identifier: a[0], a.b, f(), f(x) /// /// </summary> /// <param name="node"></param> /// <returns></returns> public static bool IsSingleIdentifier(ProtoCore.AST.Node node) { Validity.Assert(null != node); if (node is ProtoCore.AST.AssociativeAST.IdentifierNode) { // Check the identifier is array indexed if (null == (node as ProtoCore.AST.AssociativeAST.IdentifierNode).ArrayDimensions) { // The identifier is not array indexed return(true); } } else if (node is ProtoCore.AST.ImperativeAST.IdentifierNode) { // Check the identifier is array indexed if (null == (node as ProtoCore.AST.ImperativeAST.IdentifierNode).ArrayDimensions) { // The identifier is not array indexed return(true); } } return(false); }
private Statement(Node parsedNode) { StartLine = parsedNode.line; EndLine = parsedNode.endLine; CurrentType = GetStatementType(parsedNode); AstNode = parsedNode; if (parsedNode is BinaryExpressionNode) { //First get all the defined variables while (parsedNode is BinaryExpressionNode) { IdentifierNode assignedVar = GetDefinedIdentifier((parsedNode as BinaryExpressionNode).LeftNode); if (assignedVar != null) { definedVariables.Add(new Variable(assignedVar)); } parsedNode = (parsedNode as BinaryExpressionNode).RightNode; } //Then get the referenced variables List <Variable> refVariableList = new List <Variable>(); GetReferencedVariables(parsedNode, refVariableList); referencedVariables = refVariableList; } else if (parsedNode is FunctionDefinitionNode) { // Handle function definitions in CBN } else { throw new ArgumentException("Must be func def or assignment"); } Variable.SetCorrectColumn(referencedVariables, CurrentType, StartLine); }
public static StatementType GetStatementType(Node astNode) { if (astNode is FunctionDefinitionNode) { return(StatementType.FuncDeclaration); } if (astNode is BinaryExpressionNode) { var currentNode = astNode as BinaryExpressionNode; if (currentNode.Optr != Operator.assign) { throw new ArgumentException(); } if (!(currentNode.LeftNode.Name.StartsWith("temp") && currentNode.LeftNode.Name.Length > 10)) { return(StatementType.Expression); } if (currentNode.RightNode is IdentifierNode) { return(StatementType.AssignmentVar); } if (currentNode.RightNode is ExprListNode) { return(StatementType.Collection); } if (currentNode.RightNode is DoubleNode || currentNode.RightNode is IntNode) { return(StatementType.Literal); } if (currentNode.RightNode is StringNode) { return(StatementType.Literal); } } return(StatementType.None); }
private Statement(Node parsedNode) { StartLine = parsedNode.line; EndLine = parsedNode.endLine; CurrentType = GetStatementType(parsedNode); this.AstNode = parsedNode; if (parsedNode is BinaryExpressionNode) { //First get all the defined variables while (parsedNode is BinaryExpressionNode) { IdentifierNode assignedVar = GetDefinedIdentifier((parsedNode as BinaryExpressionNode).LeftNode); if (assignedVar != null) definedVariables.Add(new Variable(assignedVar)); parsedNode = (parsedNode as BinaryExpressionNode).RightNode; } //Then get the referenced variables List<Variable> refVariableList = new List<Variable>(); GetReferencedVariables(parsedNode, refVariableList); referencedVariables = refVariableList; } else if (parsedNode is FunctionDefinitionNode) { // Handle function definitions in CBN } else throw new ArgumentException("Must be func def or assignment"); Variable.SetCorrectColumn(referencedVariables, CurrentType, StartLine); }
private void MapIdentifiers(Node astNode) { if (astNode == null) { return; } var definedVars = GetDefinedVariableNames(); if (astNode is IdentifierNode) { var identNode = astNode as IdentifierNode; var ident = identNode.Value; if ((inputIdentifiers.Contains(ident) || definedVars.Contains(ident)) && !tempVariables.Contains(ident) && !identNode.Equals(AstIdentifierForPreview)) { identNode.Name = identNode.Value = ident + "_" + this.GUID.ToString().Replace("-", string.Empty); } MapIdentifiers(identNode.ArrayDimensions); } else if (astNode is IdentifierListNode) { var node = astNode as IdentifierListNode; MapIdentifiers(node.LeftNode); MapIdentifiers(node.RightNode); } else if (astNode is FunctionCallNode) { var node = astNode as FunctionCallNode; for (int i = 0; i < node.FormalArguments.Count; ++i) { MapIdentifiers(node.FormalArguments[i]); } MapIdentifiers(node.ArrayDimensions); } else if (astNode is ArrayNode) { var node = astNode as ArrayNode; MapIdentifiers(node.Expr); } else if (astNode is ExprListNode) { var node = astNode as ExprListNode; for (int i = 0; i < node.list.Count; ++i) { MapIdentifiers(node.list[i]); } MapIdentifiers(node.ArrayDimensions); } else if (astNode is FunctionDotCallNode) { var node = astNode as FunctionDotCallNode; } else if (astNode is InlineConditionalNode) { var node = astNode as InlineConditionalNode; MapIdentifiers(node.ConditionExpression); MapIdentifiers(node.TrueExpression); MapIdentifiers(node.FalseExpression); } else if (astNode is RangeExprNode) { var node = astNode as RangeExprNode; MapIdentifiers(node.FromNode); MapIdentifiers(node.ToNode); MapIdentifiers(node.StepNode); MapIdentifiers(node.ArrayDimensions); } else if (astNode is BinaryExpressionNode) { var node = astNode as BinaryExpressionNode; MapIdentifiers(node.LeftNode); MapIdentifiers(node.RightNode); } else { } }
public static StatementType GetStatementType(Node astNode) { if (astNode is FunctionDefinitionNode) return StatementType.FuncDeclaration; if (astNode is BinaryExpressionNode) { var currentNode = astNode as BinaryExpressionNode; if (currentNode.Optr != Operator.assign) throw new ArgumentException(); if (!(currentNode.LeftNode.Name.StartsWith("temp") && currentNode.LeftNode.Name.Length > 10)) return StatementType.Expression; if (currentNode.RightNode is IdentifierNode) return StatementType.AssignmentVar; if (currentNode.RightNode is ExprListNode) return StatementType.Collection; if (currentNode.RightNode is DoubleNode || currentNode.RightNode is IntNode) return StatementType.Literal; if (currentNode.RightNode is StringNode) return StatementType.Literal; } return StatementType.None; }
public static void GetReferencedVariables(Node astNode, List<Variable> refVariableList) { //DFS Search to find all identifier nodes if (astNode == null) return; if (astNode is FunctionCallNode) { var currentNode = astNode as FunctionCallNode; foreach (AssociativeNode node in currentNode.FormalArguments) GetReferencedVariables(node, refVariableList); } else if (astNode is IdentifierNode) { var resultVariable = new Variable(astNode as IdentifierNode); refVariableList.Add(resultVariable); GetReferencedVariables((astNode as IdentifierNode).ArrayDimensions, refVariableList); } else if (astNode is ArrayNode) { var currentNode = astNode as ArrayNode; GetReferencedVariables(currentNode.Expr, refVariableList); GetReferencedVariables(currentNode.Type, refVariableList); } else if (astNode is ExprListNode) { var currentNode = astNode as ExprListNode; foreach (AssociativeNode node in currentNode.list) GetReferencedVariables(node, refVariableList); } else if (astNode is FunctionDotCallNode) { var currentNode = astNode as FunctionDotCallNode; GetReferencedVariables(currentNode.FunctionCall, refVariableList); } else if (astNode is InlineConditionalNode) { var currentNode = astNode as InlineConditionalNode; GetReferencedVariables(currentNode.ConditionExpression, refVariableList); GetReferencedVariables(currentNode.TrueExpression, refVariableList); GetReferencedVariables(currentNode.FalseExpression, refVariableList); } else if (astNode is RangeExprNode) { var currentNode = astNode as RangeExprNode; GetReferencedVariables(currentNode.FromNode, refVariableList); GetReferencedVariables(currentNode.ToNode, refVariableList); GetReferencedVariables(currentNode.StepNode, refVariableList); } else if (astNode is BinaryExpressionNode) { var currentNode = astNode as BinaryExpressionNode; GetReferencedVariables(currentNode.RightNode, refVariableList); } else { //Its could be something like a literal //Or node not completely implemented YET } }
public static IdentifierNode GetDefinedIdentifier(Node leftNode) { if (leftNode is IdentifierNode) return leftNode as IdentifierNode; else if (leftNode is IdentifierListNode) return GetDefinedIdentifier((leftNode as IdentifierListNode).LeftNode); else if (leftNode is FunctionCallNode) return null; else throw new ArgumentException("Left node type incorrect"); }
public static Statement CreateInstance(Node parsedNode) { if (parsedNode == null) throw new ArgumentNullException(); return new Statement(parsedNode); }
private static IdentifierNode GetDefinedIdentifier(Node leftNode) { if(leftNode is TypedIdentifierNode) return new IdentifierNode(leftNode as IdentifierNode); if (leftNode is IdentifierNode) { var identiferNode = leftNode as IdentifierNode; if (identiferNode.ArrayDimensions != null) return null; else return identiferNode; } else if (leftNode is IdentifierListNode || leftNode is FunctionCallNode) return null; else throw new ArgumentException("Left node type incorrect"); }
private static IdentifierNode GetDefinedIdentifier(Node leftNode) { if(leftNode is TypedIdentifierNode) return new IdentifierNode(leftNode as IdentifierNode); if (leftNode is IdentifierNode) return leftNode as IdentifierNode; else if (leftNode is IdentifierListNode || leftNode is FunctionCallNode) return null; else throw new ArgumentException("Left node type incorrect"); }