private static IEnumerable <AST.Node> ParseNonAssignments(Core core, string expression, string postfixGuid) { List <string> compiled = new List <string>(); string[] expr = GetStatementsString(expression); foreach (string s in expr) { compiled.Add(s); } for (int i = 0; i < compiled.Count(); i++) { if (compiled[i].StartsWith("\n")) { string newlines = string.Empty; int lastPosButOne = 0; string original = compiled[i]; for (int j = 0; j < original.Length; j++) { if (!original[j].Equals('\n')) { lastPosButOne = j; break; } else { newlines += original[j]; } } string newStatement = original.Substring(lastPosButOne); if (!IsNotAssigned(newStatement)) { string name = string.Format("temp_{0}_{1}", i, postfixGuid); newStatement = name + " = " + newStatement; } compiled[i] = newlines + newStatement; } else { if (!IsNotAssigned(compiled[i])) { string name = string.Format("temp_{0}_{1}", i, postfixGuid); compiled[i] = name + " = " + compiled[i]; } } } StringBuilder newCode = new StringBuilder(); compiled.ForEach(x => newCode.Append(x)); try { ProtoCore.AST.Node codeBlockNode = ProtoCore.Utils.ParserUtils.ParseWithCore(newCode.ToString(), core); return(ParserUtils.GetAstNodes(codeBlockNode)); } catch (Exception) { return(new List <ProtoCore.AST.Node>()); } }
private static void ParseUserCodeCore(Core core, string expression, out List <AssociativeNode> astNodes, out List <AssociativeNode> commentNodes) { astNodes = new List <AssociativeNode>(); core.ResetForPrecompilation(); core.IsParsingCodeBlockNode = true; core.ParsingMode = ParseMode.AllowNonAssignment; ParseResult parseResult = ParserUtils.ParseWithCore(expression, core); commentNodes = ParserUtils.GetAstNodes(parseResult.CommentBlockNode); var nodes = ParserUtils.GetAstNodes(parseResult.CodeBlockNode); Validity.Assert(nodes != null); int index = 0; int typedIdentIndex = 0; foreach (var node in nodes) { var n = node as AssociativeNode; Validity.Assert(n != null); // Append the temporaries only if it is not a function def or class decl bool isFunctionOrClassDef = n is FunctionDefinitionNode; if (n is ImportNode) { core.BuildStatus.LogSemanticError(Resources.ImportStatementNotSupported); } else if (n is ClassDeclNode) { core.BuildStatus.LogSemanticError(Resources.ClassDeclarationNotSupported); } else if (isFunctionOrClassDef) { // Add node as it is astNodes.Add(node); } else { // Handle temporary naming for temporary Binary exp. nodes and non-assignment nodes var ben = node as BinaryExpressionNode; if (ben != null && ben.Optr == Operator.assign) { var lNode = ben.LeftNode as IdentifierNode; if (lNode != null && lNode.Value == Constants.kTempProcLeftVar) { string name = Constants.kTempVarForNonAssignment + index; var newNode = new BinaryExpressionNode(new IdentifierNode(name), ben.RightNode); astNodes.Add(newNode); index++; } else { // Add node as it is astNodes.Add(node); index++; } } else { if (node is TypedIdentifierNode) { // e.g. a : int = %tTypedIdent_<Index>; var ident = new IdentifierNode(Constants.kTempVarForTypedIdentifier + typedIdentIndex); NodeUtils.CopyNodeLocation(ident, node); var typedNode = new BinaryExpressionNode(node as TypedIdentifierNode, ident, Operator.assign); NodeUtils.CopyNodeLocation(typedNode, node); astNodes.Add(typedNode); typedIdentIndex++; } else { string name = Constants.kTempVarForNonAssignment + index; var newNode = new BinaryExpressionNode(new IdentifierNode(name), n); astNodes.Add(newNode); index++; } } } } }
private static IEnumerable <AST.Node> ParseUserCodeCore(Core core, string expression, string postfixGuid, ref bool parseSuccess) { List <ProtoCore.AST.Node> astNodes = new List <ProtoCore.AST.Node>(); core.ResetForPrecompilation(); core.IsParsingCodeBlockNode = true; core.ParsingMode = ParseMode.AllowNonAssignment; ProtoCore.AST.Node codeBlockNode = ProtoCore.Utils.ParserUtils.ParseWithCore(expression, core); parseSuccess = true; List <ProtoCore.AST.Node> nodes = ParserUtils.GetAstNodes(codeBlockNode); Validity.Assert(nodes != null); int index = 0; foreach (var node in nodes) { ProtoCore.AST.AssociativeAST.AssociativeNode n = node as ProtoCore.AST.AssociativeAST.AssociativeNode; ProtoCore.Utils.Validity.Assert(n != null); // Append the temporaries only if it is not a function def or class decl bool isFunctionOrClassDef = n is FunctionDefinitionNode || n is ClassDeclNode; // Handle non Binary expression nodes separately if (n is ProtoCore.AST.AssociativeAST.ModifierStackNode) { core.BuildStatus.LogSemanticError("Modifier Blocks are not supported currently."); } else if (n is ProtoCore.AST.AssociativeAST.ImportNode) { core.BuildStatus.LogSemanticError("Import statements are not supported in CodeBlock Nodes."); } else if (isFunctionOrClassDef) { // Add node as it is astNodes.Add(node); } else { // Handle temporary naming for temporary Binary exp. nodes and non-assignment nodes BinaryExpressionNode ben = node as BinaryExpressionNode; if (ben != null && ben.Optr == ProtoCore.DSASM.Operator.assign) { ModifierStackNode mNode = ben.RightNode as ModifierStackNode; if (mNode != null) { core.BuildStatus.LogSemanticError("Modifier Blocks are not supported currently."); } IdentifierNode lNode = ben.LeftNode as IdentifierNode; if (lNode != null && lNode.Value == ProtoCore.DSASM.Constants.kTempProcLeftVar) { string name = string.Format("temp_{0}_{1}", index++, postfixGuid); BinaryExpressionNode newNode = new BinaryExpressionNode(new IdentifierNode(name), ben.RightNode); astNodes.Add(newNode); } else { // Add node as it is astNodes.Add(node); } } else { // These nodes are non-assignment nodes string name = string.Format("temp_{0}_{1}", index++, postfixGuid); BinaryExpressionNode newNode = new BinaryExpressionNode(new IdentifierNode(name), n); astNodes.Add(newNode); } } } return(astNodes); }