├── .classpath
├── .idea
├── .gitignore
├── .name
├── encodings.xml
├── libraries
│ └── MyCGrammer.xml
├── misc.xml
├── modules.xml
└── workspace.xml
├── .project
├── .settings
├── org.eclipse.core.resources.prefs
├── org.eclipse.jdt.core.prefs
└── org.eclipse.wst.common.project.facet.core.xml
├── LICENSE
├── README.md
├── bitmincc-clean.iml
├── config.xml
├── lib
├── BITMiniCC_20200404_obf.jar
├── Mars4_5.jar
├── MyCGrammer.jar
├── Rars.jar
├── antlr-4.8-complete.jar
├── dom4j-1.6.1.jar
├── jackson-annotations-2.10.2.jar
├── jackson-core-2.10.2.jar
├── jackson-databind-2.10.2.jar
├── jaxen-1.1-beta-6.jar
├── jdom.jar
└── jython.jar
├── src
└── bit
│ └── minisys
│ └── minicc
│ ├── MyMiniCompiler.java
│ ├── icgen
│ ├── LabelGenerator.java
│ ├── MyICBuilder.java
│ ├── MyICPrinter.java
│ ├── Quat.java
│ ├── SymbolPrinter.java
│ └── TemporaryValue.java
│ ├── ncgen
│ └── MyCodeGen.java
│ ├── parser
│ ├── MyListener.java
│ └── ast
│ │ ├── ASTArrayAccess.java
│ │ ├── ASTArrayDeclarator.java
│ │ ├── ASTBinaryExpression.java
│ │ ├── ASTBreakStatement.java
│ │ ├── ASTCastExpression.java
│ │ ├── ASTCharConstant.java
│ │ ├── ASTCompilationUnit.java
│ │ ├── ASTCompoundStatement.java
│ │ ├── ASTConditionExpression.java
│ │ ├── ASTContinueStatement.java
│ │ ├── ASTDeclaration.java
│ │ ├── ASTDeclarator.java
│ │ ├── ASTExpression.java
│ │ ├── ASTExpressionStatement.java
│ │ ├── ASTFloatConstant.java
│ │ ├── ASTFunctionCall.java
│ │ ├── ASTFunctionDeclarator.java
│ │ ├── ASTFunctionDefine.java
│ │ ├── ASTGotoStatement.java
│ │ ├── ASTIdentifier.java
│ │ ├── ASTInitList.java
│ │ ├── ASTIntegerConstant.java
│ │ ├── ASTIterationDeclaredStatement.java
│ │ ├── ASTIterationStatement.java
│ │ ├── ASTLabeledStatement.java
│ │ ├── ASTMemberAccess.java
│ │ ├── ASTNode.java
│ │ ├── ASTParamsDeclarator.java
│ │ ├── ASTPostfixExpression.java
│ │ ├── ASTReturnStatement.java
│ │ ├── ASTSelectionStatement.java
│ │ ├── ASTStatement.java
│ │ ├── ASTStringConstant.java
│ │ ├── ASTToken.java
│ │ ├── ASTTypename.java
│ │ ├── ASTUnaryExpression.java
│ │ ├── ASTUnaryTypename.java
│ │ ├── ASTVariableDeclarator.java
│ │ └── ASTVisitor.java
│ ├── scanner
│ └── MyScanner.java
│ └── semantic
│ ├── FuncTable.java
│ └── VarTable.java
└── test
├── .keep
├── ic_test
├── .keep
├── test.ast.json
├── test.c
├── test.ic
├── test.ic.xml
├── test.pp.c
└── test.tokens
├── nc_test
├── 1_Fibonacci.c
├── 2_Prime.c
└── 3_PerfectNumber.c
├── parser_test
├── 2_parser_test1.c
├── 3_parser_test2.c
└── 4_parser_test3.c
├── scan_test
└── 1_scanner_test.c
└── semantic_test
├── 0_var_not_defined.c
├── 1_var_defined_again.c
├── 2_break_not_in_loop.c
└── 6_goto_label_not_exist.c
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | bitmincc-clean
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/libraries/MyCGrammer.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | 1645862130979
74 |
75 |
76 | 1645862130979
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | file://$PROJECT_DIR$/src/bit/minisys/minicc/MyMiniCompiler.java
85 | 28
86 |
87 |
88 |
89 | file://$PROJECT_DIR$/src/bit/minisys/minicc/scanner/MyScanner.java
90 | 12
91 |
92 |
93 |
94 |
95 |
96 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | bitmincc-clean
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.wst.common.project.facet.core.builder
10 |
11 |
12 |
13 |
14 | org.eclipse.xtext.ui.shared.xtextBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 |
25 | org.eclipse.jdt.core.javanature
26 | org.eclipse.xtext.ui.shared.xtextNature
27 | org.eclipse.wst.common.project.facet.core.nature
28 |
29 |
30 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding//src/bit/minisys/minicc/MiniCCompiler.java=UTF-8
3 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6 | org.eclipse.jdt.core.compiler.compliance=1.8
7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12 | org.eclipse.jdt.core.compiler.source=1.8
13 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.wst.common.project.facet.core.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 |
294 | Copyright (C)
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | , 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 本实验是 基于 https://github.com/jiweixing/BIT-MiniCC 的框架编写迷你C语言编译器,从中体会计算机编译代码的整个过程。
2 |
3 | 本次实验环境为
4 |
5 | IntelliJ IDEA Community Edition 2021.1
6 |
7 | Java1.8
8 |
9 | Antlr4.8
10 |
11 | (从github上下载框架打开后需要调整编码为UTF-8)
12 |
13 | 详细参考博客:https://blog.csdn.net/weixin_43877853/article/details/123229579?spm=1001.2014.3001.5501
14 |
--------------------------------------------------------------------------------
/bitmincc-clean.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
--------------------------------------------------------------------------------
/config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/lib/BITMiniCC_20200404_obf.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/BITMiniCC_20200404_obf.jar
--------------------------------------------------------------------------------
/lib/Mars4_5.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/Mars4_5.jar
--------------------------------------------------------------------------------
/lib/MyCGrammer.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/MyCGrammer.jar
--------------------------------------------------------------------------------
/lib/Rars.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/Rars.jar
--------------------------------------------------------------------------------
/lib/antlr-4.8-complete.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/antlr-4.8-complete.jar
--------------------------------------------------------------------------------
/lib/dom4j-1.6.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/dom4j-1.6.1.jar
--------------------------------------------------------------------------------
/lib/jackson-annotations-2.10.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/jackson-annotations-2.10.2.jar
--------------------------------------------------------------------------------
/lib/jackson-core-2.10.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/jackson-core-2.10.2.jar
--------------------------------------------------------------------------------
/lib/jackson-databind-2.10.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/jackson-databind-2.10.2.jar
--------------------------------------------------------------------------------
/lib/jaxen-1.1-beta-6.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/jaxen-1.1-beta-6.jar
--------------------------------------------------------------------------------
/lib/jdom.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/jdom.jar
--------------------------------------------------------------------------------
/lib/jython.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/lib/jython.jar
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/MyMiniCompiler.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc;
2 |
3 | import MyCGrammer.MyCGrammerLexer;
4 | import MyCGrammer.MyCGrammerParser;
5 | import bit.minisys.minicc.icgen.MyICBuilder;
6 | import bit.minisys.minicc.ncgen.MyCodeGen;
7 | import bit.minisys.minicc.parser.MyListener;
8 | import bit.minisys.minicc.parser.ast.ASTNode;
9 | import bit.minisys.minicc.scanner.MyScanner;
10 | import org.antlr.v4.runtime.ANTLRInputStream;
11 | import org.antlr.v4.runtime.CommonTokenStream;
12 | import org.antlr.v4.runtime.tree.ParseTree;
13 | import org.antlr.v4.runtime.tree.ParseTreeWalker;
14 |
15 | import java.io.FileInputStream;
16 | import java.io.IOException;
17 | import java.io.InputStream;
18 | public class MyMiniCompiler {
19 | public static void main(String[] args)throws IOException {
20 | String inputFile = "test/nc_test/1_Fibonacci.c";
21 | InputStream is = System.in;
22 | is = new FileInputStream(inputFile);
23 | ANTLRInputStream input = new ANTLRInputStream(is);
24 | MyCGrammerLexer lexer = new MyCGrammerLexer(input);
25 | CommonTokenStream tokens = new CommonTokenStream(lexer);
26 | MyCGrammerParser parser = new MyCGrammerParser(tokens);
27 | ParseTree tree = parser.compilationUnit();
28 |
29 | String fName = inputFile.trim();
30 | String temp[] = fName.split("\\\\");
31 | String tokenFileName =temp[temp.length - 1] + ".tokens";
32 | MyScanner myScanner = new MyScanner(tokenFileName,tokens);
33 |
34 | String jsonFileName = temp[temp.length - 1] + ".json";
35 | ParseTreeWalker walker = new ParseTreeWalker();
36 | MyListener listener = new MyListener();
37 | listener.oFile=jsonFileName;
38 | walker.walk(listener, tree);
39 |
40 | String icfileName = temp[temp.length - 1] + ".ic";
41 | String errorfileName = temp[temp.length - 1] + ".error";
42 | String symbolName = temp[temp.length - 1] + ".symbol";
43 |
44 | MyICBuilder icBuilder = new MyICBuilder();
45 | icBuilder.Errorfilename=errorfileName;
46 | icBuilder.Icfilename=icfileName;
47 | icBuilder.Symbolname=symbolName;
48 | ASTNode node = listener.NodeStack.peek();
49 | icBuilder.test(node);
50 |
51 | String asmName=temp[temp.length - 1]+".asm";
52 | MyCodeGen codeGen = new MyCodeGen("x86",icBuilder);
53 | codeGen.ASMName=asmName;
54 | codeGen.run();
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/icgen/LabelGenerator.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.icgen;
2 |
3 | import bit.minisys.minicc.parser.ast.ASTNode;
4 | import bit.minisys.minicc.parser.ast.ASTVisitor;
5 |
6 | public class LabelGenerator extends ASTNode {
7 | private Integer id;
8 | public String Type;
9 | public Integer num;
10 | public String name() {
11 | if(id==0){
12 | return "@"+Type;
13 | }
14 | else return "@"+id+Type;
15 | }
16 | @Override
17 | public void accept(ASTVisitor visitor) throws Exception {
18 |
19 | }
20 | public LabelGenerator(Integer id, String Type, Integer num) {
21 | super("TemporaryValue");
22 | this.id = id;
23 | this.Type=Type;
24 | this.num=num;
25 | }
26 | public LabelGenerator(String type) {
27 | super(type);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/icgen/MyICBuilder.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.icgen;
2 |
3 | import bit.minisys.minicc.parser.ast.*;
4 | import bit.minisys.minicc.semantic.FuncTable;
5 | import bit.minisys.minicc.semantic.VarTable;
6 |
7 |
8 | import java.io.File;
9 | import java.io.FileWriter;
10 | import java.io.IOException;
11 | import java.util.*;
12 |
13 | public class MyICBuilder {
14 | public String Errorfilename;
15 | public String Icfilename;
16 | public String Symbolname;
17 | public static List ProcTable = new LinkedList<>();
18 | public static List GlobalVarTable = new LinkedList<>();
19 | public static List ErrorTable = new LinkedList<>();
20 | public static int tmpRegID=0;
21 | public static int tmpLabelID=1;
22 | public static int IfLabelID=0;
23 | public static Map map;
24 | public static List quats;
25 | private static ASTNode loopStartLabel=null;
26 | private static ASTNode loopNextLabel=null;
27 | private static ASTNode loopEndLabel=null;
28 | private static Map Label;
29 | public static List Scope=new LinkedList<>();
30 | public static Stack> Var = new Stack<>();
31 | public MyICBuilder() {
32 | map = new HashMap();
33 | Label=new HashMap();
34 | quats = new LinkedList();
35 | tmpRegID = 0;
36 | }
37 |
38 | public static void visit(ASTCompilationUnit astCompilationUnit){
39 | for(ASTNode child : astCompilationUnit.items){
40 | if(child instanceof ASTDeclaration){
41 | visit((ASTDeclaration) child);
42 | }
43 | else if(child instanceof ASTFunctionDefine){
44 | visit((ASTFunctionDefine) child);
45 | }
46 | }
47 | }
48 | public static void GetfuncDeclara(FuncTable funcTable,ASTFunctionDeclarator declarator){
49 | ASTVariableDeclarator variableDeclarator = (ASTVariableDeclarator) declarator.declarator;
50 | ASTIdentifier identifier=variableDeclarator.identifier;
51 | funcTable.funcName=identifier.value;
52 | funcTable.type=declarator.getType();
53 | }
54 | public static void GetVarTable(VarTable varTable,ASTInitList node){
55 | ASTNode child = node.declarator;
56 | if(child.getClass()==ASTVariableDeclarator.class){
57 | ASTIdentifier identifier=((ASTVariableDeclarator) child).identifier;
58 | varTable.name=identifier.value;
59 | varTable.dimension=1;
60 | varTable.length=1;
61 | varTable.type=child.getType();
62 | if(node.exprs!=null){
63 | varTable.value = node.exprs.get(0);
64 | visit(varTable.value);
65 | ASTNode opnd=map.get(varTable.value);
66 | Quat quat = new Quat("=",identifier,opnd,null);
67 | quats.add(quat);
68 | }
69 | }
70 | else if(child.getClass()==ASTArrayDeclarator.class){
71 | varTable.type=child.getType();
72 | int dim=0;
73 | int length=1;
74 | while (child.getClass()!=ASTVariableDeclarator.class){
75 | ASTNode integer=((ASTArrayDeclarator) child).expr;
76 | length=length*((ASTIntegerConstant) integer).value;
77 | dim=dim+1;
78 | child=((ASTArrayDeclarator) child).declarator;
79 | }
80 | ASTIdentifier identifier=((ASTVariableDeclarator) child).identifier;
81 | varTable.name=identifier.value;
82 | varTable.dimension=dim;
83 | varTable.length=length;
84 | }
85 | }
86 | public static void visit(ASTDeclaration astDeclaration){
87 | if(astDeclaration.parent.getClass()==ASTCompilationUnit.class){
88 | ASTToken astToken = astDeclaration.specifiers.get(0);
89 | for(ASTInitList astInitList : astDeclaration.initLists){
90 | ASTDeclarator declarator = astInitList.declarator;
91 | if(declarator.getClass()==ASTFunctionDeclarator.class){
92 | FuncTable funcTable = new FuncTable();
93 | funcTable.VariableTable=new LinkedList<>();
94 | funcTable.specifiers=astDeclaration.specifiers.get(0).value;
95 | GetfuncDeclara(funcTable,(ASTFunctionDeclarator) declarator);
96 | ProcTable.add(funcTable);
97 | }
98 | else{
99 | VarTable varTable = new VarTable();
100 | varTable.specifiers=astToken.value;
101 | GetVarTable(varTable,astInitList);
102 | boolean flag=true;
103 | for (VarTable var:GlobalVarTable){
104 | if(var.name.equals(varTable.name)){
105 | flag=false;
106 | break;
107 | }
108 | }
109 | if(!flag){
110 | String Error="ES02: var_"+varTable.name+"_defined_again\n";
111 | ErrorTable.add(Error);
112 | }
113 | GlobalVarTable.add(varTable);
114 | }
115 | }
116 | }
117 | else if(astDeclaration.parent.getClass()==ASTIterationDeclaredStatement.class){
118 | ASTToken astToken = astDeclaration.specifiers.get(0);
119 | for(ASTInitList astInitList : astDeclaration.initLists){
120 | ASTDeclarator declarator = astInitList.declarator;
121 | VarTable varTable = new VarTable();
122 | varTable.specifiers=astToken.value;
123 | GetVarTable(varTable,astInitList);
124 | boolean flag=true;
125 | for (VarTable var:Scope){
126 | if(var.name.equals(varTable.name)){
127 | flag=false;
128 | break;
129 | }
130 | }
131 | if(!flag){
132 | String Error="ES02: var_"+varTable.name+"_defined_again\n";
133 | ErrorTable.add(Error);
134 | }
135 | Scope.add(varTable);
136 | }
137 | }
138 | else if(astDeclaration.parent.getClass()==ASTCompoundStatement.class){
139 | ASTToken astToken = astDeclaration.specifiers.get(0);
140 | for(ASTInitList astInitList : astDeclaration.initLists){
141 | ASTDeclarator declarator = astInitList.declarator;
142 | VarTable varTable = new VarTable();
143 | varTable.specifiers=astToken.value;
144 | GetVarTable(varTable,astInitList);
145 | boolean flag=true;
146 | for (VarTable var:Scope){
147 | if(var.name.equals(varTable.name)){
148 | flag=false;
149 | break;
150 | }
151 | }
152 | if(!flag){
153 | String Error="ES02: var_"+varTable.name+"_defined_again\n";
154 | ErrorTable.add(Error);
155 | }
156 | Scope.add(varTable);
157 | }
158 | }
159 | else{
160 | ASTCompoundStatement astCompoundStatement = (ASTCompoundStatement) astDeclaration.parent;
161 | ASTFunctionDefine astFunctionDefine = (ASTFunctionDefine) astCompoundStatement.parent;
162 | ASTFunctionDeclarator astFunctionDeclarator = (ASTFunctionDeclarator) astFunctionDefine.declarator;
163 | ASTVariableDeclarator astVariableDeclarator = (ASTVariableDeclarator) astFunctionDeclarator.declarator;
164 | ASTIdentifier astIdentifier = astVariableDeclarator.identifier;
165 | for(ASTInitList astInitList : astDeclaration.initLists){
166 | VarTable varTable = new VarTable();
167 | varTable.specifiers=astDeclaration.specifiers.get(0).value;
168 | GetVarTable(varTable,astInitList);
169 | for(FuncTable funcTable : ProcTable){
170 | if(funcTable.funcName.equals(astIdentifier.value)){
171 | funcTable.VariableTable.add(varTable);
172 | }
173 | }
174 | }
175 | }
176 | }
177 | public static void visit(ASTFunctionDefine astFunctionDefine){
178 | FuncTable funcTable = new FuncTable();
179 | funcTable.VariableTable=new LinkedList<>();
180 | funcTable.type=astFunctionDefine.getType();
181 | ASTFunctionDeclarator astFunctionDeclarator = (ASTFunctionDeclarator) astFunctionDefine.declarator;
182 | ASTVariableDeclarator astVariableDeclarator = (ASTVariableDeclarator) astFunctionDeclarator.declarator;
183 | for(int i=0;i>=") ||op.equals("&=") ||op.equals("^=") ||op.equals("|=")) {
329 | visit(astBinaryExpression.expr1);
330 | res = map.get(astBinaryExpression.expr1);
331 | if (astBinaryExpression.expr2 instanceof ASTIdentifier) {
332 | opnd1 = astBinaryExpression.expr2;
333 | }
334 |
335 | else if(astBinaryExpression.expr2 instanceof ASTIntegerConstant) {
336 | opnd1 = astBinaryExpression.expr2;
337 | }
338 | else if(astBinaryExpression.expr2 instanceof ASTStringConstant) {
339 | opnd1 = astBinaryExpression.expr2;
340 | }
341 | else if(astBinaryExpression.expr2 instanceof ASTBinaryExpression) {
342 | ASTBinaryExpression value = (ASTBinaryExpression) astBinaryExpression.expr2;
343 | op = value.op.value;
344 | visit(value.expr1);
345 | opnd1 = map.get(value.expr1);
346 | visit(value.expr2);
347 | opnd2 = map.get(value.expr2);
348 | }
349 |
350 | else if(astBinaryExpression.expr2 instanceof ASTUnaryExpression){
351 | ASTUnaryExpression value = (ASTUnaryExpression) astBinaryExpression.expr2;
352 | op = value.op.value;
353 | visit(value);
354 | opnd1 = map.get(value.expr);
355 | }
356 | else if(astBinaryExpression.expr2 instanceof ASTPostfixExpression){
357 | ASTPostfixExpression value = (ASTPostfixExpression) astBinaryExpression.expr2;
358 | op = value.op.value;
359 | visit(value);
360 | opnd2 = map.get(value.expr);
361 | }
362 | else {
363 | visit(astBinaryExpression.expr2);
364 | opnd1=map.get(astBinaryExpression.expr2);
365 | // else ...
366 | }
367 | }
368 | else {
369 | res = new TemporaryValue(++tmpRegID);
370 | VarTable varTable = new VarTable();
371 | varTable.name=((TemporaryValue) res).name();
372 | varTable.type="VariableDeclarator";
373 | Scope.add(varTable);
374 | visit(astBinaryExpression.expr1);
375 | opnd1 = map.get(astBinaryExpression.expr1);
376 | visit(astBinaryExpression.expr2);
377 | opnd2 = map.get(astBinaryExpression.expr2);
378 | }
379 |
380 | // build quat
381 | Quat quat = new Quat(op, res, opnd1, opnd2);
382 | quats.add(quat);
383 | map.put(astBinaryExpression, res);
384 | }
385 | public static void visit(ASTIdentifier astIdentifier){
386 | boolean flag=false;
387 | if(Scope.size()>0){
388 | for(VarTable varTable:Scope){
389 | if(astIdentifier.value.equals(varTable.name)){
390 | flag=true;
391 | break;
392 | }
393 | }
394 | }
395 | FuncTable funcTable=ProcTable.get(ProcTable.size()-1);
396 | if(funcTable.VariableTable.size()>0&&(!flag)){
397 | for(VarTable varTable : funcTable.VariableTable){
398 | if(astIdentifier.value.equals(varTable.name)){
399 | flag=true;
400 | break;
401 | }
402 | }
403 | }
404 | if(GlobalVarTable.size()>0&&(!flag)){
405 | for(VarTable varTable:GlobalVarTable){
406 | if(astIdentifier.value.equals(varTable.name)){
407 | flag=true;
408 | break;
409 | }
410 | }
411 | }
412 | if(!flag){
413 | String ERROR="ES01: "+astIdentifier.value+"_is_not_defined\n";
414 | ErrorTable.add(ERROR);
415 | }
416 | map.put(astIdentifier, astIdentifier);
417 | }
418 | public static void visit(ASTIntegerConstant astIntegerConstant){
419 | map.put(astIntegerConstant,astIntegerConstant);
420 | }
421 | public static void visit(ASTStringConstant astStringConstant){
422 | map.put(astStringConstant,astStringConstant);
423 | }
424 | public static void visit(ASTFunctionCall astFunctionCall){
425 | String type="";
426 | String name="";
427 | if(astFunctionCall.argList!=null){
428 | for(int i =0;i0){
479 | for(VarTable varTable:Scope){
480 | if(((ASTIdentifier) astExpression).value.equals(varTable.name)){
481 | flag=true;
482 | break;
483 | }
484 | }
485 | }
486 | FuncTable funcTable=ProcTable.get(ProcTable.size()-1);
487 | if(funcTable.VariableTable.size()>0&&(!flag)){
488 | for(VarTable varTable : funcTable.VariableTable){
489 | if(((ASTIdentifier) astExpression).value.equals(varTable.name)){
490 | flag=true;
491 | break;
492 | }
493 | }
494 | }
495 | if(GlobalVarTable.size()>0&&(!flag)){
496 | for(VarTable varTable:GlobalVarTable){
497 | if(((ASTIdentifier) astExpression).value.equals(varTable.name)){
498 | flag=true;
499 | break;
500 | }
501 | }
502 | }
503 | if(!flag){
504 | String ERROR="ES01: "+((ASTIdentifier) astExpression).value+"_is_not_defined\n";
505 | ErrorTable.add(ERROR);
506 | }
507 | opnd1=astExpression;
508 | map.put(astArrayAccess.arrayName,astExpression);
509 | }
510 | else{
511 | visit(astExpression);
512 | opnd1=map.get(astExpression);
513 | }
514 | res = new TemporaryValue(++tmpRegID);
515 | VarTable varTable = new VarTable();
516 | varTable.name=((TemporaryValue) res).name();
517 | varTable.type="VariableDeclarator";
518 | Scope.add(varTable);
519 | map.put(astArrayAccess,res);
520 | Quat quat = new Quat("[]",res,opnd1,opnd2);
521 | quats.add(quat);
522 | }
523 |
524 |
525 | /**
526 | * Statement
527 | * @JsonSubTypes.Type(value = ASTBreakStatement.class,name = "BreakStatement"),
528 | * @JsonSubTypes.Type(value = ASTCompoundStatement.class,name = "CompoundStatement"),
529 | * @JsonSubTypes.Type(value = ASTContinueStatement.class,name="ContinueStatement"),
530 | * @JsonSubTypes.Type(value = ASTExpressionStatement.class,name = "ExpressionStatement"),
531 | * @JsonSubTypes.Type(value = ASTGotoStatement.class,name = "GotoStatement"),
532 | * @JsonSubTypes.Type(value = ASTIterationDeclaredStatement.class,name = "IterationDeclaredStatement"),
533 | * @JsonSubTypes.Type(value = ASTIterationStatement.class,name = "IterationStatement"),
534 | * @JsonSubTypes.Type(value = ASTLabeledStatement.class,name = "LabeledStatement"),
535 | * @JsonSubTypes.Type(value = ASTReturnStatement.class,name = "ReturnStatement"),
536 | * @JsonSubTypes.Type(value = ASTSelectionStatement.class,name = "SelectionStatement")
537 | */
538 | public static void visit(ASTStatement statement) {
539 | if(statement instanceof ASTIterationDeclaredStatement) {
540 | visit((ASTIterationDeclaredStatement)statement);
541 | }else if(statement instanceof ASTIterationStatement) {
542 | visit((ASTIterationStatement)statement);
543 | }else if(statement instanceof ASTCompoundStatement) {
544 | visit((ASTCompoundStatement)statement);
545 | }else if(statement instanceof ASTSelectionStatement) {
546 | visit((ASTSelectionStatement)statement);
547 | }else if(statement instanceof ASTExpressionStatement) {
548 | visit((ASTExpressionStatement)statement);
549 | }else if(statement instanceof ASTBreakStatement) {
550 | visit((ASTBreakStatement)statement);
551 | }else if(statement instanceof ASTContinueStatement) {
552 | visit((ASTContinueStatement)statement);
553 | }else if(statement instanceof ASTReturnStatement) {
554 | visit((ASTReturnStatement)statement);
555 | }else if(statement instanceof ASTGotoStatement) {
556 | visit((ASTGotoStatement)statement);
557 | }else if(statement instanceof ASTLabeledStatement) {
558 | visit((ASTLabeledStatement)statement);
559 | }
560 | }
561 | public static void visit(ASTSelectionStatement astSelectionStatement){
562 | IfLabelID++;
563 | ASTNode StartCheckIfLabel=new LabelGenerator(IfLabelID,"If",0);
564 | quats.add(new Quat("Label",StartCheckIfLabel,null,null));
565 | for(ASTExpression astExpression:astSelectionStatement.cond){
566 | visit(astExpression);
567 | }
568 | ASTNode res = map.get(astSelectionStatement.cond.get(0));
569 | ASTNode OtherwiseLabel=new LabelGenerator(IfLabelID,"Else",0);
570 | ASTNode EndifLabel=new LabelGenerator(IfLabelID,"Endif",0);
571 | if(astSelectionStatement.otherwise!=null)
572 | {
573 | quats.add(new Quat("JF",OtherwiseLabel,res,null));
574 | }
575 | else{
576 | quats.add(new Quat("JF",EndifLabel,null,null));
577 | }
578 | visit(astSelectionStatement.then);
579 | //ASTNode EndifLabel=new LabelGenerator(IfLabelID,"Endif",0);
580 | if(astSelectionStatement.otherwise!=null){
581 | quats.add(new Quat("JMP",EndifLabel,null,null));
582 | quats.add(new Quat("Label",OtherwiseLabel,null,null));
583 | visit(astSelectionStatement.otherwise);
584 | }
585 | quats.add(new Quat("Label",EndifLabel,null,null));
586 | }
587 | public static void visit(ASTCompoundStatement astCompoundStatement){
588 | boolean flag=false;
589 | if(astCompoundStatement.parent.getClass()==ASTFunctionDefine.class){
590 | flag=true;
591 | }
592 | Var.push(new LinkedList<>());
593 | for(ASTNode blockItem : astCompoundStatement.blockItems){
594 | if(blockItem instanceof ASTDeclaration){
595 | Scope=Var.peek();
596 | visit((ASTDeclaration) blockItem);
597 | }
598 | else if(blockItem instanceof ASTStatement){
599 | visit((ASTStatement) blockItem);
600 | }
601 | }
602 | if(flag){
603 | FuncTable funcTable=ProcTable.get(ProcTable.size()-1);
604 | Scope=Var.peek();
605 | if(Scope.size()!=0){
606 | for(int i = 0;i quats;
15 | public MyICPrinter(List quats) {
16 | this.quats = quats;
17 | }
18 | public void print(String filename) {
19 | StringBuilder sb = new StringBuilder();
20 | for (Quat quat : quats) {
21 | String op = quat.getOp();
22 | String res = astStr(quat.getRes());
23 | String opnd1 = astStr(quat.getOpnd1());
24 | String opnd2 = astStr(quat.getOpnd2());
25 | sb.append("("+op+","+ opnd1+","+opnd2 +"," + res+")\n");
26 | }
27 | // write
28 | try {
29 | FileWriter fileWriter = new FileWriter(new File(filename));
30 | fileWriter.write(sb.toString());
31 | fileWriter.close();
32 | } catch (IOException e) {
33 | e.printStackTrace();
34 | }
35 | }
36 |
37 | private String astStr(ASTNode node) {
38 | if (node == null) {
39 | return "";
40 | }else if (node instanceof ASTIdentifier) {
41 | return ((ASTIdentifier)node).value;
42 | }else if (node instanceof ASTIntegerConstant) {
43 | return ((ASTIntegerConstant)node).value+"";
44 | }else if (node instanceof TemporaryValue) {
45 | return ((TemporaryValue)node).name();
46 | }
47 | else if(node instanceof ASTStringConstant){
48 | return ((ASTStringConstant) node).value;
49 | }
50 | else if(node instanceof LabelGenerator){
51 | return ((LabelGenerator) node).name();
52 | }
53 | else {
54 | return "";
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/icgen/Quat.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.icgen;
2 |
3 | import bit.minisys.minicc.parser.ast.ASTNode;
4 |
5 | // ��Ԫʽ��ʽ���м����, �������ͷ���ֵ�Ľṹֱ��ʹ��AST�ڵ㣬Ҳ�����Զ���IR�ڵ�
6 | public class Quat {
7 | private String op;
8 | private ASTNode res;
9 | private ASTNode opnd1;
10 | private ASTNode opnd2;
11 | public Quat(String op, ASTNode res, ASTNode opnd1, ASTNode opnd2) {
12 | this.op = op;
13 | this.res = res;
14 | this.opnd1 = opnd1;
15 | this.opnd2 = opnd2;
16 | }
17 |
18 | public String getOp() {
19 | return op;
20 | }
21 | public ASTNode getOpnd1() {
22 | return opnd1;
23 | }
24 | public ASTNode getOpnd2() {
25 | return opnd2;
26 | }
27 | public ASTNode getRes() {
28 | return res;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/icgen/SymbolPrinter.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.icgen;
2 |
3 | import bit.minisys.minicc.semantic.FuncTable;
4 | import bit.minisys.minicc.semantic.VarTable;
5 |
6 | import java.io.File;
7 | import java.io.FileWriter;
8 | import java.io.IOException;
9 | import java.util.LinkedList;
10 | import java.util.List;
11 |
12 | public class SymbolPrinter {
13 | private List ProcTable;
14 | private List GlobalVarTable;
15 | private List Scope;
16 | public SymbolPrinter() {
17 | ProcTable = new LinkedList<>();
18 | GlobalVarTable = new LinkedList<>();
19 | Scope=new LinkedList<>();
20 | }
21 | public void SetProcTable(List procTable){
22 | this.ProcTable=procTable;
23 | }
24 | public void SetGlobalVarTable(List GlobalVarTable){
25 | this.GlobalVarTable= GlobalVarTable;
26 | }
27 | public void SetScope(List Scope){
28 | this.GlobalVarTable= Scope;
29 | }
30 | public void PrintProcTable(String filename){
31 | StringBuilder sb = new StringBuilder();
32 | sb.append("ProcTable\n");
33 | for(FuncTable funcTable:this.ProcTable){
34 | sb.append("FuncTable\n");
35 | sb.append(funcTable.specifiers+" | "+funcTable.funcName+" | "+funcTable.type+"\n");
36 | if(funcTable.VariableTable.size()!=0){
37 | sb.append(funcTable.funcName+"'s"+" VariableTable\n");
38 | for(VarTable varTable:funcTable.VariableTable){
39 | sb.append(varTable.specifiers+" | "+varTable.name+" | "+varTable.type+"\n");
40 | }
41 | }
42 | }
43 | if(GlobalVarTable.size()!=0){
44 | sb.append("GlobalVarTable\n");
45 | for(VarTable varTable:GlobalVarTable){
46 | sb.append(varTable.specifiers+" | "+varTable.name+" | "+varTable.type+"\n");
47 | }
48 | }
49 | try {
50 | FileWriter fileWriter = new FileWriter(new File(filename));
51 | fileWriter.write(sb.toString());
52 | fileWriter.close();
53 | } catch (IOException e) {
54 | e.printStackTrace();
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/icgen/TemporaryValue.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.icgen;
2 |
3 | import bit.minisys.minicc.parser.ast.ASTNode;
4 | import bit.minisys.minicc.parser.ast.ASTVisitor;
5 |
6 | // ��ʱ����
7 | public class TemporaryValue extends ASTNode{
8 |
9 | private Integer id;
10 |
11 | public String name() {
12 | return "@"+id;
13 | }
14 | @Override
15 | public void accept(ASTVisitor visitor) throws Exception {
16 |
17 | }
18 | public TemporaryValue(Integer id) {
19 | super("TemporaryValue");
20 | this.id = id;
21 | }
22 | public TemporaryValue(String type) {
23 | super(type);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/ncgen/MyCodeGen.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.ncgen;
2 |
3 | import bit.minisys.minicc.icgen.LabelGenerator;
4 | import bit.minisys.minicc.icgen.MyICBuilder;
5 | import bit.minisys.minicc.icgen.Quat;
6 | import bit.minisys.minicc.icgen.TemporaryValue;
7 | import bit.minisys.minicc.parser.ast.*;
8 | import bit.minisys.minicc.pp.internal.B;
9 | import bit.minisys.minicc.semantic.FuncTable;
10 | import bit.minisys.minicc.semantic.VarTable;
11 |
12 | import java.io.File;
13 | import java.io.FileWriter;
14 | import java.io.IOException;
15 | import java.util.*;
16 |
17 | public class MyCodeGen {
18 | public static String CodeType;
19 | private static int Tab;
20 | public String ASMName;
21 | public static MyICBuilder Builder;
22 | private static boolean printStr;
23 | private static boolean printflag;
24 | private static boolean printInt;
25 | private static boolean GetInt;
26 | private static boolean GetIntFunc;
27 | private static String funcname;
28 | public static Stack ParamStack;
29 | public static Map TempValue;
30 | public static List print_scanf;
31 | public static StringBuilder stringBuilder;
32 | private static String jumpOP;
33 | private static boolean array;
34 | public static String arrayName;
35 | public static String AstStr(ASTNode node){
36 | if (node == null) {
37 | return "";
38 | }else if (node instanceof ASTIdentifier) {
39 | return ((ASTIdentifier)node).value;
40 | }else if (node instanceof ASTIntegerConstant) {
41 | return ((ASTIntegerConstant)node).value+"";
42 | }else if (node instanceof TemporaryValue) {
43 | return ((TemporaryValue)node).name();
44 | }
45 | else if(node instanceof ASTStringConstant){
46 | return ((ASTStringConstant) node).value;
47 | }
48 | else if(node instanceof LabelGenerator){
49 | return ((LabelGenerator) node).name();
50 | }
51 | else {
52 | return "";
53 | }
54 | }
55 | public MyCodeGen(String type,MyICBuilder icBuilder){
56 | CodeType=type;
57 | Tab=0;
58 | Builder=icBuilder;
59 | printInt=false;
60 | array=false;
61 | printStr=false;
62 | GetInt=false;
63 | GetIntFunc=false;
64 | printflag=false;
65 | stringBuilder = new StringBuilder();
66 | ParamStack=new Stack();
67 | TempValue=new HashMap();
68 | print_scanf=new LinkedList<>();
69 | funcname="null";
70 | }
71 | public static void GenTab(){
72 | for(int i=0;i elements;
12 |
13 | public ASTArrayAccess() {
14 | super("ArrayAccess");
15 | }
16 | public ASTArrayAccess(ASTExpression arrayname, List element) {
17 | super("ArrayAccess");
18 | this.arrayName = arrayname;
19 | this.elements = element;
20 | }
21 |
22 | public static class Builder{
23 | private ASTExpression arrayName;
24 | private List elements = new LinkedList();
25 |
26 | public void setArrayName(ASTExpression arrayName) {
27 | this.arrayName = arrayName;
28 | }
29 |
30 | public void addElement(Object object) {
31 | if(elements == null) {
32 | elements = new LinkedList();
33 | }
34 | if(object instanceof ASTExpression) {
35 | elements.add((ASTExpression)object);
36 | }else if(object instanceof List) {
37 | for (Object element : (List>)object) {
38 | addElement(element);
39 | }
40 | }else {
41 | throw new RuntimeException("ArrayAccess's elements accept expression only.");
42 | }
43 | }
44 | public ASTArrayAccess build() {
45 | return new ASTArrayAccess(arrayName,elements);
46 | }
47 | }
48 | @Override
49 | public void accept(ASTVisitor visitor) throws Exception {
50 | visitor.visit(this);
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTArrayDeclarator.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.List;
4 |
5 | import com.fasterxml.jackson.annotation.JsonIgnore;
6 | import com.fasterxml.jackson.annotation.JsonTypeName;
7 |
8 | @JsonTypeName("ArrayDeclarator")
9 | public class ASTArrayDeclarator extends ASTDeclarator{
10 | public ASTDeclarator declarator;
11 | public ASTExpression expr;
12 |
13 |
14 | public ASTArrayDeclarator() {
15 | super("ArrayDeclarator");
16 | }
17 | public ASTArrayDeclarator(ASTDeclarator declarator, ASTExpression expressions )
18 | {
19 | super("ArrayDeclarator");
20 | this.declarator = declarator;
21 | this.expr = expressions;
22 | }
23 |
24 | public static class Builder {
25 | private ASTDeclarator decl;
26 | private ASTExpression exprs;
27 |
28 | public void setDecl(ASTDeclarator decl) {
29 | this.decl = decl;
30 | }
31 |
32 |
33 | public void setExprs(Object exprs) {
34 | if(exprs instanceof List) {
35 | this.exprs = (ASTExpression) ((List>) exprs).get(0);
36 | }else if(exprs instanceof ASTExpression) {
37 | this.exprs = (ASTExpression) exprs;
38 | }
39 | }
40 |
41 | public ASTArrayDeclarator build() {
42 | return new ASTArrayDeclarator(decl, exprs);
43 | }
44 | }
45 |
46 | @Override
47 | public void accept(ASTVisitor visitor) throws Exception {
48 | visitor.visit(this);
49 | }
50 | @JsonIgnore
51 | @Override
52 | public String getName() {
53 | if(declarator != null)
54 | return declarator.getName();
55 | return null;
56 | }
57 |
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTBinaryExpression.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("BinaryExpression")
6 | public class ASTBinaryExpression extends ASTExpression{
7 |
8 | public ASTToken op;
9 | public ASTExpression expr1;
10 | public ASTExpression expr2;
11 |
12 | public ASTBinaryExpression() {
13 | super("BinaryExpression");
14 | }
15 |
16 | public ASTBinaryExpression(ASTToken op,ASTExpression e1,ASTExpression e2) {
17 | super("BinaryExpression");
18 | this.op = op;
19 | this.expr1 = e1;
20 | this.expr2 = e2;
21 | }
22 | @Override
23 | public void accept(ASTVisitor visitor) throws Exception {
24 | visitor.visit(this);
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTBreakStatement.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("BreakStatement")
6 | public class ASTBreakStatement extends ASTStatement{
7 |
8 | public ASTBreakStatement() {
9 | super("BreakStatement");
10 | }
11 |
12 | @Override
13 | public void accept(ASTVisitor visitor) throws Exception {
14 | visitor.visit(this);
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTCastExpression.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("CastExpression")
6 | public class ASTCastExpression extends ASTExpression{
7 |
8 | public ASTTypename typename;
9 | public ASTExpression expr;
10 |
11 | public ASTCastExpression() {
12 | super("CastExpression");
13 | }
14 | public ASTCastExpression(ASTTypename typename, ASTExpression expr) {
15 | super("CastExpression");
16 | this.typename = typename;
17 | this.expr = expr;
18 | }
19 | @Override
20 | public void accept(ASTVisitor visitor) throws Exception {
21 | visitor.visit(this);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTCharConstant.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("CharConstant")
6 | public class ASTCharConstant extends ASTExpression{
7 | //public Character value;
8 | public String value;
9 | public Integer tokenId;
10 | public ASTCharConstant() {
11 | super("CharConstant");
12 | }
13 | public ASTCharConstant(String value,Integer tokenId) {
14 | super("CharConstant");
15 | this.value = value;
16 | this.tokenId = tokenId;
17 | }
18 | @Override
19 | public void accept(ASTVisitor visitor) throws Exception {
20 | visitor.visit(this);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTCompilationUnit.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import com.fasterxml.jackson.annotation.JsonTypeName;
6 |
7 | @JsonTypeName("Program")
8 | public class ASTCompilationUnit extends ASTNode{
9 |
10 | public List items; // item ֻ����declaration �� functionDefine
11 |
12 |
13 | public ASTCompilationUnit() {
14 | super("CompilationUnit");
15 | this.items = new ArrayList();
16 | }
17 |
18 | public ASTCompilationUnit(List items) {
19 | super("Program");
20 | this.items = items;
21 | }
22 |
23 | public static class Builder{
24 | public List items = new ArrayList();
25 | public void addNode(Object node){
26 | if (items == null) {
27 | items = new ArrayList();
28 | }
29 | if(node instanceof ASTDeclaration) {
30 | items.add((ASTDeclaration)node);
31 | }else if(node instanceof ASTFunctionDefine) {
32 | items.add((ASTFunctionDefine)node);
33 | }else if(node instanceof List){
34 | for (Object element : (List>)node) {
35 | addNode(element);
36 | }
37 | //((List) node).stream().forEachOrdered(this::addNode);
38 | }else {
39 | throw new RuntimeException("externalDeclaration Invalid type.");
40 | }
41 | }
42 | public ASTCompilationUnit build() {
43 | return new ASTCompilationUnit(this.items);
44 | }
45 | }
46 |
47 | @Override
48 | public void accept(ASTVisitor visitor) throws Exception {
49 | visitor.visit(this);
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTCompoundStatement.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import com.fasterxml.jackson.annotation.JsonTypeName;
7 |
8 | @JsonTypeName("CompoundStatement")
9 | public class ASTCompoundStatement extends ASTStatement{
10 | public List blockItems; // ����ֻ���� ���� ���� �������
11 |
12 | public ASTCompoundStatement() {
13 | super("CompoundStatement");
14 | this.blockItems = new ArrayList();
15 | }
16 | public ASTCompoundStatement(List items) {
17 | super("CompoundStatement");
18 | this.blockItems = items;
19 | }
20 | @Override
21 | public void accept(ASTVisitor visitor) throws Exception {
22 | visitor.visit(this);
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTConditionExpression.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.LinkedList;
4 |
5 | import com.fasterxml.jackson.annotation.JsonTypeName;
6 |
7 | @JsonTypeName("ConditionExpression")
8 | public class ASTConditionExpression extends ASTExpression{
9 |
10 | public ASTExpression condExpr;
11 | public LinkedList trueExpr;
12 | public ASTExpression falseExpr;
13 |
14 | public ASTConditionExpression() {
15 | super("ConditionExpression");
16 | }
17 |
18 | public ASTConditionExpression(ASTExpression cond,LinkedList trueExpr, ASTExpression falseExpr) {
19 | super("ConditionExpression");
20 | this.condExpr = cond;
21 | this.trueExpr = trueExpr;
22 | this.falseExpr = falseExpr;
23 | }
24 | @Override
25 | public void accept(ASTVisitor visitor) throws Exception {
26 | visitor.visit(this);
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTContinueStatement.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("ContinueStatement")
6 | public class ASTContinueStatement extends ASTStatement{
7 | public ASTContinueStatement() {
8 | super("ContinueStatement");
9 | }
10 |
11 | @Override
12 | public void accept(ASTVisitor visitor) throws Exception {
13 | visitor.visit(this);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTDeclaration.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.LinkedList;
4 | import java.util.List;
5 |
6 | import com.fasterxml.jackson.annotation.JsonTypeName;
7 |
8 | @JsonTypeName("Declaration")
9 | public class ASTDeclaration extends ASTNode{
10 |
11 | public List specifiers;
12 | public List initLists;
13 |
14 | public ASTDeclaration() {
15 | super("Declaration");
16 | }
17 |
18 | public ASTDeclaration(List specList, List initList) {
19 | super("Declaration");
20 | this.specifiers = specList;
21 | this.initLists = initList;
22 | }
23 |
24 |
25 | public static class Builder {
26 | private LinkedList specfiers=new LinkedList();
27 | private LinkedList initLists = new LinkedList();
28 |
29 | public void addSpecfiers(Object node) {
30 | if(specfiers == null) {
31 | specfiers=new LinkedList();
32 | }
33 | if (node instanceof ASTToken) specfiers.add((ASTToken) node);
34 | else if (node instanceof List) {
35 | for (Object element : (List>)node) {
36 | addSpecfiers(element);
37 | }
38 | //((List) node).stream().forEachOrdered(this::addSpecfiers);
39 | }
40 | else throw new RuntimeException("Declaration's specfiers accepts String only.");
41 | }
42 |
43 | public void addInitList(Object node) {
44 | if (initLists == null) {
45 | initLists = new LinkedList();
46 | }
47 | if (node instanceof ASTInitList) initLists.add((ASTInitList) node);
48 | else if (node instanceof List) {
49 | for (Object element : (List>)node) {
50 | addInitList(element);
51 | }
52 | //((List) node).stream().forEachOrdered(this::addInitList);
53 | }
54 | else throw new RuntimeException("Declaration's Initlist accepts InitList only.");
55 | }
56 |
57 | public ASTDeclaration build() { return new ASTDeclaration(specfiers,initLists); }
58 | }
59 | @Override
60 | public void accept(ASTVisitor visitor) throws Exception {
61 | visitor.visit(this);
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTDeclarator.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonIgnore;
4 | import com.fasterxml.jackson.annotation.JsonSubTypes;
5 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
6 | import com.fasterxml.jackson.annotation.JsonTypeName;
7 | import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
8 |
9 | @JsonTypeName("Declarator")
10 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME,include = As.PROPERTY,property = "type")
11 | @JsonSubTypes({
12 | @JsonSubTypes.Type(value = ASTArrayDeclarator.class,name = "ArrayDeclarator"),
13 | @JsonSubTypes.Type(value = ASTVariableDeclarator.class,name = "VariableDeclarator"),
14 | @JsonSubTypes.Type(value = ASTFunctionDeclarator.class,name="FunctionDeclarator"),
15 | })
16 | public abstract class ASTDeclarator extends ASTNode {
17 | public ASTDeclarator(String type) {
18 | super(type);
19 | }
20 |
21 | @Override
22 | public void accept(ASTVisitor visitor) throws Exception {
23 | visitor.visit(this);
24 | }
25 |
26 | @JsonIgnore
27 | public abstract String getName();
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTExpression.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonIgnore;
4 | import com.fasterxml.jackson.annotation.JsonSubTypes;
5 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
6 | import com.fasterxml.jackson.annotation.JsonTypeName;
7 | import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
8 |
9 | import bit.minisys.minicc.internal.symbol.Type;
10 |
11 | @JsonTypeName("Expression")
12 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME,include = As.PROPERTY,property = "type")
13 | @JsonSubTypes({
14 | @JsonSubTypes.Type(value = ASTIdentifier.class,name = "Identifier"),
15 | @JsonSubTypes.Type(value = ASTArrayAccess.class,name = "ArrayAccess"),
16 | @JsonSubTypes.Type(value = ASTBinaryExpression.class,name="BinaryExpression"),
17 | @JsonSubTypes.Type(value = ASTCastExpression.class,name = "CastExpression"),
18 | @JsonSubTypes.Type(value = ASTCharConstant.class,name = "CharConstant"),
19 | @JsonSubTypes.Type(value = ASTConditionExpression.class,name = "ConditionExpression"),
20 | @JsonSubTypes.Type(value = ASTFloatConstant.class,name = "FloatConstant"),
21 | @JsonSubTypes.Type(value = ASTFunctionCall.class,name = "FunctionCall"),
22 | @JsonSubTypes.Type(value = ASTIntegerConstant.class,name = "IntegerConstant"),
23 | @JsonSubTypes.Type(value = ASTMemberAccess.class,name = "MemberAccess"),
24 | @JsonSubTypes.Type(value = ASTPostfixExpression.class,name = "PostfixExpression"),
25 | @JsonSubTypes.Type(value = ASTStringConstant.class,name = "StringConstant"),
26 | @JsonSubTypes.Type(value = ASTUnaryExpression.class,name = "UnaryExpression"),
27 | @JsonSubTypes.Type(value = ASTUnaryTypename.class,name = "UnaryTypename")
28 | })
29 | public abstract class ASTExpression extends ASTNode{
30 |
31 | @JsonIgnore
32 | public Type type;
33 | @JsonIgnore
34 | public Boolean canAssigned;
35 |
36 | public ASTExpression(String type){
37 | super(type);
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTExpressionStatement.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.List;
4 |
5 | import com.fasterxml.jackson.annotation.JsonTypeName;
6 | @JsonTypeName("ExpressionStatement")
7 | public class ASTExpressionStatement extends ASTStatement{
8 |
9 | public List exprs;
10 | public ASTExpressionStatement() {
11 | super("ExpressionStatement");
12 | }
13 |
14 | public ASTExpressionStatement(List exprs) {
15 | super("ExpressionStatement");
16 | this.exprs = exprs;
17 | }
18 | @Override
19 | public void accept(ASTVisitor visitor) throws Exception {
20 | visitor.visit(this);
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTFloatConstant.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("FloatConstant")
6 | public class ASTFloatConstant extends ASTExpression{
7 | public Double value;
8 | public Integer tokenId;
9 |
10 | public ASTFloatConstant() {
11 | super("FloatConstant");
12 | }
13 | public ASTFloatConstant(Double value,Integer tokenId) {
14 | super("FloatConstant");
15 | this.value = value;
16 | this.tokenId = tokenId;
17 | }
18 | @Override
19 | public void accept(ASTVisitor visitor) throws Exception {
20 | visitor.visit(this);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTFunctionCall.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.LinkedList;
4 | import java.util.List;
5 |
6 | import com.fasterxml.jackson.annotation.JsonTypeName;
7 |
8 | @JsonTypeName("FunctionCall")
9 | public class ASTFunctionCall extends ASTExpression{
10 |
11 | public ASTExpression funcname;
12 | public List argList;
13 |
14 | public ASTFunctionCall() {
15 | super("FunctionCall");
16 | }
17 | public ASTFunctionCall(ASTExpression name, List args) {
18 | super("FunctionCall");
19 | this.funcname = name;
20 | this.argList = args;
21 | }
22 |
23 | public static class Builder{
24 | private ASTExpression name;
25 | private List argList=new LinkedList();
26 |
27 | public void setName(ASTExpression name) {
28 | this.name = name;
29 | }
30 |
31 | public void addArg(Object node) {
32 | if (argList == null) {
33 | argList=new LinkedList();
34 | }
35 | if(node instanceof ASTExpression) {
36 | argList.add((ASTExpression)node);
37 | }else if(node instanceof List) {
38 | for (Object element : (List>)node) {
39 | addArg(element);
40 | }
41 | //((List) node).stream().forEachOrdered(this::addArg);
42 | }else {
43 | throw new RuntimeException("FuncCall's argList accept Expression only.");
44 | }
45 | }
46 | public ASTFunctionCall build() {
47 | return new ASTFunctionCall(name, argList);
48 | }
49 | }
50 |
51 | @Override
52 | public void accept(ASTVisitor visitor) throws Exception {
53 | visitor.visit(this);
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTFunctionDeclarator.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.ArrayList;
4 | import java.util.LinkedList;
5 | import java.util.List;
6 |
7 | import com.fasterxml.jackson.annotation.JsonIgnore;
8 | import com.fasterxml.jackson.annotation.JsonTypeName;
9 |
10 | @JsonTypeName("FunctionDeclarator")
11 | public class ASTFunctionDeclarator extends ASTDeclarator{
12 | public ASTDeclarator declarator;
13 | public List params;
14 |
15 | public ASTFunctionDeclarator() {
16 | super("FunctionDeclarator");
17 | this.params = new ArrayList();
18 | }
19 |
20 | public ASTFunctionDeclarator(ASTDeclarator declarator,List paramsDeclarators) {
21 | super("FunctionDeclarator");
22 | this.declarator = declarator;
23 | this.params = paramsDeclarators;
24 | }
25 |
26 | public static class Builder{
27 | private ASTDeclarator decl;
28 | private List params=new LinkedList();
29 |
30 | public void setDecl(ASTDeclarator decl) {
31 | this.decl = decl;
32 | }
33 | public void addParams(Object node) {
34 | if(params == null) {
35 | params = new LinkedList();
36 | }
37 | if(node instanceof ASTParamsDeclarator) {
38 | params.add((ASTParamsDeclarator)node);
39 | }else if(node instanceof List) {
40 | for (Object element : (List>)node) {
41 | addParams(element);
42 | }
43 | // ((List) node).stream().forEachOrdered(this::addParams);
44 | }else {
45 | throw new RuntimeException("FunctionDeclator's params accept ParamsDeclarator only.");
46 | }
47 | }
48 | public ASTFunctionDeclarator build() {
49 | return new ASTFunctionDeclarator(decl, params);
50 | }
51 | }
52 | @Override
53 | public void accept(ASTVisitor visitor) throws Exception {
54 | visitor.visit(this);
55 | }
56 |
57 | @JsonIgnore
58 | @Override
59 | public String getName() {
60 | if(declarator !=null)
61 | return declarator.getName();
62 | return null;
63 | }
64 |
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTFunctionDefine.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.ArrayList;
4 | import java.util.LinkedList;
5 | import java.util.List;
6 |
7 | import com.fasterxml.jackson.annotation.JsonTypeName;
8 |
9 | @JsonTypeName("FunctionDefine")
10 | public class ASTFunctionDefine extends ASTNode{
11 |
12 | public List specifiers;
13 | public ASTDeclarator declarator;
14 | public ASTCompoundStatement body;
15 | public ASTFunctionDefine() {
16 | super("FunctionDefine");
17 | this.specifiers = new ArrayList();
18 | }
19 | public ASTFunctionDefine(List specList, ASTDeclarator declarator, ASTCompoundStatement bodyStatement) {
20 | super("FunctionDefine");
21 | this.specifiers = specList;
22 | this.declarator = declarator;
23 | this.body = bodyStatement;
24 | }
25 |
26 |
27 | public static class Builder{
28 | private LinkedList specifiers = new LinkedList();
29 | private ASTDeclarator declarator;
30 | private ASTCompoundStatement body;
31 |
32 | public void addSpecifiers(Object node) {
33 | if (specifiers == null) {
34 | specifiers = new LinkedList();
35 | }
36 | if(node instanceof ASTToken) {
37 | specifiers.add((ASTToken)node);
38 | }else if(node instanceof List){
39 | for (Object element : (List>)node) {
40 | addSpecifiers(element);
41 | }
42 | //((List) node).stream().forEachOrdered(this::addSpecifiers);
43 | }else {
44 | throw new RuntimeException("FunctionDefine's specifiers are not String");
45 | }
46 | }
47 |
48 | public void setDeclarator(ASTDeclarator declarator) {
49 | this.declarator = declarator;
50 | }
51 |
52 | public void setBody(ASTCompoundStatement body) {
53 | this.body = body;
54 | }
55 | public ASTFunctionDefine build() {
56 | return new ASTFunctionDefine(specifiers,declarator,body);
57 | }
58 | }
59 | @Override
60 | public void accept(ASTVisitor visitor) throws Exception {
61 | visitor.visit(this);
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTGotoStatement.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("GotoStatement")
6 | public class ASTGotoStatement extends ASTStatement{
7 | public ASTIdentifier label;
8 |
9 | public ASTGotoStatement() {
10 | super("GotoStatement");
11 | }
12 |
13 | public ASTGotoStatement(ASTIdentifier label) {
14 | super("GotoStatement");
15 | this.label = label;
16 | }
17 | @Override
18 | public void accept(ASTVisitor visitor) throws Exception {
19 | visitor.visit(this);
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTIdentifier.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | //import com.fasterxml.jackson.annotation.JsonIgnore;
4 | import com.fasterxml.jackson.annotation.JsonTypeName;
5 |
6 | //import bit.minisys.minicc.symbol.Symbol;
7 |
8 | // ��ʶ��
9 | @JsonTypeName("Identifier")
10 | public class ASTIdentifier extends ASTExpression{
11 | public String value;
12 | public Integer tokenId;
13 | //@JsonIgnore
14 | //public Symbol info;
15 | public ASTIdentifier() {
16 | super("Identifier");
17 | }
18 |
19 | public ASTIdentifier(String value,Integer tokenId) {
20 | super("Identifier");
21 | this.value = value;
22 | this.tokenId = tokenId;
23 | }
24 | @Override
25 | public void accept(ASTVisitor visitor) throws Exception {
26 | visitor.visit(this);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTInitList.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.LinkedList;
4 | import java.util.List;
5 |
6 |
7 | import com.fasterxml.jackson.annotation.JsonTypeName;
8 | @JsonTypeName("InitList")
9 | public class ASTInitList extends ASTNode{
10 | public ASTDeclarator declarator;
11 | public List exprs;
12 |
13 | public ASTInitList() {
14 | super("InitList");
15 | }
16 |
17 | public ASTInitList(ASTDeclarator d, List e) {
18 | super("InitList");
19 | this.declarator = d;
20 | this.exprs = e;
21 | }
22 |
23 | public static class Builder {
24 | private ASTDeclarator declarator;
25 | private List initialize = new LinkedList();
26 |
27 | public void setDeclarator(ASTDeclarator declarator) {
28 | this.declarator = declarator;
29 | }
30 |
31 | public void addInitialize(Object node) {
32 | if (initialize == null) {
33 | initialize = new LinkedList();
34 | }
35 | if(node instanceof ASTExpression) {
36 | initialize.add((ASTExpression)node);
37 | }else if(node instanceof List) {
38 | for (Object element : (List>)node) {
39 | addInitialize(element);
40 | }
41 | //((List) node).stream().forEachOrdered(this::addInitialize);
42 | }else {
43 | throw new RuntimeException("Initialize recepts Expression only.");
44 | }
45 | }
46 |
47 | public ASTInitList build() { return new ASTInitList(declarator,initialize); }
48 | }
49 | @Override
50 | public void accept(ASTVisitor visitor) throws Exception {
51 | visitor.visit(this);
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTIntegerConstant.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | // ���ͳ���
6 | @JsonTypeName("IntegerConstant")
7 | public class ASTIntegerConstant extends ASTExpression{
8 | public Integer value;
9 | public Integer tokenId;
10 |
11 | public ASTIntegerConstant() {
12 | super("IntegerConstant");
13 | }
14 | public ASTIntegerConstant(Integer value,Integer tokenId) {
15 | super("IntegerConstant");
16 | this.value = value;
17 | this.tokenId = tokenId;
18 | }
19 | @Override
20 | public void accept(ASTVisitor visitor) throws Exception {
21 | visitor.visit(this);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTIterationDeclaredStatement.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.LinkedList;
4 |
5 | import com.fasterxml.jackson.annotation.JsonTypeName;
6 | @JsonTypeName("IterationDeclaredStatement")
7 | public class ASTIterationDeclaredStatement extends ASTStatement{
8 |
9 | public ASTDeclaration init;
10 | public LinkedList cond;
11 | public LinkedList step;
12 |
13 | public ASTStatement stat;
14 |
15 | public ASTIterationDeclaredStatement() {
16 | super("IterationDeclaredStatement");
17 | }
18 | public ASTIterationDeclaredStatement(ASTDeclaration init,LinkedList cond,LinkedList step,ASTStatement stat) {
19 | super("IterationDeclaredStatement");
20 | this.init = init;
21 | this.cond = cond;
22 | this.step = step;
23 | this.stat = stat;
24 | }
25 | @Override
26 | public void accept(ASTVisitor visitor) throws Exception {
27 | visitor.visit(this);
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTIterationStatement.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.LinkedList;
4 |
5 | import com.fasterxml.jackson.annotation.JsonTypeName;
6 | @JsonTypeName("IterationStatement")
7 | public class ASTIterationStatement extends ASTStatement{
8 | public LinkedList init;
9 | public LinkedList cond;
10 | public LinkedList step;
11 |
12 | public ASTStatement stat;
13 | public ASTIterationStatement() {
14 | super("IterationStatement");
15 | }
16 | public ASTIterationStatement(LinkedList init,LinkedList cond,LinkedList step,ASTStatement stat) {
17 | super("IterationStatement");
18 | this.init = init;
19 | this.cond = cond;
20 | this.step = step;
21 | this.stat = stat;
22 | }
23 |
24 | @Override
25 | public void accept(ASTVisitor visitor) throws Exception {
26 | visitor.visit(this);
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTLabeledStatement.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("LabeledStatement")
6 | public class ASTLabeledStatement extends ASTStatement{
7 |
8 | public ASTIdentifier label;
9 | public ASTStatement stat;
10 |
11 | public ASTLabeledStatement() {
12 | super("LabeledStatement");
13 | }
14 | public ASTLabeledStatement(ASTIdentifier label, ASTStatement stat) {
15 | super("LabeledStatement");
16 | this.label = label;
17 | this.stat = stat;
18 | }
19 |
20 | @Override
21 | public void accept(ASTVisitor visitor) throws Exception {
22 | visitor.visit(this);
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTMemberAccess.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("MemberAccess")
6 | public class ASTMemberAccess extends ASTExpression{
7 |
8 | public ASTToken op;
9 | public ASTExpression master;
10 | public ASTIdentifier member;
11 | public ASTMemberAccess() {
12 | super("MemberAccess");
13 | }
14 | public ASTMemberAccess(ASTExpression master, ASTToken op, ASTIdentifier member) {
15 | super("MemberAccess");
16 | this.master = master;
17 | this.op = op;
18 | this.member = member;
19 | }
20 |
21 | @Override
22 | public void accept(ASTVisitor visitor) throws Exception {
23 | visitor.visit(this);
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTNode.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.ArrayList;
4 |
5 | import org.antlr.v4.runtime.tree.Tree;
6 |
7 | import com.fasterxml.jackson.annotation.JsonIgnore;
8 | import com.fasterxml.jackson.annotation.JsonSubTypes;
9 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
10 | import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
11 |
12 | import bit.minisys.minicc.internal.symbol.SymbolTable;
13 |
14 |
15 |
16 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME,include = As.PROPERTY,property = "type",visible = false)
17 | @JsonSubTypes({
18 | @JsonSubTypes.Type(value = ASTCompilationUnit.class,name = "Program"),
19 | @JsonSubTypes.Type(value = ASTExpression.class,name = "Expression"),
20 | @JsonSubTypes.Type(value = ASTStatement.class,name = "Statement"),
21 | @JsonSubTypes.Type(value = ASTFunctionDefine.class,name="FunctionDefine"),
22 | @JsonSubTypes.Type(value = ASTDeclaration.class,name = "Declaration"),
23 | @JsonSubTypes.Type(value = ASTToken.class,name = "Token"),
24 | @JsonSubTypes.Type(value = ASTTypename.class,name = "Typename"),
25 | @JsonSubTypes.Type(value = ASTDeclarator.class,name = "Declarator"),
26 | @JsonSubTypes.Type(value = ASTParamsDeclarator.class,name = "ParamsDeclarator"),
27 | @JsonSubTypes.Type(value = ASTInitList.class,name = "InitList"),
28 |
29 | })
30 | public abstract class ASTNode implements Tree{
31 | public abstract void accept(ASTVisitor visitor) throws Exception;
32 | @JsonIgnore
33 | private String type;
34 |
35 | @JsonIgnore
36 | public ArrayList children = new ArrayList();
37 |
38 | @JsonIgnore
39 | public ASTNode parent;
40 |
41 | @JsonIgnore
42 | public String getType() {
43 | return type;
44 | }
45 | public ASTNode(String type) {
46 | this.type = type;
47 | }
48 | @JsonIgnore
49 | public SymbolTable scope;
50 |
51 | @JsonIgnore
52 | public String getNodeText() {
53 | return this.type;
54 | }
55 |
56 | @JsonIgnore
57 | @Override
58 | public Tree getChild(int index) {
59 | // TODO Auto-generated method stub
60 | if(index < this.children.size()) {
61 | return this.children.get(index);
62 | }
63 | return null;
64 | }
65 |
66 | @JsonIgnore
67 | @Override
68 | public int getChildCount() {
69 | // TODO Auto-generated method stub
70 | return this.children.size();
71 | }
72 |
73 | @JsonIgnore
74 | @Override
75 | public Tree getParent() {
76 | // TODO Auto-generated method stub
77 | return this.parent;
78 | }
79 |
80 | @JsonIgnore
81 | @Override
82 | public Object getPayload() {
83 | // TODO Auto-generated method stub
84 | return this.type;
85 | }
86 |
87 | @JsonIgnore
88 | @Override
89 | public String toStringTree() {
90 | // TODO Auto-generated method stub
91 | return this.type;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTParamsDeclarator.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.ArrayList;
4 | import java.util.LinkedList;
5 | import java.util.List;
6 |
7 | import com.fasterxml.jackson.annotation.JsonTypeName;
8 |
9 | @JsonTypeName("ParamsDeclarator")
10 | public class ASTParamsDeclarator extends ASTNode{
11 |
12 | public List specfiers;
13 | public ASTDeclarator declarator;
14 |
15 | public ASTParamsDeclarator() {
16 | super("ParamsDeclarator");
17 | this.specfiers = new ArrayList();
18 | }
19 | public ASTParamsDeclarator(List specList, ASTDeclarator declarator) {
20 | super("ParamsDeclarator");
21 | this.specfiers = specList;
22 | this.declarator = declarator;
23 | }
24 |
25 | public static class Builder{
26 | private List specfiers=new LinkedList();
27 | private ASTDeclarator declarator;
28 |
29 | public void addSpecfiers(Object node) {
30 | if(specfiers == null) {
31 | specfiers = new LinkedList();
32 | }
33 | if (node instanceof ASTToken) specfiers.add((ASTToken) node);
34 | else if (node instanceof List) {
35 | for (Object element : (List>)node) {
36 | addSpecfiers(element);
37 | }
38 | //((List) node).stream().forEachOrdered(this::addSpecfiers);
39 | }
40 | else throw new RuntimeException("ParamsDeclarator specfiers accepts String only.");
41 | }
42 |
43 | public void setDeclarator(ASTDeclarator declarator) {
44 | this.declarator = declarator;
45 | }
46 |
47 | public ASTParamsDeclarator build() {
48 | return new ASTParamsDeclarator(specfiers,declarator);
49 | }
50 | }
51 |
52 | @Override
53 | public void accept(ASTVisitor visitor) throws Exception {
54 | visitor.visit(this);
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTPostfixExpression.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("PostfixExpression")
6 | public class ASTPostfixExpression extends ASTExpression{
7 |
8 | public ASTExpression expr;
9 | public ASTToken op;
10 |
11 | public ASTPostfixExpression() {
12 | super("PostfixExpression");
13 | }
14 | public ASTPostfixExpression(ASTExpression expr,ASTToken op){
15 | super("PostfixExpression");
16 | this.expr = expr;
17 | this.op = op;
18 | }
19 | @Override
20 | public void accept(ASTVisitor visitor) throws Exception {
21 | visitor.visit(this);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTReturnStatement.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.LinkedList;
4 |
5 | import com.fasterxml.jackson.annotation.JsonTypeName;
6 | @JsonTypeName("ReturnStatement")
7 | public class ASTReturnStatement extends ASTStatement{
8 |
9 | public LinkedList expr;
10 |
11 | public ASTReturnStatement() {
12 | super("ReturnStatement");
13 | this.expr = new LinkedList();
14 | }
15 | public ASTReturnStatement(LinkedList expr) {
16 | super("ReturnStatement");
17 | this.expr = expr;
18 | }
19 | @Override
20 | public void accept(ASTVisitor visitor) throws Exception {
21 | visitor.visit(this);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTSelectionStatement.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.LinkedList;
4 |
5 | import com.fasterxml.jackson.annotation.JsonTypeName;
6 | @JsonTypeName("SelectionStatement")
7 | public class ASTSelectionStatement extends ASTStatement{
8 |
9 | public LinkedList cond;
10 | public ASTStatement then;
11 | public ASTStatement otherwise;
12 |
13 | public ASTSelectionStatement() {
14 | super("SelectionStatement");
15 | }
16 | public ASTSelectionStatement(LinkedList cond,ASTStatement then,ASTStatement otherwise) {
17 | super("SelectionStatement");
18 | this.cond = cond;
19 | this.then = then;
20 | this.otherwise = otherwise;
21 | }
22 | @Override
23 | public void accept(ASTVisitor visitor) throws Exception {
24 | visitor.visit(this);
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTStatement.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonSubTypes;
4 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
5 | import com.fasterxml.jackson.annotation.JsonTypeName;
6 | import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
7 |
8 | @JsonTypeName("Statement")
9 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME,include = As.PROPERTY,property = "type")
10 | @JsonSubTypes({
11 | @JsonSubTypes.Type(value = ASTBreakStatement.class,name = "BreakStatement"),
12 | @JsonSubTypes.Type(value = ASTCompoundStatement.class,name = "CompoundStatement"),
13 | @JsonSubTypes.Type(value = ASTContinueStatement.class,name="ContinueStatement"),
14 | @JsonSubTypes.Type(value = ASTExpressionStatement.class,name = "ExpressionStatement"),
15 | @JsonSubTypes.Type(value = ASTGotoStatement.class,name = "GotoStatement"),
16 | @JsonSubTypes.Type(value = ASTIterationDeclaredStatement.class,name = "IterationDeclaredStatement"),
17 | @JsonSubTypes.Type(value = ASTIterationStatement.class,name = "IterationStatement"),
18 | @JsonSubTypes.Type(value = ASTLabeledStatement.class,name = "LabeledStatement"),
19 | @JsonSubTypes.Type(value = ASTReturnStatement.class,name = "ReturnStatement"),
20 | @JsonSubTypes.Type(value = ASTSelectionStatement.class,name = "SelectionStatement")
21 | })
22 | public abstract class ASTStatement extends ASTNode{
23 |
24 | public ASTStatement(String type) {
25 | super(type);
26 | }
27 |
28 | @Override
29 | public void accept(ASTVisitor visitor) throws Exception {
30 | // TODO Auto-generated method stub
31 |
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTStringConstant.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("StringConstant")
6 | public class ASTStringConstant extends ASTExpression{
7 | public String value;
8 | public Integer tokenId;
9 | public ASTStringConstant() {
10 | super("StringConstant");
11 | }
12 | public ASTStringConstant(String value,Integer tokenId) {
13 | super("StringConstant");
14 | this.value = value;
15 | this.tokenId = tokenId;
16 | }
17 | @Override
18 | public void accept(ASTVisitor visitor) throws Exception {
19 | visitor.visit(this);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTToken.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 |
4 | import com.fasterxml.jackson.annotation.JsonTypeName;
5 |
6 | @JsonTypeName("Token")
7 | public class ASTToken extends ASTNode{
8 |
9 | public String value;
10 | public Integer tokenId;
11 | public ASTToken() {
12 | super("Token");
13 | }
14 |
15 | public ASTToken(String value,Integer tokenId) {
16 | super("Token");
17 | this.value = value;
18 | this.tokenId = tokenId;
19 | }
20 |
21 | @Override
22 | public void accept(ASTVisitor visitor) throws Exception {
23 | visitor.visit(this);
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTTypename.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import java.util.List;
4 |
5 | import com.fasterxml.jackson.annotation.JsonTypeName;
6 |
7 | @JsonTypeName("Typename")
8 | public class ASTTypename extends ASTNode{
9 | public List specfiers;
10 | public ASTDeclarator declarator;
11 |
12 | public ASTTypename() {
13 | super("Typename");
14 | }
15 | public ASTTypename(List specList, ASTDeclarator absDeclarator) {
16 | super("Typename");
17 | this.specfiers = specList;
18 | this.declarator = absDeclarator;
19 | }
20 | @Override
21 | public void accept(ASTVisitor visitor) throws Exception {
22 | visitor.visit(this);
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTUnaryExpression.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | // һԪ���ʽ�ڵ�
6 | @JsonTypeName("UnaryExpression")
7 | public class ASTUnaryExpression extends ASTExpression{
8 | public ASTToken op;
9 | public ASTExpression expr;
10 |
11 | public ASTUnaryExpression() {
12 | super("UnaryExpression");
13 | }
14 | public ASTUnaryExpression(ASTToken op,ASTExpression expr) {
15 | super("UnaryExpression");
16 | this.op = op;
17 | this.expr = expr;
18 | }
19 | @Override
20 | public void accept(ASTVisitor visitor) throws Exception {
21 | visitor.visit(this);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTUnaryTypename.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonTypeName;
4 |
5 | @JsonTypeName("UnaryTypename")
6 | public class ASTUnaryTypename extends ASTExpression{
7 |
8 | public ASTToken op;
9 | public ASTTypename typename;
10 |
11 | public ASTUnaryTypename() {
12 | super("UnaryTypename");
13 | }
14 |
15 | public ASTUnaryTypename(ASTToken op,ASTTypename typename) {
16 | super("UnaryTypename");
17 | this.op = op;
18 | this.typename = typename;
19 | }
20 |
21 |
22 |
23 | @Override
24 | public void accept(ASTVisitor visitor) throws Exception {
25 | visitor.visit(this);
26 | }
27 |
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTVariableDeclarator.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | import com.fasterxml.jackson.annotation.JsonIgnore;
4 | import com.fasterxml.jackson.annotation.JsonTypeName;
5 | @JsonTypeName("VariableDeclarator")
6 | public class ASTVariableDeclarator extends ASTDeclarator{
7 | public ASTIdentifier identifier;
8 |
9 | public ASTVariableDeclarator() {
10 | super("VariableDeclarator");
11 | }
12 |
13 | public ASTVariableDeclarator(ASTIdentifier declarator) {
14 | super("VariableDeclarator");
15 | this.identifier = declarator;
16 | }
17 | @Override
18 | public void accept(ASTVisitor visitor) throws Exception {
19 | visitor.visit(this);
20 | }
21 |
22 | @JsonIgnore
23 | @Override
24 | public String getName() {
25 | return identifier.value;
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/parser/ast/ASTVisitor.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.parser.ast;
2 |
3 | public interface ASTVisitor {
4 |
5 | void visit(ASTCompilationUnit program)throws Exception;
6 | void visit(ASTDeclaration declaration)throws Exception;
7 | void visit(ASTArrayDeclarator arrayDeclarator)throws Exception;
8 | void visit(ASTVariableDeclarator variableDeclarator)throws Exception;
9 | void visit(ASTFunctionDeclarator functionDeclarator)throws Exception;
10 | void visit(ASTParamsDeclarator paramsDeclarator)throws Exception;
11 | void visit(ASTArrayAccess arrayAccess)throws Exception;
12 | void visit(ASTBinaryExpression binaryExpression)throws Exception;
13 | void visit(ASTBreakStatement breakStat)throws Exception;
14 | void visit(ASTContinueStatement continueStatement)throws Exception;
15 | void visit(ASTCastExpression castExpression)throws Exception;
16 | void visit(ASTCharConstant charConst)throws Exception;
17 | void visit(ASTCompoundStatement compoundStat)throws Exception;
18 | void visit(ASTConditionExpression conditionExpression)throws Exception;
19 | void visit(ASTExpression expression)throws Exception;
20 | void visit(ASTExpressionStatement expressionStat)throws Exception;
21 | void visit(ASTFloatConstant floatConst)throws Exception;
22 | void visit(ASTFunctionCall funcCall)throws Exception;
23 | void visit(ASTGotoStatement gotoStat)throws Exception;
24 | void visit(ASTIdentifier identifier)throws Exception;
25 | void visit(ASTInitList initList)throws Exception;
26 | void visit(ASTIntegerConstant intConst)throws Exception;
27 | void visit(ASTIterationDeclaredStatement iterationDeclaredStat)throws Exception;
28 | void visit(ASTIterationStatement iterationStat)throws Exception;
29 | void visit(ASTLabeledStatement labeledStat)throws Exception;
30 | void visit(ASTMemberAccess memberAccess)throws Exception;
31 | void visit(ASTPostfixExpression postfixExpression)throws Exception;
32 | void visit(ASTReturnStatement returnStat)throws Exception;
33 | void visit(ASTSelectionStatement selectionStat)throws Exception;
34 | void visit(ASTStringConstant stringConst)throws Exception;
35 | void visit(ASTTypename typename)throws Exception;
36 | void visit(ASTUnaryExpression unaryExpression)throws Exception;
37 | void visit(ASTUnaryTypename unaryTypename)throws Exception;
38 | void visit(ASTFunctionDefine functionDefine)throws Exception;
39 | void visit(ASTDeclarator declarator)throws Exception;
40 | void visit(ASTStatement statement)throws Exception;
41 | void visit(ASTToken token)throws Exception;
42 | }
43 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/scanner/MyScanner.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.scanner;
2 |
3 | import org.antlr.v4.runtime.CommonTokenStream;
4 |
5 | import java.io.File;
6 | import java.io.FileWriter;
7 | import java.io.IOException;
8 | import java.util.List;
9 |
10 | public class MyScanner {
11 | public MyScanner(String tokenFileName,CommonTokenStream tokens) throws IOException {
12 | FileWriter fileWriter = new FileWriter(new File(tokenFileName));
13 | for(int i=0;i VariableTable;
7 | public String funcName;
8 | public String specifiers;
9 | public String type;
10 | }
11 |
--------------------------------------------------------------------------------
/src/bit/minisys/minicc/semantic/VarTable.java:
--------------------------------------------------------------------------------
1 | package bit.minisys.minicc.semantic;
2 |
3 | import bit.minisys.minicc.parser.ast.ASTExpression;
4 |
5 | public class VarTable {
6 | public String name;
7 | public String type;
8 | public String specifiers;
9 | public ASTExpression value;
10 | public int dimension;
11 | public int length;
12 | }
13 |
--------------------------------------------------------------------------------
/test/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/test/.keep
--------------------------------------------------------------------------------
/test/ic_test/.keep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hansT-T/bit-mini-complier/26e54939cf13d1311239d12fffd8d108a68fd07a/test/ic_test/.keep
--------------------------------------------------------------------------------
/test/ic_test/test.ast.json:
--------------------------------------------------------------------------------
1 | {"type":"Program","items":[{"type":"Declaration","specifiers":[{"type":"Token","value":"int","tokenId":0}],"initLists":[{"type":"InitList","declarator":{"type":"ArrayDeclarator","declarator":{"type":"ArrayDeclarator","declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"a","tokenId":1}},"expr":{"type":"IntegerConstant","value":10,"tokenId":3}},"expr":{"type":"IntegerConstant","value":20,"tokenId":6}},"exprs":[]}]},{"type":"Declaration","specifiers":[{"type":"Token","value":"int","tokenId":9}],"initLists":[{"type":"InitList","declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"b","tokenId":10}},"exprs":[{"type":"IntegerConstant","value":1,"tokenId":12}]}]},{"type":"FunctionDefine","specifiers":[{"type":"Token","value":"int","tokenId":14}],"declarator":{"type":"FunctionDeclarator","declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"f1","tokenId":15}},"params":[{"type":"ParamsDeclarator","specfiers":[{"type":"Token","value":"int","tokenId":17}],"declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"x","tokenId":18}}},{"type":"ParamsDeclarator","specfiers":[{"type":"Token","value":"int","tokenId":20}],"declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"y","tokenId":21}}}]},"body":{"type":"CompoundStatement","blockItems":[{"type":"Declaration","specifiers":[{"type":"Token","value":"int","tokenId":24}],"initLists":[{"type":"InitList","declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"z","tokenId":25}},"exprs":[{"type":"BinaryExpression","op":{"type":"Token","value":"+","tokenId":28},"expr1":{"type":"Identifier","value":"x","tokenId":27},"expr2":{"type":"Identifier","value":"y","tokenId":29}}]}]},{"type":"ReturnStatement","expr":[{"type":"Identifier","value":"z","tokenId":32}]}]}},{"type":"FunctionDefine","specifiers":[{"type":"Token","value":"void","tokenId":35}],"declarator":{"type":"FunctionDeclarator","declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"f2","tokenId":36}},"params":[]},"body":{"type":"CompoundStatement","blockItems":[{"type":"ExpressionStatement","exprs":[{"type":"FunctionCall","funcname":{"type":"Identifier","value":"Mars_PrintStr","tokenId":40},"argList":[{"type":"StringConstant","value":"\"in f2\\n\"","tokenId":42}]}]},{"type":"ReturnStatement","expr":null}]}},{"type":"FunctionDefine","specifiers":[{"type":"Token","value":"int","tokenId":48}],"declarator":{"type":"FunctionDeclarator","declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"main","tokenId":49}},"params":[]},"body":{"type":"CompoundStatement","blockItems":[{"type":"Declaration","specifiers":[{"type":"Token","value":"int","tokenId":53}],"initLists":[{"type":"InitList","declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"a1","tokenId":54}},"exprs":[{"type":"IntegerConstant","value":1,"tokenId":56}]}]},{"type":"Declaration","specifiers":[{"type":"Token","value":"int","tokenId":58}],"initLists":[{"type":"InitList","declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"a2","tokenId":59}},"exprs":[{"type":"IntegerConstant","value":2,"tokenId":61}]}]},{"type":"Declaration","specifiers":[{"type":"Token","value":"int","tokenId":63}],"initLists":[{"type":"InitList","declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"res","tokenId":64}},"exprs":[]}]},{"type":"ExpressionStatement","exprs":[{"type":"BinaryExpression","op":{"type":"Token","value":"=","tokenId":67},"expr1":{"type":"Identifier","value":"res","tokenId":66},"expr2":{"type":"UnaryExpression","op":{"type":"Token","value":"!","tokenId":68},"expr":{"type":"Identifier","value":"a1","tokenId":69}}}]},{"type":"ExpressionStatement","exprs":[{"type":"BinaryExpression","op":{"type":"Token","value":"=","tokenId":72},"expr1":{"type":"Identifier","value":"res","tokenId":71},"expr2":{"type":"UnaryExpression","op":{"type":"Token","value":"~","tokenId":73},"expr":{"type":"Identifier","value":"a1","tokenId":74}}}]},{"type":"ExpressionStatement","exprs":[{"type":"BinaryExpression","op":{"type":"Token","value":"=","tokenId":77},"expr1":{"type":"Identifier","value":"res","tokenId":76},"expr2":{"type":"BinaryExpression","op":{"type":"Token","value":"+","tokenId":79},"expr1":{"type":"Identifier","value":"a1","tokenId":78},"expr2":{"type":"Identifier","value":"a2","tokenId":80}}}]},{"type":"ExpressionStatement","exprs":[{"type":"BinaryExpression","op":{"type":"Token","value":"=","tokenId":83},"expr1":{"type":"Identifier","value":"res","tokenId":82},"expr2":{"type":"BinaryExpression","op":{"type":"Token","value":"%","tokenId":85},"expr1":{"type":"Identifier","value":"a1","tokenId":84},"expr2":{"type":"Identifier","value":"a2","tokenId":86}}}]},{"type":"ExpressionStatement","exprs":[{"type":"BinaryExpression","op":{"type":"Token","value":"=","tokenId":89},"expr1":{"type":"Identifier","value":"res","tokenId":88},"expr2":{"type":"BinaryExpression","op":{"type":"Token","value":"<<","tokenId":91},"expr1":{"type":"Identifier","value":"a1","tokenId":90},"expr2":{"type":"Identifier","value":"a2","tokenId":92}}}]},{"type":"ExpressionStatement","exprs":[{"type":"BinaryExpression","op":{"type":"Token","value":"=","tokenId":95},"expr1":{"type":"Identifier","value":"res","tokenId":94},"expr2":{"type":"PostfixExpression","expr":{"type":"Identifier","value":"a1","tokenId":96},"op":{"type":"Token","value":"++","tokenId":97}}}]},{"type":"ExpressionStatement","exprs":[{"type":"BinaryExpression","op":{"type":"Token","value":"=","tokenId":100},"expr1":{"type":"Identifier","value":"res","tokenId":99},"expr2":{"type":"UnaryExpression","op":{"type":"Token","value":"++","tokenId":101},"expr":{"type":"Identifier","value":"a1","tokenId":102}}}]},{"type":"SelectionStatement","cond":[{"type":"BinaryExpression","op":{"type":"Token","value":"&&","tokenId":107},"expr1":{"type":"Identifier","value":"a1","tokenId":106},"expr2":{"type":"Identifier","value":"a2","tokenId":108}}],"then":{"type":"CompoundStatement","blockItems":[{"type":"ExpressionStatement","exprs":[{"type":"BinaryExpression","op":{"type":"Token","value":"=","tokenId":112},"expr1":{"type":"Identifier","value":"res","tokenId":111},"expr2":{"type":"FunctionCall","funcname":{"type":"Identifier","value":"f1","tokenId":113},"argList":[{"type":"Identifier","value":"a1","tokenId":115},{"type":"Identifier","value":"a2","tokenId":117}]}}]}]},"otherwise":{"type":"SelectionStatement","cond":[{"type":"UnaryExpression","op":{"type":"Token","value":"!","tokenId":124},"expr":{"type":"Identifier","value":"a1","tokenId":125}}],"then":{"type":"CompoundStatement","blockItems":[{"type":"ExpressionStatement","exprs":[{"type":"BinaryExpression","op":{"type":"Token","value":"=","tokenId":129},"expr1":{"type":"Identifier","value":"res","tokenId":128},"expr2":{"type":"FunctionCall","funcname":{"type":"Identifier","value":"f1","tokenId":130},"argList":[{"type":"Identifier","value":"b","tokenId":132},{"type":"Identifier","value":"a2","tokenId":134}]}}]}]},"otherwise":{"type":"CompoundStatement","blockItems":[{"type":"ExpressionStatement","exprs":[{"type":"FunctionCall","funcname":{"type":"Identifier","value":"f2","tokenId":140},"argList":[]}]}]}}},{"type":"IterationDeclaredStatement","init":{"type":"Declaration","specifiers":[{"type":"Token","value":"int","tokenId":147}],"initLists":[{"type":"InitList","declarator":{"type":"VariableDeclarator","identifier":{"type":"Identifier","value":"i","tokenId":148}},"exprs":[{"type":"IntegerConstant","value":0,"tokenId":150}]}]},"cond":[{"type":"BinaryExpression","op":{"type":"Token","value":"<","tokenId":153},"expr1":{"type":"Identifier","value":"i","tokenId":152},"expr2":{"type":"Identifier","value":"a1","tokenId":154}}],"step":[{"type":"PostfixExpression","expr":{"type":"Identifier","value":"i","tokenId":156},"op":{"type":"Token","value":"++","tokenId":157}}],"stat":{"type":"CompoundStatement","blockItems":[{"type":"BreakStatement"},{"type":"ContinueStatement"},{"type":"ExpressionStatement","exprs":[{"type":"BinaryExpression","op":{"type":"Token","value":"+=","tokenId":165},"expr1":{"type":"Identifier","value":"res","tokenId":164},"expr2":{"type":"IntegerConstant","value":1,"tokenId":166}}]}]}},{"type":"LabeledStatement","label":{"type":"Identifier","value":"k","tokenId":169},"stat":{"type":"GotoStatement","label":{"type":"Identifier","value":"k","tokenId":172}}},{"type":"ReturnStatement","expr":[{"type":"IntegerConstant","value":0,"tokenId":175}]}]}}]}
--------------------------------------------------------------------------------
/test/ic_test/test.c:
--------------------------------------------------------------------------------
1 | int a[10][20];
2 | int b = 1;
3 |
4 | int f1(int x,int y){
5 | int z = x + y;
6 | return z;
7 | }
8 | void f2(){
9 | Mars_PrintStr("in f2\n");
10 | return;
11 | }
12 |
13 | int main(){
14 | int a1 = 1;
15 | int a2 = 2;
16 | int res;
17 | // unary oper
18 | res = !a1;
19 | res = ~a1;
20 | // binary oper
21 | res = a1+a2;
22 | res = a1%a2;
23 | res = a1 << a2;
24 | // selfchange
25 | res = a1++;
26 | res = ++a1;
27 | // short-circuit evaluation and "if" control-flow
28 | if(a1 && a2){
29 | res = f1(a1,a2);
30 | }else if(!a1){
31 | // b is global
32 | res = f1(b,a2);
33 | }else{
34 | f2();
35 | }
36 | // "for" control-flow
37 | for(int i = 0; i i32
2 | var $b i32
3 | func &f1(var %x i32, var %y i32) i32{
4 | var %z i32
5 | dassign %6(
6 | add i32(dread i32 %x,dread i32 %y))
7 | dassign %7(regread i32 %6)
8 | dassign %z(regread i32 %7)
9 | return (dread i32 %z)}
10 |
11 | func &f2() void{
12 | dassign %8(addrof a32 _1sc)
13 | call Mars_PrintStr(regread a32 %8)
14 | return ()}
15 |
16 | func &main() i32{
17 | var %a1 i32
18 | var %a2 i32
19 | var %res i32
20 | dassign %10(constval i32 1)
21 | dassign %a1(regread i32 %10)
22 | dassign %12(constval i32 2)
23 | dassign %a2(regread i32 %12)
24 | dassign %14(
25 | lnot i32(dread i32 %a1))
26 | dassign %res(regread i32 %14)
27 | dassign %15(
28 | neg i32(dread i32 %a1))
29 | dassign %res(regread i32 %15)
30 | dassign %16(
31 | add i32(dread i32 %a1,dread i32 %a2))
32 | dassign %17(regread i32 %16)
33 | dassign %res(regread i32 %17)
34 | dassign %18(
35 | rem i32(dread i32 %a1,dread i32 %a2))
36 | dassign %19(regread i32 %18)
37 | dassign %res(regread i32 %19)
38 | dassign %20(
39 | shl i32(dread i32 %a1,dread i32 %a2))
40 | dassign %21(regread i32 %20)
41 | dassign %res(regread i32 %21)
42 | dassign %22(dread i32 %a1)
43 | dassign %23(constval i32 1)
44 | dassign %a1(
45 | add i32(dread i32 %a1,regread i32 %23))
46 | dassign %res(regread i32 %22)
47 | dassign %24(constval i32 1)
48 | dassign %a1(
49 | add i32(dread i32 %a1,regread i32 %24))
50 | dassign %25(dread i32 %a1)
51 | dassign %res(regread i32 %25)
52 | brfalse <@1shortwaytrue>(dread i32 %a1)
53 | dassign %26(
54 | land i32(dread i32 %a1,dread i32 %a2))
55 | goto <@1shortwayfalse>
56 | @1shortwaytrue:
57 | dassign %27(constval i32 0)
58 | goto <@1shortwayend>
59 | @1shortwayfalse:
60 | dassign %27(regread i32 %26)
61 | @1shortwayend:
62 | brfalse <@1otherwise1>(regread i32 %27)
63 | dassign %28(
64 | call f1(dread i32 %a1,dread i32 %a2))
65 | dassign %res(regread i32 %28)
66 | goto <@1endif>
67 | @1otherwise1:
68 | dassign %29(
69 | lnot i32(dread i32 %a1))
70 | brfalse <@1otherwise2>(regread i32 %29)
71 | dassign $b(
72 | iread i32(constval i32 800))
73 | dassign %30(
74 | call f1(dread i32 $b,dread i32 %a2))
75 | dassign %res(regread i32 %30)
76 | goto <@1endif>
77 | @1otherwise2:
78 | call f2()
79 | @1endif:
80 | var %i i32
81 | dassign %32(constval i32 0)
82 | dassign %i(regread i32 %32)
83 | @1LoopCheckLabel:
84 | dassign %33(
85 | lt i32(dread i32 %i,dread i32 %a1))
86 | dassign %34(regread i32 %33)
87 | brfalse <@1LoopEndLabel>(regread i32 %34)
88 | goto <@1LoopEndLabel>
89 | goto <@1LoopStepLabel>
90 | dassign %35(constval i32 1)
91 | dassign %36(
92 | add i32(dread i32 %res,regread i32 %35))
93 | dassign %res(regread i32 %36)
94 | @1LoopStepLabel:
95 | dassign %37(dread i32 %i)
96 | dassign %38(constval i32 1)
97 | dassign %i(
98 | add i32(dread i32 %i,regread i32 %38))
99 | goto <@1LoopCheckLabel>
100 | @1LoopEndLabel:
101 | @k:
102 | goto <@k>
103 | dassign %39(constval i32 0)
104 | return (regread i32 %39)}
105 |
106 |
--------------------------------------------------------------------------------
/test/ic_test/test.ic.xml:
--------------------------------------------------------------------------------
1 | var $a <[10][20]> i32
2 | var $b i32
3 | func &f1(var %x i32, var %y i32) i32{
4 | var %z i32
5 | dassign %6(
6 | add i32(dread i32 %x,dread i32 %y))
7 | dassign %7(regread i32 %6)
8 | dassign %z(regread i32 %7)
9 | return (dread i32 %z)}
10 |
11 | func &f2() void{
12 | dassign %8(addrof a32 _1sc)
13 | call Mars_PrintStr(regread a32 %8)
14 | return ()}
15 |
16 | func &main() i32{
17 | var %a1 i32
18 | var %a2 i32
19 | var %res i32
20 | dassign %10(constval i32 1)
21 | dassign %a1(regread i32 %10)
22 | dassign %12(constval i32 2)
23 | dassign %a2(regread i32 %12)
24 | dassign %14(
25 | lnot i32(dread i32 %a1))
26 | dassign %res(regread i32 %14)
27 | dassign %15(
28 | neg i32(dread i32 %a1))
29 | dassign %res(regread i32 %15)
30 | dassign %16(
31 | add i32(dread i32 %a1,dread i32 %a2))
32 | dassign %17(regread i32 %16)
33 | dassign %res(regread i32 %17)
34 | dassign %18(
35 | rem i32(dread i32 %a1,dread i32 %a2))
36 | dassign %19(regread i32 %18)
37 | dassign %res(regread i32 %19)
38 | dassign %20(
39 | shl i32(dread i32 %a1,dread i32 %a2))
40 | dassign %21(regread i32 %20)
41 | dassign %res(regread i32 %21)
42 | dassign %22(dread i32 %a1)
43 | dassign %23(constval i32 1)
44 | dassign %a1(
45 | add i32(dread i32 %a1,regread i32 %23))
46 | dassign %res(regread i32 %22)
47 | dassign %24(constval i32 1)
48 | dassign %a1(
49 | add i32(dread i32 %a1,regread i32 %24))
50 | dassign %25(dread i32 %a1)
51 | dassign %res(regread i32 %25)
52 | brfalse <@1shortwaytrue>(dread i32 %a1)
53 | dassign %26(
54 | land i32(dread i32 %a1,dread i32 %a2))
55 | goto <@1shortwayfalse>
56 | @1shortwaytrue:
57 | dassign %27(constval i32 0)
58 | goto <@1shortwayend>
59 | @1shortwayfalse:
60 | dassign %27(regread i32 %26)
61 | @1shortwayend:
62 | brfalse <@1otherwise1>(regread i32 %27)
63 | dassign %28(
64 | call f1(dread i32 %a1,dread i32 %a2))
65 | dassign %res(regread i32 %28)
66 | goto <@1endif>
67 | @1otherwise1:
68 | dassign %29(
69 | lnot i32(dread i32 %a1))
70 | brfalse <@1otherwise2>(regread i32 %29)
71 | dassign $b(
72 | iread i32(constval i32 800))
73 | dassign %30(
74 | call f1(dread i32 $b,dread i32 %a2))
75 | dassign %res(regread i32 %30)
76 | goto <@1endif>
77 | @1otherwise2:
78 | call f2()
79 | @1endif:
80 | var %i i32
81 | dassign %32(constval i32 0)
82 | dassign %i(regread i32 %32)
83 | @1LoopCheckLabel:
84 | dassign %33(
85 | lt i32(dread i32 %i,dread i32 %a1))
86 | dassign %34(regread i32 %33)
87 | brfalse <@1LoopEndLabel>(regread i32 %34)
88 | goto <@1LoopEndLabel>
89 | goto <@1LoopStepLabel>
90 | dassign %35(constval i32 1)
91 | dassign %36(
92 | add i32(dread i32 %res,regread i32 %35))
93 | dassign %res(regread i32 %36)
94 | @1LoopStepLabel:
95 | dassign %37(dread i32 %i)
96 | dassign %38(constval i32 1)
97 | dassign %i(
98 | add i32(dread i32 %i,regread i32 %38))
99 | goto <@1LoopCheckLabel>
100 | @1LoopEndLabel:
101 | @k:
102 | goto <@k>
103 | dassign %39(constval i32 0)
104 | return (regread i32 %39)}
105 |
106 |
--------------------------------------------------------------------------------
/test/ic_test/test.pp.c:
--------------------------------------------------------------------------------
1 | int a[10][20];
2 | int b = 1;
3 |
4 | int f1(int x,int y){
5 | int z = x + y;
6 | return z;
7 | }
8 | void f2(){
9 | Mars_PrintStr("in f2\n");
10 | return;
11 | }
12 |
13 | int main(){
14 | int a1 = 1;
15 | int a2 = 2;
16 | int res;
17 | // unary oper
18 | res = !a1;
19 | res = ~a1;
20 | // binary oper
21 | res = a1+a2;
22 | res = a1%a2;
23 | res = a1 << a2;
24 | // selfchange
25 | res = a1++;
26 | res = ++a1;
27 | // short-circuit evaluation and "if" control-flow
28 | if(a1 && a2){
29 | res = f1(a1,a2);
30 | }else if(!a1){
31 | // b is global
32 | res = f1(b,a2);
33 | }else{
34 | f2();
35 | }
36 | // "for" control-flow
37 | for(int i = 0; i,1:0]
2 | [@1,4:4='a',,1:4]
3 | [@2,5:5='[',<'['>,1:5]
4 | [@3,6:7='10',,1:6]
5 | [@4,8:8=']',<']'>,1:8]
6 | [@5,9:9='[',<'['>,1:9]
7 | [@6,10:11='20',,1:10]
8 | [@7,12:12=']',<']'>,1:12]
9 | [@8,13:13=';',<';'>,1:13]
10 | [@9,16:18='int',<'int'>,2:0]
11 | [@10,20:20='b',,2:4]
12 | [@11,22:22='=',<'='>,2:6]
13 | [@12,24:24='1',,2:8]
14 | [@13,25:25=';',<';'>,2:9]
15 | [@14,30:32='int',<'int'>,4:0]
16 | [@15,34:35='f1',,4:4]
17 | [@16,36:36='(',<'('>,4:6]
18 | [@17,37:39='int',<'int'>,4:7]
19 | [@18,41:41='x',,4:11]
20 | [@19,42:42=',',<','>,4:12]
21 | [@20,43:45='int',<'int'>,4:13]
22 | [@21,47:47='y',,4:17]
23 | [@22,48:48=')',<')'>,4:18]
24 | [@23,49:49='{',<'{'>,4:19]
25 | [@24,53:55='int',<'int'>,5:1]
26 | [@25,57:57='z',,5:5]
27 | [@26,59:59='=',<'='>,5:7]
28 | [@27,61:61='x',,5:9]
29 | [@28,63:63='+',<'+'>,5:11]
30 | [@29,65:65='y',,5:13]
31 | [@30,66:66=';',<';'>,5:14]
32 | [@31,70:75='return',<'return'>,6:1]
33 | [@32,77:77='z',,6:8]
34 | [@33,78:78=';',<';'>,6:9]
35 | [@34,81:81='}',<'}'>,7:0]
36 | [@35,84:87='void',<'void'>,8:0]
37 | [@36,89:90='f2',,8:5]
38 | [@37,91:91='(',<'('>,8:7]
39 | [@38,92:92=')',<')'>,8:8]
40 | [@39,93:93='{',<'{'>,8:9]
41 | [@40,97:109='Mars_PrintStr',,9:1]
42 | [@41,110:110='(',<'('>,9:14]
43 | [@42,111:119='"in f2\n"',,9:15]
44 | [@43,120:120=')',<')'>,9:24]
45 | [@44,121:121=';',<';'>,9:25]
46 | [@45,125:130='return',<'return'>,10:1]
47 | [@46,131:131=';',<';'>,10:7]
48 | [@47,134:134='}',<'}'>,11:0]
49 | [@48,139:141='int',<'int'>,13:0]
50 | [@49,143:146='main',,13:4]
51 | [@50,147:147='(',<'('>,13:8]
52 | [@51,148:148=')',<')'>,13:9]
53 | [@52,149:149='{',<'{'>,13:10]
54 | [@53,153:155='int',<'int'>,14:1]
55 | [@54,157:158='a1',,14:5]
56 | [@55,160:160='=',<'='>,14:8]
57 | [@56,162:162='1',,14:10]
58 | [@57,163:163=';',<';'>,14:11]
59 | [@58,167:169='int',<'int'>,15:1]
60 | [@59,171:172='a2',,15:5]
61 | [@60,174:174='=',<'='>,15:8]
62 | [@61,176:176='2',,15:10]
63 | [@62,177:177=';',<';'>,15:11]
64 | [@63,181:183='int',<'int'>,16:1]
65 | [@64,185:187='res',,16:5]
66 | [@65,188:188=';',<';'>,16:8]
67 | [@66,208:210='res',,18:1]
68 | [@67,212:212='=',<'='>,18:5]
69 | [@68,214:214='!',<'!'>,18:7]
70 | [@69,215:216='a1',,18:8]
71 | [@70,217:217=';',<';'>,18:10]
72 | [@71,221:223='res',,19:1]
73 | [@72,225:225='=',<'='>,19:5]
74 | [@73,227:227='~',<'~'>,19:7]
75 | [@74,228:229='a1',,19:8]
76 | [@75,230:230=';',<';'>,19:10]
77 | [@76,251:253='res',,21:1]
78 | [@77,255:255='=',<'='>,21:5]
79 | [@78,257:258='a1',,21:7]
80 | [@79,259:259='+',<'+'>,21:9]
81 | [@80,260:261='a2',,21:10]
82 | [@81,262:262=';',<';'>,21:12]
83 | [@82,266:268='res',,22:1]
84 | [@83,270:270='=',<'='>,22:5]
85 | [@84,272:273='a1',,22:7]
86 | [@85,274:274='%',<'%'>,22:9]
87 | [@86,275:276='a2',,22:10]
88 | [@87,277:277=';',<';'>,22:12]
89 | [@88,281:283='res',,23:1]
90 | [@89,285:285='=',<'='>,23:5]
91 | [@90,287:288='a1',,23:7]
92 | [@91,290:291='<<',<'<<'>,23:10]
93 | [@92,293:294='a2',,23:13]
94 | [@93,295:295=';',<';'>,23:15]
95 | [@94,315:317='res',,25:1]
96 | [@95,319:319='=',<'='>,25:5]
97 | [@96,321:322='a1',,25:7]
98 | [@97,323:324='++',<'++'>,25:9]
99 | [@98,325:325=';',<';'>,25:11]
100 | [@99,329:331='res',,26:1]
101 | [@100,333:333='=',<'='>,26:5]
102 | [@101,335:336='++',<'++'>,26:7]
103 | [@102,337:338='a1',,26:9]
104 | [@103,339:339=';',<';'>,26:11]
105 | [@104,395:396='if',<'if'>,28:1]
106 | [@105,397:397='(',<'('>,28:3]
107 | [@106,398:399='a1',,28:4]
108 | [@107,401:402='&&',<'&&'>,28:7]
109 | [@108,404:405='a2',,28:10]
110 | [@109,406:406=')',<')'>,28:12]
111 | [@110,407:407='{',<'{'>,28:13]
112 | [@111,412:414='res',,29:2]
113 | [@112,416:416='=',<'='>,29:6]
114 | [@113,418:419='f1',,29:8]
115 | [@114,420:420='(',<'('>,29:10]
116 | [@115,421:422='a1',,29:11]
117 | [@116,423:423=',',<','>,29:13]
118 | [@117,424:425='a2',,29:14]
119 | [@118,426:426=')',<')'>,29:16]
120 | [@119,427:427=';',<';'>,29:17]
121 | [@120,431:431='}',<'}'>,30:1]
122 | [@121,432:435='else',<'else'>,30:2]
123 | [@122,437:438='if',<'if'>,30:7]
124 | [@123,439:439='(',<'('>,30:9]
125 | [@124,440:440='!',<'!'>,30:10]
126 | [@125,441:442='a1',,30:11]
127 | [@126,443:443=')',<')'>,30:13]
128 | [@127,444:444='{',<'{'>,30:14]
129 | [@128,467:469='res',,32:2]
130 | [@129,471:471='=',<'='>,32:6]
131 | [@130,473:474='f1',,32:8]
132 | [@131,475:475='(',<'('>,32:10]
133 | [@132,476:476='b',,32:11]
134 | [@133,477:477=',',<','>,32:12]
135 | [@134,478:479='a2',,32:13]
136 | [@135,480:480=')',<')'>,32:15]
137 | [@136,481:481=';',<';'>,32:16]
138 | [@137,485:485='}',<'}'>,33:1]
139 | [@138,486:489='else',<'else'>,33:2]
140 | [@139,490:490='{',<'{'>,33:6]
141 | [@140,495:496='f2',,34:2]
142 | [@141,497:497='(',<'('>,34:4]
143 | [@142,498:498=')',<')'>,34:5]
144 | [@143,499:499=';',<';'>,34:6]
145 | [@144,503:503='}',<'}'>,35:1]
146 | [@145,531:533='for',<'for'>,37:1]
147 | [@146,534:534='(',<'('>,37:4]
148 | [@147,535:537='int',<'int'>,37:5]
149 | [@148,539:539='i',,37:9]
150 | [@149,541:541='=',<'='>,37:11]
151 | [@150,543:543='0',,37:13]
152 | [@151,544:544=';',<';'>,37:14]
153 | [@152,546:546='i',,37:16]
154 | [@153,547:547='<',<'<'>,37:17]
155 | [@154,548:549='a1',,37:18]
156 | [@155,550:550=';',<';'>,37:20]
157 | [@156,552:552='i',,37:22]
158 | [@157,553:554='++',<'++'>,37:23]
159 | [@158,555:555=')',<')'>,37:25]
160 | [@159,556:556='{',<'{'>,37:26]
161 | [@160,561:565='break',<'break'>,38:2]
162 | [@161,566:566=';',<';'>,38:7]
163 | [@162,571:578='continue',<'continue'>,39:2]
164 | [@163,579:579=';',<';'>,39:10]
165 | [@164,584:586='res',,40:2]
166 | [@165,588:589='+=',<'+='>,40:6]
167 | [@166,591:591='1',,40:9]
168 | [@167,592:592=';',<';'>,40:10]
169 | [@168,596:596='}',<'}'>,41:1]
170 | [@169,620:620='k',,43:1]
171 | [@170,621:621=':',<':'>,43:2]
172 | [@171,625:628='goto',<'goto'>,44:1]
173 | [@172,630:630='k',,44:6]
174 | [@173,631:631=';',<';'>,44:7]
175 | [@174,647:652='return',<'return'>,46:1]
176 | [@175,654:654='0',,46:8]
177 | [@176,655:655=';',<';'>,46:9]
178 | [@177,658:658='}',<'}'>,47:0]
179 | [@178,659:658='',,47:1]
180 |
--------------------------------------------------------------------------------
/test/nc_test/1_Fibonacci.c:
--------------------------------------------------------------------------------
1 | int fibonacci(int num){
2 | int res;
3 | if(num < 1){
4 | res = 0;
5 | }else if(num <= 2){
6 | res = 1;
7 | }else{
8 | res = fibonacci(num-1)+fibonacci(num-2);
9 | }
10 | return res;
11 | }
12 | int main(){
13 | Mars_PrintStr("Please input a number:\n");
14 | int n = Mars_GetInt();
15 | int res = fibonacci(n);
16 | Mars_PrintStr("This number's fibonacci value is :\n");
17 | Mars_PrintInt(res);
18 | return 0;
19 | }
--------------------------------------------------------------------------------
/test/nc_test/2_Prime.c:
--------------------------------------------------------------------------------
1 | int prime(int n){
2 | int sum = 0;
3 | int i,j,flag = 1;
4 | for(i = 2; i<=n; i++){
5 | flag = 1;
6 | for(j = 2; j*j <= i; j++){
7 | if(i%j == 0){
8 | flag = 0;
9 | break;
10 | }
11 | }
12 | if(flag == 1){
13 | sum ++;
14 | Mars_PrintInt(i);
15 | }
16 | }
17 | return sum;
18 | }
19 | int main(){
20 | Mars_PrintStr("Please input a number:\n");
21 | int n = Mars_GetInt();
22 | int res = prime(n);
23 | Mars_PrintStr("The number of prime numbers within n is:\n");
24 | Mars_PrintInt(res);
25 | return 0;
26 | }
--------------------------------------------------------------------------------
/test/nc_test/3_PerfectNumber.c:
--------------------------------------------------------------------------------
1 | void perfectNumber(int n){
2 | int p[80]; //淇濆瓨鍒嗚В鐨勫洜瀛�
3 | int i,num,count,s,c = 0;
4 | for(num = 2; num < n; num++)
5 | {
6 | count = 0;
7 | s = num;
8 | for(i = 1; i < num/2+1; i++) //寰幆澶勭悊姣忎釜鏁�
9 | {
10 | if(num % i == 0) //鑳借i鏁撮櫎
11 | {
12 | p[count++] = i; //淇濆瓨鍥犲瓙锛岃璁℃暟鍣╟ount澧炲姞1
13 | s -= i; //鍑忓幓涓�涓洜瀛�
14 | }
15 | }
16 | if( 0 == s)
17 | {
18 | Mars_PrintInt(num);
19 | c++;
20 | }
21 | }
22 | Mars_PrintStr("The sum is :\n");
23 | Mars_PrintInt(c);
24 | return ;
25 | }
26 | int main(){
27 | Mars_PrintStr("All perfect numbers within 100:\n"); // A perfect number is a number equal to the sum of its factors
28 | perfectNumber(100);
29 | return 0;
30 | }
--------------------------------------------------------------------------------
/test/parser_test/2_parser_test1.c:
--------------------------------------------------------------------------------
1 | int fibonacci ( int n ) {
2 | if ( n < 2 )
3 | return n;
4 | int f = 0 , g = 1 ;
5 | int result = 0 ;
6 | for ( int i = 0 ; i < n ; i ++ ) {
7 | result = f + g ;
8 | f = g ;
9 | g = result ;
10 | }
11 | return result ;
12 | }
13 | int main ( ) {
14 | int a = 10;
15 | int res = fibonacci ( a ) ;
16 | _OUTPUT ( res ) ;
17 | return 0 ;
18 | }
19 |
--------------------------------------------------------------------------------
/test/parser_test/3_parser_test2.c:
--------------------------------------------------------------------------------
1 | int main() {
2 | int a[5], i, j, t;
3 | for(i = 0; i < 5; i++) {
4 | a[i] = MARS_GETI();
5 | }
6 | for(i = 0; i < 4; i++) {
7 | for(j = 0; j < 5 - i; j++) {
8 | if(a[j] > a[j+1]) {
9 | t = a[j];
10 | a[j] = a[j+1];
11 | a[j+1] = t;
12 | }
13 | }
14 | }
15 | for(i = 0; i < 5; i++) {
16 | t = a[i];
17 | MARS_PUTI(t);
18 | MARS_PUTS("\n");
19 | }
20 | return;
21 | }
22 |
--------------------------------------------------------------------------------
/test/parser_test/4_parser_test3.c:
--------------------------------------------------------------------------------
1 | int main() {
2 | int a;
3 |
4 | a = MARS_GETI();
5 | a *= 10;
6 | MARS_PUTI(a);
7 | a ++;
8 | MARS_PUTI(a);
9 | return 0;
10 | }
11 |
12 |
--------------------------------------------------------------------------------
/test/scan_test/1_scanner_test.c:
--------------------------------------------------------------------------------
1 | int main()
2 | {
3 | //integer-constant
4 | +1 ;
5 | -10 ;
6 | 1000 ;
7 | 1l ;
8 | 1U ;
9 | 10ul ;
10 | 10LU ;
11 | 1000ull ;
12 | 1000LLU ;
13 |
14 | +0 ;
15 | -00 ;
16 | 007 ;
17 | 00ul ;
18 | 00LLU ;
19 |
20 | +0x0 ;
21 | -0x00 ;
22 | 0XABCDEF ;
23 | 0xful ;
24 | 0XFLLU ;
25 |
26 | //floating-constant
27 | 0.0 ;
28 | +1.1e+1 ;
29 | -1.1E-1 ;
30 | 1.1e1f ;
31 | 1.1E1L ;
32 |
33 | 0x0p0;
34 | 0x0.0p0 ;
35 | +0xa.ap+1 ;
36 | -0xa.aP-1 ;
37 | 0xa.ap1f ;
38 | 0xa.aP1L ;
39 |
40 | //character-constant
41 | 'a' ;
42 | L'a' ;
43 | U'a' ;
44 | u'a' ;
45 | '\n' ;
46 | '\?' ;
47 | '\24' ;
48 |
49 | //string-literal
50 | "abcdefg123456\\" ;
51 | u8"a" ;
52 | u"a" ;
53 | U"a" ;
54 | L"a" ;
55 |
56 | //identifier
57 | int __a ;
58 | int __1 ;
59 | int a1 ;
60 | int a ;
61 | int a_1 ;
62 | int a_ ;
63 |
64 | //keyword
65 | int i=1 ;
66 | float f ;
67 | double d ;
68 | char c ;
69 | long l ;
70 | short s ;
71 | signed si ;
72 | unsigned short us ;
73 | typedef struct
74 | {
75 | int num1;
76 | }test;
77 | test test1 ;
78 | test *test2 ;
79 | static int sti ;
80 | const int ci ;
81 | sizeof( int ) ;
82 | if( 1 )
83 | {
84 |
85 | }
86 | else
87 | {
88 |
89 | }
90 | for( ; ; )
91 | {
92 | continue ;
93 | }
94 | while( 1 )
95 | {
96 |
97 | }
98 | switch(i){
99 | case 1: break;
100 | default: break;
101 | }
102 | do
103 | {
104 |
105 | }while( 0 ) ;
106 | goto gotoFlag ;
107 | //operators
108 | int array[10] = {0} ;
109 | test1.num1 = 0 ;
110 | test2->num1 = 0 ;
111 | i++ ;
112 | i-- ;
113 | i = 1 + 1 ;
114 | i = 1 - 1 ;
115 | i = 1 * 1 ;
116 | i = 1 / 1 ;
117 | i = 1 % 1 ;
118 | i = !i ;
119 | i = i & 1 ;
120 | i = i | 1 ;
121 | i = i && 1 ;
122 | i = i || 1 ;
123 | i = i ^ 1 ;
124 | i = i >> 1 ;
125 | i = i << 1 ;
126 | i = i > 1 ? 1 : 2 ;
127 | i += 1 ;
128 | i -= 1 ;
129 | i *= 1 ;
130 | i /= 1 ;
131 | i %= 1 ;
132 | i &= 1 ;
133 | i |= 1 ;
134 | i ^= 1 ;
135 | i >>= 1 ;
136 | i <<= 1 ;
137 | if( i==1 ){}
138 | if( i!=1 ){}
139 | if( i>1 ){}
140 | if( i<1 ){}
141 | if( i>=1 ){}
142 | if( i<=1 ){}
143 | gotoFlag:
144 | return 0 ;
145 | }
146 | void function( int arg1 , int arg2 )
147 | {
148 | }
--------------------------------------------------------------------------------
/test/semantic_test/0_var_not_defined.c:
--------------------------------------------------------------------------------
1 | //int f();
2 | int main()
3 | {
4 | int res = f();
5 | int c = a+1;
6 | return 0;
7 | }
8 | int f(){
9 | return 1;
10 | }
--------------------------------------------------------------------------------
/test/semantic_test/1_var_defined_again.c:
--------------------------------------------------------------------------------
1 | int f();
2 | //int f();
3 | int f(){
4 | return 1;
5 | }
6 |
7 | int f(){
8 | return 2;
9 | }
10 |
11 | int main()
12 | {
13 | int a;
14 | int a;
15 |
16 | int b;
17 | {
18 | int b;
19 | }
20 | return 0;
21 | }
--------------------------------------------------------------------------------
/test/semantic_test/2_break_not_in_loop.c:
--------------------------------------------------------------------------------
1 | int main()
2 | {
3 | break;
4 |
5 | for(;;){
6 | break;
7 | }
8 |
9 | break;
10 |
11 | return 0;
12 | }
--------------------------------------------------------------------------------
/test/semantic_test/6_goto_label_not_exist.c:
--------------------------------------------------------------------------------
1 | int main()
2 | {
3 | k1:
4 | goto k2;
5 | k2:
6 | goto k1;
7 |
8 | goto end;
9 | return 0;
10 | }
--------------------------------------------------------------------------------