File: xljump.c

package info (click to toggle)
xlispstat 3.52.0-3
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 7,472 kB
  • ctags: 12,480
  • sloc: ansic: 89,534; lisp: 21,690; sh: 1,525; makefile: 520; csh: 1
file content (221 lines) | stat: -rw-r--r-- 6,067 bytes parent folder | download | duplicates (2)
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
/* xljump - execution context routines */
/* Copyright (c) 1989, by David Michael Betz.                            */
/* You may give out copies of this software; for conditions see the file */
/* COPYING included with this distribution.                              */

#include "xlisp.h"

/* forward declarations */
LOCAL VOID findandjump P2H(int, char *);

/* xlbegin - beginning of an execution context */
VOID xlbegin P3C(CONTEXT *, cptr, int, flags, LVAL, expr)
{
    cptr->c_flags = flags;
    cptr->c_expr = expr;
    cptr->c_xlstack = xlstack;
    cptr->c_xlenv = xlenv;
    cptr->c_xlfenv = xlfenv;
    cptr->c_xldenv = xldenv;
    cptr->c_xlcontext = xlcontext;
    cptr->c_xlargv = xlargv;
    cptr->c_xlargc = xlargc;
    cptr->c_xlfp = xlfp;
    cptr->c_xlsp = xlsp;
#ifdef BYTECODE
    cptr->c_xlcstop = xlcstop;
#endif /* BYTECODE */
    xlcontext = cptr;
}

/* xlend - end of an execution context */
VOID xlend P1C(CONTEXT *, cptr)
{
    xlcontext = cptr->c_xlcontext;
}

/* xlgo - go to a label */
/* TAA MOD 4/94 -- changed mechanism that GO target was
   propagated back to tagbody(). Formerly the context block
   was altered, but this caused problems with GO's within
   UNWIND-PROTECTS (Error reported by Gottfried Ira, 3/94) */
VOID xlgo P1C(LVAL, label)
{
#ifdef LEXBIND
  CONTEXT *cptr, *ctarg;
  FRAMEP argv;
  LVAL env,frame,tags;
  int argc;

  /* find a lexically visible tag context */
  for (ctarg = NULL, env = xlenv; consp(env); env = cdr(env))
    for (frame = car(env); consp(frame); frame = cdr(frame))
      if (tagentry_p(car(frame)) && consp(tagentry_value(car(frame))))
	for (tags = car(tagentry_value(car(frame)));
	     consp(tags);
	     tags = cdr(tags))
	  if (label == car(tags)) {
	    ctarg = tagentry_context(car(frame));
	    goto find_and_jump;
	  }

  /* find a tagbody context */
 find_and_jump:
  for (cptr = xlcontext; cptr != NULL; cptr = cptr->c_xlcontext)
    if (cptr->c_flags & CF_GO && cptr == ctarg) {
      argc = cptr->c_xlargc;
      argv = cptr->c_xlargv;
      while (--argc >= 0)
	if (*argv++ == label) {
	  xljump(cptr,(cptr->c_xlargc - argc),NIL);
	}
    }
  xlerror("no target for GO", label);
#else
  CONTEXT *cptr;
  FRAMEP argv;
  int argc;

  /* find a tagbody context */
  for (cptr = xlcontext; cptr != NULL; cptr = cptr->c_xlcontext)
    if (cptr->c_flags & CF_GO) {
      argc = cptr->c_xlargc;
      argv = cptr->c_xlargv;
      while (--argc >= 0)
	if (*argv++ == label) {
	  xljump(cptr,(cptr->c_xlargc - argc),NIL);
	}
    }
  xlfail("no target for GO");
#endif
}

