Sat, 25 Aug 2012 04:19:22 +0300
Added support for continue-statements
0 | 1 | /* |
2 | * botc source code | |
3 | * Copyright (C) 2012 Santeri `Dusk` Piippo | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
3 | 14 | * 3. Neither the name of the developer nor the names of its contributors may |
15 | * be used to endorse or promote products derived from this software without | |
16 | * specific prior written permission. | |
0 | 17 | * 4. Redistributions in any form must be accompanied by information on how to |
18 | * obtain complete source code for the software and any accompanying | |
19 | * software that uses the software. The source code must either be included | |
20 | * in the distribution or be available for no more than the cost of | |
21 | * distribution plus a nominal fee, and must be freely redistributable | |
22 | * under reasonable conditions. For an executable file, complete source | |
23 | * code means the source code for all modules it contains. It does not | |
24 | * include source code for modules or files that typically accompany the | |
25 | * major components of the operating system on which the executable file | |
26 | * runs. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
34 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
36 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
37 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
38 | * POSSIBILITY OF SUCH DAMAGE. | |
39 | */ | |
40 | ||
41 | #ifndef __OBJWRITER_H__ | |
42 | #define __OBJWRITER_H__ | |
43 | ||
44 | #include <stdio.h> | |
26
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
45 | #include <typeinfo> |
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
46 | #include <string.h> |
0 | 47 | #include "common.h" |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
48 | #include "databuffer.h" |
0 | 49 | #include "str.h" |
50 | ||
26
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
51 | extern int g_CurMode; |
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
52 | |
0 | 53 | class ObjWriter { |
54 | public: | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
55 | // ==================================================================== |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
56 | // MEMBERS |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
57 | |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
58 | // Pointer to the file we're writing to |
0 | 59 | FILE* fp; |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
60 | |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
61 | // Path to the file we're writing to |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
62 | str filepath; |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
63 | |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
64 | // The main buffer - the contents of this is what we |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
65 | // write to file after parsing is complete |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
66 | DataBuffer* MainBuffer; |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
67 | |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
68 | // onenter buffer - the contents of the onenter{} block |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
69 | // is buffered here and is merged further at the end of state |
26
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
70 | DataBuffer* OnEnterBuffer; |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
71 | |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
72 | // Mainloop buffer - the contents of the mainloop{} block |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
73 | // is buffered here and is merged further at the end of state |
26
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
74 | DataBuffer* MainLoopBuffer; |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
75 | |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
76 | // Switch buffer - switch case data is recorded to this |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
77 | // buffer initially, instead of into main buffer. |
49
8e2f7a031410
rename ObjWriter::RecordBuffer to SwitchBuffer, as it is designed specifically for switch and will break if used genericly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
48
diff
changeset
|
78 | DataBuffer* SwitchBuffer; |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
79 | |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
80 | // How many bytes have we written to file? |
16
393359908179
Added mainloop/onenter/onexit support, fixed state writing.. this thing can compile the script for the `jumping arghbot` now!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
81 | unsigned int numWrittenBytes; |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
82 | |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
83 | // How many references did we resolve in the main buffer? |
43
1b35c9985989
Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents:
42
diff
changeset
|
84 | unsigned int numWrittenReferences; |
0 | 85 | |
1
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
86 | // ==================================================================== |
f0c61c204bc8
Added support for #include directives, added basic header and statistics printing.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
87 | // METHODS |
0 | 88 | ObjWriter (str path); |
89 | void WriteString (char* s); | |
90 | void WriteString (const char* s); | |
91 | void WriteString (str s); | |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
32
diff
changeset
|
92 | void WriteBuffer (DataBuffer* buf); |
26
54eaea6dc27c
Mainloop and onenter definitions are now written into separate buffers first and only written to file after state ends. Why? Zandronum seems to demand that mainloop definitions MUST be written right after any onenter one. This way, mainloop and onenter definitions can be written without this restriction in the script. Also now I have a cool uchar-buffer class :)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
16
diff
changeset
|
93 | void WriteBuffers (); |
34
0a9a5902beaa
Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
32
diff
changeset
|
94 | void WriteStringTable (); |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
95 | void WriteToFile (); |
37
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
96 | DataBuffer* GetCurrentBuffer (); |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
97 | |
45
e1d3b7ea975c
Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
98 | unsigned int AddMark (str name); |
e1d3b7ea975c
Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents:
44
diff
changeset
|
99 | unsigned int FindMark (str name); |
37
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
100 | unsigned int AddReference (unsigned int mark); |
38 | 101 | void MoveMark (unsigned int mark); |
41
47e686c96d8f
Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents:
39
diff
changeset
|
102 | void OffsetMark (unsigned int mark, int offset); |
39
07b7ab8080cf
Fixed mark positioning - multiple if statements should work properly now
Teemu Piippo <crimsondusk64@gmail.com>
parents:
38
diff
changeset
|
103 | void DeleteMark (unsigned int mark); |
0 | 104 | |
105 | template <class T> void Write (T stuff) { | |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
106 | GetCurrentBuffer ()->Write<T> (stuff); |
0 | 107 | } |
32
d11a034aabfd
- The output cmd-line argument is now optional - one is generated from the input file if not given.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
108 | |
42
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
109 | // Default to word |
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
110 | void Write (word stuff) { |
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
111 | Write<word> (stuff); |
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
112 | } |
37
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
113 | |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
114 | private: |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
115 | // Write given data to file. |
37
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
116 | template <class T> void WriteDataToFile (T stuff) { |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
117 | // One byte at a time |
50
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
118 | union_t<T> uni; |
2e333a3ca49a
Added default label for switch
Teemu Piippo <crimsondusk64@gmail.com>
parents:
49
diff
changeset
|
119 | uni.val = stuff; |
37
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
120 | for (unsigned int x = 0; x < sizeof (T); x++) { |
42
5cd91fd1526c
FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
41
diff
changeset
|
121 | fwrite (&uni.b[x], 1, 1, fp); |
37
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
122 | numWrittenBytes++; |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
123 | } |
c349dca807f9
Added mark/reference system to be able to refer to positions in the buffered bytecode. Labels and go-to support.
Teemu Piippo <crimsondusk64@gmail.com>
parents:
36
diff
changeset
|
124 | } |
0 | 125 | }; |
126 | ||
127 | #endif // __OBJWRITER_H__ |