#!/usr/bin/env rexx
/* ---------------------------------------------------------------- */
/* Number class, a subclass of the String class                     */
/* Values can be any valid number.                                  */
/*                                                                  */
/* All arithmetic methods need to be defined here, because          */
/* the result of the operation should be another object of          */
/* the Number class, and the result of the String class operators   */
/* always is a String class object.                                 */
/*                                                                  */
/* Note that subclasses of the Number class will yield results      */
/* that belong to that particular receiver subclass.                */
/*                                                                  */
/* NOTE: Requieres oorexx 4.0 or later                              */
/* ---------------------------------------------------------------- */
/*                                                                  */
/* Originally by Ruurd J. Idenburg                                  */
/*                                                                  */
/* No copyright, no licence, no guarantees or warrantees, be it     */
/* explicit, implicit or whatever. Usage is totally and completely  */
/* at the users own risk, the author shall not be liable for any    */
/* damages whatsoever, for any reason whatsoever.                   */
/*                                                                  */
/* Please keep this comment block intact when modifying this code   */
/* and add a note with date and a description.                      */
/*                                                                  */
/* ---------------------------------------------------------------- */
/* 2020/03/05 - Initial version approximately (RJI)                 */
/* 2022/02/14 - Support prefix + and - using forward to super (RJI) */
/* 2022/02/15 - 'UNKNOWN method to handle all .number methods (RJI) */
/* ---------------------------------------------------------------- */

::class number public subclass string

::attribute methods class private
::attribute calculationResult private

::method activate class
-- all (hopefully) String methods that should result in .number instance
  self~methods = ('+','-','/','*','%','//','**', -
                  abs,ceiling,floor,format,max,min,modulo,round,trunc)
-- remove them from method lookup chain, setting method objects to .nil
  unknownMethods = .StringTable~new
  do method over self~methods
    unknownMethods[method] = .nil
  end
-- make the methods unavailable so we can trap them in our UNKNOWN method
  self~defineMethods(unknownMethods)

::method init
	self~init:super
	if (self)~datatype\='NUM' then do	   -- if self is not a number
		raise syntax 93.904 array(1,self)  -- then raise an error and quit	
	end

::method unknown
  use arg method, args
-- if not a .String instance method return the nil object (.nil)
  if ''~instanceMethod(method)~isNil then return .nil
-- pass the method and arguments to the superclass and wait for the result
  forward class(super) message (method) arguments (args) continue
-- keep the result 
  self~calculationResult = result 
-- if not an arithmetic operator/method return the result .String object
  if \self~class~methods~allItems~hasItem(method) then do
    return self~calculationResult
  end
-- create an new .number instance as the result of the arithmetic operation
  return self~class~new(self~calculationResult)

