home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 549b.lha / M2P_v1.0_sources / source.lzh / LexAn.mpp < prev   
Text File  |  1991-08-10  |  5KB  |  162 lines

  1. (*======================================================================*)
  2. (*                         Modula-2 Lexical Analyzer                    *)
  3. (*======================================================================*)
  4. (*  Version:  1.00              Author:   Dennis Brueni                 *)
  5. (*  Date:     07-07-91          Changes:  original                      *)
  6. (*======================================================================*)
  7. (*  The lexical analyzer proper resides in two separate modules.        *)
  8. (*                                                                      *)
  9. (*      (1)  LexAn      Your lookin' at it.                             *)
  10. (*                                                                      *)
  11. (*      (2)  FSM        The Heart of the Beast.  The implememtation of  *)
  12. (*                      of the Finite State Machine.                    *)
  13. (*======================================================================*)
  14.  
  15. IMPLEMENTATION MODULE LexAn;
  16.  
  17. IMPORT FIO,Strings,SymLists,MacLists,SymTab,FSM;
  18.  
  19. @INCLUDE "MACROS"
  20.  
  21. (*----------------------------------------------------------------------*)
  22.  
  23. VAR MacroList: SymLists.SymList;
  24.  
  25. (*----------------------------------------------------------------------*)
  26.  
  27. @LongAddressing
  28. @NoCopyStrings
  29.  
  30. PROCEDURE SetSourceFile(FileName: ARRAY OF CHAR):BOOLEAN;
  31.  
  32. BEGIN
  33.    FSM.SourceFile := FIO.Open(FileName);
  34.    IF NOT FIO.GoodFILE(FSM.SourceFile) THEN
  35.       FSM.SourceFile := FIO.INPUT;
  36.       RETURN FALSE;
  37.    ELSE
  38.       RETURN TRUE;
  39.    END;
  40. END SetSourceFile;
  41.  
  42. (*----------------------------------------------------------------------*)
  43.  
  44. @NoCopyStrings
  45.  
  46. PROCEDURE SetDestFile(FileName: ARRAY OF CHAR):BOOLEAN;
  47.  
  48. BEGIN
  49.    FSM.DestFile := FIO.Create(FileName);
  50.    IF NOT FIO.GoodFILE(FSM.DestFile) THEN
  51.       FSM.DestFile := FIO.OUTPUT;
  52.       RETURN FALSE;
  53.    ELSE
  54.       RETURN TRUE;
  55.    END;
  56. END SetDestFile;
  57.  
  58. (*----------------------------------------------------------------------*)
  59. @NoLongAddressing
  60.  
  61. PROCEDURE FeedMacro(list: SymLists.SymList);
  62.  
  63. VAR temp: SymLists.SymList;
  64.  
  65. BEGIN
  66.    temp:=MacroList;
  67.    MacroList:=SymLists.ConCat(list,temp);
  68.    SymLists.Destroy(temp);
  69. END FeedMacro;
  70.  
  71. (*----------------------------------------------------------------------*)
  72.  
  73. PROCEDURE GetToken; 
  74.  
  75. VAR temp: MacLists.TokRecPtr;
  76.     retry:BOOLEAN;
  77.  
  78.         PROCEDURE GetNext(VAR temp: MacLists.TokRecPtr):BOOLEAN;
  79.         BEGIN
  80.            IF SymLists.Empty(MacroList) THEN
  81.               RETURN FALSE;
  82.            ELSE;
  83.               temp:=SymLists.First(MacroList);
  84.               SymLists.Delete(MacroList,temp^.sym^);
  85.               RETURN TRUE;
  86.            END;
  87.         END GetNext;
  88.  
  89. BEGIN
  90.    retry:=FALSE;
  91.    IF GetNext(temp) THEN
  92.       WHILE (temp^.typ=FSM.M2KillArg) AND GetNext(temp) DO
  93.          SymTab.Delete(temp^.sym^);
  94.          retry:=NOT GetNext(temp);
  95.       END;
  96.       IF retry THEN
  97.          FSM.GetBSU;
  98.       ELSE
  99.          WITH temp^ DO
  100.             WITH FSM.Token DO
  101.                Strings.Assign(sym^,String);
  102.                Class:=typ;
  103.                Length:=len;
  104.             END;
  105.          END;
  106.       END;
  107.    ELSE
  108.       FSM.GetBSU; 
  109.    END;
  110.    @IF DEBUG THEN
  111.       IF PrintTokens THEN PrintToken(FSM.Token); END;
  112.    @END
  113. END GetToken;
  114.  
  115. (*----------------------------------------------------------------------*)
  116. @LongAddressing
  117.  
  118. PROCEDURE PrintToken(VAR Tok : FSM.TokenRec);
  119. VAR i: CARDINAL;
  120.  
  121. @IF DEBUG THEN
  122.         PROCEDURE StringOut(VAR Str: ARRAY OF CHAR);
  123.         BEGIN
  124.            FIO.WriteChar(FIO.OUTPUT,47C);
  125.            FIO.WriteString(FIO.OUTPUT,Str);
  126.            FIO.WriteChar(FIO.OUTPUT,47C);
  127.         END StringOut;
  128.  
  129.    BEGIN
  130.       WITH Tok DO   
  131.          FIO.WriteString(FIO.OUTPUT,'        ');
  132.          FIO.WriteString(FIO.OUTPUT,FSM.Strings[Class]);
  133.          FOR i:= Strings.Length(FSM.Strings[Class]) TO 12 DO 
  134.             FIO.WriteChar(FIO.OUTPUT,' ');
  135.          END;
  136.          CASE Class OF
  137.            FSM.M2ID,
  138.            FSM.M2Str,
  139.            FSM.M2String:StringOut(String);
  140.           |FSM.M2ch:   StringOut(String);
  141.                        FIO.WriteString(FIO.OUTPUT,' = ORD(');
  142.                        FIO.WriteCard(FIO.OUTPUT,ORD(String[0]));
  143.                        FIO.WriteChar(FIO.OUTPUT,')');
  144.          ELSE
  145.             FIO.WriteString(FIO.OUTPUT,String);
  146.          END;
  147.          FIO.WriteLn(FIO.OUTPUT);
  148.       END;
  149. @ELSE 
  150.    BEGIN
  151. @END 
  152. END PrintToken;
  153.   
  154. (************************************************************************)
  155.  
  156. BEGIN
  157.    SymLists.Create(MacroList);
  158.    @IF DEBUG THEN
  159.       PrintTokens:=FALSE;   
  160.    @END
  161. END LexAn.
  162.