File: slib_core.cc

package info (click to toggle)
speech-tools 1:2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,848 kB
  • sloc: cpp: 67,350; ansic: 12,175; sh: 4,047; java: 3,748; makefile: 1,109; lisp: 711; perl: 501; awk: 85; xml: 9
file content (370 lines) | stat: -rw-r--r-- 10,271 bytes parent folder | download | duplicates (5)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*  
 *                   COPYRIGHT (c) 1988-1994 BY                             *
 *        PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
 *        See the source file SLIB.C for more information.                  *

 * Reorganization of files (Mar 1999) by Alan W Black <awb@cstr.ed.ac.uk>

 * System functions

*/
#include <cstdio>
#include "siod.h"
#include "siodp.h"

static LISP sym_lambda = NIL;
static LISP sym_progn = NIL;

LISP setvar(LISP var,LISP val,LISP env)
{LISP tmp;
 if NSYMBOLP(var) err("wrong type of argument(non-symbol) to setvar",var);
 tmp = envlookup(var,env);
 if NULLP(tmp) return(VCELL(var) = val);
 return(CAR(tmp)=val);}
 
static LISP leval_setq(LISP args,LISP env)
{return(setvar(car(args),leval(car(cdr(args)),env),env));}

static LISP syntax_define(LISP args)
{
    if SYMBOLP(car(args)) 
                  return(args);
    else
    {
        need_n_cells(4);
        return(syntax_define(
                             cons(car(car(args)),
                             cons(cons(sym_lambda,
                             cons(cdr(car(args)),
                                  cdr(args))),
                                  NIL))));
    }
}
      
static LISP leval_define(LISP args,LISP env)
{LISP tmp,var,val;
 tmp = syntax_define(args);
 var = car(tmp);
 if NSYMBOLP(var) err("wrong type of argument(non-symbol) to define",var);
 val = leval(car(cdr(tmp)),env);
 tmp = envlookup(var,env);
 if NNULLP(tmp) return(CAR(tmp) = val);
 if NULLP(env) return(VCELL(var) = val);
 tmp = car(env);
 setcar(tmp,cons(var,car(tmp)));
 setcdr(tmp,cons(val,cdr(tmp)));
 return(val);}
 
static LISP leval_if(LISP *pform,LISP *penv)
{LISP args,env;
 args = cdr(*pform);
 env = *penv;
 if NNULLP(leval(car(args),env)) 
    *pform = car(cdr(args)); else *pform = car(cdr(cdr(args)));
 return(truth);}

static LISP arglchk(LISP x)
{
#if (!ENVLOOKUP_TRICK)
 LISP l;
 if SYMBOLP(x) return(x);
 for(l=x;CONSP(l);l=CDR(l));
 if NNULLP(l) err("improper formal argument list",x);
#endif
 return(x);}

static LISP leval_lambda(LISP args,LISP env)
{LISP body;
 if NULLP(cdr(cdr(args)))
   body = car(cdr(args));
  else body = cons(sym_progn,cdr(args));
 return(closure(env,cons(arglchk(car(args)),body)));}
                         
static LISP leval_progn(LISP *pform,LISP *penv)
{LISP env,l,next;
 env = *penv;
 gc_protect(&env);
 l = cdr(*pform);
 next = cdr(l);
 while (NNULLP(next)) 
 {
     leval(car(l),env);
     l=next;
     next=cdr(next);
 }
 gc_unprotect(&env);
 *pform = car(l); 
 return(truth);}

static LISP leval_or(LISP *pform,LISP *penv)
{LISP env,l,next,val;
 env = *penv;
 l = cdr(*pform);
 next = cdr(l);
 while(NNULLP(next))
   {val = leval(car(l),env);
    if NNULLP(val) {*pform = val; return(NIL);}
    l=next;next=cdr(next);}
 *pform = car(l); 
 return(truth);}

static LISP leval_and(LISP *pform,LISP *penv)
{LISP env,l,next;
 env = *penv;
 l = cdr(*pform);
 if NULLP(l) {*pform = truth; return(NIL);}
 next = cdr(l);
 while(NNULLP(next))
   {if NULLP(leval(car(l),env)) {*pform = NIL; return(NIL);}
    l=next;next=cdr(next);}
 *pform = car(l); 
 return(truth);}

static LISP leval_catch(LISP args,LISP env)
{struct catch_frame frame;
 int k;
 LISP l;
 volatile LISP val = NIL;
 frame.tag = leval(car(args),env);
 frame.next = catch_framep;
 k = setjmp(frame.cframe);
 catch_framep = &frame;
 if (k == 2)
   {catch_framep = frame.next;
    return(frame.retval);}
 for(l=cdr(args); NNULLP(l); l = cdr(l))
   val = leval(car(l),env);
 catch_framep = frame.next;
 return(val);}

