-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.c
87 lines (77 loc) · 1.29 KB
/
basic.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int tag;
void *p1;
void *p2;
} pair;
typedef struct {
int tag;
char cs;
} str;
#define PAIR 0
#define STR 1
pair *newpair(void *p1, void *p2) {
pair *p = (pair *)malloc(sizeof(pair));
p->tag = PAIR;
p->p1 = p1;
p->p2 = p2;
return p;
}
str *newstr(char *cs) {
int len = strlen(cs);
int sz = sizeof(int) + (4*(len+1))/4;
str *s = (str *)malloc(sz);
s->tag = STR;
strcpy(&s->cs,cs);
return s;
}
void printh(void *x) {
if (*((int *)x) == PAIR) {
// it is a pair
pair *p = (pair *)x;
printf("[");
printh(p->p1);
printf(",");
printh(p->p2);
printf("]");
}
else {
// it is a string
str *s = (str *)x;
printf("\"");
fputs(&s->cs, stdout);
printf("\"");
}
}
int print(void *x) {
printh(x);
printf("\n");
return 0;
}
void *convert(int argc, char **argv) {
int i;
void **r = (void **)0;
for (i = argc - 1; i > 0; i--) {
if (!r)
r = newstr(argv[i]);
else
r = newpair(newstr(argv[i]),r);
}
if (!r)
r = newstr("");
return r;
}
int labort() {
exit(1);
return 0;
}
void *_main(void *x0) {
_start__main: {void *args = x0;
return args;
}
}
void *main(void *argc,void *argv) {
return print(_main(convert(argc , argv)));
}