This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathCoder.cpp
More file actions
228 lines (188 loc) · 4.38 KB
/
Coder.cpp
File metadata and controls
228 lines (188 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Copyright (C) 2013-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/*
* Coder.cpp
* lcidlc
*
* Created by Mark Waddingham on 27/07/2013.
*
*/
#include <stdio.h>
#include "foundation.h"
#include "Coder.h"
struct Coder
{
FILE *stream;
uint32_t depth;
bool padded;
};
static void CoderIndent(CoderRef self)
{
const char *s_tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
fprintf(self -> stream, "%.*s", self -> depth > 32 ? 32 : self -> depth, s_tabs);
}
bool CoderStart(const char *p_filename, CoderRef& r_coder)
{
bool t_success;
t_success = true;
CoderRef self;
self = nil;
if (t_success)
t_success = MCMemoryNew(self);
if (t_success)
{
self -> stream = fopen(p_filename, "w");
if (self -> stream == nil)
t_success = false;
}
if (t_success)
{
self -> depth = 0;
r_coder = self;
}
else
MCMemoryDelete(self);
return t_success;
}
bool CoderFinish(CoderRef self)
{
fclose(self -> stream);
MCMemoryDelete(self);
return true;
}
void CoderCancel(CoderRef self)
{
CoderFinish(self);
}
void CoderWriteLine(CoderRef self, const char *p_format, ...)
{
va_list t_args;
va_start(t_args, p_format);
vfprintf(self -> stream, p_format, t_args);
va_end(t_args);
fprintf(self -> stream, "\n");
self -> padded = false;
}
void CoderWrite(CoderRef self, const char *p_format, ...)
{
va_list t_args;
va_start(t_args, p_format);
vfprintf(self -> stream, p_format, t_args);
va_end(t_args);
}
void CoderWriteStatement(CoderRef self, const char *p_format, ...)
{
CoderBeginStatement(self);
va_list t_args;
va_start(t_args, p_format);
vfprintf(self -> stream, p_format, t_args);
va_end(t_args);
CoderEndStatement(self);
}
void CoderBeginStatement(CoderRef self)
{
CoderIndent(self);
}
void CoderEndStatement(CoderRef self)
{
fprintf(self -> stream, ";\n");
self -> padded = false;
}
void CoderBeginPreprocessor(CoderRef self, const char *p_format, ...)
{
va_list t_args;
va_start(t_args, p_format);
vfprintf(self -> stream, p_format, t_args);
va_end(t_args);
fprintf(self -> stream, "\n");
self -> padded = false;
}
void CoderEndPreprocessor(CoderRef self, const char *p_format, ...)
{
va_list t_args;
va_start(t_args, p_format);
vfprintf(self -> stream, p_format, t_args);
va_end(t_args);
fprintf(self -> stream, "\n");
self -> padded = false;
}
void CoderBegin(CoderRef self, const char *p_format, ...)
{
if (*p_format != '\0')
{
CoderIndent(self);
va_list t_args;
va_start(t_args, p_format);
vfprintf(self -> stream, p_format, t_args);
va_end(t_args);
fprintf(self -> stream, "\n");
}
CoderIndent(self);
fprintf(self -> stream, "{\n");
self -> depth += 1;
self -> padded = false;
}
void CoderEndBegin(CoderRef self, const char *p_format, ...)
{
CoderEnd(self, "");
CoderIndent(self);
va_list t_args;
va_start(t_args, p_format);
vfprintf(self -> stream, p_format, t_args);
va_end(t_args);
fprintf(self -> stream, "\n");
CoderBegin(self, "");
self -> padded = false;
}
void CoderEnd(CoderRef self, const char *p_format, ...)
{
self -> depth -= 1;
CoderIndent(self);
fprintf(self -> stream, "}\n");
if (*p_format != '\0')
{
CoderIndent(self);
va_list t_args;
va_start(t_args, p_format);
vfprintf(self -> stream, p_format, t_args);
va_end(t_args);
fprintf(self -> stream, "\n");
}
self -> padded = false;
}
void CoderBeginIf(CoderRef self, const char *p_format, ...)
{
CoderIndent(self);
fprintf(self -> stream, "if (");
va_list t_args;
va_start(t_args, p_format);
vfprintf(self -> stream, p_format, t_args);
va_end(t_args);
fprintf(self -> stream, ")\n");
CoderBegin(self, "");
self -> padded = false;
}
void CoderElse(CoderRef self)
{
CoderEndBegin(self, "else");
}
void CoderEndIf(CoderRef self)
{
CoderEnd(self, "");
}
void CoderPad(CoderRef self)
{
if (self -> padded)
return;
CoderWriteLine(self, "");
self -> padded = true;
}