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,

  redef r effect_mode.val,

  _ unit
  ) : effect r
is

  # exit with the given code
  #
  exit(code i32) => 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 i32)
  pre
    0 ≤ code ≤ 127
  => p.set_exit_code code


  # install default instance of exit
  #
  type.install_default is
    if !effect.is_installed exit
      _ := exit default_exit_handler effect_mode.default unit


  # default exit handler
  #
  type.default_exit_handler : Exit_Handler is


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


    # exit with the given code
    #
    exit(code i32) =>
      fuzion.std.exit code


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


    # set the stored exit code
    #
    set_exit_code(code i32) outcome unit
      pre
        0 ≤ code ≤ 127
      post
        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 i32) => 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 i32)
  pre
    0 ≤ code ≤ 127
  => exit0.set_exit_code code


# install given exit handler and run code with it.
#
public exit(handler Exit_Handler, code ()->unit) =>
  exit handler (effect_mode.inst code) unit


# Exit_Handler -- abstract exit
#
public Exit_Handler ref is

  # exit with the given code
  #
  public exit(code i32) 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 i32) outcome unit
    pre
      0 ≤ code ≤ 127
    => abstract