Started to try to get back into this; work on exceptions and loops.

This commit is contained in:
Simon Brooke 2021-07-24 08:54:55 +01:00
parent 16f78f4077
commit d2101dbd47
No known key found for this signature in database
GPG key ID: A7A4F18D1D4DF987
9 changed files with 101 additions and 10 deletions

62
src/ops/exceptions.c Normal file
View file

@ -0,0 +1,62 @@
/*
* exceptions.c
*
* This is really, really unfinished and doesn't yet work. One of the really key
* things about exceptions is that the stack frames between the throw and the
* catch should not be derefed, so eval/apply will need to be substantially
* re-written.
*
* (c) 2021 Simon Brooke <simon@journeyman.cc>
* Licensed under GPL version 2.0, or, at your option, any later version.
*/
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "consspaceobject.h"
#include "conspage.h"
#include "debug.h"
#include "dump.h"
#include "equal.h"
#include "integer.h"
#include "intern.h"
#include "io.h"
#include "lispops.h"
#include "map.h"
#include "print.h"
#include "read.h"
#include "stack.h"
#include "vectorspace.h"
/**
* OK, the idea here (and I know this is less than perfect) is that the basic `try`
* function in PSSE takes two arguments, the first, `body`, being a list of forms,
* and the second, `catch`, being a catch handler (which is also a list of forms).
* Forms from `body` are evaluated in turn until one returns an exception object,
* or until the list is exhausted. If the list was exhausted, then the value of
* evaluating the last form in `body` is returned. If an exception was encountered,
* then each of the forms in `catch` is evaluated and the value of the last of
* those is returned.
*
* This is experimental. It almost certainly WILL change.
*/
struct cons_pointer lisp_try(struct stack_frame *frame,
struct cons_pointer frame_pointer,
struct cons_pointer env)
{
struct cons_pointer result = c_progn(frame, frame_pointer, frame->arg[0], env);
if (loopexitp(result))
{
// TODO: need to put the exception into the environment!
result = c_progn(frame, frame_pointer, frame->arg[1], env);
}
return result;
}

View file

@ -107,7 +107,7 @@ struct cons_pointer eval_forms( struct stack_frame *frame,
list = c_cdr( list );
}
return result;
return c_reverse( result);
}
/**
@ -991,7 +991,7 @@ c_progn( struct stack_frame *frame, struct cons_pointer frame_pointer,
result = eval_form( frame, frame_pointer, c_car( expressions ), env );
dec_ref( r );
expressions = c_cdr( expressions );
expressions = exceptionp(result) ? NIL : c_cdr( expressions );
}
return result;
@ -1259,7 +1259,7 @@ struct cons_pointer lisp_source( struct stack_frame *frame,
case SPECIALTV:
result = c_assoc( source_key, cell.payload.special.meta );
break;
case LAMBDATV:
case LAMBDATV:
result = make_cons( c_string_to_lisp_symbol( L"lambda" ),
make_cons( cell.payload.lambda.args,
cell.payload.lambda.body ) );

View file

@ -29,6 +29,10 @@
struct cons_pointer c_reverse( struct cons_pointer arg );
struct cons_pointer
c_progn( struct stack_frame *frame, struct cons_pointer frame_pointer,
struct cons_pointer expressions, struct cons_pointer env );
/**
* Useful building block; evaluate this single form in the context of this
* parent stack frame and this environment.