/* astdefs.cpp XIDEK: Extensible Script Language Development Kit Abstract Syntax Tree Definitions Copyright (c) 1996-2002 Parsifal Software. All Rights Reserved. For further information about this program or the AnaGram parser generator, please contact: Parsifal Software http://www.parsifalsoft.com info@parsifalsoft.com +1-800-879-2577, Voice/Fax +1-508-358-2564 P.O. Box 219 Wayland, MA 01778 USA */ #include #include #include "comdefs.h" #include "astdefs.h" // Number of subnodes for a given node. // This array must be aligned correctly with the NodeType // enumeration in astdefs.h const int nSubnodes[] = { 0, // NullNode, 1, // Script, 2, // IfStatement, 3, // IfElseStatement, 2, // WhileStatement, 4, // ForStatement, 5, // SimpleFor 2, // DoStatement, 2, // RepeatStatement, 0, // Break, 0, // Continue, 1, // ExpressionStatement, 0, // StatementBlock, 2, // CommaExpression, 2, // Assignment, 3, // ConditionalExpression, 2, // LogicalOrExpression, 2, // LogicalAndExpression, 2, // Binary, 1, // Unary, 0, // Constant, 0, // Variable, 0, // FunctionCall, 0, // Dump, 0, // Print, 1, // return }; // Node constructors AstNode::AstNode(const FileLocation &c, NodeType t, AstNode *x, AstNode *y, AstNode *z, AstNode *w, AstNode *v) : type(t), context(c) { subnode[0] = x; subnode[1] = y; subnode[2] = z; subnode[3] = w; subnode[4] = v; } ListNode::ListNode(const FileLocation &c, NodeType t, const AgStack &l) : AstNode(c, t), list(l.contents()) { // Nothing to do } AssignmentNode::AssignmentNode(const FileLocation &c, AstNode *d, Opcode op, AstNode *x) : AstNode(c, typeAssignment, d, x), opcode(op) {} BinaryNode::BinaryNode(const FileLocation &c, Opcode op, AstNode *x, AstNode *y) : AstNode(c, typeBinary, x, y), opcode(op) {} UnaryNode::UnaryNode(const FileLocation &c, Opcode op, AstNode *x) : AstNode(c, typeUnary, x), opcode(op) {} ConstantNode::ConstantNode(const FileLocation &c, const Value &v) : AstNode(c, typeConstant), value(v) {} VariableNode::VariableNode(const FileLocation &c, const AgString &s) : AstNode(c, typeVariable), name(s) {} FunctionCallNode::FunctionCallNode (const FileLocation &c, const AgString &n, const AgStack &args) : AstNode(c, typeFunctionCall), name(n), argList(args) {} AbstractSyntaxTree::~AbstractSyntaxTree() { if (nodeStack.isNotUniqueInstance()) return; int n = nodeStack.size(); while (n--) delete nodeStack.pop(); } void AstNode::reportError(const char *msg) const { char buf[100]; sprintf(buf, "Error(%d,%d): %s", context.line, context.column, msg); throw ErrorDiagnostic(buf); }