-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.c
148 lines (146 loc) · 2.63 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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
typedef struct {
long length;
unsigned char chars[1];
} string;
string consts[256];
string empty = {0, ""};
long *_initArray(long size, long init)
{
long i;
long *a = malloc((size+1) * sizeof(long));
a[0] = size;
for (i = 1; i <= size; i++)
a[i] = init;
return a+1;
}
void _checkIndexArray(long *a, long i)
{
if(i<0 || i>=a[-1]) {
fprintf(stderr, "indice %ld excedido!\n", i);
exit(-1);
}
}
long *_allocRecord(long ctos, ...)
{
long i;
long *p, *a;
va_list va;
p = a = malloc(ctos*sizeof(long));
va_start(va, ctos);
for (i = 0; i < ctos; i ++)
*p++ = va_arg(va, long);
return a;
}
void _checkNil(long* r)
{
if(r==0) {
fprintf(stderr, "Nil!\n");
exit(-1);
}
}
long _stringCompare(string *s, string *t)
{
long i;
if(s == t)
return 0;
for(i = 0; i<s->length && i<t->length; i++)
if(s->chars[i]!=t->chars[i])
return s->chars[i]-t->chars[i];
return s->length-t->length;
}
void print(string *s)
{
long i;
unsigned char *p = s->chars;
for (i = 0; i < s->length; i++, p++)
putchar(*p);
}
void print_int(long i)
{
printf("%ld", i);
}
void flush()
{
fflush(stdout);
}
long ord(string *s)
{
if (s->length != 1) {
fprintf(stderr, "ord: string inadecuada\n");
exit(-1);
}
else
return s->chars[0];
}
string *chr(long i)
{
if (i < 0 || i >= 256) {
printf("chr(%ld) out of range\n", i);
exit(1);
}
return consts + i;
}
long size(string *s)
{
return s->length;
}
string *substring(string *s, long first, long n)
{
if (first < 0 || first + n > s->length) {
fprintf(stderr, "substring([%ld],%ld,%ld) out of range\n",
s->length, first, n);
exit(1);
}
if (n == 1)
return consts + s->chars[first];
{
string *t = malloc(sizeof(string) + n);
long i;
t->length = n;
for (i = 0; i < n; i++)
t->chars[i] = s->chars[first + i];
return t;
}
}
string *concat(string *a, string *b)
{
if (a->length == 0)
return b;
else if (b->length == 0)
return a;
else {
long i, n = a->length + b->length;
string *t = malloc(sizeof(string) + n);
t->length = n;
for (i = 0; i < a->length; i++)
t->chars[i] = a->chars[i];
for (i = 0; i < b->length; i++)
t->chars[i + a->length] = b->chars[i];
return t;
}
}
long not(long i)
{
return !i;
}
string *getstr()
{
int i = getc(stdin);
if (i == EOF)
return ∅
else
return consts + i;
}
int main()
{
extern long _tigermain(long);
int i;
for (i = 0; i < 256; i++) {
consts[i].length = 1;
consts[i].chars[0] = i;
}
return _tigermain(0 /* static link!? */ );
}