home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
549a.lha
/
M2P_v1.0
/
mods.lzh
/
M2P.mod
< prev
next >
Wrap
Text File
|
1991-08-10
|
9KB
|
361 lines
(*======================================================================*)
(* Modula-2 Preprocessor Driver *)
(*======================================================================*)
(* Version: 1.00 Author: Dennis Brueni *)
(* Date: 07-07-91 Changes: original *)
(*======================================================================*)
(* This program simply sets everything up and calls the Parser, *)
(* who takes over and does the rest. *)
(*======================================================================*)
MODULE M2P;
IMPORT
LexAn,FSM,Err;
IMPORT
FIO,Args,Env;
IMPORT
Strings,FStorage,SYSTEM;
IMPORT
Parser,SymTab,SymLists,MacLists;
(* error catcher *)
(*----------------------------------------------------------------------*)
VAR
i: CARDINAL;
Suppress: BOOLEAN;
SrcName: Strings.BigString;
DestName: Strings.BigString;
lparen: Strings.String1;
rparen: Strings.String1;
minus : Strings.String1;
(*----------------------------------------------------------------------*)
(* SUPPRESS Function TO determine IF writes TO standard OUTPUT *)
(* should be supressed since OUTPUT is going there *)
(*----------------------------------------------------------------------*)
PROCEDURE CheckSuppress;
VAR
i: CARDINAL;
BEGIN
Suppress:=FALSE;
i:=2;
WHILE i+1<Args.argc DO
Suppress:=Suppress OR (Strings.Equal(Args.argv[i]^,'TO') AND Strings.Equal(Args.argv[i+1]^,minus));
INC(i);
END;
END CheckSuppress;
(*----------------------------------------------------------------------*)
(* CHECKSWITCHES Looks for command line switches *)
(*----------------------------------------------------------------------*)
PROCEDURE CheckSwitches;
BEGIN
WHILE (Args.argc > i) DO
IF (Args.argc > i+1) AND Strings.Equal(Args.argv[i]^,'INCLUDE') THEN
MacLists.Insert(Parser.IncludeList, Args.argv[i+1]^,FSM.M2ID);
INC(i);
ELSIF Strings.Equal(Args.argv[i]^,'STRIP') THEN
FSM.StripFlag:=TRUE;
ELSIF Strings.Equal(Args.argv[i]^,'NOSTRIP') THEN
FSM.StripFlag:=FALSE;
ELSE
RETURN;
END;
INC(i);
END;
END CheckSwitches;
(*----------------------------------------------------------------------*)
(* CHECKINFILE Attempts to open the input file *)
(*----------------------------------------------------------------------*)
PROCEDURE CheckInFile;
BEGIN
Strings.Assign(Args.argv[i]^,SrcName);
IF Strings.Equal(SrcName,minus) THEN
IF NOT Suppress THEN
FIO.WriteString(FIO.OUTPUT,'Reading from standard INPUT.');
FIO.WriteLn(FIO.OUTPUT);
END;
FIO.SetPrompt(FIO.INPUT,'M2P> ');
FSM.SourceFile:=FIO.INPUT;
ELSE
IF LexAn.SetSourceFile(SrcName) THEN
IF NOT Suppress THEN
FIO.WriteString(FIO.OUTPUT,'Processing : ');
FIO.WriteString(FIO.OUTPUT,SrcName);
FIO.WriteLn(FIO.OUTPUT);
END;
ELSE
FIO.WriteString(FIO.OUTPUT,'Could not open: ');
FIO.WriteString(FIO.OUTPUT,SrcName);
FIO.WriteLn(FIO.OUTPUT);
Err.Fatal(Err.IOError);
END;
END;
INC(i);
END CheckInFile;
(*----------------------------------------------------------------------*)
(* CHECKARGS Check local bindings for input file *)
(*----------------------------------------------------------------------*)
PROCEDURE CheckArgs;
VAR
temp: SymTab.SymRecPtr;
list: SymLists.SymList;
BEGIN
SymLists.Create(list);
IF i < Args.argc THEN
IF Strings.Equal(Args.argv[i]^,lparen) THEN
INC(i);
WHILE (i<Args.argc) AND NOT Strings.Equal(Args.argv[i]^,rparen) DO
temp:=SymTab.LookUp(Args.argv[i]^);
IF temp # NIL THEN
Err.Message(Err.FlagReDefined);
END;
SymTab.Insert(Args.argv[i]^,FALSE,list,list);
INC(i);
END;
IF (i < Args.argc) THEN
INC(i);
END;
END;
END;
END CheckArgs;
(*----------------------------------------------------------------------*)
(* CHECKOUTFILE Attempts to open the output FILE *)
(*----------------------------------------------------------------------*)
PROCEDURE CheckOutFile;
VAR
len: CARDINAL;
BEGIN
IF (Args.argc > i+1) AND Strings.Equal(Args.argv[i]^,'TO') THEN
INC(i);
Strings.Assign(Args.argv[i]^,DestName);
IF Strings.Equal(DestName,minus)
(* standard output *)
THEN
FSM.DestFile:=FIO.OUTPUT;
ELSE
IF LexAn.SetDestFile(DestName) THEN
FIO.WriteString(FIO.OUTPUT,'Output file : ');
FIO.WriteString(FIO.OUTPUT,DestName);
FIO.WriteLn(FIO.OUTPUT);
ELSE
FIO.WriteString(FIO.OUTPUT,'Could not open: ');
FIO.WriteString(FIO.OUTPUT,DestName);
FIO.WriteLn(FIO.OUTPUT);
Err.Fatal(Err.IOError);
END;
END;
INC(i)
ELSE
Strings.Assign(SrcName,DestName);
len:=Strings.Length(DestName);
IF (CAP(DestName[len-1])='P') AND
(* Known Preprocessor *)
(CAP(DestName[len-2])='P') AND
(* extensions *)
(CAP(DestName[len-4])='.') THEN
CASE CAP(DestName[len-3]) OF
'M':
DestName[len-2]:='o';
(* .MPP => .mod *)
DestName[len-1]:='d';
|'D':
DestName[len-2]:='e';
(* .DPP => .def *)
DestName[len-1]:='f';
|'C',
(* .CPP => .c *)
'H':
DestName[len-2]:=0C;
(* .HPP => .h *)
|'A':
DestName[len-2]:='s';
(* .APP => .asm *)
DestName[len-1]:='m';
|'L':
DestName[len-2]:='s';
(* .LPP => .lsp *)
DestName[len-1]:='p';
|'T':
DestName[len-2]:='x';
(* .TPP => .txt *)
DestName[len-1]:='t';
|'P':
DestName[len-2]:='a';
(* .PPP => .pas *)
DestName[len-1]:='s';
|'O':
DestName[len-3]:='m';
(* .OPP => .mod *)
DestName[len-2]:='o';
(*----oberon----*)
DestName[len-1]:='d';
|'F':
DestName[len-2]:='o';
(* .FPP => .for *)
DestName[len-1]:='r';
|'S':
DestName[len-2]:='c';
(* .SPP => .scm *)
DestName[len-1]:='m';
ELSE
FIO.WriteString(FIO.OUTPUT,'Unrecognized extension: .');
FIO.WriteChar(FIO.OUTPUT,DestName[len-3]);
FIO.WriteChar(FIO.OUTPUT,DestName[len-2]);
FIO.WriteChar(FIO.OUTPUT,DestName[len-1]);
FIO.WriteLn(FIO.OUTPUT);
Err.Fatal(Err.BadParams);
END;
IF LexAn.SetDestFile(DestName) THEN
FIO.WriteString(FIO.OUTPUT,'Output file : ');
FIO.WriteString(FIO.OUTPUT,DestName);
FIO.WriteLn(FIO.OUTPUT);
ELSE
FIO.WriteString(FIO.OUTPUT,'Could not open: ');
FIO.WriteString(FIO.OUTPUT,DestName);
FIO.WriteLn(FIO.OUTPUT);
Err.Fatal(Err.IOError);
END;
ELSE
FIO.WriteString(FIO.OUTPUT,'Unrecognized input file extension');
FIO.WriteLn(FIO.OUTPUT);
Err.Fatal(Err.BadParams);
END;
END;
END CheckOutFile;
(*----------------------------------------------------------------------*)
(* CHECKENV Checks the environment variable, ENV:M2PFlags *)
(*----------------------------------------------------------------------*)
PROCEDURE CheckEnv;
VAR
list: SymLists.SymList;
M2PEnv: ARRAY [0..1023] OF CHAR;
String: ARRAY [0.. 255] OF CHAR;
BEGIN
SymLists.Create(list);
Env.GetEnv('M2PFlags',M2PEnv);
WHILE Env.ParseEnv(M2PEnv,String) DO
SymTab.Insert(String,FALSE,list,list);
END;
END CheckEnv;
(************************************************************************)
PROCEDURE FinalTerminate;
BEGIN
FIO.CloseAllFiles;
FStorage.DISPOSEALL;
Args.TermProc;
END FinalTerminate;
(************************************************************************)
(* Main Line CODE *)
(************************************************************************)
BEGIN
lparen[0]:='(';
rparen[0]:=')';
minus [0]:='-';
CheckSuppress;
IF NOT Suppress THEN
FIO.WriteString(FIO.OUTPUT,'M2P 1.00 -- by Dennis Brueni -- Written in Modula-2');
FIO.WriteLn(FIO.OUTPUT);
END;
IF Args.argc < 2 THEN
FIO.WriteString(FIO.OUTPUT,'Usage: ');
FIO.WriteString(FIO.OUTPUT,Args.argv[0]^);
FIO.WriteString(FIO.OUTPUT,' <switches> { [<infile> | -] [( {flag} )] [TO <outfile> | TO -] }');
FIO.WriteLn(FIO.OUTPUT);
FIO.WriteString(FIO.OUTPUT,' where <switches> = { STRIP | NOSTRIP | INCLUDE <path> ');
FIO.WriteChar(FIO.OUTPUT,'}');
FIO.WriteLn(FIO.OUTPUT);
ELSE
i:=1;
CheckSwitches;
WHILE i < Args.argc DO
IF NOT Suppress THEN
FIO.WriteLn(FIO.OUTPUT);
END;
SymTab.Initialize;
Err.LineNum:=1;
CheckEnv;
CheckInFile;
CheckArgs;
CheckOutFile;
Parser.Parse();
IF NOT Suppress THEN
Err.LastMessage;
END;
IF SYSTEM.ADDRESS(FSM.SourceFile) # SYSTEM.ADDRESS(FIO.INPUT) THEN
FIO.Close(FSM.SourceFile);
END;
IF SYSTEM.ADDRESS(FSM.DestFile) # SYSTEM.ADDRESS(FIO.OUTPUT) THEN
FIO.Close(FSM.DestFile);
END;
END;
END;
FinalTerminate;
END M2P.