public override void ExitIfStatement([NotNull] CmanParser.IfStatementContext context) { ASTBodyStatementNode elseBody = null; ASTBodyStatementNode trueBody = null; IASTExprNode condition = null; if (_nodes.Peek() is ASTBodyStatementNode) { var tmp = _nodes.Pop(); if (_nodes.Peek() is ASTBodyStatementNode) { elseBody = (ASTBodyStatementNode)tmp; trueBody = (ASTBodyStatementNode)_nodes.Pop(); condition = (IASTExprNode)_nodes.Pop(); } else { trueBody = (ASTBodyStatementNode)tmp; condition = (IASTExprNode)_nodes.Pop(); } } else { condition = (IASTExprNode)_nodes.Pop(); //((ASTIfStatementNode)_nodes.Peek()).Condition = condition; } //TODO: ifnode не привязывает к себе ноды var ifNode = (ASTIfStatementNode)_nodes.Peek(); ifNode.Condition = condition; ifNode.TrueBody = trueBody; ifNode.ElseBody = elseBody; return; }
public override void ExitReturnStatement([NotNull] CmanParser.ReturnStatementContext context) { if (_nodes.Peek() is IASTExprNode) { IASTExprNode expr = (IASTExprNode)_nodes.Pop(); ((ASTReturnStatementNode)_nodes.Peek()).Expression = expr; } }
public void AddExpression(IASTExprNode e) { if (Expressions == null) { Expressions = new List <IASTExprNode>(); } ((ASTNode)e).Parent = this; Expressions.Insert(0, e); }
public static bool IsValuable(IASTExprNode expr) { switch (expr) { case IASTLiteral litNode: return(true); case ASTNotOpNode notOpNode: return(IsValuable(notOpNode.Expression)); } return(false); }
public override void ExitWhileStatement([NotNull] CmanParser.WhileStatementContext context) { ASTWhileStatementNode whileStmtNode; ASTBodyStatementNode bodyNode = null; if (_nodes.Peek() is ASTBodyStatementNode) { bodyNode = (ASTBodyStatementNode)_nodes.Pop(); } IASTExprNode condNode = (IASTExprNode)_nodes.Pop(); whileStmtNode = (ASTWhileStatementNode)_nodes.Peek(); whileStmtNode.Body = bodyNode; whileStmtNode.Condition = condNode; }
//Some helpers /// <summary> /// Check expression for rvalue or lvalue /// </summary> /// <param name="expr">Expression for check</param> /// <returns>Expression is rvalue or not</returns> private bool IsRvalue(IASTExprNode expr) { if (expr is ASTVariableNode) { return(false); } if (expr is ASTIndexOpNode) { return(false); } if (expr is ASTCallStatementNode) { return(false); } return(true); }
private void CheckImplicitCast(IASTExprNode expr, Type type) { var exprType = ASTExprHelper.GetExpressionType(expr); if (exprType != type) { var tmp = (ASTNode)expr; _messages.Add(new MessageRecord( MsgCode.ImplicitCast, tmp.SourcePath, tmp.StartLine, tmp.StartPos, exprType, type )); } }
private void CheckExpression(IASTExprNode exprNode) { if (exprNode is ASTVariableNode varNode) { CheckVariable(varNode); } if (exprNode is ASTCallStatementNode callNode) { CheckSubCall(callNode, true); } if (exprNode is IASTBinOpNode binOpNode) { CheckBinOp(binOpNode); } if (exprNode is IASTUnarOpNode unarOpNode) { CheckUnarOp(unarOpNode); } }
/// <summary> /// Builds concrete expression /// </summary> /// <param name="exprNode">Expression node</param> private void BuildExpression(IASTExprNode exprNode) { if (exprNode is IASTLiteral literalNode) { BuildLiteral(literalNode); } if (exprNode is ASTVariableNode varNode) { BuildVariable(varNode); } if (exprNode is ASTIndexOpNode indexNode) { if (indexNode.Expression is ASTIndexOpNode subIndexOp) { BuildExpression((IASTExprNode)subIndexOp); } if (indexNode.Expression is ASTVariableNode indexVarNode) { BuildVariable(indexVarNode); } BuildExpression(indexNode.Index); _emitter.Box(); _emitter.VirtualCall(typeof(Dictionary <object, object>), "get_Item", new Type[] { typeof(object) }); } if (exprNode is ASTCallStatementNode callNode) { BuildCallStatement(callNode); } if (exprNode is IASTBinOpNode binOpNode) { BuildBinOpExpr(binOpNode); } //TODO: unary operators if (exprNode is ASTMinusOpNode minusNode) { BuildMinusOpExpr(minusNode); } if (exprNode is ASTNotOpNode notNode) { BuildNotOpExpr(notNode); } }
public static Type GetExpressionType(IASTExprNode expr) { switch (expr) { case IASTLiteral litNode: return(GetLiteralType(litNode)); case IASTBinOpNode binOpNode: return(GetBinOpType(binOpNode)); case IASTUnarOpNode unarOpNode: return(GetUnarOpType(unarOpNode)); case ASTCallStatementNode callNode: return(typeof(object)); case ASTVariableNode varNode: return(typeof(object)); } return(null); }
public override void ExitForStatement([NotNull] CmanParser.ForStatementContext context) { ASTBodyStatementNode body = null; IASTExprNode step = null; IASTExprNode cond = null; ASTNode counter = null; Queue <ASTNode> childs = new Queue <ASTNode>(); while (!(_nodes.Peek() is ASTForStatementNode)) { childs.Enqueue(_nodes.Pop()); //childs.Push(_nodes.Pop()); } //has body if (childs.Peek() is ASTBodyStatementNode) { body = (ASTBodyStatementNode)childs.Dequeue(); } int childCount = childs.Count(); //counter, condition, step if (childCount == 3) { step = (IASTExprNode)childs.Dequeue(); } cond = (IASTExprNode)childs.Dequeue(); counter = childs.Dequeue(); ASTForStatementNode forNode = (ASTForStatementNode)_nodes.Peek(); forNode.Counter = counter; forNode.Condition = cond; forNode.Step = step; forNode.Body = body; }
public static object GetValue(IASTExprNode expr) { switch (expr) { case ASTNotOpNode notOpNode: return(!Convert.ToBoolean(GetValue(notOpNode.Expression))); case ASTMinusOpNode minusOpNode: return(decimal.Negate((decimal)GetValue(minusOpNode.Expression))); case ASTNumberLiteralNode numNode: return(Convert.ToDecimal(numNode.Value)); case ASTStringLiteralNode strNode: return(strNode.Value); case ASTNullLiteralNode nullNode: return(null); case ASTBoolLiteralNode boolNode: return(boolNode.Value); } return(null); }