static LISP lthrow(LISP tag,LISP value)
{struct catch_frame *l;
 for(l=catch_framep; l; l = (*l).next)
   if EQ((*l).tag,tag)
     {(*l).retval = value;
      longjmp((*l).cframe,2);}
 err("no *catch found with this tag",tag);
 return(NIL);}

static LISP leval_let(LISP *pform,LISP *penv)
{LISP env,l;
 l = cdr(*pform);
 env = *penv;
 *penv = extend_env(leval_args(car(cdr(l)),env),car(l),env);
 *pform = car(cdr(cdr(l)));
 return(truth);}

static LISP leval_quote(LISP args,LISP env)
{(void)env;
 return(car(args));}

static LISP leval_tenv(LISP args,LISP env)
{(void)args;
 return(env);}

static LISP leval_while(LISP args,LISP env)
{LISP l;
 while NNULLP(leval(car(args),env))
   for(l=cdr(args);NNULLP(l);l=cdr(l))
     leval(car(l),env);
 return(NIL);}

static LISP siod_typeof(LISP exp)
{
    switch TYPE(exp)
	{
	case tc_nil:
	    return NIL;
	case tc_cons:
	    return rintern("cons");
	case tc_flonum:
	    return rintern("flonum");
	case tc_string:
	    return rintern("string");
	case tc_subr_0:
	case tc_subr_1:
	case tc_subr_2:
	case tc_subr_3:
	case tc_subr_4:
	case tc_lsubr:
	case tc_fsubr:
	case tc_msubr:
	    return rintern("subr");
	case tc_c_file:
	    return rintern("c_file");
	case tc_closure:
	    return rintern("closure");
	default:
	    struct user_type_hooks *p;
	    EST_String tkb;
	    char ttkbuffer[1024];
	    p = get_user_type_hooks(TYPE(exp));
	    if (p->print_string)
	    {
		(*p->print_string)(exp, ttkbuffer);
		tkb = ttkbuffer;
		return rintern(tkb.after("#<").before(" "));
	    }
	    else
	    {
		if (p->name)
		    return rintern(p->name);
		else
		    return rintern("unknown");
	    }

	}
}

static LISP symbolp(LISP x)
{if SYMBOLP(x) return(truth); else return(NIL);}

LISP symbol_boundp(LISP x,LISP env)
{LISP tmp;
 if NSYMBOLP(x) err("not a symbol",x);
 tmp = envlookup(x,env);
 if NNULLP(tmp) return(truth);
 if EQ(VCELL(x),unbound_marker) return(NIL); else return(truth);}

LISP symbol_value(LISP x,LISP env)
{LISP tmp;
 if NSYMBOLP(x) err("not a symbol",x);
 tmp = envlookup(x,env);
 if NNULLP(tmp) return(CAR(tmp));
 tmp = VCELL(x);
 if EQ(tmp,unbound_marker) err("unbound variable",x);
 return(tmp);}

static LISP l_unwind_protect(LISP args, LISP env)
{
    // Do normal, if an error occurs do onerror
    jmp_buf * volatile local_errjmp = est_errjmp;
    est_errjmp = walloc(jmp_buf,1);
    volatile long local_errjmp_ok = errjmp_ok;
    errjmp_ok=1;   /* allow errjmps in here */
    volatile LISP r=NIL;
    volatile LISP previous_open_files = open_files;

    if (setjmp(*est_errjmp) != 0)
    {
	wfree(est_errjmp);
	est_errjmp = local_errjmp;
	errjmp_ok = local_errjmp_ok;
	siod_reset_prompt();
	// Close any that were opened below here
	close_open_files_upto(previous_open_files);
	if (siod_ctrl_c == TRUE)
	    err("forwarded through unwind-protect",NIL);
	r = leval(car(cdr(args)),env);
    }
    else
    {
	r = leval(car(args),env);
	wfree(est_errjmp);
	est_errjmp = local_errjmp;
	errjmp_ok = local_errjmp_ok;
    }

    return r;
}

static LISP oblistfn(void)
{return(copy_list(oblistvar));}

