Esempio n. 1
0
 public void CheckLabel(ASTNodeType label)
 {
     if (!GetLabel().Equals(label))
     {
         throw new Exception("Expected label " + label.ToString() + " but instead have label " + GetLabel().ToString());
     }
 }
 // Default constructor. Initializes data members.
 public ASTNode()
 {
     nodeType        = ASTNodeType.ASTTYPE_NONE;
     leftMostSibling = this;
     leftMostChild   = null;
     rightSibling    = null;
     parent          = null;
 }
Esempio n. 3
0
        public static UnaryExpressionNode UnaryExpressionNode(ASTNodeType nodeType, ASTNode expr)
        {
            UnaryExpressionNode unaryNode = s_UnaryNodePool.Get();

            unaryNode.type       = nodeType;
            unaryNode.expression = expr;
            return(unaryNode);
        }
Esempio n. 4
0
        /*
         * Converts convertedNode to the type T and returns the result.
         * If conversion fails then throws a VisitorException.
         * The exception contains an error message and attaches errorNode or
         * if not given then convertedNode as the source of the error.
         */
        protected T As <T>(ASTNode convertedNode, ASTNodeType expectedtype, ASTNode errorNode = null) where T : ASTNode
        {
            var converted = convertedNode as T;

            if (converted != null)
            {
                return(converted);
            }
            throw new VisitorException(string.Format("expected type {0}, got {1}", expectedtype, convertedNode.type), errorNode ?? convertedNode);
        }
Esempio n. 5
0
        /*
         * Asserts node to be of type T.
         * If conversion fails then throws a VisitorException.
         * The exception contains an error message and attaches errorNode or
         * if not given then node as the source of the error.
         */
        protected void Expect <T>(ASTNode node, ASTNodeType expectedtype, ASTNode errorNode = null) where T : ASTNode
        {
            var converted = node as T;

            if (converted != null)
            {
                return;
            }
            throw new VisitorException(string.Format("expected type {0}, got {1}", expectedtype, node.type), errorNode ?? node);
        }
        private bool CompareOperands(ASTNodeType op, IComparable lhs, IComparable rhs)
        {
            if (lhs.GetType() != rhs.GetType())
            {
                // Different types. Try to convert one to match the other.

                if (rhs is double)
                {
                    // Special case for rhs doubles because we don't want to lose precision.
                    lhs = (double)lhs;
                }
                else if (rhs is bool)
                {
                    // Special case for rhs bools because we want to down-convert the lhs.
                    var tmp = Convert.ChangeType(lhs, typeof(bool));
                    lhs = (IComparable)tmp;
                }
                else
                {
                    var tmp = Convert.ChangeType(rhs, lhs.GetType());
                    rhs = (IComparable)tmp;
                }
            }

            try
            {
                // Evaluate the whole expression
                switch (op)
                {
                case ASTNodeType.EQUAL:
                    return(lhs.CompareTo(rhs) == 0);

                case ASTNodeType.NOT_EQUAL:
                    return(lhs.CompareTo(rhs) != 0);

                case ASTNodeType.LESS_THAN:
                    return(lhs.CompareTo(rhs) < 0);

                case ASTNodeType.LESS_THAN_OR_EQUAL:
                    return(lhs.CompareTo(rhs) <= 0);

                case ASTNodeType.GREATER_THAN:
                    return(lhs.CompareTo(rhs) > 0);

                case ASTNodeType.GREATER_THAN_OR_EQUAL:
                    return(lhs.CompareTo(rhs) >= 0);
                }
            }
            catch (ArgumentException e)
            {
                throw new RunTimeError(null, e.Message);
            }

            throw new RunTimeError(null, "Unknown operator in expression");
        }
Esempio n. 7
0
 public OperatorDeclaration(ASTNodeType type, String keyword,
                            bool delim, CodeBody body, VariableType returnType,
                            List <Specifier> specs, SourcePosition start, SourcePosition end)
     : base(type, start, end)
 {
     OperatorKeyword = keyword;
     isDelimiter     = delim;
     Body            = body;
     ReturnType      = returnType;
     Specifiers      = specs;
     Locals          = new List <VariableDeclaration>();
 }
Esempio n. 8
0
 public OperatorDeclaration(ASTNodeType type, String keyword, 
     bool delim, CodeBody body, VariableType returnType,
     List<Specifier> specs, SourcePosition start, SourcePosition end)
     : base(type, start, end)
 {
     OperatorKeyword = keyword;
     isDelimiter = delim;
     Body = body;
     ReturnType = returnType;
     Specifiers = specs;
     Locals = new List<VariableDeclaration>();
 }
Esempio n. 9
0
 public ASTNode(ASTNodeType type, string lexeme, ASTNode parent)
 {
     Type = type;
     if (lexeme != null)
     {
         Lexeme = lexeme;
     }
     else
     {
         Lexeme = "";
     }
     Parent = parent;
 }
