Debian Codegraph @

src/c/printer/write_object.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * write_object.d - basic printer routine
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <ecl/ecl.h>
16 #include <ecl/internal.h>
17 #include <ecl/bytecodes.h>
18
19 bool
20 _ecl_will_print_as_hash(cl_object x)
21 {
22 const cl_env_ptr the_env = ecl_process_env();
23 cl_object circle_counter = ecl_cmp_symbol_value(the_env, @'si::*circle-counter*');
24 cl_object circle_stack = ecl_cmp_symbol_value(the_env, @'si::*circle-stack*');
25 cl_object code = ecl_gethash_safe(x, circle_stack, OBJNULL);
26 if (ECL_FIXNUMP(circle_counter)) {
27 return !(code == OBJNULL || code == ECL_NIL);
28 } else if (code == OBJNULL) {
29 /* Was not found before */
30 _ecl_sethash(x, circle_stack, ECL_NIL);
31 return 0;
32 } else {
33 return 1;
34 }
35 }
36
37 /* To print circular structures, we traverse the structure by adding
38 a pair <element, flag> to the interpreter stack for each element visited.
39 flag is initially NIL and becomes T if the element is visited again.
40 After the visit we squeeze out all the non circular elements.
41 The flags is used during printing to distinguish between the first visit
42 to the element.
43 */
44
45 cl_object
46 si_search_print_circle(cl_object x)
47 {
48 const cl_env_ptr the_env = ecl_process_env();
49 cl_object circle_counter = ecl_cmp_symbol_value(the_env, @'si::*circle-counter*');
50 cl_object circle_stack = ecl_cmp_symbol_value(the_env, @'si::*circle-stack*');
51 cl_object code;
52
53 code = ecl_gethash_safe(x, circle_stack, OBJNULL);
54 if (!ECL_FIXNUMP(circle_counter)) {
55 if (code == OBJNULL) {
56 /* Was not found before */
57 _ecl_sethash(x, circle_stack, ECL_NIL);
58 return ecl_make_fixnum(0);
59 } else if (code == ECL_NIL) {
60 /* This object is referenced twice */
61 _ecl_sethash(x, circle_stack, ECL_T);
62 return ecl_make_fixnum(1);
63 } else {
64 return ecl_make_fixnum(2);
65 }
66 } else {
67 if (code == OBJNULL || code == ECL_NIL) {
68 /* Is not referenced or was not found before */
69 return ecl_make_fixnum(0);
70 } else if (code == ECL_T) {
71 /* This object is referenced twice, but has no code yet */
72 circle_counter = ecl_make_fixnum(ecl_fixnum(circle_counter) + 1);
73 _ecl_sethash(x, circle_stack, circle_counter);
74 ECL_SETQ(the_env, @'si::*circle-counter*', circle_counter);
75 return ecl_make_fixnum(-ecl_fixnum(circle_counter));
76 } else {
77 return code;
78 }
79 }
80 }
81
82 cl_object
83 si_write_object_with_circle(cl_object x, cl_object stream, cl_object print_function)
84 {
85 bool circle = ecl_print_circle();
86 if (circle && !Null(x) && !ECL_FIXNUMP(x) && !ECL_CHARACTERP(x) &&
87 (ecl_t_of(x) != t_symbol || (Null(x->symbol.hpack))))
88 {
89 /* everything except fixnums, characters or interned symbols can
90 possibly contain cycles */
91 cl_object circle_counter;
92 cl_fixnum code;
93 const cl_env_ptr env = ecl_process_env();
94 circle_counter = ecl_cmp_symbol_value(env, @'si::*circle-counter*');
95 if (circle_counter == ECL_NIL) {
96 cl_object hash =
97 cl__make_hash_table(@'eq',
98 ecl_make_fixnum(1024),
99 cl_core.rehash_size,
100 cl_core.rehash_threshold);
101 ecl_bds_bind(env, @'si::*circle-counter*', ECL_T);
102 ecl_bds_bind(env, @'si::*circle-stack*', hash);
103 si_write_object_with_circle(x, cl_core.null_stream, print_function);
104 ECL_SETQ(env, @'si::*circle-counter*', ecl_make_fixnum(0));
105 si_write_object_with_circle(x, stream, print_function);
106 cl_clrhash(hash);
107 ecl_bds_unwind_n(env, 2);
108 goto OUTPUT;
109 }
110 code = ecl_fixnum(si_search_print_circle(x));
111 if (!ECL_FIXNUMP(circle_counter)) {
112 /* We are only inspecting the object to be printed. */
113 /* Only run X if it was not referenced before */
114 if (code != 0)
115 goto OUTPUT;
116 } else if (code == 0) {
117 /* Object is not referenced twice */
118 } else if (code < 0) {
119 /* Object is referenced twice. We print its definition */
120 ecl_write_char('#', stream);
121 _ecl_write_fixnum(-code, stream);
122 ecl_write_char('=', stream);
123 } else {
124 /* Second reference to the object */
125 ecl_write_char('#', stream);
126 _ecl_write_fixnum(code, stream);
127 ecl_write_char('#', stream);
128 goto OUTPUT;
129 }
130 }
131 return _ecl_funcall3(print_function, x, stream);
132 OUTPUT:
133 @(return x);
134
135 }
136
137 cl_object
138 si_write_object(cl_object x, cl_object stream)
139 {
140 #ifdef ECL_CMU_FORMAT
141 if (ecl_symbol_value(@'*print-pretty*') != ECL_NIL) {
142 cl_object f = _ecl_funcall2(@'pprint-dispatch', x);
143 if (VALUES(1) != ECL_NIL) {
144 _ecl_funcall3(f, stream, x);
145 @(return x);
146 }
147 }
148 #endif /* ECL_CMU_FORMAT */
149 return si_write_object_with_circle(x, stream, @'si::write-ugly-object');
150 }