Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

exit.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 exit
#
#  Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------

# exit -- effect that terminates a computation in exit
#
private:public exit (

  # the handler this effect uses to exit
  p Exit_Handler,

  _, _ unit
  ) : effect
is

  # exit with the given code
  #
  exit(code u8) => p.exit code


  # exit with the stored code
  #
  exit => p.exit


  # set the stored exit code, which, by default, is zero
  #
  set_exit_code(code u8)
  pre debug: code ≤ 127
  => p.set_exit_code code


  # install default instance of exit
  #
  type.install_default unit =>
    if !exit.is_instated
      (exit default_exit_handler unit unit).default


  # default exit handler
  #
  type.default_exit_handler =>

    ref : Exit_Handler

      # mutable value of the current exit code set
      #
      exit_code := u8 0


      # exit with the given code
      #
      redef exit(code u8) =>
        fuzion.std.exit code.as_i32


      # exit with the stored code
      #
      redef exit =>
        fuzion.std.exit exit_code.as_i32


      # set the stored exit code
      #
      redef set_exit_code(code u8) outcome unit
        post then
          debug: exit_code = code
        =>
          if exit_code = 0
            set exit_code := code
            unit
          else
            error "exit code is already set"


# exit with no argument returns exit.env, the currently installed
# exit handler.
#
exit0 =>
  exit.install_default
  exit.env


# exit with a code argument calls exit.exit code, i.e., it uses the
# current exit effect to exit with the given message.
#
public exit(code u8) => exit0.exit code


# exit with the currently stored exit code
#
public exit => exit0.exit


# change the currently stored exit code
#
public set_exit_code(code u8)
pre debug: code ≤ 127
  => exit0.set_exit_code code


# install given exit handler and run code with it.
#
# In case the handler aborts this effect, this feature will
# return normally after a call to `exit.env.exit n`.
#
public exit(handler Exit_Handler, code ()->unit) =>
  exit.instate unit (exit handler unit unit) code _->unit


# Exit_Handler -- abstract exit
#
public Exit_Handler ref is

  # exit with the given code
  #
  public exit(code u8) void => abstract


  # exit with the stored code
  #
  public exit void => abstract


  # set the stored exit code, which, by default, is zero
  #
  public set_exit_code(code u8) outcome unit
  pre debug: code ≤ 127
    => abstract
last changed: 2025-01-23