/*
|
/*
|
* except.c -- exception handling
|
* except.c -- exception handling
|
*/
|
*/
|
|
|
|
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <string.h>
|
#include <setjmp.h>
|
#include <setjmp.h>
|
|
|
#include "error.h"
|
#include "error.h"
|
#include "except.h"
|
#include "except.h"
|
|
|
|
|
#define MAX_ENV_NEST_DEPTH 10
|
#define MAX_ENV_NEST_DEPTH 10
|
|
|
|
|
static jmp_buf *environments[MAX_ENV_NEST_DEPTH];
|
static jmp_buf *environments[MAX_ENV_NEST_DEPTH];
|
static int currentEnvironment = -1;
|
static int currentEnvironment = -1;
|
|
|
|
|
void throwException(int exception) {
|
void throwException(int exception) {
|
if (currentEnvironment < 0) {
|
if (currentEnvironment < 0) {
|
error("exception %d thrown while no environment active", exception);
|
error("exception %d thrown while no environment active", exception);
|
}
|
}
|
longjmp(*environments[currentEnvironment], exception);
|
longjmp(*environments[currentEnvironment], exception);
|
}
|
}
|
|
|
|
|
void pushEnvironment(jmp_buf *environment) {
|
void pushEnvironment(jmp_buf *environment) {
|
if (currentEnvironment == MAX_ENV_NEST_DEPTH - 1) {
|
if (currentEnvironment == MAX_ENV_NEST_DEPTH - 1) {
|
error("too many environments active");
|
error("too many environments active");
|
}
|
}
|
currentEnvironment++;
|
currentEnvironment++;
|
environments[currentEnvironment] = environment;
|
environments[currentEnvironment] = environment;
|
}
|
}
|
|
|
|
|
void popEnvironment(void) {
|
void popEnvironment(void) {
|
if (currentEnvironment < 0) {
|
if (currentEnvironment < 0) {
|
error("cannot pop environment - none active");
|
error("cannot pop environment - none active");
|
}
|
}
|
currentEnvironment--;
|
currentEnvironment--;
|
}
|
}
|
|
|