private OperationType HandleConstant(ParseContext context) { IRValueNode constant = Expression.CreateConstant(context); _operands.Push(constant); return(OperationType.Operand); }
private bool ParseAssignment(string text, out IRValueNode leftOperand, out IRValueNode rightOperand) { leftOperand = null; rightOperand = null; // Parse the expression var ast = RParser.Parse(text); if (ast.Errors.Count == 0) { // Expected 'Variable <- Expression' var scope = ast.Children[0] as GlobalScope; if (scope?.Children.Count > 0) { var exp = (scope.Children[0] as IExpressionStatement)?.Expression; if (exp?.Children.Count == 1) { var op = exp.Children[0] as IOperator; if (op != null) { if (op.OperatorType == OperatorType.LeftAssign && op.LeftOperand != null && op.RightOperand != null) { leftOperand = op.LeftOperand; rightOperand = op.RightOperand; return(true); } } } } } return(false); }
public RObject Evaluate(IAstNode node) { IRValueNode rValue = node as IRValueNode; if (rValue == null) { return(RNull.Null); } return(RNull.Null); }
/// <summary> /// Constructs AST node from operator (root) and one or two operands. /// In order for the node to be successfully created stacks must contain /// an operator and, depending on the operator, one or two operands. /// </summary> /// <example> /// The newly created subtree (operator and root and operands are children) /// is then pushed into the operands stack. Example: in a*b+c before '+' /// can be processed, a*b is turned into an subtree and pushed as an operand /// to the operands stack. Then new subtree can be created with + at the root /// and 'c' and 'a*b' as its child nodes. /// </example> /// <param name="context">Parsing context</param> /// <returns>Parsing error of any</returns> private ParseErrorType MakeNode(ParseContext context) { IOperator operatorNode = _operators.Pop(); IRValueNode rightOperand = this.SafeGetOperand(); if (rightOperand == null) { // Oddly, no operands return(ParseErrorType.RightOperandExpected); } if (operatorNode.IsUnary) { operatorNode.AppendChild(rightOperand); operatorNode.RightOperand = rightOperand; } else { IRValueNode leftOperand = this.SafeGetOperand(); if (leftOperand == null) { // Operand is missing in expression like x <- []. // Operator on top of the stack is <- since [] was not // successfully parsed. So we need to mark right operand // as error token. context.AddError(new ParseError(ParseErrorType.LeftOperandExpected, ErrorLocation.Token, GetIndexerOrFunctionErrorRange(context, operatorNode))); return(ParseErrorType.LeftOperandExpected); } if (leftOperand.End <= operatorNode.Start && rightOperand.Start >= operatorNode.End) { operatorNode.LeftOperand = leftOperand; operatorNode.RightOperand = rightOperand; operatorNode.AppendChild(leftOperand); operatorNode.AppendChild(rightOperand); } else { return(ParseErrorType.UnexpectedToken); } } _operands.Push(operatorNode); return(ParseErrorType.None); }
private ParseErrorType HandleFunctionOrIndexer(IOperator operatorNode) { // Indexing or function call is performed on the topmost operand which // generally should be a variable or a node that evaluates to it. // However, we leave syntax check to separate code. IRValueNode operand = this.SafeGetOperand(); if (operand == null) { // Oddly, no operand return(ParseErrorType.IndentifierExpected); } operatorNode.LeftOperand = operand; operatorNode.AppendChild(operand); _operands.Push(operatorNode); return(ParseErrorType.None); }
private static IRValueNode CreateConstant(ParseContext context) { var tokens = context.Tokens; var currentToken = tokens.CurrentToken; IRValueNode term = null; switch (currentToken.TokenType) { case RTokenType.NaN: case RTokenType.Infinity: case RTokenType.Number: term = new NumericalValue(); break; case RTokenType.Complex: term = new ComplexValue(); break; case RTokenType.Logical: term = new LogicalValue(); break; case RTokenType.String: term = new StringValue(); break; case RTokenType.Null: term = new NullValue(); break; case RTokenType.Missing: term = new MissingValue(); break; } Debug.Assert(term != null); term.Parse(context, null); return(term); }
private bool ParseAssignment(string text, out IRValueNode leftOperand, out IRValueNode rightOperand) { leftOperand = null; rightOperand = null; // Parse the expression var ast = RParser.Parse(text); if (ast.Errors.Count == 0) { // Expected 'Variable <- Expression' var scope = ast.Children[0] as GlobalScope; if (scope?.Children.Count > 0) { var exp = (scope.Children[0] as IExpressionStatement)?.Expression; if (exp?.Children.Count == 1) { var op = exp.Children[0] as IOperator; if (op != null) { if (op.OperatorType == OperatorType.LeftAssign && op.LeftOperand != null && op.RightOperand != null) { leftOperand = op.LeftOperand; rightOperand = op.RightOperand; return true; } } } } } return false; }