SymTab.def 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. DEFINITION MODULE SymTab;
  2. (* Symbol table with static type checking for SimpleMod2
  3. (simplified Modula-2 without procedures).
  4. - flat scopes with levels: globals at level 0, WITH statement
  5. bodies push the record's fields as an inner scope (shadowing
  6. allowed, duplicates within one level rejected)
  7. - every symbol carries a type descriptor index (InvalidType if
  8. unknown, e.g. imported names); unknown types suppress follow-on
  9. errors to avoid cascades
  10. - type descriptors: aliases (one per TYPE declaration, so
  11. self-references such as POINTER TO Person resolve), subranges,
  12. enumerations, arrays, records (with field lists), sets,
  13. pointers, predefined types, string literals
  14. - INTEGER, CARDINAL and subranges thereof form one lenient
  15. "integer family"; mixed INTEGER/REAL arithmetic is rejected *)
  16. CONST
  17. MaxSyms = 256;
  18. InvalidType = -1;
  19. (* symbol kinds *)
  20. KindConst = 0;
  21. KindType = 1;
  22. KindVar = 2;
  23. KindImport = 3;
  24. KindModule = 4;
  25. KindPredef = 5;
  26. KindField = 6;
  27. (* type classes returned by ClassOf *)
  28. ClInvalid = 0;
  29. ClInt = 1;
  30. ClReal = 2;
  31. ClChar = 3;
  32. ClBool = 4;
  33. ClEnum = 5;
  34. ClArray = 6;
  35. ClRecord = 7;
  36. ClSet = 8;
  37. ClPtr = 9;
  38. ClStr = 10;
  39. (* operator codes for RelCheck *)
  40. OpEq = 0; OpNeq1 = 1; OpNeq2 = 2;
  41. OpLt = 3; OpLe = 4; OpGt = 5; OpGe = 6;
  42. OpIn = 7;
  43. (* operator codes for AddOp / MulOp *)
  44. OpAdd = 0; OpSub = 1; OpOr = 2;
  45. OpTimes = 0; OpSlash = 1; OpDiv = 2; OpMod = 3; OpAnd = 4;
  46. TYPE
  47. Name = ARRAY [0 .. 63] OF CHAR;
  48. TypeIndex = INTEGER;
  49. (* ---------------- symbols and scopes ---------------- *)
  50. PROCEDURE Init;
  51. (* Clears the table and enters predefined identifiers
  52. (INTEGER, CARDINAL, SHORTINT, LONGINT, REAL, LONGREAL, CHAR,
  53. BOOLEAN, TRUE, FALSE, NIL). *)
  54. PROCEDURE Enter (name: ARRAY OF CHAR; kind: INTEGER): BOOLEAN;
  55. (* Enters name at the current scope level (type InvalidType).
  56. Returns FALSE on duplicate within the same level. *)
  57. PROCEDURE EnterPending (name: ARRAY OF CHAR; kind: INTEGER): BOOLEAN;
  58. (* Like Enter, but remembers the entry for a later FixPending call
  59. (used for VAR identifier lists whose type is parsed afterwards).
  60. Returns FALSE on duplicate (entry not recorded). *)
  61. PROCEDURE FixPending (t: TypeIndex);
  62. (* Assigns type t to all pending entries, clears the buffer. *)
  63. PROCEDURE PendCount (): CARDINAL;
  64. (* Number of entries currently waiting for FixPending. *)
  65. PROCEDURE PendName (i: CARDINAL; VAR n: Name);
  66. (* Name of the i-th pending entry (empty string if out of range).
  67. Used by the QBE backend to emit storage for VAR lists whose
  68. type is parsed after the identifiers. *)
  69. PROCEDURE FieldPending (rec: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
  70. (* Records a field name for record descriptor rec (type fixed later
  71. with FixPendingF). Returns FALSE on duplicate field. *)
  72. PROCEDURE FixPendingF (rec: TypeIndex; t: TypeIndex);
  73. (* Assigns type t to pending fields owned by rec. *)
  74. PROCEDURE Lookup (name: ARRAY OF CHAR): BOOLEAN;
  75. (* TRUE if name is visible (innermost scope wins). *)
  76. PROCEDURE SymType (name: ARRAY OF CHAR): TypeIndex;
  77. (* Type of innermost visible entry, InvalidType if absent. *)
  78. PROCEDURE SetSymType (name: ARRAY OF CHAR; t: TypeIndex);
  79. (* Sets type of innermost visible entry. *)
  80. PROCEDURE SymKind (name: ARRAY OF CHAR): INTEGER;
  81. (* Kind of innermost visible entry, -1 if absent. *)
  82. PROCEDURE Equal (a, b: ARRAY OF CHAR): BOOLEAN;
  83. PROCEDURE PushScope;
  84. PROCEDURE PopScope;
  85. (* WITH statement support: PushRecord pushes a record's fields. *)
  86. PROCEDURE PushRecord (t: TypeIndex): BOOLEAN;
  87. (* Pushes a scope containing t's fields (as KindField). FALSE if t
  88. is not a record type (or invalid). Caller must PopScope after. *)
  89. PROCEDURE PrintTable;
  90. (* ---------------- type descriptors ---------------- *)
  91. PROCEDURE NewAlias (): TypeIndex;
  92. PROCEDURE NewSub (base: TypeIndex): TypeIndex;
  93. PROCEDURE NewEnum (): TypeIndex;
  94. PROCEDURE NewArray (elem: TypeIndex): TypeIndex;
  95. PROCEDURE NewRecord (): TypeIndex;
  96. PROCEDURE NewSet (base: TypeIndex): TypeIndex;
  97. PROCEDURE NewPtr (base: TypeIndex): TypeIndex;
  98. PROCEDURE NewStr (): TypeIndex;
  99. (* Fresh descriptors; base/elem may be InvalidType. InvalidType is
  100. returned when the table is full. *)
  101. PROCEDURE SetTarget (t, base: TypeIndex);
  102. (* Sets an alias target (TYPE declaration completion). *)
  103. PROCEDURE IntType (): TypeIndex;
  104. PROCEDURE RealType (): TypeIndex;
  105. PROCEDURE CharType (): TypeIndex;
  106. PROCEDURE BoolType (): TypeIndex;
  107. PROCEDURE ClassOf (t: TypeIndex): INTEGER;
  108. (* Resolves aliases; InvalidType maps to ClInvalid. *)
  109. PROCEDURE IsIntFamily (t: TypeIndex): BOOLEAN;
  110. (* INTEGER, CARDINAL or subrange thereof (Invalid suppresses). *)
  111. PROCEDURE SameType (a, b: TypeIndex): BOOLEAN;
  112. (* Same resolved descriptor (Invalid suppresses). *)
  113. PROCEDURE FieldExists (rec: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
  114. PROCEDURE FieldType (rec: TypeIndex; name: ARRAY OF CHAR): TypeIndex;
  115. PROCEDURE ArrayElem (t: TypeIndex): TypeIndex;
  116. PROCEDURE PtrBase (t: TypeIndex): TypeIndex;
  117. (* ---------------- predicates used by grammar checks ---------------- *)
  118. (* All return TRUE if either operand is InvalidType (no cascades). *)
  119. PROCEDURE Assignable (src, dst: TypeIndex): BOOLEAN;
  120. (* Assignment compatibility (210). *)
  121. PROCEDURE ArithCheck (l, r: TypeIndex; divmod: BOOLEAN;
  122. VAR res: TypeIndex): BOOLEAN;
  123. (* + - * / (divmod FALSE) or DIV MOD (TRUE); res is result type (211). *)
  124. PROCEDURE UnaryCheck (t: TypeIndex; VAR res: TypeIndex): BOOLEAN;
  125. (* Unary + - (part of 211). *)
  126. PROCEDURE BoolCheck (t: TypeIndex): BOOLEAN;
  127. (* BOOLEAN required: NOT/AND/OR operands (212), conditions (214). *)
  128. PROCEDURE RelCheck (l, r: TypeIndex; op: INTEGER): BOOLEAN;
  129. (* = # <> < <= > >= IN (213/222 chosen by caller via op). *)
  130. PROCEDURE EqCheck (l, r: TypeIndex): BOOLEAN;
  131. (* = # compatibility, also reused for CASE label matching. *)
  132. PROCEDURE InCheck (l, set: TypeIndex): BOOLEAN;
  133. PROCEDURE SetElemCheck (first, elem: TypeIndex): BOOLEAN;
  134. PROCEDURE SetFor (elem: TypeIndex): TypeIndex;
  135. (* Fresh SET OF elem descriptor for set literals. *)
  136. PROCEDURE StrLen (s: ARRAY OF CHAR): CARDINAL;
  137. END SymTab.