-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.c
72 lines (61 loc) · 1.34 KB
/
stmt.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
#include "stmt.h"
#include "machine.h"
#include <string.h>
#include <stdio.h>
stmt_t *stmt_init(char *label, wam_op_t op, int narg, ...)
{
stmt_t *stmt = malloc(sizeof(stmt_t));
strcpy(stmt->label, label);
stmt->op = op;
stmt->jump = -1;
va_list ap;
va_start(ap, narg);
int i;
for (i = 0; i < narg; i++) {
char *arg = va_arg(ap, char *);
stmt->args[i] = malloc(sizeof(arg) + 1);
strcpy(stmt->args[i], arg);
}
stmt->narg = narg;
return stmt;
}
void stmt_destroy(stmt_t *stmt)
{
int i;
for (i = 0; i < stmt->narg; i++) {
free(stmt->args[i]);
}
free(stmt);
}
stmt_t *stmt_copy(stmt_t *stmt)
{
stmt_t *copy = malloc(sizeof(stmt_t));
memcpy(copy, stmt, sizeof(stmt_t));
int i;
for (i = 0; i < stmt->narg; i++) {
copy->args[i] = malloc(sizeof(strlen(stmt->args[i])));
strcpy(copy->args[i], stmt->args[i]);
}
strcpy(copy->label, stmt->label);
return copy;
}
void stmt_info(stmt_t *stmt)
{
if (strcmp(stmt->label, ";") == 0) {
printf("; %d\n", stmt->op);
}
char buf[MAX_LINE_LEN] = {};
if (stmt->label[0] != 0) {
sprintf(buf, "%12s: ", stmt->label);
} else {
sprintf(buf, " ");
}
sprintf(buf, "%s%s", buf, OP_NAMES(stmt->op));
int i;
for (i = 0; i < stmt->narg; i++) {
sprintf(buf, "%s %s", buf, stmt->args[i]);
}
if (stmt->jump >= 0)
sprintf(buf, "%s (%d)", buf, stmt->jump);
printf("%s\n", buf);
}