AST.def 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. DEFINITION MODULE AST;
  2. (* Abstract syntax tree for M2comp step 3.
  3. Recipe: H. Mössenböck, "How to Build Abstract Syntax Trees with
  4. Coco/R" (docs/AST.pdf) — every nonterminal builds its subtree via
  5. output attributes, with plain semantic actions (no special notation).
  6. Nodes are heap pointer records with child links (left/right/extra/
  7. more) and a sibling link (next), mirroring the dsc/next shape of
  8. the Oberon SymTable (docs/oberon-master) while SymTab itself stays
  9. flat. Types are stored as descriptor indices (SymTab.TypeIndex,
  10. seen here as INTEGER to keep coupling one-way); there is no
  11. separate type subtree. Error-tolerant: nodes are built even when
  12. the type is InvalidType, so dumps exist for rejected sources too. *)
  13. CONST
  14. (* declaration nodes *)
  15. nkNone = 0;
  16. nkModule = 1; (* name; left=decl chain; right=block *)
  17. nkBlock = 2; (* left=decl chain; right=stmt chain *)
  18. nkImport = 3; (* name=module ("" for plain IMPORT); left=names *)
  19. nkImpName = 4; (* name *)
  20. nkExport = 5; (* left=names *)
  21. nkConst = 6; (* name; typ; left=expr *)
  22. nkTypeDecl = 7; (* name; typ *)
  23. nkVar = 8; (* name; typ *)
  24. nkMember = 9; (* record field name; typ *)
  25. nkParam = 10; (* name; typ; op=ORD(isVar) *)
  26. nkProc = 11; (* name; typ=rettype; left=params; right=block *)
  27. (* statement nodes *)
  28. nkAssign = 20; (* left=dst; right=src *)
  29. nkCall = 21; (* left=callee designator; right=actuals; num=pnum *)
  30. nkIf = 22; (* left=cond; right=then; extra=else-or-nil *)
  31. nkWhile = 23; (* left=cond; right=body *)
  32. nkRepeat = 24; (* left=body; right=cond *)
  33. nkLoop = 25; (* left=body *)
  34. nkFor = 26; (* name=control var; left=lo; right=hi;
  35. extra=by-or-nil; more=body *)
  36. nkReturn = 27; (* left=expr-or-nil *)
  37. nkExit = 28;
  38. (* expression nodes *)
  39. nkBin = 30; (* op=SymTab op code (OpIn for IN); left; right *)
  40. nkUn = 31; (* op=opNot/opNeg/opPos; left *)
  41. nkName = 32; (* name; typ *)
  42. nkInt = 33; (* num=value; typ *)
  43. nkReal = 34; (* text; typ *)
  44. nkChar = 35; (* num=ORD; typ *)
  45. nkStr = 36; (* text; typ *)
  46. nkField = 37; (* left=record; name=field; typ *)
  47. nkIndex = 38; (* left=array; right=index chain; typ *)
  48. nkDeref = 39; (* left; typ *)
  49. nkCallExpr = 40; (* left=callee; right=actuals; num=pnum; typ=ret *)
  50. (* unary operator codes (binary ones reuse SymTab op codes) *)
  51. opNot = 1; opNeg = 2; opPos = 3;
  52. TYPE
  53. Node = POINTER TO NodeRec;
  54. NodeRec = RECORD
  55. kind: INTEGER;
  56. typ: INTEGER;
  57. op: INTEGER;
  58. num: INTEGER;
  59. name: ARRAY [0 .. 63] OF CHAR;
  60. text: ARRAY [0 .. 63] OF CHAR;
  61. left, right, extra, more, next: Node;
  62. END;
  63. PROCEDURE New (kind: INTEGER): Node;
  64. (* Fresh node, all links NIL, typ InvalidType (-1). *)
  65. PROCEDURE SetRoot (n: Node);
  66. PROCEDURE GetRoot (): Node;
  67. (* Module root for the driver dump. *)
  68. PROCEDURE Append (head, tail: Node): Node;
  69. (* NIL-safe sibling append; returns head (or tail). *)
  70. PROCEDURE SetChainType (head: Node; t: INTEGER);
  71. (* Assigns typ to every node of a sibling chain (VAR/FP/field
  72. identifier lists whose type is parsed afterwards). *)
  73. PROCEDURE MkName (nm: ARRAY OF CHAR; t: INTEGER): Node;
  74. PROCEDURE MkInt (v: INTEGER; t: INTEGER): Node;
  75. PROCEDURE MkChar (v: INTEGER; t: INTEGER): Node;
  76. PROCEDURE MkReal (tx: ARRAY OF CHAR; t: INTEGER): Node;
  77. PROCEDURE MkStr (tx: ARRAY OF CHAR; t: INTEGER): Node;
  78. PROCEDURE MkBin (op: INTEGER; l, r: Node; t: INTEGER): Node;
  79. PROCEDURE MkUn (op: INTEGER; e: Node; t: INTEGER): Node;
  80. PROCEDURE MkField (rec: Node; field: ARRAY OF CHAR; t: INTEGER): Node;
  81. PROCEDURE MkIndex (arr, idx: Node; t: INTEGER): Node;
  82. PROCEDURE MkDeref (p: Node; t: INTEGER): Node;
  83. PROCEDURE MkCallExpr (callee, actuals: Node; pnum,
  84. t: INTEGER): Node;
  85. PROCEDURE MkAssign (dst, src: Node): Node;
  86. PROCEDURE MkCall (callee, actuals: Node; pnum: INTEGER): Node;
  87. PROCEDURE MkIf (c, th, el: Node): Node;
  88. PROCEDURE MkWhile (c, body: Node): Node;
  89. PROCEDURE MkRepeat (body, c: Node): Node;
  90. PROCEDURE MkLoop (body: Node): Node;
  91. PROCEDURE MkFor (nm: ARRAY OF CHAR; lo, hi, by,
  92. body: Node): Node;
  93. PROCEDURE MkReturn (e: Node): Node;
  94. PROCEDURE MkExit (): Node;
  95. PROCEDURE MkBlock (decls, stmts: Node): Node;
  96. PROCEDURE MkModule (nm: ARRAY OF CHAR; decls,
  97. body: Node): Node;
  98. PROCEDURE MkProc (nm: ARRAY OF CHAR; ret: INTEGER; params,
  99. body: Node): Node;
  100. PROCEDURE MkConst (nm: ARRAY OF CHAR; e: Node; t: INTEGER): Node;
  101. PROCEDURE MkTypeDecl (nm: ARRAY OF CHAR; t: INTEGER): Node;
  102. PROCEDURE MkVar (nm: ARRAY OF CHAR; t: INTEGER): Node;
  103. PROCEDURE MkMember (nm: ARRAY OF CHAR; t: INTEGER): Node;
  104. PROCEDURE MkParam (nm: ARRAY OF CHAR; t: INTEGER;
  105. isVar: BOOLEAN): Node;
  106. PROCEDURE MkImport (mod: ARRAY OF CHAR; names: Node): Node;
  107. PROCEDURE MkImpName (nm: ARRAY OF CHAR): Node;
  108. PROCEDURE MkExport (names: Node): Node;
  109. PROCEDURE ParseInt (tx: ARRAY OF CHAR; VAR v: INTEGER): BOOLEAN;
  110. (* Decimal or H-suffixed hex integer literal text to value. *)
  111. PROCEDURE DumpTree (n: Node);
  112. (* Indented dump of the tree to StdOut (NIL-safe, depth-capped). *)
  113. END AST.