| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- DEFINITION MODULE SymTab;
- (* Symbol table with static type checking for SimpleMod2
- (simplified Modula-2 without procedures).
- - flat scopes with levels: globals at level 0, WITH statement
- bodies push the record's fields as an inner scope (shadowing
- allowed, duplicates within one level rejected)
- - every symbol carries a type descriptor index (InvalidType if
- unknown, e.g. imported names); unknown types suppress follow-on
- errors to avoid cascades
- - type descriptors: aliases (one per TYPE declaration, so
- self-references such as POINTER TO Person resolve), subranges,
- enumerations, arrays, records (with field lists), sets,
- pointers, predefined types, string literals
- - INTEGER, CARDINAL and subranges thereof form one lenient
- "integer family"; mixed INTEGER/REAL arithmetic is rejected *)
- CONST
- InvalidType = -1;
- (* symbol kinds *)
- KindConst = 0;
- KindType = 1;
- KindVar = 2;
- KindImport = 3;
- KindModule = 4;
- KindPredef = 5;
- KindField = 6;
- KindProc = 7;
- KindParam = 8;
- (* type classes returned by ClassOf *)
- ClInvalid = 0;
- ClInt = 1;
- ClReal = 2;
- ClChar = 3;
- ClBool = 4;
- ClEnum = 5;
- ClArray = 6;
- ClRecord = 7;
- ClSet = 8;
- ClPtr = 9;
- ClStr = 10;
- ClClass = 11;
- (* operator codes for RelCheck *)
- OpEq = 0; OpNeq1 = 1; OpNeq2 = 2;
- OpLt = 3; OpLe = 4; OpGt = 5; OpGe = 6;
- OpIn = 7;
- (* operator codes for AddOp / MulOp *)
- OpAdd = 0; OpSub = 1; OpOr = 2;
- OpTimes = 0; OpSlash = 1; OpDiv = 2; OpMod = 3; OpAnd = 4;
- TYPE
- Name = ARRAY [0 .. 63] OF CHAR;
- TypeIndex = INTEGER;
- (* ---------------- symbols and scopes ---------------- *)
- PROCEDURE Init;
- (* Clears the table and enters predefined identifiers
- (INTEGER, CARDINAL, SHORTINT, LONGINT, REAL, LONGREAL, CHAR,
- BOOLEAN, TRUE, FALSE, NIL). *)
- PROCEDURE Enter (name: ARRAY OF CHAR; kind: INTEGER): BOOLEAN;
- (* Enters name at the current scope level (type InvalidType).
- Returns FALSE on duplicate within the same level. *)
- PROCEDURE EnterPending (name: ARRAY OF CHAR; kind: INTEGER): BOOLEAN;
- (* Like Enter, but remembers the entry for a later FixPending call
- (used for VAR identifier lists whose type is parsed afterwards).
- Returns FALSE on duplicate (entry not recorded). *)
- PROCEDURE FixPending (t: TypeIndex);
- (* Assigns type t to all pending entries, clears the buffer. *)
- PROCEDURE PendCount (): CARDINAL;
- (* Number of entries currently waiting for FixPending. *)
- PROCEDURE PendName (i: CARDINAL; VAR n: Name);
- (* Name of the i-th pending entry (empty string if out of range).
- Used by the QBE backend to emit storage for VAR lists whose
- type is parsed after the identifiers. *)
- PROCEDURE FieldPending (rec: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
- (* Records a field name for record descriptor rec (type fixed later
- with FixPendingF). Returns FALSE on duplicate field. *)
- PROCEDURE FixPendingF (rec: TypeIndex; t: TypeIndex);
- (* Assigns type t to pending fields owned by rec. *)
- PROCEDURE Lookup (name: ARRAY OF CHAR): BOOLEAN;
- (* TRUE if name is visible (innermost scope wins). *)
- PROCEDURE SymType (name: ARRAY OF CHAR): TypeIndex;
- (* Type of innermost visible entry, InvalidType if absent. *)
- PROCEDURE SetSymType (name: ARRAY OF CHAR; t: TypeIndex);
- (* Sets type of innermost visible entry. *)
- PROCEDURE SymKind (name: ARRAY OF CHAR): INTEGER;
- (* Kind of innermost visible entry, -1 if absent. *)
- PROCEDURE Equal (a, b: ARRAY OF CHAR): BOOLEAN;
- PROCEDURE PushScope;
- PROCEDURE PopScope;
- (* WITH statement support: PushRecord pushes a record's fields. *)
- PROCEDURE PushRecord (t: TypeIndex): BOOLEAN;
- (* Pushes a scope containing t's fields (as KindField). FALSE if t
- is not a record type (or invalid). Caller must PopScope after. *)
- PROCEDURE PrintTable;
- (* ---------------- procedures (headings now, bodies step 4) ---------------- *)
- PROCEDURE EnterProc (name: ARRAY OF CHAR): BOOLEAN;
- (* Enters a KindProc heading and pushes its parameter scope.
- FALSE on duplicate (no scope pushed). Resets result to
- InvalidType (proper procedure), clears fwd, empties param chain. *)
- PROCEDURE ReenterProc (name: ARRAY OF CHAR): BOOLEAN;
- (* Resumes a FORWARD-marked procedure of this scope for its body
- heading: clears fwd, pushes a fresh parameter scope. FALSE
- when no such forward heading exists (then 200 applies). *)
- PROCEDURE EnterParam (name: ARRAY OF CHAR; isVar: BOOLEAN): BOOLEAN;
- (* Enters a KindParam in the heading scope and chains it into the
- current procedure's positional param list. FALSE on duplicate. *)
- PROCEDURE SetProcRes (t: TypeIndex);
- (* Sets the current procedure's result type (InvalidType = none). *)
- PROCEDURE ProcRes (name: ARRAY OF CHAR): TypeIndex;
- (* Result type of the named procedure, InvalidType if absent/none. *)
- PROCEDURE MarkFwd;
- (* Marks the current procedure FORWARD (body must follow). *)
- PROCEDURE MarkVirtual;
- (* Marks the current procedure VIRTUAL (dynamically dispatched;
- dispatch tables arrive with class lowering). *)
- PROCEDURE CloseProc;
- (* Pops the current procedure's scope and its proc-stack entry.
- Replaces bare PopScope at the end of headings (DefUnit),
- FORWARD clauses and bodies. *)
- PROCEDURE InProc (): BOOLEAN;
- (* TRUE inside a procedure heading/body (RETURN legal). *)
- PROCEDURE CurRes (): TypeIndex;
- (* Result type of the innermost open procedure,
- InvalidType outside procedures or for proper procedures. *)
- (* ---------------- classes (Clarion form, lowering deferred) ---------------- *)
- PROCEDURE SetParent (t, p: TypeIndex);
- (* Records p as the (single) parent class of t. Multiple parents
- are rejected in the grammar with 230. *)
- PROCEDURE PushClassScope (t: TypeIndex);
- (* Pushes the body scope of class t and records it on t. *)
- PROCEDURE MethodExists (t: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
- (* TRUE if the class scope of t declares name as a procedure. *)
- PROCEDURE PushClassMembers (t: TypeIndex): BOOLEAN;
- (* Pushes a scope with t's fields (KindField) for method bodies in
- CLASS IMPLEMENTATION. FALSE if t is not a class. Method names
- are re-entered fresh by each body (copying them would collide);
- sibling-method references report 201 until step 4 calls. *)
- (* ---------------- type descriptors ---------------- *)
- PROCEDURE NewAlias (): TypeIndex;
- PROCEDURE NewSub (base: TypeIndex): TypeIndex;
- PROCEDURE NewSubR (lo, hi: INTEGER): TypeIndex;
- (* Integer subrange [lo..hi] (no base descriptor needed). Bounds
- accessors arrive with step 3; the backend cannot lower them yet. *)
- PROCEDURE NewEnum (): TypeIndex;
- PROCEDURE NewArray (elem: TypeIndex): TypeIndex;
- PROCEDURE NewRecord (): TypeIndex;
- PROCEDURE NewSet (base: TypeIndex): TypeIndex;
- PROCEDURE NewPtr (base: TypeIndex): TypeIndex;
- PROCEDURE NewStr (): TypeIndex;
- PROCEDURE NewClass (): TypeIndex;
- (* Fresh class descriptor (holds fields like a record; methods live
- in the class scope). Parent and scope via SetParent/PushClassScope. *)
- (* Fresh descriptors; base/elem may be InvalidType. InvalidType is
- returned when the table is full. *)
- PROCEDURE SetTarget (t, base: TypeIndex);
- (* Sets an alias target (TYPE declaration completion). *)
- PROCEDURE IntType (): TypeIndex;
- PROCEDURE RealType (): TypeIndex;
- PROCEDURE CharType (): TypeIndex;
- PROCEDURE BoolType (): TypeIndex;
- PROCEDURE ClassOf (t: TypeIndex): INTEGER;
- (* Resolves aliases; InvalidType maps to ClInvalid. *)
- PROCEDURE IsIntFamily (t: TypeIndex): BOOLEAN;
- (* INTEGER, CARDINAL or subrange thereof (Invalid suppresses). *)
- PROCEDURE SameType (a, b: TypeIndex): BOOLEAN;
- (* Same resolved descriptor (Invalid suppresses). *)
- PROCEDURE FieldExists (rec: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
- PROCEDURE FieldType (rec: TypeIndex; name: ARRAY OF CHAR): TypeIndex;
- PROCEDURE ArrayElem (t: TypeIndex): TypeIndex;
- PROCEDURE PtrBase (t: TypeIndex): TypeIndex;
- (* ---------------- predicates used by grammar checks ---------------- *)
- (* All return TRUE if either operand is InvalidType (no cascades). *)
- PROCEDURE Assignable (src, dst: TypeIndex): BOOLEAN;
- (* Assignment compatibility (210). *)
- PROCEDURE ArithCheck (l, r: TypeIndex; divmod: BOOLEAN;
- VAR res: TypeIndex): BOOLEAN;
- (* + - * / (divmod FALSE) or DIV MOD (TRUE); res is result type (211). *)
- PROCEDURE UnaryCheck (t: TypeIndex; VAR res: TypeIndex): BOOLEAN;
- (* Unary + - (part of 211). *)
- PROCEDURE BoolCheck (t: TypeIndex): BOOLEAN;
- (* BOOLEAN required: NOT/AND/OR operands (212), conditions (214). *)
- PROCEDURE RelCheck (l, r: TypeIndex; op: INTEGER): BOOLEAN;
- (* = # <> < <= > >= IN (213/222 chosen by caller via op). *)
- PROCEDURE EqCheck (l, r: TypeIndex): BOOLEAN;
- (* = # compatibility, also reused for CASE label matching. *)
- PROCEDURE InCheck (l, set: TypeIndex): BOOLEAN;
- PROCEDURE SetElemCheck (first, elem: TypeIndex): BOOLEAN;
- PROCEDURE SetFor (elem: TypeIndex): TypeIndex;
- (* Fresh SET OF elem descriptor for set literals. *)
- PROCEDURE StrLen (s: ARRAY OF CHAR): CARDINAL;
- PROCEDURE ConstInt (q: ARRAY OF CHAR; VAR v: INTEGER): BOOLEAN;
- (* Parses a folded integer immediate ("42", "-5", "16") as produced
- by NormInt/NegFold. FALSE for temporaries and non-numerics. *)
- END SymTab.
|