-
Notifications
You must be signed in to change notification settings - Fork 1
/
pbc.c
51 lines (45 loc) · 975 Bytes
/
pbc.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
/* pbc.c
* bytecode compilation
*/
#include <stdio.h>
#include <stdlib.h>
#include "pbc.h"
#include "lex.yy.h"
#include "y.tab.h"
extern FILE *yyin;
extern FILE *yyout;
extern FILE *ppbcout;
extern int yyparse();
extern YY_BUFFER_STATE yy_scan_string(const char *);
extern void yy_delete_buffer(YY_BUFFER_STATE buffer );
int pbc_compile(FILE *in, FILE *out)
{
yyin = in;
ppbcout = out;
return !yyparse();
}
// returns a pointer to a temporary file that contains the bytecode
FILE *pbc_compile_from_string(char *in)
{
YY_BUFFER_STATE state = yy_scan_string(in);
int inchar;
ppbcout = tmpfile();
if (!ppbcout) return NULL;
if (yyparse()) return NULL;
rewind(ppbcout);
yy_delete_buffer(state);
return ppbcout;
}
int pbc_compiles(char *in, char *out)
{
int i;
FILE *fin, *fout;
fin = fopen(in,"r");
if (!fin) return 0;
fout = fopen(out,"w");
if (!fout) return 0;
i = pbc_compile(fin,fout);
fclose(fin);
fclose(fout);
return i;
}