public void ReproMAGN3603() { string code = @"a = 1 + (2 * 3); b = (1 + 2) * 3; c = 1 + 2 * 3;"; ElementResolver elementResolver = new ElementResolver(); ParseParam parseParam = new ParseParam(Guid.NewGuid(), code, elementResolver); Assert.IsTrue(CompilerUtils.PreCompileCodeBlock(thisTest.CreateTestCore(), ref parseParam)); Assert.IsTrue(parseParam.ParsedNodes != null && parseParam.ParsedNodes.Count() > 0); var parsedNode = parseParam.ParsedNodes.ElementAt(0); BinaryExpressionNode n = parsedNode as BinaryExpressionNode; FunctionCallNode funcCall = n.RightNode as FunctionCallNode; Assert.IsTrue(n != null && funcCall != null); IdentifierNode identNode = funcCall.Function as IdentifierNode; Assert.IsTrue(identNode != null && identNode.Value == "%add"); var args = funcCall.FormalArguments; Assert.IsTrue(args.Count == 2); Assert.IsTrue(args[0] is IntNode); FunctionCallNode nestedFuncCall = args[1] as FunctionCallNode; Assert.IsTrue(nestedFuncCall != null && (nestedFuncCall.Function as IdentifierNode).Value == "%mul"); parsedNode = parseParam.ParsedNodes.ElementAt(1); n = parsedNode as BinaryExpressionNode; funcCall = n.RightNode as FunctionCallNode; Assert.IsTrue(n != null && funcCall != null); identNode = funcCall.Function as IdentifierNode; Assert.IsTrue(identNode != null && identNode.Value == "%mul"); args = funcCall.FormalArguments; Assert.IsTrue(args.Count == 2); Assert.IsTrue(args[1] is IntNode); nestedFuncCall = args[0] as FunctionCallNode; Assert.IsTrue(nestedFuncCall != null && (nestedFuncCall.Function as IdentifierNode).Value == "%add"); parsedNode = parseParam.ParsedNodes.ElementAt(2); n = parsedNode as BinaryExpressionNode; funcCall = n.RightNode as FunctionCallNode; Assert.IsTrue(n != null && funcCall != null); identNode = funcCall.Function as IdentifierNode; Assert.IsTrue(identNode != null && identNode.Value == "%add"); args = funcCall.FormalArguments; Assert.IsTrue(args.Count == 2); Assert.IsTrue(args[0] is IntNode); nestedFuncCall = args[1] as FunctionCallNode; Assert.IsTrue(nestedFuncCall != null && (nestedFuncCall.Function as IdentifierNode).Value == "%mul"); }
public void TestUnboundIdentifierInUnnamedSignedExpression() { string code = @"a*-1;"; ElementResolver elementResolver = new ElementResolver(); ParseParam parseParam = new ParseParam(Guid.NewGuid(), code, elementResolver); Assert.IsTrue(CompilerUtils.PreCompileCodeBlock(thisTest.CreateTestCore(), ref parseParam)); Assert.IsTrue(parseParam.ParsedNodes != null && parseParam.ParsedNodes.Any()); var inputIdentifier = parseParam.UnboundIdentifiers; Assert.AreEqual(1, inputIdentifier.Count); Assert.AreEqual("a", inputIdentifier.ElementAt(0).Value); }
public void TestUnboundIdentifierInBinaryExpression() { var binaryExpressions = new[] { "x==-0.5;", "x>0.5;", "x<-1;", "x!=1;", "x<=\"a\";", "x>='a';", "x==true;", "x!=false;", "x==null;" }; foreach (var expression in binaryExpressions) { ElementResolver elementResolver = new ElementResolver(); ParseParam parseParam = new ParseParam(Guid.NewGuid(), expression, elementResolver); Assert.IsTrue(CompilerUtils.PreCompileCodeBlock(thisTest.CreateTestCore(), ref parseParam)); Assert.IsTrue(parseParam.ParsedNodes != null && parseParam.ParsedNodes.Any()); var inputIdentifier = parseParam.UnboundIdentifiers; Assert.AreEqual(1, inputIdentifier.Count); Assert.AreEqual("x", inputIdentifier.ElementAt(0).Value); } }
/// <summary> /// Pre-compiles Design script code in code block node. /// </summary> /// <param name="parseParams">Container for compilation related parameters</param> /// <returns>true if code compilation succeeds, false otherwise</returns> public bool PreCompileCodeBlock(ref ParseParam parseParams) { return(CompilerUtils.PreCompileCodeBlock(compilationCore, ref parseParams, priorNames)); }
private void ProcessCode(ref string errorMessage, ref string warningMessage, ElementResolver workspaceElementResolver = null) { code = CodeBlockUtils.FormatUserText(code); codeStatements.Clear(); if (string.IsNullOrEmpty(Code)) { previewVariable = null; } try { // During loading of CBN from file, the elementResolver from the workspace is unavailable // in which case, a local copy of the ER obtained from the CBN is used var resolver = workspaceElementResolver ?? this.ElementResolver; var parseParam = new ParseParam(GUID, code, resolver); if (CompilerUtils.PreCompileCodeBlock(libraryServices.LibraryManagementCore, ref parseParam)) { if (parseParam.ParsedNodes != null) { // Create an instance of statement for each code statement written by the user foreach (var parsedNode in parseParam.ParsedNodes) { // Create a statement variable from the generated nodes codeStatements.Add(Statement.CreateInstance(parsedNode)); } SetPreviewVariable(parseParam.ParsedNodes); } } if (parseParam.Errors.Any()) { errorMessage = string.Join("\n", parseParam.Errors.Select(m => m.Message)); ProcessError(); CreateInputOutputPorts(); return; } if (parseParam.Warnings != null) { // Unbound identifiers in CBN will have input slots. // // To check function redefinition, we need to check other // CBN to find out if it has been defined yet. Now just // skip this warning. var warnings = parseParam.Warnings.Where( w => w.ID != WarningID.kIdUnboundIdentifier && w.ID != WarningID.kFunctionAlreadyDefined); if (warnings.Any()) { warningMessage = string.Join("\n", warnings.Select(m => m.Message)); } } if (parseParam.UnboundIdentifiers != null) { inputIdentifiers = new List <string>(); inputPortNames = new List <string>(); foreach (var kvp in parseParam.UnboundIdentifiers) { inputIdentifiers.Add(kvp.Value); inputPortNames.Add(kvp.Key); } } else { inputIdentifiers.Clear(); inputPortNames.Clear(); } } catch (Exception e) { errorMessage = e.Message; previewVariable = null; ProcessError(); return; } // Set the input and output ports based on the statements CreateInputOutputPorts(); }
public bool TryParseCode(ref ParseParam parseParam) { return(CompilerUtils.PreCompileCodeBlock(libraryCore, ref parseParam)); }
/// <summary> /// Pre-compiles Design script code in code block node. /// </summary> /// <param name="parseParams">Container for compilation related parameters</param> /// <returns>true if code compilation succeeds, false otherwise</returns> internal bool PreCompileCodeBlock(ParseParam parseParams) { return(CompilerUtils.PreCompileCodeBlock(compilationCore, parseParams, priorNames)); }