1 (* ==================================================================== *)
3 (* Main Module for the Gardens Point Component Pascal Compiler. *)
4 (* Copyright (c) John Gough 1999, 2000. *)
5 (* This module was extensively modified from the driver *)
6 (* automatically produced by the M2 version of COCO/R, using *)
7 (* the CPascal.atg grammar used for the JVM version of GPCP. *)
9 (* ==================================================================== *)
12 (* This is an example of a rudimentary main module for use with COCO/R.
13 The auxiliary modules <Grammar>S (scanner) and <Grammar>P (parser)
14 are assumed to have been constructed with COCO/R compiler generator. *)
36 TxtFil
:= GPTextFiles
,
39 (* ==================================================================== *)
41 (* ==================================================================== *)
43 PROCEDURE ResetOptions
*;
48 (* -------------------------- *)
50 PROCEDURE Message
*(IN msg
: ARRAY OF CHAR);
55 (* -------------------------- *)
57 PROCEDURE DoOption
*(IN opt
: ARRAY OF CHAR);
62 (* -------------------------- *)
64 PROCEDURE CondMsg(IN msg
: ARRAY OF CHAR);
66 IF CSt
.verbose
THEN CSt
.Message(msg
) END;
69 (* ==================================================================== *)
70 (* Calling the Compiler *)
71 (* ==================================================================== *)
74 VAR a
: ARRAY 16 OF CHAR;
75 b
: ARRAY 256 OF CHAR;
77 IF CPascalErrors
.forVisualStudio
OR
78 CPascalErrors
.xmlErrors
THEN RETURN END;
79 b
:= "<" + CSt
.modNam
+ ">";
80 IF Scnr
.errors
= 0 THEN
81 b
:= (b
+ " No errors");
82 ELSIF Scnr
.errors
= 1 THEN
83 b
:= (b
+ " There was one error");
85 GPText
.IntToStr(Scnr
.errors
, a
);
86 b
:= (b
+ " There were " + a
+ " errors");
88 IF Scnr
.warnings
= 1 THEN
89 b
:= (b
+ ", and one warning");
90 ELSIF Scnr
.warnings
> 1 THEN
91 GPText
.IntToStr(Scnr
.warnings
, a
);
92 b
:= (b
+ ", and " + a
+ " warnings");
94 IF ~CSt
.quiet
THEN CSt
.Message(b
) END;
97 (* ==================================================================== *)
99 PROCEDURE FixListing
*;
100 VAR doList
: BOOLEAN;
103 doList
:= (CSt
.listLevel
> Scnr
.listNever
);
104 events
:= Scnr
.errors
;
105 IF CSt
.warning
THEN INC(events
, Scnr
.warnings
) END;
107 (CSt
.listLevel
= Scnr
.listAlways
) THEN
108 Scnr
.lst
:= TxtFil
.createFile(CSt
.lstNam
);
109 IF Scnr
.lst
# NIL THEN
110 CPascalErrors
.PrintListing(doList
);
111 TxtFil
.CloseFile(Scnr
.lst
);
114 CSt
.Message("cannot create file <" + CSt
.lstNam
+ ">");
115 IF events
> 0 THEN CPascalErrors
.PrintListing(FALSE
) END;
118 CPascalErrors
.ResetErrorList();
121 (* ==================================================================== *)
123 PROCEDURE Compile
*(IN nam
: ARRAY OF CHAR; OUT retVal
: INTEGER);
127 CSt
.totalS
:= RTS
.GetMillis();
128 Scnr
.src
:= BinFil
.findLocal(nam
);
129 IF Scnr
.src
= NIL THEN
130 CSt
.Message("cannot open local file <" + nam
+ ">");
132 NameHash
.InitNameHash(CSt
.hashSize
);
134 CSt
.InitCompState(nam
);
135 Builtin
.RebindBuiltins();
136 Target
.Select(CSt
.thisMod
, CSt
.target
);
138 CondMsg("Starting Parse");
139 CPascalP
.Parse
; (* do the compilation here *)
140 CSt
.parseE
:= RTS
.GetMillis();
141 IF Scnr
.errors
= 0 THEN
142 CondMsg("Doing statement attribution");
143 CSt
.thisMod
.StatementAttribution(Visitor
.newImplementedCheck());
144 IF (Scnr
.errors
= 0) & CSt
.extras
THEN
145 CondMsg("Doing type erasure");
146 CSt
.thisMod
.TypeErasure(Visitor
.newTypeEraser());
148 IF Scnr
.errors
= 0 THEN
149 CondMsg("Doing dataflow analysis");
150 CSt
.thisMod
.DataflowAttribution();
151 CSt
.attrib
:= RTS
.GetMillis();
152 IF Scnr
.errors
= 0 THEN
154 CondMsg("Emitting symbol file");
156 Old
.EmitSymfile(CSt
.thisMod
);
158 New
.EmitSymfile(CSt
.thisMod
);
160 CSt
.symEnd
:= RTS
.GetMillis();
162 IF CSt
.isForeign() THEN
163 CondMsg("Foreign module: no code output");
165 CondMsg("Emitting assembler");
167 CSt
.asmEnd
:= RTS
.GetMillis();
168 IF CSt
.doCode
THEN Target
.Assemble() END;
175 IF Scnr
.errors
# 0 THEN retVal
:= 2 END;
176 CSt
.totalE
:= RTS
.GetMillis();
179 IF CSt
.doStats
THEN CSt
.Report
END;
183 CSt
.Message("<< COMPILER PANIC >>");
184 Scnr
.SemError
.RepSt1(299, RTS
.getStr(sysX
), 1, 1);
186 * If an exception is raised during listing, FixListing will
187 * be called twice. Avoid an attempted sharing violation...
189 IF Scnr
.lst
# NIL THEN
190 TxtFil
.CloseFile(Scnr
.lst
);
191 CSt
.Message(RTS
.getStr(sysX
));
199 (* ==================================================================== *)
200 (* Main Argument Loop *)
201 (* ==================================================================== *)
206 Builtin
.InitBuiltins
;
209 (* ==================================================================== *)