forked from isaacdavis/deancaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.c
173 lines (141 loc) · 3 KB
/
runtime.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "runtime.h"
/******************************************************************************/
/* Object */
/******************************************************************************/
Object
Object$$init$(Object xthis)
{
return xthis;
}
/******************************************************************************/
/* String */
/******************************************************************************/
String
String$$init$(String xthis)
{
Object$$init$((Object) xthis);
return xthis;
}
String
_$CreateString(char *t)
{
extern void *_V$String;
String s = (String) malloc(sizeof(struct __String) + strlen(t));
s->v_table = _V$String;
strcpy(s->contents, t);
return s;
}
/******************************************************************************/
/* IO */
/******************************************************************************/
void
IO$putChar (char c)
{
putchar(c);
}
void
IO$putInt (int v)
{
printf("%d", v);
}
void
IO$putString (String s)
{
printf("%s", s->contents);
}
int
IO$peek(void)
{
int ch = getc(stdin);
ungetc(ch, stdin);
return ch;
}
int
IO$getChar()
{
int ch = fgetc(stdin);
return ch;
}
int
IO$getInt()
{
int v;
scanf("%d",&v);
return v;
}
String
IO$getLine()
{
char buf[10240];
int c;
// Flush stdin TODO should I be doing this? Good for interactive use cases
// but maybe not for other things
while ((c = getchar()) != '\n' && c != EOF);
fgets(buf,10240,stdin);
return _$CreateString(buf);
}
/******************************************************************************/
/* ARRAY */
/******************************************************************************/
static ARRAY doAlloc(int,int,int,va_list);
ARRAY
_$ArrayAllocate(int typ, int dims, ...)
{
va_list ap;
va_start(ap,dims);
return doAlloc(typ,dims,0,ap);
}
static ARRAY
doAlloc(int typ, int ndim, int dim, va_list ap)
{
int n = va_arg(ap,int);
int ntyp = typ;
int sz,dsz,i;
ARRAY a;
if (dim+1 != ndim) ntyp = 3;
switch (ntyp) {
case 0: /* BYTE */
dsz = sizeof(char);
break;
case 1: /* CHAR */
dsz = sizeof(short);
break;
case 2: /* INT */
dsz = sizeof(int);
break;
case 3: /* REF */
dsz = sizeof(void *);
break;
}
sz = sizeof(int) + dsz * n;
a = (ARRAY) malloc(sz);
memset(a, 0, sz);
a->length = n;
if (dim + 1 != ndim) {
ARRAY *ptr = (ARRAY *) (&a->data);
for (i = 0; i < n; ++i) {
*ptr++ = doAlloc(typ,ndim,dim+1,ap);
}
}
return a;
}
/******************************************************************************/
/* main */
/******************************************************************************/
extern void _$DecafMain(ARRAY);
int
main(int argc,char ** argv)
{
int i;
ARRAY a = _$ArrayAllocate(3,1,argc);
String *sp = (String *) (&a->data);
for (i = 0; i < argc; ++i) {
sp[i] = _$CreateString(argv[i]);
}
_$DecafMain(a);
return 0;
}