Esempio n. 10
0
        public ASTNode Push(ASTNodeType type, string lexeme, int lineNumber)
        {
            var node = new ASTNode(type, lexeme, this, lineNumber);

            if (_children == null)
            {
                _children = new LinkedList <ASTNode>();
            }

            node._node = _children.AddLast(node);

            return(node);
        }
Esempio n. 11
0
        public RubyAstNode(PegAstNode node)
        {
            if (node.GetLabel() != null)
            {
                mLabel = (ASTNodeType)node.GetLabel();
            }
            else
            {
                mLabel = ASTNodeType.SELF;
            }

            mText = node.ToString();
        }
Esempio n. 12
0
 public ASTNode(ASTNodeType type, string lexeme, ASTNode parent, int lineNumber)
 {
     Type = type;
     if (lexeme != null)
     {
         Lexeme = lexeme;
     }
     else
     {
         Lexeme = "";
     }
     Parent     = parent;
     LineNumber = lineNumber;
 }
Esempio n. 13
0
 private static void ParseValue(ASTNode node, string lexeme, ASTNodeType typeDefault)
 {
     if (lexeme.StartsWith("0x"))
     {
         node.Push(ASTNodeType.SERIAL, lexeme, _curLine);
     }
     else if (int.TryParse(lexeme, out _))
     {
         node.Push(ASTNodeType.INTEGER, lexeme, _curLine);
     }
     else if (double.TryParse(lexeme, out _))
     {
         node.Push(ASTNodeType.DOUBLE, lexeme, _curLine);
     }
     else
     {
         node.Push(typeDefault, lexeme, _curLine);
     }
 }
Esempio n. 14
0
 public void AddParameterType(ASTNodeType parameterType)
 {
     parameterType.SetParent(this);
     this.parameters.Add(parameterType);
 }
Esempio n. 15
0
 protected Statement(ASTNodeType type, SourcePosition start, SourcePosition end)
     : base(type, start, end)
 {
 }
Esempio n. 16
0
 public SimpleASTNode(ASTNodeType nodeType, String text)
 {
     this.nodeType = nodeType;
     this.text     = text;
 }
Esempio n. 17
0
 public void AddParameterType(ASTNodeType parameterType)
 {
     parameterType.SetParent(this);
     this.parameters.Add(parameterType);
 }
Esempio n. 18
0
 public Expression(ASTNodeType type, SourcePosition start, SourcePosition end)
     : base(type, start, end)
 {
 }
 public IntExprASTNode(ASTNodeType type)
     : base(type)
 {
 }
Esempio n. 20
0
 public ExprASTNode(ASTNodeType nodeType)
     : base(nodeType)
 {
 }
Esempio n. 21
0
 public void SetTypeNode(ASTNodeType type)
 {
     type.SetParent(this);
     this.AddSpan(type.GetSpan());
     this.type = type;
 }
Esempio n. 22
0
 public void SetTypeNode(ASTNodeType type)
 {
     type.SetParent(this);
     this.AddSpan(type.GetSpan());
     this.type = type;
 }
Esempio n. 23
0
 public Statement(ASTNodeType type,SourcePosition start, SourcePosition end)
     : base(type, start, end)
 {
 }
Esempio n. 24
0
 public void SetReturnTypeNode(ASTNodeType retType)
 {
     retType.SetParent(this);
     this.AddSpan(retType.GetSpan());
     this.returnType = retType;
 }
Esempio n. 25
0
 // Set type constructor.
 public ASTNode(ASTNodeType type)
     : this()
 {
     this.nodeType = type;
 }
 // Set constructor
 public StatementASTNode(ASTNodeType nodeType)
     : base(nodeType)
 {
 }
Esempio n. 27
0
 public ASTVariableNode(ASTNodeType type) : base(type)
 {
 }
Esempio n. 28
0
 public ASTNode(ASTNodeType type, Lexeme lexeme = null)
 {
     this.type   = type;
     this.lexeme = lexeme;
 }
Esempio n. 29
0
 public void SetTypeNode(ASTNodeType type)
 {
     type.SetParent(this);
     this.type = type;
 }
Esempio n. 30
0
 public void SetTypeNode(ASTNodeType type)
 {
     type.SetParent(this);
     this.type = type;
 }
Esempio n. 31
0
 public ASTNode(ASTNodeType type, SourcePosition start, SourcePosition end)
 {
     Type = type;
     StartPos = start; EndPos = end;
 }
Esempio n. 32
0
 public ASTNode(ASTNodeType type, SourcePosition start, SourcePosition end)
 {
     Type     = type;
     StartPos = start; EndPos = end;
 }
Esempio n. 33
0
 public void AddElementNode(ASTNodeType element)
 {
     element.SetParent(this);
     this.elements.Add(element);
 }
Esempio n. 34
0
 // Set construcor
 public BooleanExprASTNode(ASTNodeType type)
     : base(type)
 {
 }
Esempio n. 35
0
 public Expression(ASTNodeType type, SourcePosition start, SourcePosition end)
     : base(type, start, end)
 {
 }
Esempio n. 36
0
 public void SetReturnType(ASTNodeType returnType)
 {
     returnType.SetParent(this);
     this.returnType = returnType;
 }