/* xlreturn - return from a block */
VOID xlreturn P2C(LVAL, name, LVAL, val)
{
#ifdef LEXBIND
  CONTEXT *cptr, *ctarg;
  LVAL env, frame;

  /* find the lexical context */
  for (ctarg = NULL, env = xlenv; consp(env); env = cdr(env))
    for (frame = car(env); consp(frame); frame = cdr(frame))
      if (consp(car(frame)) &&    /* Added 6/16/95, from Niels Mayer */
	  tagentry_p(car(frame)) && tagentry_value(car(frame)) == name) {
	ctarg = tagentry_context(car(frame));
	goto find_and_jump;
      }

  /* find the context and jump */
 find_and_jump:
  for (cptr = xlcontext; cptr != NULL; cptr = cptr->c_xlcontext)
    if (cptr->c_flags & CF_RETURN && cptr->c_expr == name && cptr == ctarg)
      xljump(cptr,CF_RETURN,val);
  xlerror("no target for RETURN", name);
#else
  CONTEXT *cptr;

  /* find a block context */
  for (cptr = xlcontext; cptr != NULL; cptr = cptr->c_xlcontext)
    if (cptr->c_flags & CF_RETURN && cptr->c_expr == name)
      xljump(cptr,CF_RETURN,val);
  xlfail("no target for RETURN");
#endif
}

/* xlthrow - throw to a catch */
VOID xlthrow P2C(LVAL, tag, LVAL, val)
{
    CONTEXT *cptr;

    /* find a catch context */
    for (cptr = xlcontext; cptr != NULL; cptr = cptr->c_xlcontext)
	if ((cptr->c_flags & CF_THROW) && cptr->c_expr == tag)
	    xljump(cptr,CF_THROW,val);
    xlfail("no target for THROW");
}

/* xlsignal - signal an error */
VOID xlsignal P2C(char *, emsg, LVAL, arg)
{
    CONTEXT *cptr;

    /* find an error catcher */
    for (cptr = xlcontext; cptr != NULL; cptr = cptr->c_xlcontext)
	if (cptr->c_flags & CF_ERROR) {
	    if (!null(cptr->c_expr) && emsg != NULL)
		xlerrprint("error",NULL,emsg,arg);
	    xljump(cptr,CF_ERROR,NIL);
	}
}

/* xltoplevel - go back to the top level */
VOID xltoplevel P1C(int, print)
{
  if (print)
    dbgputstr("[ back to top level ]\n");   /* TAA MOD -- was std */
  findandjump(CF_TOPLEVEL,"no top level");
}

/* xlbrklevel - go back to the previous break level */
VOID xlbrklevel(V)
{
    if (batchmode) xlfatal("uncaught error");
    findandjump(CF_BRKLEVEL,"no previous break level");
}

/* xlcleanup - clean-up after an error */
VOID xlcleanup(V)
{
    dbgputstr("[ back to previous break level ]\n");   /* TAA MOD -- was std */
    findandjump(CF_CLEANUP,"not in a break loop");
}

/* xlcontinue - continue from an error */
VOID xlcontinue(V)
{
    findandjump(CF_CONTINUE,"not in a break loop");
}

/* xljump - jump to a saved execution context */
VOID xljump P3C(CONTEXT *, target, int, mask, LVAL, val)
{
    /* unwind the execution stack */
    for (; xlcontext != target; xlcontext = xlcontext->c_xlcontext)

	/* check for an UNWIND-PROTECT */
	if ((xlcontext->c_flags & CF_UNWIND)) {
	    xltarget = target;
	    xlmask = mask;
	    break;
	}
	   
    /* restore the state */
    xlstack = xlcontext->c_xlstack;
    xlenv = xlcontext->c_xlenv;
    xlfenv = xlcontext->c_xlfenv;
    xlunbind(xlcontext->c_xldenv);
    xlargv = xlcontext->c_xlargv;
    xlargc = xlcontext->c_xlargc;
    xlfp = xlcontext->c_xlfp;
    xlsp = xlcontext->c_xlsp;
#ifdef BYTECODE
    xlcstop = xlcontext->c_xlcstop;
#endif /* BYTECODE */
    xlvalue = val;

    /* call the handler */
    longjmp(xlcontext->c_jmpbuf,mask);
}

/* findandjump - find a target context frame and jump to it */
LOCAL VOID findandjump P2C(int, mask, char *, error)
{
    CONTEXT *cptr;

    /* find a block context */
    for (cptr = xlcontext; cptr != NULL; cptr = cptr->c_xlcontext)
	if (cptr->c_flags & mask)
	    xljump(cptr,mask,NIL);
    xlabort(error);
}