objwriter.h

Sun, 26 Aug 2012 23:28:09 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 26 Aug 2012 23:28:09 +0300
changeset 64
dc5db6335601
parent 50
2e333a3ca49a
child 67
0a202714eea4
permissions
-rw-r--r--

Fixed: parser allowed command calls with one too few arguments

0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * botc source code
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012 Santeri `Dusk` Piippo
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 * All rights reserved.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright notice,
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * this list of conditions and the following disclaimer.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * this list of conditions and the following disclaimer in the documentation
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * and/or other materials provided with the distribution.
3
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
14 * 3. Neither the name of the developer nor the names of its contributors may
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
15 * be used to endorse or promote products derived from this software without
08cab2b67dce oh blerb.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1
diff changeset
16 * specific prior written permission.
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 * 4. Redistributions in any form must be accompanied by information on how to
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 * obtain complete source code for the software and any accompanying
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 * software that uses the software. The source code must either be included
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 * in the distribution or be available for no more than the cost of
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 * distribution plus a nominal fee, and must be freely redistributable
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 * under reasonable conditions. For an executable file, complete source
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 * code means the source code for all modules it contains. It does not
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 * include source code for modules or files that typically accompany the
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 * major components of the operating system on which the executable file
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 * runs.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 *
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 * POSSIBILITY OF SUCH DAMAGE.
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 */
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 #ifndef __OBJWRITER_H__
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 #define __OBJWRITER_H__
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 #include "str.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 class ObjWriter {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 ObjWriter (str path);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 void WriteString (char* s);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 void WriteString (const char* s);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 };
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 #endif // __OBJWRITER_H__

mercurial