Esempio n. 37
0
        private bool CompareOperands(ASTNodeType op, IComparable lhs, IComparable rhs)
        {
            if (op == ASTNodeType.AS)
            {
                if (lhs.GetType() != typeof(uint))
                {
                    throw new RunTimeError("The left hand side of an 'as' expression must evaluate to a serial");
                }

                if (rhs.GetType() != typeof(string))
                {
                    throw new RunTimeError("The right hand side of an 'as' expression must evaluate to a string");
                }
            }
            else if (lhs.GetType() != rhs.GetType())
            {
                // Different types. Try to convert one to match the other.

                if (rhs is double)
                {
                    // Special case for rhs doubles because we don't want to lose precision.
                    lhs = (double)lhs;
                }
                else if (rhs is bool)
                {
                    // Special case for rhs bools because we want to down-convert the lhs.
                    var tmp = Convert.ChangeType(lhs, typeof(bool));
                    lhs = (IComparable)tmp;
                }
                else
                {
                    var tmp = Convert.ChangeType(rhs, lhs.GetType());
                    rhs = (IComparable)tmp;
                }
            }

            try
            {
                // Evaluate the whole expression
                switch (op)
                {
                case ASTNodeType.EQUAL:
                    return(lhs.CompareTo(rhs) == 0);

                case ASTNodeType.NOT_EQUAL:
                    return(lhs.CompareTo(rhs) != 0);

                case ASTNodeType.LESS_THAN:
                    return(lhs.CompareTo(rhs) < 0);

                case ASTNodeType.LESS_THAN_OR_EQUAL:
                    return(lhs.CompareTo(rhs) <= 0);

                case ASTNodeType.GREATER_THAN:
                    return(lhs.CompareTo(rhs) > 0);

                case ASTNodeType.GREATER_THAN_OR_EQUAL:
                    return(lhs.CompareTo(rhs) >= 0);

                case ASTNodeType.AS:
                    Interpreter.SetVariable(rhs.ToString(), lhs.ToString());
                    return(CompareOperands(ASTNodeType.EQUAL, lhs, true));
                }
            }
            catch (ArgumentException e)
            {
                throw new RunTimeError(e.Message);
            }

            throw new RunTimeError("Unknown operator in expression");
        }
Esempio n. 38
0
 public void SetReturnType(ASTNodeType returnType)
 {
     returnType.SetParent(this);
     this.returnType = returnType;
 }
Esempio n. 39
0
        public static RubyAstNode Create(PegAstNode node)
        {
            ASTNodeType label = (ASTNodeType)node.GetLabel();

            if (node.GetLabel() != null)
            {
                return(new RubyTestAstNode(label, node.ToString()));
            }

            switch (label)
            {
            //case ASTNodeType.AstRoot:
            //	return new AstRoot ( node );
            //case ASTNodeType.Def:
            //	return new AstDef ( node );
            //case ASTNodeType.Name:
            //	return new AstName ( node );
            //case ASTNodeType.Param:
            //	return new AstParam ( node );
            //case ASTNodeType.Lambda:
            //	return new AstLambda ( node );
            //case ASTNodeType.Quote:
            //	return new AstQuote ( node );
            //case ASTNodeType.Char:
            //	return new AstChar ( node );
            //case ASTNodeType.String:
            //	return new AstString ( node );
            //case ASTNodeType.Float:
            //	return new AstFloat ( node );
            //case ASTNodeType.Int:
            //	return new AstInt ( node );
            //case ASTNodeType.Bin:
            //	return new AstBin ( node );
            //case ASTNodeType.Hex:
            //	return new AstHex ( node );
            //case ASTNodeType.Stack:
            //	return new AstStack ( node );
            //case ASTNodeType.FxnType:
            //	return new AstFxnType ( node );
            //case ASTNodeType.TypeVar:
            //	return new AstTypeVar ( node );
            //case ASTNodeType.TypeName:
            //	return new AstSimpleType ( node );
            //case ASTNodeType.StackVar:
            //	return new AstStackVar ( node );
            //case ASTNodeType.MacroRule:
            //	return new AstMacro ( node );
            //case ASTNodeType.MacroProp:
            //	return new AstMacro ( node );
            //case ASTNodeType.MacroPattern:
            //	return new AstMacroPattern ( node );
            //case ASTNodeType.MacroQuote:
            //	return new AstMacroQuote ( node );
            //case ASTNodeType.MacroTypeVar:
            //	return new AstMacroTypeVar ( node );
            //case ASTNodeType.MacroStackVar:
            //	return new AstMacroStackVar ( node );
            //case ASTNodeType.MacroName:
            //	return new AstMacroName ( node );
            //case ASTNodeType.MetaDataContent:
            //	return new AstMetaDataContent ( node );
            //case ASTNodeType.MetaDataLabel:
            //	return new AstMetaDataLabel ( node );
            //case ASTNodeType.MetaDataBlock:
            //	return new AstMetaDataBlock ( node );
            default:
                throw new Exception("unrecognized node type in AST tree: " + label);
            }
        }