forked from zsaleeba/picoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform_support.c
156 lines (130 loc) · 3.38 KB
/
platform_support.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
#include "picoc.h"
#ifdef UNIX_HOST
/* get a line of interactive input */
char *PlatformGetLine(char *Buf, int MaxLen)
{
return fgets(Buf, MaxLen, stdin);
}
/* write a character to the console */
void PlatformPutc(unsigned char OutCh)
{
putchar(OutCh);
}
/* read a file into memory */
char *PlatformReadFile(const char *FileName)
{
struct stat FileInfo;
char *ReadText;
FILE *InFile;
int BytesRead;
if (stat(FileName, &FileInfo))
ProgramFail(NULL, "can't read file %s\n", FileName);
ReadText = malloc(FileInfo.st_size + 1);
if (ReadText == NULL)
ProgramFail(NULL, "out of memory\n");
InFile = fopen(FileName, "r");
if (InFile == NULL)
ProgramFail(NULL, "can't read file %s\n", FileName);
BytesRead = fread(ReadText, 1, FileInfo.st_size, InFile);
if (BytesRead == 0)
ProgramFail(NULL, "can't read file %s\n", FileName);
ReadText[BytesRead] = '\0';
fclose(InFile);
return ReadText;
}
/* read and scan a file for definitions */
void PlatformScanFile(const char *FileName)
{
char *SourceStr = PlatformReadFile(FileName);
Parse(FileName, SourceStr, strlen(SourceStr), TRUE);
free(SourceStr);
}
/* mark where to end the program for platforms which require this */
static jmp_buf ExitBuf;
int PlatformSetExitPoint()
{
return setjmp(ExitBuf);
}
/* exit the program */
void PlatformExit()
{
longjmp(ExitBuf, 1);
}
#endif
#ifdef SURVEYOR_HOST
/* get a line of interactive input */
char *PlatformGetLine(char *Buf, int MaxLen)
{
return NULL;
}
/* write a character to the console */
void PlatformPutc(unsigned char OutCh)
{
putchar(OutCh);
}
/* mark where to end the program for platforms which require this */
int PlatformSetExitPoint()
{
return 0;
}
/* exit the program */
extern int errjmp[];
void PlatformExit()
{
errjmp[40] = 1;
longjmp(errjmp, 1);
}
#endif
/* exit with a message */
void ProgramFail(struct ParseState *Parser, const char *Message, ...)
{
va_list Args;
if (Parser != NULL)
PlatformPrintf("%s:%d: ", Parser->FileName, Parser->Line);
va_start(Args, Message);
PlatformVPrintf(Message, Args);
PlatformPrintf("\n");
PlatformExit(1);
}
/* exit lexing with a message */
void LexFail(struct LexState *Lexer, const char *Message, ...)
{
va_list Args;
PlatformPrintf("%s:%d: ", Lexer->FileName, Lexer->Line);
va_start(Args, Message);
PlatformVPrintf(Message, Args);
PlatformPrintf("\n");
PlatformExit(1);
}
/* printf for compiler error reporting */
void PlatformPrintf(const char *Format, ...)
{
va_list Args;
va_start(Args, Format);
PlatformVPrintf(Format, Args);
va_end(Args);
}
void PlatformVPrintf(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 *), PlatformPutc); break;
case 'd': PrintInt(va_arg(Args, int), PlatformPutc); break;
case 'c': PlatformPutc(va_arg(Args, int)); break;
#ifndef NO_FP
case 'f': PrintFP(va_arg(Args, double), PlatformPutc); break;
#endif
case '%': PlatformPutc('%'); break;
case '\0': FPos--; break;
}
}
else
PlatformPutc(*FPos);
}
}