seq-compiler 3.0.6

Compiler for the Seq programming language
Documentation
# Integer Math Standard Library for Seq
#
# Common mathematical operations for integer arithmetic.
#
# ## Usage
#
#   include std:imath
#
#   : main ( -- Int )
#     -5 abs int->string io.write-line
#     0
#   ;
#
# ## Available Functions
#
# - abs: ( Int -- Int ) - Absolute value
# - max: ( Int Int -- Int ) - Maximum of two values
# - min: ( Int Int -- Int ) - Minimum of two values
# - mod: ( Int Int -- Int Bool ) - Modulo operation (a mod b), returns success flag
# - gcd: ( Int Int -- Int ) - Greatest common divisor (Euclidean algorithm)
# - pow: ( Int Int -- Int ) - Power (base^exp) - WARNING: Not tail-recursive
# - sign: ( Int -- Int ) - Sign function: returns -1, 0, or 1
# - square: ( Int -- Int ) - Square a number
# - clamp: ( Int min max -- Int ) - Clamp value between min and max
#
# ## Notes
#
# - All operations use wrapping integer arithmetic (i64)
# - Recursive functions (gcd, pow) may stack overflow for very large inputs
# - Division by zero will panic at runtime
#
# ## Examples
#
#   48 18 gcd          # Returns 6
#   2 10 pow           # Returns 1024
#   15 0 100 clamp     # Returns 15 (within range)
#   -5 abs             # Returns 5
#

# Absolute value
# Both branches must have identical stack effects for type checker
: abs ( Int -- Int )
  dup dup 0 i.< if
    # Stack: n n, n < 0, so compute 0 - n
    nip 0 swap i.subtract
  else
    # Stack: n n, n >= 0, keep original
    drop
  then
;

# Maximum of two values
: max ( Int Int -- Int )
  2dup i.> if
    drop
  else
    nip
  then
;

# Minimum of two values
: min ( Int Int -- Int )
  2dup i.< if
    drop
  else
    nip
  then
;

# Modulo operation: a mod b
# Uses i.modulo which returns (Int Bool) for error handling
: mod ( Int Int -- Int Bool )
  i.modulo
;

# Greatest common divisor (Euclidean algorithm)
# Note: mod returns (Int Bool), we assume valid inputs (non-zero divisor)
: gcd ( Int Int -- Int )
  dup 0 i.= if
    drop
  else
    2dup mod           # ( a b r success )
    drop               # drop success flag (assume valid)
    rot drop           # ( b r )
    gcd
  then
;

# Power (base^exp using recursion)
: pow ( Int Int -- Int )
  dup 0 i.= if
    drop drop 1
  else
    dup 1 i.= if
      drop
    else
      over
      swap 1 i.subtract
      pow
      i.multiply
    then
  then
;

# Sign function: returns -1, 0, or 1
: sign ( Int -- Int )
  dup 0 i.= if
    drop 0
  else
    dup 0 i.< if
      drop 0 1 i.subtract
    else
      drop 1
    then
  then
;

# Square
: square ( Int -- Int )
  dup i.multiply
;

# Clamp value between min and max
# Stack: ( value min max -- clamped )
# Returns: min if value < min, max if value > max, else value
: clamp ( Int Int Int -- Int )
  # Stack: value min max
  rot rot
  # Stack: max value min
  2dup i.< if
    # value < min, return min
    nip nip
  else
    # value >= min
    drop
    # Stack: max value
    2dup i.> if
      # value > max, return max
      nip
    else
      # min <= value <= max, return value
      drop
    then
  then
;