Exemplo n.º 1
0
        private void Visit(ASTNode node)
        {
            if (node == null)
            {
                return;
            }

            if (node.GetType() == typeof(ASTDeclareNode))
            {
                _VisitDeclareNode(node as ASTDeclareNode);
            }
            else if (node.GetType() == typeof(ASTVarNode))
            {
                _VisitVarNode(node as ASTVarNode);
            }
            else if (node.GetType() == typeof(ASTCompoundNode))
            {
                foreach (ASTNode n in (node as ASTCompoundNode).Children)
                {
                    Visit(n);
                }
            }
            else
            {
                Visit(node.LeftChild);
                Visit(node.RightChild);
            }
        }
Exemplo n.º 2
0
        private object _Visit(ASTNode node)
        {
            if (node.GetType() == typeof(ASTBinopNode))
            {
                return(_BinOpVisit(node));
            }
            else if (node.GetType() == typeof(ASTDeclareNode))
            {
                _VisitDeclare(node as ASTDeclareNode);
                return(0);
            }
            else if (node.GetType() == typeof(ASTUnaryNode))
            {
                return(_UnOpVisit(node as ASTUnaryNode));
            }
            else if (node.GetType() == typeof(ASTCompoundNode))
            {
                _CompoundVisit(node as ASTCompoundNode);
                return(0);
            }
            else if (node.GetType() == typeof(ASTAssignNode))
            {
                _VisitAssign(node as ASTAssignNode);
                return(0);
            }
            else if (node.GetType() == typeof(ASTVarNode))
            {
                if (!GLOBAL_SCOPE.ContainsKey(node.Value.Value))
                {
                    throw new KeyNotFoundException("Variable does not exist! " + node.Value.Value);
                }
                else
                {
                    return(GLOBAL_SCOPE[node.Value.Value]);
                }
            }
            else if (node.GetType() == typeof(ASTNoopNode))
            {
                return(0);
            }
            else if (node.GetType() == typeof(ASTNumNode))
            {
                if (node.Value.Type == TokenType.INT_CONST)
                {
                    return(Int32.Parse(node.Value.Value));
                }
                if (node.Value.Type == TokenType.FLOAT_CONST)
                {
                    return(float.Parse(node.Value.Value));
                }
            }

            throw new Exception("Unexpected node type" + node.GetType());
        }