-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.c
249 lines (214 loc) · 7.67 KB
/
platform.c
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* picoc's interface to the underlying platform. most platform-specific code
* is in platform/platform_XX.c and platform/library_XX.c */
#include "picoc.h"
#include "interpreter.h"
/* initialise everything */
void PicocInitialise(Picoc *pc, int StackSize)
{
memset(pc, '\0', sizeof(*pc));
PlatformInit(pc);
BasicIOInit(pc);
HeapInit(pc, StackSize);
TableInit(pc);
VariableInit(pc);
LexInit(pc);
TypeInit(pc);
#ifndef NO_HASH_INCLUDE
IncludeInit(pc);
#endif
LibraryInit(pc);
#ifdef BUILTIN_MINI_STDLIB
LibraryAdd(pc, &GlobalTable, "c library", &CLibrary[0]);
CLibraryInit(pc);
#endif
PlatformLibraryInit(pc);
DebugInit(pc);
}
/* free memory */
void PicocCleanup(Picoc *pc)
{
DebugCleanup(pc);
#ifndef NO_HASH_INCLUDE
IncludeCleanup(pc);
#endif
ParseCleanup(pc);
LexCleanup(pc);
VariableCleanup(pc);
TypeCleanup(pc);
TableStrFree(pc);
HeapCleanup(pc);
PlatformCleanup(pc);
}
/* platform-dependent code for running programs */
#if defined(UNIX_HOST) || defined(WIN32)
#define CALL_MAIN_NO_ARGS_RETURN_VOID "main();"
#define CALL_MAIN_WITH_ARGS_RETURN_VOID "main(__argc,__argv);"
#define CALL_MAIN_NO_ARGS_RETURN_INT "__exit_value = main();"
#define CALL_MAIN_WITH_ARGS_RETURN_INT "__exit_value = main(__argc,__argv);"
void PicocCallMain(Picoc *pc, int argc, char **argv)
{
/* check if the program wants arguments */
struct Value *FuncValue = NULL;
if (!VariableDefined(pc, TableStrRegister(pc, "main")))
ProgramFailNoParser(pc, "main() is not defined");
VariableGet(pc, NULL, TableStrRegister(pc, "main"), &FuncValue);
if (FuncValue->Typ->Base != TypeFunction)
ProgramFailNoParser(pc, "main is not a function - can't call it");
if (FuncValue->Val->FuncDef.NumParams != 0)
{
/* define the arguments */
VariableDefinePlatformVar(pc, NULL, "__argc", &pc->IntType, (union AnyValue *)&argc, FALSE);
VariableDefinePlatformVar(pc, NULL, "__argv", pc->CharPtrPtrType, (union AnyValue *)&argv, FALSE);
}
if (FuncValue->Val->FuncDef.ReturnType == &pc->VoidType)
{
if (FuncValue->Val->FuncDef.NumParams == 0)
PicocParse(pc, "startup", CALL_MAIN_NO_ARGS_RETURN_VOID, strlen(CALL_MAIN_NO_ARGS_RETURN_VOID), TRUE, TRUE, FALSE, TRUE);
else
PicocParse(pc, "startup", CALL_MAIN_WITH_ARGS_RETURN_VOID, strlen(CALL_MAIN_WITH_ARGS_RETURN_VOID), TRUE, TRUE, FALSE, TRUE);
}
else
{
VariableDefinePlatformVar(pc, NULL, "__exit_value", &pc->IntType, (union AnyValue *)&pc->PicocExitValue, TRUE);
if (FuncValue->Val->FuncDef.NumParams == 0)
PicocParse(pc, "startup", CALL_MAIN_NO_ARGS_RETURN_INT, strlen(CALL_MAIN_NO_ARGS_RETURN_INT), TRUE, TRUE, FALSE, TRUE);
else
PicocParse(pc, "startup", CALL_MAIN_WITH_ARGS_RETURN_INT, strlen(CALL_MAIN_WITH_ARGS_RETURN_INT), TRUE, TRUE, FALSE, TRUE);
}
}
#endif
void PrintSourceTextErrorLine(IOFILE *Stream, const char *FileName, const char *SourceText, int Line, int CharacterPos)
{
int LineCount;
const char *LinePos;
const char *CPos;
int CCount;
if (SourceText != NULL)
{
/* find the source line */
for (LinePos = SourceText, LineCount = 1; *LinePos != '\0' && LineCount < Line; LinePos++)
{
if (*LinePos == '\n')
LineCount++;
}
/* display the line */
for (CPos = LinePos; *CPos != '\n' && *CPos != '\0'; CPos++)
PrintCh(*CPos, Stream);
PrintCh('\n', Stream);
/* display the error position */
for (CPos = LinePos, CCount = 0; *CPos != '\n' && *CPos != '\0' && (CCount < CharacterPos || *CPos == ' '); CPos++, CCount++)
{
if (*CPos == '\t')
PrintCh('\t', Stream);
else
PrintCh(' ', Stream);
}
}
else
{
/* assume we're in interactive mode - try to make the arrow match up with the input text */
for (CCount = 0; CCount < CharacterPos + (int)strlen(INTERACTIVE_PROMPT_STATEMENT); CCount++)
PrintCh(' ', Stream);
}
PlatformPrintf(Stream, "^\n%s:%d:%d ", FileName, Line, CharacterPos);
}
/* exit with a message */
void ProgramFail(struct ParseState *Parser, const char *Message, ...)
{
va_list Args;
PrintSourceTextErrorLine(Parser->pc->CStdOut, Parser->FileName, Parser->SourceText, Parser->Line, Parser->CharacterPos);
va_start(Args, Message);
PlatformVPrintf(Parser->pc->CStdOut, Message, Args);
va_end(Args);
PlatformPrintf(Parser->pc->CStdOut, "\n");
PlatformExit(Parser->pc, 1);
}
/* exit with a message, when we're not parsing a program */
void ProgramFailNoParser(Picoc *pc, const char *Message, ...)
{
va_list Args;
va_start(Args, Message);
PlatformVPrintf(pc->CStdOut, Message, Args);
va_end(Args);
PlatformPrintf(pc->CStdOut, "\n");
PlatformExit(pc, 1);
}
/* like ProgramFail() but gives descriptive error messages for assignment */
void AssignFail(struct ParseState *Parser, const char *Format, struct ValueType *Type1, struct ValueType *Type2, int Num1, int Num2, const char *FuncName, int ParamNo)
{
IOFILE *Stream = Parser->pc->CStdOut;
PrintSourceTextErrorLine(Parser->pc->CStdOut, Parser->FileName, Parser->SourceText, Parser->Line, Parser->CharacterPos);
PlatformPrintf(Stream, "can't %s ", (FuncName == NULL) ? "assign" : "set");
if (Type1 != NULL)
PlatformPrintf(Stream, Format, Type1, Type2);
else
PlatformPrintf(Stream, Format, Num1, Num2);
if (FuncName != NULL)
PlatformPrintf(Stream, " in argument %d of call to %s()", ParamNo, FuncName);
PlatformPrintf(Stream, "\n");
PlatformExit(Parser->pc, 1);
}
/* exit lexing with a message */
void LexFail(Picoc *pc, struct LexState *Lexer, const char *Message, ...)
{
va_list Args;
PrintSourceTextErrorLine(pc->CStdOut, Lexer->FileName, Lexer->SourceText, Lexer->Line, Lexer->CharacterPos);
va_start(Args, Message);
PlatformVPrintf(pc->CStdOut, Message, Args);
va_end(Args);
PlatformPrintf(pc->CStdOut, "\n");
PlatformExit(pc, 1);
}
/* printf for compiler error reporting */
void PlatformPrintf(IOFILE *Stream, const char *Format, ...)
{
va_list Args;
va_start(Args, Format);
PlatformVPrintf(Stream, Format, Args);
va_end(Args);
}
void PlatformVPrintf(IOFILE *Stream, const char *Format, va_list Args)
{
const char *FPos;
for (FPos = Format; *FPos != '\0'; FPos++)
{
if (*FPos == '%')
{
FPos++;
switch (*FPos)
{
case 's': PrintStr(va_arg(Args, char *), Stream); break;
case 'd': PrintSimpleInt(va_arg(Args, int), Stream); break;
case 'c': PrintCh(va_arg(Args, int), Stream); break;
case 't': PrintType(va_arg(Args, struct ValueType *), Stream); break;
#ifndef NO_FP
case 'f': PrintFP(va_arg(Args, double), Stream); break;
#endif
case '%': PrintCh('%', Stream); break;
case '\0': FPos--; break;
}
}
else
PrintCh(*FPos, Stream);
}
}
/* make a new temporary name. takes a static buffer of char [7] as a parameter. should be initialised to "XX0000"
* where XX can be any characters */
char *PlatformMakeTempName(Picoc *pc, char *TempNameBuffer)
{
int CPos = 5;
while (CPos > 1)
{
if (TempNameBuffer[CPos] < '9')
{
TempNameBuffer[CPos]++;
return TableStrRegister(pc, TempNameBuffer);
}
else
{
TempNameBuffer[CPos] = '0';
CPos--;
}
}
return TableStrRegister(pc, TempNameBuffer);
}