panic.fz
# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation. If not, see <https://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------
#
# Tokiwa Software GmbH, Germany
#
# Source code of Fuzion standard library feature panic
#
# Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------
# panic -- effect that terminates a computation in panic
#
public panic (
# the handler this effect uses to panic
p Panic_Handler,
_ unit
) : simple_effect
is
# panic with the given message
#
panic(msg String) =>
p.do_panic msg
abort
type.install_default =>
(panic default_panic_handler unit).default
# default panic handler using io.err and exit with return code 1
#
type.default_panic_handler : Panic_Handler is
# panic with the given message and exit with return code 1
#
do_panic(msg String) =>
io.err.println "*** panic: $msg"
exit 1
# panic with no argument returns panic.env, the currently installed
# panic handler.
#
public panic =>
panic.install_default
panic.env
# panic with a msg argument calls panic.panic msg, i.e., it uses the
# current panic effect to panic with the given message.
#
public panic(msg String) => panic.panic msg
# Panic_Handler -- abstract panic
#
public Panic_Handler ref is
# panic with the given message
#
# NYI: The result of do_panic should be returned by `use`, but there is
# currently no way we could do this. One way would be to add a local
# variable of type `Any` to `panic` and store the result there and then
# cast it to the correct type in `use` (see
# https://fuzion-lang.dev/design/type_casts_ref)
#
public do_panic(msg String) unit => abstract
# Install this panic handler using a new instance of panic and run
# `code` on it. In case of an abort, return `def()` (NYI: def should be
# removed and the result of do_panic should be returned instead).
#
public use(R type, code, def ()->R) =>
p := panic Panic_Handler.this unit
p.run code def
last changed: 2024-03-07