1 /*-
2 * Copyright (c) 2006, 2007 CTPP Team
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 4. Neither the name of the CTPP Team nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * CTPP2Compiler.cpp
29 *
30 * $CTPP$
31 */
32 #include <CTPP2Parser.hpp>
33 #include <CTPP2FileSourceLoader.hpp>
34 #include <CTPP2ParserException.hpp>
35 #include <CTPP2HashTable.hpp>
36 #include <CTPP2VMDumper.hpp>
37 #include <CTPP2VMOpcodes.h>
38
39 #include <sys/stat.h>
40
41 #include <stdio.h>
42 #include <string.h>
43
44 using namespace CTPP;
45
main(int argc,char ** argv)46 int main(int argc, char ** argv)
47 {
48 if (argc != 3)
49 {
50 fprintf(stdout, "CTPP2 template compiler v" CTPP_VERSION " (" CTPP_IDENT "). Copyright (c) 2004-2011 CTPP Dev. Team.\n\n");
51 fprintf(stderr, "usage: %s source.ctpp2 destination.ct2\n", argv[0]);
52 return EX_USAGE;
53 }
54
55 VMOpcodeCollector oVMOpcodeCollector;
56 StaticText oSyscalls;
57 StaticData oStaticData;
58 StaticText oStaticText;
59 HashTable oHashTable;
60 CTPP2Compiler oCompiler(oVMOpcodeCollector, oSyscalls, oStaticData, oStaticText, oHashTable);
61
62 try
63 {
64 // Load template
65 CTPP2FileSourceLoader oSourceLoader;
66 oSourceLoader.LoadTemplate(argv[1]);
67
68 // Create template parser
69 CTPP2Parser oCTPP2Parser(&oSourceLoader, &oCompiler, argv[1]);
70
71 // Compile template
72 oCTPP2Parser.Compile();
73 }
74 catch(CTPPLogicError & e)
75 {
76 fprintf(stderr, "ERROR: %s\n", e.what());
77 return EX_SOFTWARE;
78 }
79 catch(CTPPUnixException & e)
80 {
81 fprintf(stderr, "ERROR: I/O in %s: %s\n", e.what(), strerror(e.ErrNo()));
82 return EX_SOFTWARE;
83 }
84 catch(CTPPParserSyntaxError & e)
85 {
86 fprintf(stderr, "ERROR: At line %d, pos. %d: %s\n", e.GetLine(), e.GetLinePos(), e.what());
87 return EX_SOFTWARE;
88 }
89 catch (CTPPParserOperatorsMismatch &e)
90 {
91 fprintf(stderr, "ERROR: At line %d, pos. %d: expected %s, but found </%s>\n", e.GetLine(), e.GetLinePos(), e.Expected(), e.Found());
92 return EX_SOFTWARE;
93 }
94 catch(...)
95 {
96 fprintf(stderr, "ERROR: Bad thing happened.\n");
97 return EX_SOFTWARE;
98 }
99
100 // Get program core
101 UINT_32 iCodeSize = 0;
102 const VMInstruction * oVMInstruction = oVMOpcodeCollector.GetCode(iCodeSize);
103 // Dump program
104 VMDumper oDumper(iCodeSize, oVMInstruction, oSyscalls, oStaticData, oStaticText, oHashTable);
105 UINT_32 iSize = 0;
106 const VMExecutable * aProgramCore = oDumper.GetExecutable(iSize);
107
108 // Open file only if compilation is done
109 FILE * FW = fopen(argv[2], "wb");
110 if (FW == NULL) { fprintf(stderr, "ERROR: Cannot open destination file `%s` for writing\n", argv[2]); return EX_SOFTWARE; }
111
112 // Write to the disc
113 fwrite(aProgramCore, iSize, 1, FW);
114 // All done
115 fclose(FW);
116
117 // Make valgrind happy
118 fclose(stdin);
119 fclose(stdout);
120 fclose(stderr);
121
122 return EX_OK;
123 }
124 // End.
125