9 #define MAXLOCALS (1 << 10)
10 #define print(s) write(2, (s), strlen(s));
18 static void die(char *s
)
24 static int tok_jmp(int tok
)
32 static void tok_expect(int tok
)
35 die("syntax error\n");
38 static void readexpr(void);
40 static int readtype(void)
73 static void readprimary(void)
76 if (!tok_jmp(TOK_NUM
)) {
77 o_num(atoi(tok_id()));
80 if (!tok_jmp(TOK_NAME
)) {
81 for (i
= 0; i
< nlocals
; i
++) {
82 if (!strcmp(locals
[i
].name
, tok_id())) {
83 o_local(locals
[i
].addr
);
96 static void readpost(void)
106 while (!tok_jmp(',')) {
113 static void readexpr(void)
122 static void readstmt(void)
130 tok_expect(TOK_NAME
);
131 strcpy(locals
[nlocals
].name
, tok_id());
132 locals
[nlocals
].addr
= o_mklocal();
136 o_local(locals
[nlocals
- 1].addr
);
143 if (!tok_jmp(TOK_IF
)) {
150 if (!tok_jmp(TOK_ELSE
)) {
160 if (!tok_jmp(TOK_WHILE
)) {
172 if (!tok_jmp(TOK_RETURN
)) {
173 int ret
= tok_see() != ';';
184 static void readdecl(void)
188 tok_expect(TOK_NAME
);
189 strcpy(name
, tok_id());
194 while (tok_get() != ')')
203 die("syntax error\n");
206 static void parse(void)
208 while (tok_see() != TOK_EOF
)
212 int main(int argc
, char *argv
[])
217 ifd
= open(src
, O_RDONLY
);
224 obj
[strlen(obj
) - 1] = 'o';
225 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);