-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgen.c
84 lines (60 loc) · 1.08 KB
/
cgen.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
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "cgen.h"
extern int line_num;
void ssopen(sstream* S)
{
S->stream = open_memstream(& S->buffer, & S->bufsize);
}
char* ssvalue(sstream* S)
{
fflush(S->stream);
return S->buffer;
}
void ssclose(sstream* S)
{
fclose(S->stream);
}
char* template(const char* pat, ...)
{
sstream S;
ssopen(&S);
va_list arg;
va_start(arg, pat);
vfprintf(S.stream, pat, arg );
va_end(arg);
char* ret = ssvalue(&S);
ssclose(&S);
return ret;
}
/* Helper functions */
char* string_ptuc2c(char* P)
{
/*
This implementation is
***** NOT CORRECT ACCORDING TO THE PROJECT ******
*/
/* Just chech and change the first and last characters */
int Plen = strlen(P);
assert(Plen>=2);
P[0] = '"';
P[Plen-1] = '"';
return P;
}
/*
Report errors
*/
void yyerror (char const *pat, ...) {
va_list arg;
fprintf (stderr, "line %d: ", line_num);
va_start(arg, pat);
vfprintf(stderr, pat, arg);
va_end(arg);
}
int yyerror_count = 0;
const char* c_prologue =
"#include \"ptuclib.h\"\n"
"\n"
;