-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreadback.c
123 lines (112 loc) · 3.18 KB
/
readback.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
/****************************************************************/
/* This module implements the readback procedure as defined */
/* is Asperti Laneve: Interaction Systems II. */
/* The only external function is */
/* - rdbk(): it provides the readback in standard syntax */
/* of the graphical term whose root is passed */
/* as input parameter. */
/****************************************************************/
/****************************************************************/
/* Inclusion of header files. */
/****************************************************************/
#include "bohm.h"
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#define PRINT_MAX 100
static int left_to_print;
/* maximum number of characters yet to print */
static void rdbk_1(), rdbk_list();
/* the following function prints on the standard output the */
/* standard syntactical representation of the graphical term */
/* whose root is passed in input */
void rdbk(form)
FORM *form;
{
left_to_print=PRINT_MAX;
printf(" ");
rdbk_1(form,0);
printf("\n");
}
static void rdbk_1(form,port)
FORM *form;
int port;
{
if(left_to_print>0)
if(form->nport[port]<0) {
switch(form->nport[port]){
case INT:
left_to_print-=printf("%" PRIdPTR, (intptr_t)form->nform[0]);
break;
case T:
left_to_print-=printf("TRUE");
break;
case F:
left_to_print-=printf("FALSE");
break;
case NIL:
left_to_print-=printf("[]");
break;
default:
left_to_print-=printf("...");
}
}
else
switch(form->nform[port]->name){
case LAMBDA:
case LAMBDAUNB:
if(form->nport[port]==0)
left_to_print-=printf("#<function>");
else
left_to_print-=printf("...");
break;
case CONS:
if(form->nport[port]==0) {
left_to_print-=printf("[");
rdbk_1(form->nform[port],1);
rdbk_list(form->nform[port],2);
} else {
left_to_print-=printf("...");
}
break;
case FAN:
if(form->nport[port]!=0)
rdbk_1(form->nform[port],0);
else {
left_to_print-=printf("...");
}
break;
case TRIANGLE:
rdbk_1(form->nform[port],!form->nport[port]);
break;
default:
left_to_print-=printf("...");
}
else
left_to_print-=printf("...");
}
static void rdbk_list(form,port)
FORM *form;
int port;
{
if((int)form->nport[port]==NIL)
left_to_print-=printf("]");
else if(left_to_print<=0)
left_to_print-=printf("...]");
else if(form->nport[port]<0) {
left_to_print-=printf("|");
rdbk_1(form,port);
left_to_print-=printf("]");
} else if(form->nform[port]->name==TRIANGLE ||
(form->nform[port]->name==FAN && form->nport[port]!=0))
rdbk_list(form->nform[port],!form->nport[port]);
else if(form->nform[port]->name!=CONS || form->nport[port]!=0) {
left_to_print-=printf("|");
rdbk_1(form,port);
left_to_print-=printf("]");
} else {
left_to_print-=printf(",");
rdbk_1(form->nform[port],1);
rdbk_list(form->nform[port],2);
}
}