From 6177cd686578e83aab1e81f8d0500809d60b9137 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Tue, 21 Nov 2017 15:05:12 +0000 Subject: [PATCH] removed the DEH symbol ({%). added new experimental features - the signal method to the Error class for simplified error/exception handling - the catch method to the BlockContext for simplified error/exception catching instead of the full-fledged on:do --- moo/kernel/Apex.moo | 44 ++++++++++++++++++++++++++++++++++++ moo/kernel/Context.moo | 2 +- moo/kernel/Except.moo | 51 ++++++++++++++++++------------------------ moo/lib/comp.c | 18 --------------- moo/lib/moo-prv.h | 1 - 5 files changed, 67 insertions(+), 49 deletions(-) diff --git a/moo/kernel/Apex.moo b/moo/kernel/Apex.moo index 9b4c40f..0733ad7 100644 --- a/moo/kernel/Apex.moo +++ b/moo/kernel/Apex.moo @@ -354,4 +354,48 @@ extend Error method(#primitive) asInteger. method(#primitive) asCharacter. method(#primitive) asString. + + method signal + { + | exctx exblk retval actpos ctx | + + exctx := (thisContext sender) findExceptionContext. + + while (exctx notNil) + { + exblk := exctx findExceptionHandlerFor: (self class). + if (exblk notNil and: [actpos := exctx basicSize - 1. exctx basicAt: actpos]) + { + exctx basicAt: actpos put: false. + [ retval := exblk value: self ] ensure: [ exctx basicAt: actpos put: true ]. + thisContext unwindTo: (exctx sender) return: nil. + System return: retval to: (exctx sender). + }. + exctx := (exctx sender) findExceptionContext. + }. + + ## ----------------------------------------------------------------- + ## FATAL ERROR - no exception handler. + ## ----------------------------------------------------------------- + ##thisContext unwindTo: nil return: nil. + ##thisContext unwindTo: (Processor activeProcess initialContext) return: nil. + +## TOOD: IMPROVE THIS EXPERIMENTAL BACKTRACE... +System logNl: '== BACKTRACE =='. +ctx := thisContext. +while (ctx notNil) +{ + if (ctx class == MethodContext) { System logNl: (' ' & ctx method owner name & '>>' & ctx method name) }. + ## TODO: include blockcontext??? + ctx := ctx sender. +}. +System logNl: '== END OF BACKTRACE =='. + + thisContext unwindTo: (thisProcess initialContext) return: nil. + ('### ERROR NOT HANDLED #### ' & self class name & ' - ' & self asString) dump. + ## TODO: debug the current process???? " + + ##Processor activeProcess terminate. + thisProcess terminate. + } } diff --git a/moo/kernel/Context.moo b/moo/kernel/Context.moo index b7cbae4..3b092fb 100644 --- a/moo/kernel/Context.moo +++ b/moo/kernel/Context.moo @@ -165,11 +165,11 @@ class(#pointer,#final,#limited) BlockContext(Context) self primitiveFailed. } + method ifTrue: aBlock { ##^(self value) ifTrue: aBlock. ^if (self value) { aBlock value }. - } method ifFalse: aBlock diff --git a/moo/kernel/Except.moo b/moo/kernel/Except.moo index 9651840..2075e0d 100644 --- a/moo/kernel/Except.moo +++ b/moo/kernel/Except.moo @@ -6,7 +6,8 @@ ## class Exception(Apex) { - var signalContext, handlerContext, messageText. + var signalContext, handlerContext. + var(#get) messageText. (* TODO: can i convert 'thisProcess primError' to a relevant exception? @@ -18,7 +19,6 @@ TODO: can i convert 'thisProcess primError' to a relevant exception? } *) - method(#class) signal { ^self new signal @@ -34,23 +34,12 @@ TODO: can i convert 'thisProcess primError' to a relevant exception? self.handlerContext := context. } - method messageText - { - ^self.messageText - } method asString { ^(self class name) & ' - ' & self.messageText. } - (* TODO: remove this.... - method __signal - { - self.signalContext := thisContext. - ((thisContext sender) findExceptionContext) handleException: self. - }*) - method signal { | exctx exblk retval actpos ctx | @@ -62,8 +51,7 @@ TODO: can i convert 'thisProcess primError' to a relevant exception? while (exctx notNil) { exblk := exctx findExceptionHandlerFor: (self class). - if (exblk notNil and: - [actpos := exctx basicSize - 1. exctx basicAt: actpos]) + if (exblk notNil and: [actpos := exctx basicSize - 1. exctx basicAt: actpos]) { self.handlerContext := exctx. exctx basicAt: actpos put: false. @@ -180,7 +168,8 @@ extend Context { ## ------------------------------------------------------------------- ## <> - ## private: called by VM upon unwinding + ## private: called by VM upon unwinding as well as by + ## Exception< + exception_active := true. + ^self value. + } + method ensure: aBlock { | retval pending | @@ -394,6 +393,12 @@ thisContext isExceptionContext dump. [ v := self value. pending := false. ] ensure: [ if (pending) { aBlock value } ]. ^v. } + + method catch + { + ^self on: Exception do: [:ex | ex ] + on: Error do: [:err | err ]. + } } @@ -440,10 +445,6 @@ class NotImplementedException(Exception) { } -class ErrorExceptionizationFailureException(Exception) -{ -} - class InvalidArgumentException(Exception) { } @@ -452,14 +453,6 @@ class ProhibitedMessageException(Exception) { } -(* -pooldic ErrorToException -{ - ErrorCode.EINVAL := InvalidArgumentException. - ErrorCode.ENOIMPL := NotImplementedException. -} -*) - extend Apex { method(#dual,#liberal) primitiveFailed(method) diff --git a/moo/lib/comp.c b/moo/lib/comp.c index 33c6d06..2bc409a 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -1682,26 +1682,8 @@ retry: break; case '{': /* extension */ -#if 0 SET_TOKEN_TYPE (moo, MOO_IOTOK_LBRACE); goto single_char_token; -#else - SET_TOKEN_TYPE (moo, MOO_IOTOK_RETURN); - ADD_TOKEN_CHAR(moo, c); - GET_CHAR_TO (moo, c); - - if (c == '@') - { - /* {@ */ - TOKEN_TYPE(moo) = MOO_IOTOK_DEH_BLOCK; /* default exception handling block */ - ADD_TOKEN_CHAR (moo, c); - } - else - { - unget_char (moo, &moo->c->lxc); - } - break; -#endif case '}': /* extension */ SET_TOKEN_TYPE (moo, MOO_IOTOK_RBRACE); goto single_char_token; diff --git a/moo/lib/moo-prv.h b/moo/lib/moo-prv.h index c825fcc..776aec0 100644 --- a/moo/lib/moo-prv.h +++ b/moo/lib/moo-prv.h @@ -361,7 +361,6 @@ struct moo_iotok_t MOO_IOTOK_HASHBRACK, /* #[ - byte array literal */ MOO_IOTOK_PERCPAREN, /* %( - array expression */ MOO_IOTOK_PERCBRACE, /* %{ - dictionary expression */ - MOO_IOTOK_DEHBRACE, /* {% */ MOO_IOTOK_PERIOD, MOO_IOTOK_COMMA, MOO_IOTOK_SEMICOLON