|
1 /* |
|
2 * (c) 2003 Mario de Sousa |
|
3 * |
|
4 * Offered to the public under the terms of the GNU General Public License |
|
5 * as published by the Free Software Foundation; either version 2 of the |
|
6 * License, or (at your option) any later version. |
|
7 * |
|
8 * This program is distributed in the hope that it will be useful, but |
|
9 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
11 * Public License for more details. |
|
12 * |
|
13 * This code is made available on the understanding that it will not be |
|
14 * used in safety-critical situations without a full and competent review. |
|
15 */ |
|
16 |
|
17 /* |
|
18 * An IEC 61131-3 IL and ST compiler. |
|
19 * |
|
20 * Based on the |
|
21 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
22 * |
|
23 */ |
|
24 |
|
25 /* |
|
26 **************************************************************** |
|
27 **************************************************************** |
|
28 **************************************************************** |
|
29 ********* ********* |
|
30 ********* ********* |
|
31 ********* O V E R A L L A R C H I T E C T U R E ********* |
|
32 ********* ********* |
|
33 ********* ********* |
|
34 **************************************************************** |
|
35 **************************************************************** |
|
36 **************************************************************** |
|
37 |
|
38 The compiler works in 4(+1) stages: |
|
39 Stage 1 - Lexical analyser - implemented with flex (iec.flex) |
|
40 Stage 2 - Syntax parser - implemented with bison (iec.y) |
|
41 Stage 3 - Semantics analyser - not yet implemented |
|
42 Stage 4 - Code generator - implemented in C++ |
|
43 Stage 4+1 - Binary code generator - gcc, javac, etc... |
|
44 |
|
45 |
|
46 Data structures passed between stages, in global variables: |
|
47 1->2 : tokens (int), and token values (char *) |
|
48 2->1 : symbol tables (defined in symtable.hh) |
|
49 2->3 : abstract syntax tree (tree of C++ classes, in absyntax.hh file) |
|
50 3->4 : Same as 2->3 |
|
51 4->4+1 : file with program in c, java, etc... |
|
52 |
|
53 |
|
54 The compiler works in several passes: |
|
55 Pass 1: executes stages 1 and 2 simultaneously |
|
56 Pass 2: executes stage 3 |
|
57 Pass 3: executes stage 4 |
|
58 Pass 4: executes stage 4+1 |
|
59 */ |
|
60 |
|
61 |
|
62 |
|
63 //#include <stdio.h> // printf() |
|
64 |
|
65 #include <stdlib.h> // EXIT_FAILURE |
|
66 #include "absyntax/absyntax.hh" // symbol_c type |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 /* A macro for printing out internal parser errors... */ |
|
72 #include <iostream> // required for std::cerr |
|
73 #define ERROR error_exit(__FILE__,__LINE__) |
|
74 void error_exit(const char *file_name, int line_no) { |
|
75 std::cerr << "\nInternal program error in file " << file_name |
|
76 << " at line " << line_no << "\n\n\n"; |
|
77 exit(EXIT_FAILURE); |
|
78 } |
|
79 |
|
80 |
|
81 |
|
82 /* forward declarations... */ |
|
83 int stage1_2(const char *filename, const char *includedir, symbol_c **tree_root); |
|
84 //int stage3(symbol_c *tree_root); |
|
85 int stage4(symbol_c *tree_root); |
|
86 |
|
87 |
|
88 static void printusage(const char *cmd) { |
|
89 printf("%s [<input_file>] [-I <include_directory>]\n", cmd); |
|
90 } |
|
91 |
|
92 |
|
93 |
|
94 int main(int argc, char **argv) { |
|
95 symbol_c *tree_root; |
|
96 char * includedir = NULL; |
|
97 |
|
98 if (argc == 4) { |
|
99 if (strcmp(argv[2], "-I") != 0) { |
|
100 printusage(argv[0]); |
|
101 return EXIT_FAILURE; |
|
102 } |
|
103 includedir = argv[3]; |
|
104 argc = 2; |
|
105 } |
|
106 |
|
107 if (argc != 2) { |
|
108 printusage(argv[0]); |
|
109 return EXIT_FAILURE; |
|
110 } |
|
111 |
|
112 /* 1st Pass */ |
|
113 if (stage1_2(argv[1], includedir, &tree_root) < 0) |
|
114 return EXIT_FAILURE; |
|
115 |
|
116 /* 2nd Pass */ |
|
117 /* not yet implemented... */ |
|
118 /* |
|
119 if (stage3(tree_root) < 0) |
|
120 return EXIT_FAILURE; |
|
121 */ |
|
122 |
|
123 /* 3rd Pass */ |
|
124 if (stage4(tree_root) < 0) |
|
125 return EXIT_FAILURE; |
|
126 |
|
127 /* 4th Pass */ |
|
128 /* Currently implemented in the Makefile! */ |
|
129 |
|
130 return 0; |
|
131 } |
|
132 |
|
133 |