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; }
public static UnaryExpressionNode UnaryExpressionNode(ASTNodeType nodeType, ASTNode expr) { UnaryExpressionNode unaryNode = s_UnaryNodePool.Get(); unaryNode.type = nodeType; unaryNode.expression = expr; return(unaryNode); }
/* * 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); }
/* * 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"); }
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>(); }
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>(); }
public ASTNode(ASTNodeType type, string lexeme, ASTNode parent) { Type = type; if (lexeme != null) { Lexeme = lexeme; } else { Lexeme = ""; } Parent = parent; }
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); }
public RubyAstNode(PegAstNode node) { if (node.GetLabel() != null) { mLabel = (ASTNodeType)node.GetLabel(); } else { mLabel = ASTNodeType.SELF; } mText = node.ToString(); }
public ASTNode(ASTNodeType type, string lexeme, ASTNode parent, int lineNumber) { Type = type; if (lexeme != null) { Lexeme = lexeme; } else { Lexeme = ""; } Parent = parent; LineNumber = lineNumber; }
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); } }
public void AddParameterType(ASTNodeType parameterType) { parameterType.SetParent(this); this.parameters.Add(parameterType); }
protected Statement(ASTNodeType type, SourcePosition start, SourcePosition end) : base(type, start, end) { }
public SimpleASTNode(ASTNodeType nodeType, String text) { this.nodeType = nodeType; this.text = text; }
public Expression(ASTNodeType type, SourcePosition start, SourcePosition end) : base(type, start, end) { }
public IntExprASTNode(ASTNodeType type) : base(type) { }
public ExprASTNode(ASTNodeType nodeType) : base(nodeType) { }
public void SetTypeNode(ASTNodeType type) { type.SetParent(this); this.AddSpan(type.GetSpan()); this.type = type; }
public Statement(ASTNodeType type,SourcePosition start, SourcePosition end) : base(type, start, end) { }
public void SetReturnTypeNode(ASTNodeType retType) { retType.SetParent(this); this.AddSpan(retType.GetSpan()); this.returnType = retType; }
// Set type constructor. public ASTNode(ASTNodeType type) : this() { this.nodeType = type; }
// Set constructor public StatementASTNode(ASTNodeType nodeType) : base(nodeType) { }
public ASTVariableNode(ASTNodeType type) : base(type) { }
public ASTNode(ASTNodeType type, Lexeme lexeme = null) { this.type = type; this.lexeme = lexeme; }
public void SetTypeNode(ASTNodeType type) { type.SetParent(this); this.type = type; }
public ASTNode(ASTNodeType type, SourcePosition start, SourcePosition end) { Type = type; StartPos = start; EndPos = end; }
public void AddElementNode(ASTNodeType element) { element.SetParent(this); this.elements.Add(element); }
// Set construcor public BooleanExprASTNode(ASTNodeType type) : base(type) { }
public void SetReturnType(ASTNodeType returnType) { returnType.SetParent(this); this.returnType = returnType; }
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"); }
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); } }