objwriter.cxx

Mon, 30 Jul 2012 03:38:02 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 30 Jul 2012 03:38:02 +0300
changeset 38
e4bbd540663b
parent 37
c349dca807f9
child 39
07b7ab8080cf
permissions
-rw-r--r--

Added if() support

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
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
41 #define __OBJWRITER_CXX__
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 #include <stdio.h>
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 #include <stdlib.h>
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 #include <string.h>
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 #include "common.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 #include "str.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 #include "objwriter.h"
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
48 #include "stringtable.h"
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 #include "bots.h"
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
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: 20
diff changeset
52 extern bool g_GotMainLoop;
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: 20
diff changeset
53
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 ObjWriter::ObjWriter (str path) {
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: 26
diff changeset
55 MainBuffer = new DataBuffer;
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: 20
diff changeset
56 MainLoopBuffer = new DataBuffer;
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: 20
diff changeset
57 OnEnterBuffer = new DataBuffer;
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
58 numWrittenBytes = 0;
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: 26
diff changeset
59 filepath = path;
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 void ObjWriter::WriteString (char* s) {
20
d7b13805d1e0 Added string table and support for string parameters in commands.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 16
diff changeset
63 Write<long> (strlen (s));
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 for (unsigned int u = 0; u < strlen (s); u++)
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 Write<char> (s[u]);
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 void ObjWriter::WriteString (const char* s) {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 WriteString (const_cast<char*> (s));
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 }
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 void ObjWriter::WriteString (str s) {
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 WriteString (s.chars());
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: 20
diff changeset
74 }
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: 20
diff changeset
75
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
76 void ObjWriter::WriteBuffer (DataBuffer* buf) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
77 for (unsigned int x = 0; x < buf->writesize; x++) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
78 unsigned char c = *(buf->buffer+x);
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
79 Write<unsigned char> (c);
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
80 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
81 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
82
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: 20
diff changeset
83 void ObjWriter::WriteBuffers () {
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: 20
diff changeset
84 // If there was no mainloop defined, write a dummy one now.
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: 20
diff changeset
85 if (!g_GotMainLoop) {
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: 20
diff changeset
86 MainLoopBuffer->Write<long> (DH_MAINLOOP);
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: 20
diff changeset
87 MainLoopBuffer->Write<long> (DH_ENDMAINLOOP);
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: 20
diff changeset
88 }
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: 20
diff changeset
89
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: 20
diff changeset
90 // Write the onenter and mainloop buffers, IN THAT ORDER
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: 20
diff changeset
91 for (int i = 0; i < 2; i++) {
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: 20
diff changeset
92 DataBuffer* buf = (!i) ? OnEnterBuffer : MainLoopBuffer;
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
93 WriteBuffer (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: 20
diff changeset
94
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: 20
diff changeset
95 // Clear the buffer afterwards for potential next state
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: 20
diff changeset
96 delete buf;
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: 20
diff changeset
97 buf = new DataBuffer;
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: 20
diff changeset
98 }
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: 20
diff changeset
99
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: 20
diff changeset
100 // Next state definitely has no mainloop yet
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: 20
diff changeset
101 g_GotMainLoop = false;
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: 26
diff changeset
102 }
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: 26
diff changeset
103
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
104 // Write string table
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
105 void ObjWriter::WriteStringTable () {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
106 // If we added strings here, we need to write a list of them.
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
107 unsigned int stringcount = CountStringTable ();
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
108 if (stringcount) {
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
109 Write<long> (DH_STRINGLIST);
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
110 Write<long> (stringcount);
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
111
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
112 for (unsigned int a = 0; a < stringcount; a++)
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
113 WriteString (g_StringTable[a]);
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
114 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
115
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
116 printf ("%u / %u string%s written\n", stringcount, MAX_LIST_STRINGS, PLURAL (stringcount));
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
117 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 33
diff changeset
118
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: 26
diff changeset
119 // Write main buffer to file
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: 26
diff changeset
120 void ObjWriter::WriteToFile () {
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: 26
diff changeset
121 fp = fopen (filepath, "w");
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: 26
diff changeset
122 CHECK_FILE (fp, filepath, "writing");
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: 26
diff changeset
123
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: 26
diff changeset
124 if (sizeof (unsigned char) != 1)
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: 26
diff changeset
125 error ("size of unsigned char isn't 1, but %d!\n", sizeof (unsigned char));
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: 26
diff changeset
126
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: 26
diff changeset
127 for (unsigned int x = 0; x < MainBuffer->writesize; x++) {
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: 34
diff changeset
128 // Check if this position is a reference
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: 34
diff changeset
129 for (unsigned int r = 0; r < MAX_MARKS; r++) {
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: 34
diff changeset
130 if (MainBuffer->refs[r] && MainBuffer->refs[r]->pos == x) {
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: 34
diff changeset
131 word ref = static_cast<word> (MainBuffer->marks[MainBuffer->refs[r]->num]->pos);
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
132 printf ("insert position %ld at script pos %u\n",
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
133 ref, x);
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: 34
diff changeset
134 WriteDataToFile<word> (ref);
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
135
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
136 // This reference is now used up
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
137 delete MainBuffer->refs[r];
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
138 MainBuffer->refs[r] = NULL;
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
139
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
140 // All other references need their positions bumped up as the
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
141 // bytecode gained 4 more bytes with the written reference
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
142 for (unsigned int s = 0; s < MAX_MARKS; s++)
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
143 if (MainBuffer->refs[s])
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
144 MainBuffer->refs[s]->pos += sizeof (word);
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: 34
diff changeset
145 }
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: 34
diff changeset
146 }
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: 34
diff changeset
147
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: 34
diff changeset
148 WriteDataToFile<unsigned char> (*(MainBuffer->buffer+x));
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: 26
diff changeset
149 }
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: 26
diff changeset
150
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: 26
diff changeset
151 printf ("-- %u byte%s written to %s\n", numWrittenBytes, PLURAL (numWrittenBytes), filepath.chars());
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: 26
diff changeset
152 fclose (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: 34
diff changeset
153 }
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: 34
diff changeset
154
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: 34
diff changeset
155 DataBuffer* ObjWriter::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: 34
diff changeset
156 return (g_CurMode == MODE_MAINLOOP) ? MainLoopBuffer :
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: 34
diff changeset
157 (g_CurMode == MODE_ONENTER) ? OnEnterBuffer :
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: 34
diff changeset
158 MainBuffer;
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: 34
diff changeset
159 }
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: 34
diff changeset
160
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: 34
diff changeset
161 ScriptMark* g_ScriptMark = NULL;
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: 34
diff changeset
162
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: 34
diff changeset
163 // Adds a mark
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: 34
diff changeset
164 unsigned int ObjWriter::AddMark (int type, str name) {
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: 34
diff changeset
165 return GetCurrentBuffer()->AddMark (type, name);
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: 34
diff changeset
166 }
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: 34
diff changeset
167
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: 34
diff changeset
168 // Adds a reference
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: 34
diff changeset
169 unsigned int ObjWriter::AddReference (unsigned int mark) {
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: 34
diff changeset
170 DataBuffer* b = 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: 34
diff changeset
171 return b->AddMarkReference (mark);
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: 34
diff changeset
172 }
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: 34
diff changeset
173
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: 34
diff changeset
174 // Finds a mark
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: 34
diff changeset
175 unsigned int ObjWriter::FindMark (int type, str name) {
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: 34
diff changeset
176 DataBuffer* b = 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: 34
diff changeset
177 for (unsigned int u = 0; u < MAX_MARKS; u++) {
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: 34
diff changeset
178 if (b->marks[u] && b->marks[u]->type == type && !b->marks[u]->name.icompare (name))
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: 34
diff changeset
179 return u;
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: 34
diff changeset
180 }
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: 34
diff changeset
181 return MAX_MARKS;
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
182 }
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
183
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
184 // Moves a mark to the current position
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
185 void ObjWriter::MoveMark (unsigned int mark) {
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
186 GetCurrentBuffer()->MoveMark (mark);
0
8dce9696d62d Initial commit
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 }

mercurial