/// <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 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++; } } } } }