include term-colours.4th
include defer-is.4th
+include catch-throw.4th
include float.4th
include debugging.4th
defer collect-garbage
-\ ------ Types ------
+\ ---- Types ---- {{{
variable nexttype
0 nexttype !
: istype? ( obj type -- obj bool )
over = ;
+\ }}}
+
+\ ---- Exceptions ---- {{{
+
+variable nextexception
+1 nextexception !
+: make-exception
+ create nextexception @ ,
+ 1 nextexception +!
+ does> @ ;
+
+make-exception recoverable-exception
+make-exception unrecoverable-exception
+
+: display-warning ( addr count -- )
+ bold fg red
+ ." Exception: "
+ type
+ reset-term ;
+
+: throw" immediate
+ [compile] s"
+
+ ['] rot , ['] dup ,
+
+ [compile] if
+ ['] -rot ,
+ ['] display-warning ,
+ [compile] then
+
+ ['] throw ,
+;
+
+\ }}}
+
\ ---- List-structured memory ---- {{{
10000 constant scheme-memsize
then
nextfree @ scheme-memsize >= if
- fg red bold
- ." Out of memory! Aborting."
- reset-term abort
+ unrecoverable-exception throw s" Out of memory!"
then
;
get-vars-vals if
2swap 2drop car
else
- bold fg red ." Tried to read unbound variable." reset-term cr abort
+ recoverable-exception throw" Tried to read unbound variable."
then
;
2swap 2drop ( val vals )
set-car!
else
- bold fg red ." Tried to set unbound variable." reset-term cr abort
+ recoverable-exception throw" Tried to set unbound variable."
then
;
global-env obj@ define-var
;
-: arg-count-error
- bold fg red ." Incorrect argument count." reset-term cr
- abort
-;
-
: ensure-arg-count ( args n -- )
dup 0= if
drop nil objeq? false = if
- arg-count-error
+ recoverable-exception throw" Too many arguments for primitive procedure."
then
else
-rot nil? if
- arg-count-error
+ recoverable-exception throw" Too few arguments for primitive procedure."
then
cdr rot 1- recurse
: ensure-arg-type ( arg type -- arg )
istype? false = if
- arg-type-error
+ recoverable-exception throw" Incorrect argument type for primitive procedure."
then
;
: flatten-proc-args ( argvals argnames -- argvals' argnames' )
nil? if
2over nil? false = if
- bold fg red ." Too many arguments supplied to compound method. Aborting." reset-term cr
- abort
+ recoverable-exception throw" Too many arguments for compound procedure."
else
2drop
then
2over
nil? if
- bold fg red ." Too few arguments supplied to compound method. Aborting." reset-term cr
- abort
+ recoverable-exception throw" Too few arguments for compound procedure."
else
cdr
then
R> drop ['] eval goto-deferred \ Tail call optimization
endof
- bold fg red ." Object not applicable. Aborting." reset-term cr
- abort
+ recoverable-exception throw" Object not applicable."
endcase
;
then
then
- bold fg red ." Error evaluating expression - unrecognized type. Aborting." reset-term cr
- abort
+ recoverable-exception throw" Tried to evaluate object with unknown type."
; is eval
\ }}}
compound-proc-type istype? if printcomp exit then
none-type istype? if printnone exit then
- bold fg red ." Error printing expression - unrecognized type. Aborting" reset-term cr
- abort
+ recoverable-exception throw" Tried to print object with unknown type."
; is print
\ }}}
\ ---- REPL ----
+( REPL calls REPL-BODY in a loop until repl-body returns true. )
+: repl-body ( -- bool )
+ cr bold fg green ." > " reset-term
+
+ read-console
+
+ 2dup EOF character-type objeq? if
+ 2drop
+ bold fg blue ." Moriturus te saluto." reset-term cr
+ true exit
+ then
+
+ global-env obj@ eval
+
+ fg cyan ." ; " print reset-term
+
+ false
+;
+
: repl
cr ." Welcome to scheme.forth.jl!" cr
." Use Ctrl-D to exit." cr
enable-gc
begin
- cr bold fg green ." > " reset-term
- read-console
+ ['] repl-body catch
+ case
+ recoverable-exception of false endof
+ unrecoverable-exception of true endof
- 2dup EOF character-type objeq? if
- 2drop
- bold fg blue ." Moriturus te saluto." reset-term cr
- exit
- then
-
- global-env obj@ eval
+ \ Rethrow anything else:
+ throw
- fg cyan ." ; " print reset-term
- again
+ \ If we're still here, loop again
+ false
+ endcase
+ until
;
forth definitions