1
2 /******************************************************************************
3 * MODULE : tinyscheme_tm.cpp
4 * DESCRIPTION: TinyScheme interface
5 * COPYRIGHT : (C) 2011 Massimiliano Gubinelli
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12 #include "tinyscheme_tm.hpp"
13 #include "object.hpp"
14 #include "glue.hpp"
15
16
17
18
19 #define TST_BLACKBOX 17
20
21
22 scheme* the_scheme = scheme_init_new();
23
24
finalize_blackbox(void * p)25 void finalize_blackbox(void *p) {
26 tm_delete((blackbox*)p);
27 }
28
29
30
31 /******************************************************************************
32 * Entry points to Scheme
33 ******************************************************************************/
34
35
scm_eval_string(const char * str)36 scm scm_eval_string (const char *str)
37 {
38 scheme_load_string(the_scheme, (char*)str);
39 return the_scheme->value;
40 }
41
scm_eval_file(FILE * f)42 scm scm_eval_file (FILE *f)
43 {
44 scheme_load_file(the_scheme,f);
45 return the_scheme->value;
46 }
47
scm_apply(scm func,scm args)48 scm scm_apply (scm func, scm args)
49 {
50 scheme_call(the_scheme, func, args);
51 return the_scheme->value;
52 }
53
54
scm_lookup_string(const char * name)55 scm scm_lookup_string(const char *name)
56 {
57 return scheme_eval(the_scheme, mk_symbol(the_scheme, name));
58 }
59
scm_define(scm symbol,scm value)60 void scm_define(scm symbol, scm value)
61 {
62 scheme_define(the_scheme, the_scheme->global_env, symbol, value);
63 }
64
65 scm object_stack;
66
67 /******************************************************************************
68 * Installation of guile and initialization of guile
69 ******************************************************************************/
70
71 void
start_scheme(int argc,char ** argv,void (* call_back)(int,char **))72 start_scheme (int argc, char** argv, void (*call_back) (int, char**)) {
73 // gh_enter (argc, argv, call_back);
74 call_back(argc, argv);
75
76 }
77
78 void
initialize_scheme()79 initialize_scheme () {
80 if(!scheme_init(the_scheme)) {
81 cout << "Could not initialize TinyScheme" << LF;
82 }
83 scheme_set_output_port_file(the_scheme, stdout);
84 scheme_set_input_port_file(the_scheme, stdin);
85
86 const char* init_prg =
87 #if 0
88 "(read-set! keywords 'prefix)\n"
89 "(read-enable 'positions)\n"
90 "(debug-enable 'debug)\n"
91 ";(debug-enable 'backtrace)\n"
92 "\n"
93 #endif
94 "(define (display-to-string obj)\n"
95 " (call-with-output-string\n"
96 " (lambda (port) (display obj port))))\n"
97 "(define (object->string obj)\n"
98 " (call-with-output-string\n"
99 " (lambda (port) (write obj port))))\n"
100 "\n"
101 "(define (texmacs-version) \"" TEXMACS_VERSION "\")\n"
102 "(define object-stack '(()))";
103
104 scm_eval_string (init_prg);
105 initialize_glue ();
106 object_stack= scm_lookup_string ("object-stack");
107
108
109 scm_eval_string("(load (url-concretize \"$TEXMACS_PATH/progs/init-tinyscheme.scm\"))");
110 scm_eval_string("(load (url-concretize \"$TEXMACS_PATH/progs/init-scheme-tm.scm\"))");
111
112 //REPL
113 //scm_eval_file (stdin);
114 scheme_load_named_file(the_scheme,stdin,0);
115
116 }
117
118 #if 0
119 /******************************************************************************
120 * Catching errors (with thanks to Dale P. Smith)
121 ******************************************************************************/
122
123 scm
124 TeXmacs_lazy_catcher (void *data, scm tag, scm throw_args) {
125 scm eport= scm_current_error_port();
126 scm_handle_by_message_noexit (data, tag, throw_args);
127 scm_force_output (eport);
128 scm_ithrow (tag, throw_args, 1);
129 return scm_UNSPECIFIED; /* never returns */
130 }
131
132 scm
133 TeXmacs_catcher (void *data, scm tag, scm args) {
134 (void) data;
135 return scm_cons (tag, args);
136 }
137 #endif
138
139 /******************************************************************************
140 * Evaluation of files
141 ******************************************************************************/
142 #if 0
143 static scm
144 TeXmacs_lazy_eval_file (char *file) {
145
146 return scm_internal_lazy_catch (scm_BOOL_T,
147 (scm_t_catch_body) scm_c_primitive_load, file,
148 (scm_t_catch_handler) TeXmacs_lazy_catcher, file);
149 }
150
151 static scm
152 TeXmacs_eval_file (char *file) {
153 return scm_internal_catch (scm_BOOL_T,
154 (scm_t_catch_body) TeXmacs_lazy_eval_file, file,
155 (scm_t_catch_handler) TeXmacs_catcher, file);
156 }
157 #endif
158 scm
eval_scheme_file(string file)159 eval_scheme_file (string file) {
160 //static int cumul= 0;
161 //timer tm;
162 if (DEBUG_STD) debug_std << "Evaluating " << file << "...\n";
163 c_string _file (file);
164 FILE *f = fopen(_file, "r");
165 scm result= scm_eval_file (f);
166 fclose(f);
167 //int extra= tm->watch (); cumul += extra;
168 //cout << extra << "\t" << cumul << "\t" << file << "\n";
169 return result;
170 }
171
172 /******************************************************************************
173 * Evaluation of strings
174 ******************************************************************************/
175 #if 0
176 static scm
177 TeXmacs_lazy_eval_string (char *s) {
178 return scm_internal_lazy_catch (scm_BOOL_T,
179 (scm_t_catch_body) scm_c_eval_string, s,
180 (scm_t_catch_handler) TeXmacs_lazy_catcher, s);
181 }
182
183 static scm
184 TeXmacs_eval_string (char *s) {
185 return scm_internal_catch (scm_BOOL_T,
186 (scm_t_catch_body) TeXmacs_lazy_eval_string, s,
187 (scm_t_catch_handler) TeXmacs_catcher, s);
188 }
189 #endif
190 scm
eval_scheme(string s)191 eval_scheme (string s) {
192 // cout << "Eval] " << s << "\n";
193 c_string _s (s);
194 scm result= scm_eval_string (_s);
195 return result;
196 }
197
198 /******************************************************************************
199 * Using scheme objects as functions
200 ******************************************************************************/
201
202 struct arg_list { int n; scm* a; };
203
204 scm
TeXmacs_call_scm(arg_list * args)205 TeXmacs_call_scm (arg_list* args) {
206 switch (args->n) {
207 default:
208 {
209 int i;
210 scm l= scm_null ();
211 for (i=args->n; i>=1; i--)
212 l= scm_cons (args->a[i], l);
213 return scm_apply (args->a[0], l);
214 }
215 }
216 }
217 #if 0
218 static scm
219 TeXmacs_lazy_call_scm (arg_list* args) {
220 return scm_internal_lazy_catch (scm_BOOL_T,
221 (scm_t_catch_body) TeXmacs_call, (void*) args,
222 (scm_t_catch_handler) TeXmacs_lazy_catcher, (void*) args);
223 }
224
225 static scm
226 TeXmacs_call_scm (arg_list *args) {
227 return scm_internal_catch (scm_BOOL_T,
228 (scm_t_catch_body) TeXmacs_lazy_call_scm, (void*) args,
229 (scm_t_catch_handler) TeXmacs_catcher, (void*) args);
230 }
231 #endif
232 scm
call_scheme(scm fun)233 call_scheme (scm fun) {
234 scm a[]= { fun }; arg_list args= { 0, a };
235 return TeXmacs_call_scm (&args);
236 }
237
238 scm
call_scheme(scm fun,scm a1)239 call_scheme (scm fun, scm a1) {
240 scm a[]= { fun, a1 }; arg_list args= { 1, a };
241 return TeXmacs_call_scm (&args);
242 }
243
244 scm
call_scheme(scm fun,scm a1,scm a2)245 call_scheme (scm fun, scm a1, scm a2) {
246 scm a[]= { fun, a1, a2 }; arg_list args= { 2, a };
247 return TeXmacs_call_scm (&args);
248 }
249
250 scm
call_scheme(scm fun,scm a1,scm a2,scm a3)251 call_scheme (scm fun, scm a1, scm a2, scm a3) {
252 scm a[]= { fun, a1, a2, a3 }; arg_list args= { 3, a };
253 return TeXmacs_call_scm (&args);
254 }
255
256 scm
call_scheme(scm fun,scm a1,scm a2,scm a3,scm a4)257 call_scheme (scm fun, scm a1, scm a2, scm a3, scm a4) {
258 scm a[]= { fun, a1, a2, a3, a4 }; arg_list args= { 4, a };
259 return TeXmacs_call_scm (&args);
260 }
261
262 scm
call_scheme(scm fun,array<scm> a)263 call_scheme (scm fun, array<scm> a) {
264 const int n= N(a);
265 STACK_NEW_ARRAY(v, scm, n+1);
266 int i;
267 v[0]= fun;
268 for (i=0; i<n; i++) v[i+1]= a[i];
269 arg_list args= { n, v };
270 scm ret= TeXmacs_call_scm (&args);
271 STACK_DELETE_ARRAY(scm);
272 return ret;
273 }
274
275
276 /******************************************************************************
277 * Gluing
278 ******************************************************************************/
279
280
281 string
scheme_dialect()282 scheme_dialect () {
283 return "littlescheme";
284 }
285
scm_define_glue(const char * name,scm_foreign_func f)286 void scm_define_glue(const char *name, scm_foreign_func f)
287 {
288 // cout << "Define glue: " << name << LF;
289 scm_define(symbol_to_scm(name), mk_foreign_func (the_scheme, f));
290 }
291
292
293
294
295 /******************************************************************************
296 * Strings
297 ******************************************************************************/
298
299 scm
string_to_scm(string s)300 string_to_scm (string s) {
301 c_string _s (s);
302 scm r= mk_counted_string (the_scheme,_s, N(s));
303 return r;
304 }
305
306 /******************************************************************************
307 * Symbols
308 ******************************************************************************/
309
310 scm
symbol_to_scm(string s)311 symbol_to_scm (string s) {
312 c_string _s (s);
313 scm r= mk_symbol (the_scheme,_s);
314 return r;
315 }
316
317