-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime_io.c
58 lines (52 loc) · 1.28 KB
/
runtime_io.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
#include "mcp_forth.h"
#include <stdio.h>
static int rt_type(void * param, m4_stack_t * stack)
{
if(!(stack->len >= 2)) return M4_STACK_UNDERFLOW_ERROR;
fwrite((void *)stack->data[-2], 1, stack->data[-1], stdout);
fflush(stdout);
stack->data -= 2;
stack->len -= 2;
return 0;
}
static int rt_cr(void * param, m4_stack_t * stack)
{
puts("");
return 0;
}
static int rt_dot(void * param, m4_stack_t * stack)
{
if(!(stack->len >= 1)) return M4_STACK_UNDERFLOW_ERROR;
printf("%d ", stack->data[-1]);
fflush(stdout);
stack->data -= 1;
stack->len -= 1;
return 0;
}
static int rt_key(void * param, m4_stack_t * stack)
{
if(!(stack->len < stack->max)) return M4_STACK_OVERFLOW_ERROR;
int c = getchar();
if(c == EOF) c = 4; /* ASCII EOT (end of transmission) */
*stack->data = c;
stack->data += 1;
stack->len += 1;
return 0;
}
static int rt_emit(void * param, m4_stack_t * stack)
{
if(!(stack->len >= 1)) return M4_STACK_UNDERFLOW_ERROR;
putchar(stack->data[-1]);
fflush(stdout);
stack->data -= 1;
stack->len -= 1;
return 0;
}
const m4_runtime_cb_array_t m4_runtime_lib_io[] = {
{"type", {rt_type}},
{"cr", {rt_cr}},
{".", {rt_dot}},
{"key", {rt_key}},
{"emit", {rt_emit}},
{NULL},
};