databuffer.h

Mon, 13 Aug 2012 19:12:21 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 13 Aug 2012 19:12:21 +0300
changeset 49
8e2f7a031410
parent 48
976c57f153b3
child 50
2e333a3ca49a
permissions
-rw-r--r--

rename ObjWriter::RecordBuffer to SwitchBuffer, as it is designed specifically for switch and will break if used genericly

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:
diff changeset
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:
diff changeset
2 * botc source code
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:
diff changeset
3 * Copyright (C) 2012 Santeri `Dusk` Piippo
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:
diff changeset
4 * All rights reserved.
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:
diff changeset
5 *
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:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
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:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
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:
diff changeset
8 *
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:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright notice,
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:
diff changeset
10 * this list of conditions and the following disclaimer.
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:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
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:
diff changeset
12 * this list of conditions and the following disclaimer in the documentation
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:
diff changeset
13 * and/or other materials provided with the distribution.
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:
diff changeset
14 * 3. Neither the name of the developer nor the names of its contributors may
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:
diff changeset
15 * be used to endorse or promote products derived from this software without
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:
diff changeset
16 * specific prior written permission.
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:
diff changeset
17 * 4. Redistributions in any form must be accompanied by information on how to
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:
diff changeset
18 * obtain complete source code for the software and any accompanying
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:
diff changeset
19 * software that uses the software. The source code must either be included
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:
diff changeset
20 * in the distribution or be available for no more than the cost of
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:
diff changeset
21 * distribution plus a nominal fee, and must be freely redistributable
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:
diff changeset
22 * under reasonable conditions. For an executable file, complete source
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:
diff changeset
23 * code means the source code for all modules it contains. It does not
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:
diff changeset
24 * include source code for modules or files that typically accompany the
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:
diff changeset
25 * major components of the operating system on which the executable 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:
diff changeset
26 * runs.
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:
diff changeset
27 *
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:
diff changeset
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
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:
diff changeset
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
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:
diff changeset
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
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:
diff changeset
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
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:
diff changeset
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
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:
diff changeset
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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:
diff changeset
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
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:
diff changeset
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
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:
diff changeset
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
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:
diff changeset
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
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:
diff changeset
38 * POSSIBILITY OF SUCH DAMAGE.
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:
diff changeset
39 */
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:
diff changeset
40
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:
diff changeset
41 #ifndef __DATABUFFER_H__
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:
diff changeset
42 #define __DATABUFFER_H__
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:
diff changeset
43 #include <stdio.h>
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:
diff changeset
44 #include <string.h>
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:
diff changeset
45 #include "common.h"
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:
diff changeset
46
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
47 #define MAX_MARKS 256
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
48
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
49 extern int g_NextMark;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
50
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:
diff changeset
51 // ============================================================================
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:
diff changeset
52 // DataBuffer: A dynamic data buffer.
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:
diff changeset
53 class DataBuffer {
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:
diff changeset
54 public:
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:
diff changeset
55 // The actual buffer
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:
diff changeset
56 unsigned char* buffer;
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:
diff changeset
57
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:
diff changeset
58 // Allocated size of the buffer
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:
diff changeset
59 unsigned int allocsize;
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:
diff changeset
60
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:
diff changeset
61 // Written size of the buffer
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:
diff changeset
62 unsigned int writesize;
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:
diff changeset
63
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
64 // Marks and references
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
65 ScriptMark* marks[MAX_MARKS];
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
66 ScriptMarkReference* refs[MAX_MARKS];
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
67
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
68 // ====================================================================
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:
diff changeset
69 // METHODS
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
70
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
71 // ====================================================================
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
72 // Constructor
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:
diff changeset
73 DataBuffer (unsigned int size=128) {
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:
diff changeset
74 writesize = 0;
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:
diff changeset
75
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:
diff changeset
76 buffer = new unsigned char[size];
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:
diff changeset
77 allocsize = size;
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
78
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
79 // Clear the marks table out
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
80 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: 36
diff changeset
81 marks[u] = 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: 36
diff changeset
82 refs[u] = 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: 36
diff changeset
83 }
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:
diff changeset
84 }
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:
diff changeset
85
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
86 // ====================================================================
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:
diff changeset
87 ~DataBuffer () {
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:
diff changeset
88 delete buffer;
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
89
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
90 // Delete any marks and references
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
91 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: 36
diff changeset
92 if (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: 36
diff changeset
93 delete 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: 36
diff changeset
94
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
95 if (refs[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: 36
diff changeset
96 delete refs[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: 36
diff changeset
97 }
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:
diff changeset
98 }
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:
diff changeset
99
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 // ====================================================================
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
101 // Write stuff to the buffer
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
102 template<class T> void Write (T stuff) {
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
103 if (writesize + sizeof (T) >= allocsize) {
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
104 // We don't have enough space in the buffer to write
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
105 // the stuff - thus resize. First, store the old
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
106 // buffer temporarily:
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:
diff changeset
107 char* copy = new char[allocsize];
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:
diff changeset
108 memcpy (copy, buffer, allocsize);
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:
diff changeset
109
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
110 // Remake the buffer with the new size. Have enough space
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
111 // for the stuff we're going to write, as well as a bit
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
112 // of leeway so we don't have to resize immediately again.
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:
diff changeset
113 size_t newsize = allocsize + sizeof (T) + 128;
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:
diff changeset
114 delete buffer;
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:
diff changeset
115 buffer = new unsigned char[newsize];
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:
diff changeset
116 allocsize = newsize;
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:
diff changeset
117
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
118 // Now, copy the stuff back.
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:
diff changeset
119 memcpy (buffer, copy, allocsize);
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:
diff changeset
120 delete copy;
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:
diff changeset
121 }
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:
diff changeset
122
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
123 // Buffer is now guaranteed to have enough space.
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
124 // Write the stuff one byte at a time.
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
125 union_t<T> uni;
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
126 uni.val = stuff;
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:
diff changeset
127 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
128 if (writesize >= allocsize) // should NEVER happen because resizing is done above
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:
diff changeset
129 error ("DataBuffer: written size exceeds allocated size!\n");
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
130
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
131 buffer[writesize] = uni.b[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:
diff changeset
132 writesize++;
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:
diff changeset
133 }
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:
diff changeset
134 }
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:
diff changeset
135
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
136 // ====================================================================
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 32
diff changeset
137 // Merge another data buffer into this one.
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 32
diff changeset
138 void Merge (DataBuffer* other) {
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 34
diff changeset
139 if (!other)
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
140 return;
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
141 int oldsize = writesize;
36
a8838b5f1213 Parser can now read expressions 100% properly and can perform variable assignment. I'd call this a milestone!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 34
diff changeset
142
40
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
143 for (unsigned int x = 0; x < other->writesize; x++)
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
144 Write<byte> (*(other->buffer+x));
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 32
diff changeset
145
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
146 // Merge its marks and references
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
147 unsigned int u = 0;
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
148 for (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: 36
diff changeset
149 if (other->marks[u]) {
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
150 // Merge the mark and offset its position.
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
151 if (marks[u])
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
152 error ("DataBuffer: duplicate mark %d!\n");
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
153
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
154 marks[u] = other->marks[u];
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
155 marks[u]->pos += oldsize;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
156
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
157 // The original mark becomes NULL so that the deconstructor
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
158 // will not delete it prematurely. (should it delete any
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
159 // marks in the first place since there is no such thing
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
160 // as at temporary mark?)
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
161 other->marks[u] = NULL;
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
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: 36
diff changeset
163
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
164 if (other->refs[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: 36
diff changeset
165 // Same for references
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
166 // TODO: add a g_NextRef system like here, akin to marks!
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
167 unsigned int r = AddMarkReference (other->refs[u]->num, false);
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
168 refs[r]->pos = other->refs[u]->pos + oldsize;
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
169 }
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
170 }
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
171
34
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 32
diff changeset
172 delete other;
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 32
diff changeset
173 }
0a9a5902beaa Expression parser mostly up and running!! Still work to do on it though.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 32
diff changeset
174
40
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
175 // Clones this databuffer to a new one and returns it.
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
176 DataBuffer* Clone () {
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
177 DataBuffer* other = new DataBuffer;
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
178 for (unsigned int x = 0; x < writesize; x++)
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
179 other->Write<unsigned char> (*(buffer+x));
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
180 return other;
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
181 }
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
182
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
183 // ====================================================================
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
184 // Adds a mark to the buffer. A mark is a "pointer" to a particular
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
185 // position in the bytecode. The actual permanent position cannot
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
186 // be predicted in any way or form, thus these things will be used
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
187 // to "mark" a position like that for future use.
45
e1d3b7ea975c Removed mark types as they served absolutely zero purpose
Teemu Piippo <crimsondusk64@gmail.com>
parents: 43
diff changeset
188 unsigned int AddMark (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
189 // Find a free slot for the mark
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
190 unsigned int u = g_NextMark++;
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
191
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
192 if (marks[u])
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
193 error ("DataBuffer: attempted to re-create mark %u!\n", u);
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
194
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
195 if (u >= MAX_MARKS)
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
196 error ("mark quota exceeded, all labels, if-structs and loops add marks\n");
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
197
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
198 ScriptMark* m = new ScriptMark;
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
199 m->name = 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: 36
diff changeset
200 m->pos = writesize;
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
201 marks[u] = m;
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
202 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: 36
diff changeset
203 }
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
204
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
205 // ====================================================================
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
206 // A ref is another "mark" that references a mark. When the bytecode
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
207 // is written to file, they are changed into their marks' current
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
208 // positions. Marks themselves are never written to files, only refs are
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
209 unsigned int AddMarkReference (unsigned int marknum, bool placeholder = true) {
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
210 unsigned int 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: 36
diff changeset
211 for (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: 36
diff changeset
212 if (!refs[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: 36
diff changeset
213 break;
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:
diff changeset
214
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
215 if (u == MAX_MARKS)
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
216 error ("mark reference quota exceeded, all goto-statements, if-structs and loops add refs\n");
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
217
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
218 // NOTE: Do not check if the mark actually exists here since a
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
219 // reference may come in the code earlier than the actual 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: 36
diff changeset
220 // and the new mark number can be predicted.
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
221 // 11/8/12: eh? The mark is always created first.
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
222 ScriptMarkReference* r = new ScriptMarkReference;
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
223 r->num = marknum;
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
224 r->pos = writesize;
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
225 refs[u] = r;
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:
diff changeset
226
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
227 // Write a dummy placeholder for the reference
48
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
228 if (placeholder)
976c57f153b3 Added switch support... fixed more problems with marks in the process
Teemu Piippo <crimsondusk64@gmail.com>
parents: 45
diff changeset
229 Write<word> (1234);
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
230
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
231 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: 36
diff changeset
232 }
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
233
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
234 // Delete a mark and all references to it.
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
235 void DeleteMark (unsigned int marknum) {
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
236 if (!marks[marknum])
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
237 return;
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:
diff changeset
238
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
239 // Delete the 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: 36
diff changeset
240 delete marks[marknum];
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
241 marks[marknum] = 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: 36
diff changeset
242
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
243 // Delete its references
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
244 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: 36
diff changeset
245 if (refs[u]->num == marknum) {
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
246 delete refs[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: 36
diff changeset
247 refs[u] = 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: 36
diff changeset
248 }
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
249 }
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:
diff changeset
250 }
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
251
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
252 // Adjusts a mark to the current position
42
5cd91fd1526c FINALLY, marks and references work smoothly without hacks. if and while work properly.
Teemu Piippo <crimsondusk64@gmail.com>
parents: 41
diff changeset
253 void MoveMark (unsigned int mark, int offset = -1) {
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
254 if (!marks[mark])
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
255 return;
39
07b7ab8080cf Fixed mark positioning - multiple if statements should work properly now
Teemu Piippo <crimsondusk64@gmail.com>
parents: 38
diff changeset
256 marks[mark]->pos = writesize;
38
e4bbd540663b Added if() support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 37
diff changeset
257 }
40
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
258
41
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
259 void OffsetMark (unsigned int mark, size_t offset) {
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
260 if (!marks[mark])
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
261 return;
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
262 marks[mark]->pos += offset;
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
263 }
47e686c96d8f Added while loop support. However, script marks keep getting wrong position numbers..
Teemu Piippo <crimsondusk64@gmail.com>
parents: 40
diff changeset
264
40
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
265 // Dump the buffer (for debugging purposes)
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
266 void Dump() {
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
267 for (unsigned int x = 0; x < writesize; x++)
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
268 printf ("%d. [%d]\n", x, *(buffer+x));
9e4f785501db Committed the other changes done during a failed attempt of else
Teemu Piippo <crimsondusk64@gmail.com>
parents: 39
diff changeset
269 }
43
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
270
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
271 // Count the amount of marks
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
272 unsigned int CountMarks () {
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
273 unsigned int count = 0;
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
274 for (unsigned int u = 0; u < MAX_MARKS; u++)
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
275 count += !!marks[u];
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
276 return count;
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
277 }
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
278
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
279 // Count the amount of refs
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
280 unsigned int CountReferences () {
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
281 unsigned int count = 0;
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
282 for (unsigned int u = 0; u < MAX_MARKS; u++)
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
283 count += !!refs[u];
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
284 return count;
1b35c9985989 Added for-loop support
Teemu Piippo <crimsondusk64@gmail.com>
parents: 42
diff changeset
285 }
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:
diff changeset
286 };
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:
diff changeset
287
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:
diff changeset
288 #endif // __DATABUFFER_H__

mercurial