LISP let_macro(LISP form)
{LISP p,fl,al,tmp;
 fl = NIL;
 al = NIL;
 for(p=car(cdr(form));NNULLP(p);p=cdr(p))
  {tmp = car(p);
   if SYMBOLP(tmp) {fl = cons(tmp,fl); al = cons(NIL,al);}
   else {fl = cons(car(tmp),fl); al = cons(car(cdr(tmp)),al);}}
 p = cdr(cdr(form));
 if NULLP(cdr(p)) p = car(p); else p = cons(sym_progn,p);
 setcdr(form,cons(reverse(fl),cons(reverse(al),cons(p,NIL))));
 setcar(form,cintern("let-internal"));
 return(form);}
   
void init_subrs_core(void)
{
    gc_protect_sym(&sym_lambda,"lambda");
    gc_protect_sym(&sym_progn,"begin");

 init_fsubr("quote",leval_quote,
 "(quote DATA)\n\
  Return data (unevaluated).");
 init_fsubr("set!",leval_setq,
 "(set! SYMBOL VAL)\n\
  Set SYMBOL to have value VAL, returns VAL.");
 init_fsubr("define",leval_define,
 "(define (FUNCNAME ARG1 ARG2 ...) . BODY)\n\
 Define a new function call FUNCNAME with arguments ARG1, ARG2 ... and\n\
 BODY.");
 init_fsubr("lambda",leval_lambda,
 "(lambda (ARG1 ARG2 ...) . BODY)\n\
  Create closure (anonymous function) with arguments ARG1, ARG2 ... and \n\
 BODY.");
 init_msubr("if",leval_if,
 "(if COND TRUEPART FALSEPART)\n\
  If COND evaluates to non-nil evaluate TRUEPART and return result,\n\
  otherwise evaluate and return FALSEPART.  If COND is nil and FALSEPART\n\
  is nil, nil is returned.");
 init_fsubr("while",leval_while,
 "(while COND . BODY)\n\
  While COND evaluates to non-nil evaluate BODY.");
 init_msubr("begin",leval_progn,
 "(begin . BODY)\n\
  Evaluate s-expressions in BODY returning value of from last expression.");
 init_fsubr("*catch",leval_catch,
 "(*catch TAG . BODY)\n\
  Evaluate BODY, if a *throw occurs with TAG then return value specified\n\
  by *throw.");
 init_subr_2("*throw",lthrow,
 "(*throw TAG VALUE)\n\
  Jump to *catch with TAG, causing *catch to return VALUE.");
 init_msubr("let-internal",leval_let,
 "(let-internal STUFF)\n\
  Internal function used to implement let.");
 init_msubr("or",leval_or,
 "(or DISJ1 DISJ2 ...)\n\
  Evaluate each disjunction DISJn in turn until one evaluates to non-nil.\n\
  Otherwise return nil.");
 init_msubr("and",leval_and,
 "(and CONJ1 CONJ2 ... CONJN)\n\
  Evaluate each conjunction CONJn in turn until one evaluates to nil.\n\
  Otherwise return value of CONJN.");
 init_subr_1("typeof",siod_typeof,
 "(typeof OBJ)\n\
  Returns typeof of given object.");
 init_subr_1("symbol?",symbolp,
 "(symbol? DATA)\n\
  Returns t if DATA is a symbol, nil otherwise.");
 init_subr_2("symbol-bound?",symbol_boundp,
 "(symbol-bound? VARNAME)\n\
  Return t is VARNAME has a value, nil otherwise.");
 init_subr_2("symbol-value",symbol_value,
 "(symbol-value SYMBOLNAME)\n\
  Returns the value of SYMBOLNAME, an error is given SYMBOLNAME is not a\n\
  bound symbol.");
 init_fsubr("the-environment",leval_tenv,
 "(the-environment)\n\
  Returns the current (SIOD) environment.");
 init_fsubr("unwind-protect",l_unwind_protect,
 "(unwind-protect NORMALFORM ERRORFORM)\n\
 If an error is found while evaluating NORMALFORM catch it and evaluate\n\
 ERRORFORM and continue.  If an error occurs while evaluating NORMALFORM\n\
 all file open evaluating NORMALFORM up to the error while be automatically\n\
 closed.  Note interrupts (ctrl-c) is not caught by this function.");
 init_subr_0("oblist",oblistfn,
 "(oblist)\n\
  Return oblist.");
 init_subr_1("let-internal-macro",let_macro,
 "(let ((VAR1 VAL1) (VAR2 VAL2) ...) . BODY)\n\
  Evaluate BODY in an environment where VAR1 is set to VAL1, VAR2 is set\n\
  to VAL2 etc.");
 init_subr_3("set-symbol-value!",setvar,
 "(set-symbol-value! SYMBOLNAME VALUE)\n\
  Set SYMBOLNAME's value to VALUE, this is much faster than set! but use\n\
  with caution.");

}