~ubuntu-branches/ubuntu/saucy/bnfc/saucy

« back to all changes in this revision

Viewing changes to src/formats/cpp_stl/STLTop.hs

  • Committer: Package Import Robot
  • Author(s): Joachim Breitner
  • Date: 2013-05-24 12:49:41 UTC
  • mfrom: (7.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130524124941-tepbsbvdogyegb6k
Tags: 2.6.0.3-2
* Change Homepage field (Closes: #677988)
* Enable compat level 9
* Bump standards version to 3.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{-
 
2
    BNF Converter: C++ Main file
 
3
    Copyright (C) 2004  Author:  Markus Forsberg, Michael Pellauer
 
4
 
 
5
    Modified from CPPTop to STLTop 2006 by Aarne Ranta.
 
6
 
 
7
    This program is free software; you can redistribute it and/or modify
 
8
    it under the terms of the GNU General Public License as published by
 
9
    the Free Software Foundation; either version 2 of the License, or
 
10
    (at your option) any later version.
 
11
 
 
12
    This program is distributed in the hope that it will be useful,
 
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
    GNU General Public License for more details.
 
16
 
 
17
    You should have received a copy of the GNU General Public License
 
18
    along with this program; if not, write to the Free Software
 
19
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
-}
 
21
 
 
22
module STLTop (makeSTL) where
 
23
 
 
24
import Utils
 
25
import CF
 
26
import CFtoSTLAbs
 
27
import CFtoFlex
 
28
import CFtoBisonSTL
 
29
import CFtoCVisitSkelSTL
 
30
import CFtoSTLPrinter
 
31
import CFtoLatex
 
32
import System.Exit (exitFailure)
 
33
import Data.Char
 
34
import STLUtils
 
35
 
 
36
makeSTL :: Bool -> Bool -> Maybe String -> String -> CF -> IO ()
 
37
makeSTL make linenumbers inPackage name cf = do
 
38
    let (hfile, cfile) = cf2CPPAbs linenumbers inPackage name cf
 
39
    writeFileRep "Absyn.H" hfile
 
40
    writeFileRep "Absyn.C" cfile
 
41
    let (flex, env) = cf2flex inPackage name cf
 
42
    writeFileRep (name ++ ".l") flex
 
43
    putStrLn "   (Tested with flex 2.5.31)"
 
44
    let bison = cf2Bison linenumbers inPackage name cf env
 
45
    writeFileRep (name ++ ".y") bison
 
46
    putStrLn "   (Tested with bison 1.875a)"
 
47
    let header = mkHeaderFile inPackage cf (allCats cf) (allEntryPoints cf) env
 
48
    writeFileRep "Parser.H" header
 
49
    let (skelH, skelC) = cf2CVisitSkel inPackage cf
 
50
    writeFileRep "Skeleton.H" skelH
 
51
    writeFileRep "Skeleton.C" skelC
 
52
    let (prinH, prinC) = cf2CPPPrinter inPackage cf
 
53
    writeFileRep "Printer.H" prinH
 
54
    writeFileRep "Printer.C" prinC
 
55
    writeFileRep "Test.C" (cpptest inPackage cf)
 
56
    let latex = cfToLatex name cf
 
57
    writeFileRep (name ++ ".tex") latex
 
58
    if make then (writeFileRep "Makefile" $ makefile name) else return ()
 
59
 
 
60
makefile :: String -> String
 
61
makefile name = unlines 
 
62
  [
 
63
   "CC = g++",
 
64
   "CCFLAGS = -g",
 
65
   "FLEX = flex",
 
66
   "BISON = bison",
 
67
   "LATEX = latex",
 
68
   "DVIPS = dvips",
 
69
   "",
 
70
   "all: Test" ++ name ++ " " ++ name ++ ".ps",
 
71
   "",
 
72
   "clean:",
 
73
   -- peteg: don't nuke what we generated - move that to the "vclean" target.
 
74
   "\trm -f *.o " ++ name ++ ".dvi " ++ name ++ ".aux " ++ name ++ ".log " ++ name ++ ".ps Test" ++ name,
 
75
   "",
 
76
   "distclean:",
 
77
   "\t rm -f *.o Absyn.C Absyn.H Test.C Parser.C Parser.H Lexer.C Skeleton.C Skeleton.H Printer.C Printer.H " ++ name ++ ".l " ++ name ++ ".y " ++ name ++ ".tex " ++ name ++ ".dvi " ++ name ++ ".aux " ++ name ++ ".log " ++ name ++ ".ps Test" ++ name ++ " Makefile",
 
78
   "",
 
79
   "Test" ++ name ++ ": Absyn.o Lexer.o Parser.o Printer.o Test.o",
 
80
   "\t@echo \"Linking Test" ++ name ++ "...\"",
 
81
   "\t${CC} ${CCFLAGS} *.o -o Test" ++ name ++ "",
 
82
   "        ",
 
83
   "Absyn.o: Absyn.C Absyn.H",
 
84
   "\t${CC} ${CCFLAGS} -c Absyn.C",
 
85
   "",
 
86
   "Lexer.C: " ++ name ++ ".l",
 
87
   "\t${FLEX} -oLexer.C " ++ name ++ ".l",
 
88
   "",
 
89
   "Parser.C: " ++ name ++ ".y",
 
90
   "\t${BISON} " ++ name ++ ".y -o Parser.C",
 
91
   "",
 
92
   "Lexer.o: Lexer.C Parser.H",
 
93
   "\t${CC} ${CCFLAGS} -c Lexer.C ",
 
94
   "",
 
95
   "Parser.o: Parser.C Absyn.H",
 
96
   "\t${CC} ${CCFLAGS} -c Parser.C",
 
97
   "",
 
98
   "Printer.o: Printer.C Printer.H Absyn.H",
 
99
   "\t${CC} ${CCFLAGS} -c Printer.C",
 
100
   "",
 
101
   "Skeleton.o: Skeleton.C Skeleton.H Absyn.H",
 
102
   "\t${CC} ${CCFLAGS} -c Skeleton.C",
 
103
   "",
 
104
   "Test.o: Test.C Parser.H Printer.H Absyn.H",
 
105
   "\t${CC} ${CCFLAGS} -c Test.C",
 
106
   "",
 
107
   "" ++ name ++ ".dvi: " ++ name ++ ".tex",
 
108
   "\t${LATEX} " ++ name ++ ".tex",
 
109
   "",
 
110
   "" ++ name ++ ".ps: " ++ name ++ ".dvi",
 
111
   "\t${DVIPS} " ++ name ++ ".dvi -o " ++ name ++ ".ps",
 
112
   ""
 
113
  ]
 
114
  
 
115
cpptest :: Maybe String -> CF -> String
 
116
cpptest inPackage cf =
 
117
  unlines
 
118
   [
 
119
    "/*** Compiler Front-End Test automatically generated by the BNF Converter ***/",
 
120
    "/*                                                                          */",
 
121
    "/* This test will parse a file, print the abstract syntax tree, and then    */",
 
122
    "/* pretty-print the result.                                                 */",
 
123
    "/*                                                                          */",
 
124
    "/****************************************************************************/",
 
125
    "#include <stdio.h>",
 
126
    "#include \"Parser.H\"",
 
127
    "#include \"Printer.H\"",
 
128
    "#include \"Absyn.H\"",
 
129
    "",
 
130
    "int main(int argc, char ** argv)",
 
131
    "{",
 
132
    "  FILE *input;",
 
133
    "  if (argc > 1) ",
 
134
    "  {",
 
135
    "    input = fopen(argv[1], \"r\");",
 
136
    "    if (!input)",
 
137
    "    {",
 
138
    "      fprintf(stderr, \"Error opening input file.\\n\");",
 
139
    "      exit(1);",
 
140
    "    }",
 
141
    "  }",
 
142
    "  else input = stdin;",
 
143
    "  /* The default entry point is used. For other options see Parser.H */",
 
144
    "  " ++ scope ++ def ++ " *parse_tree = " ++ scope ++ "p" ++ def ++ "(input);",
 
145
    "  if (parse_tree)",
 
146
    "  {",
 
147
    "    printf(\"\\nParse Succesful!\\n\");",
 
148
    "    printf(\"\\n[Abstract Syntax]\\n\");",
 
149
    "    " ++ scope ++ "ShowAbsyn *s = new " ++ scope ++ "ShowAbsyn();",
 
150
    "    printf(\"%s\\n\\n\", s->show(parse_tree));",
 
151
    "    printf(\"[Linearized Tree]\\n\");",
 
152
    "    " ++ scope ++ "PrintAbsyn *p = new " ++ scope ++ "PrintAbsyn();",
 
153
    "    printf(\"%s\\n\\n\", p->print(parse_tree));",
 
154
    "    return 0;",
 
155
    "  }",
 
156
    "  return 1;",
 
157
    "}",
 
158
    ""
 
159
   ]
 
160
  where
 
161
   def = head (allEntryPoints cf)
 
162
   scope = nsScope inPackage
 
163
 
 
164
mkHeaderFile inPackage cf cats eps env = unlines
 
165
 [
 
166
  "#ifndef " ++ hdef,
 
167
  "#define " ++ hdef,
 
168
  "",
 
169
  "#include<vector>",
 
170
  "#include<string>",
 
171
  "",
 
172
  nsStart inPackage,
 
173
  concatMap mkForwardDec cats,
 
174
  "typedef union",
 
175
  "{",
 
176
  "  int int_;",
 
177
  "  char char_;",
 
178
  "  double double_;",
 
179
  "  char* string_;",
 
180
  (concatMap mkVar cats) ++ "} YYSTYPE;",
 
181
  "",
 
182
  concatMap mkFuncs eps,
 
183
  nsEnd inPackage,
 
184
  "",
 
185
  "#define " ++ nsDefine inPackage "_ERROR_" ++ " 258",
 
186
  mkDefines (259 :: Int) env,
 
187
  "extern " ++ nsScope inPackage ++ "YYSTYPE " ++ nsString inPackage ++ "yylval;",
 
188
  "",
 
189
  "#endif"
 
190
 ]
 
191
 where
 
192
  hdef = nsDefine inPackage "PARSER_HEADER_FILE"
 
193
  mkForwardDec s | (normCat s == s) = "class " ++ (identCat s) ++ ";\n"
 
194
  mkForwardDec _ = ""
 
195
  mkVar s | (normCat s == s) = "  " ++ (identCat s) ++"*" +++ (map toLower (identCat s)) ++ "_;\n"
 
196
  mkVar _ = ""
 
197
  mkDefines n [] = mkString n
 
198
  mkDefines n ((_,s):ss) = ("#define " ++ s +++ (show n) ++ "\n") ++ (mkDefines (n+1) ss) -- "nsDefine inPackage s" not needed (see cf2flex::makeSymEnv)
 
199
  mkString n =  if isUsedCat cf "String" 
 
200
   then ("#define " ++ nsDefine inPackage "_STRING_ " ++ show n ++ "\n") ++ mkChar (n+1)
 
201
   else mkChar n
 
202
  mkChar n =  if isUsedCat cf "Char" 
 
203
   then ("#define " ++ nsDefine inPackage "_CHAR_ " ++ show n ++ "\n") ++ mkInteger (n+1)
 
204
   else mkInteger n
 
205
  mkInteger n =  if isUsedCat cf "Integer" 
 
206
   then ("#define " ++ nsDefine inPackage "_INTEGER_ " ++ show n ++ "\n") ++ mkDouble (n+1)
 
207
   else mkDouble n
 
208
  mkDouble n =  if isUsedCat cf "Double" 
 
209
   then ("#define " ++ nsDefine inPackage "_DOUBLE_ " ++ show n ++ "\n") ++ mkIdent(n+1)
 
210
   else mkIdent n
 
211
  mkIdent n =  if isUsedCat cf "Ident" 
 
212
   then ("#define " ++ nsDefine inPackage "_IDENT_ " ++ show n ++ "\n")
 
213
   else ""
 
214
  mkFuncs s | (normCat s == s) = (identCat s) ++ "*" +++ "p" ++ (identCat s) ++ "(FILE *inp);\n" ++
 
215
                                 (identCat s) ++ "*" +++ "p" ++ (identCat s) ++ "(const char *str);\n"
 
216
  mkFuncs _ = ""