Number

This module represents the semantics of numbers in the expression language (see expr.bnf).

  export Number, new_number

Exports class Number and constructor new_number for use by other modules.

  Number : set of Entity := {}

This sets up Number as a set of objects. Initially, the set is empty. It is populated with objects as new Number objects are created. In other words, Number always represents the current instances of class Number. Class Number is not explicitly declared, but is treated as one and the same as the set of instances.

  slim_value : map from Number to number

Slim_value is an associative array that can be indexed with values of type Number. Thus slim_value(x) can be set to a numeric value which represents the numeric value of number object x. In other words, this declaration sets up slim_value as a numeric valued attribute of class Number. Note that case is significant and that number is the built-in numeric type in Slim, whereas Number is a programmer defined abstract type (class).

  func new_number(v : String) -> Number
    result := new(Number)
    slim_value(result) := mknum(v)

This constructs an instance of class Number and initializes the only attribute. Following a call to new(Number) the new instance is automatically added to Number, the set of instances of class Number.

  func display(self : Number) -> Number
    put slim_value(self)
    return self

This method displays the numeric value of the Number object on the console.

func divide(self : Number, other : Number) -> Number
  result := new(Number)
  slim_value(result) := slim_value(self) / slim_value(other)

func minus(self : Number, other : Number) -> Number
  result := new(Number)
  slim_value(result) := slim_value(self) - slim_value(other)

func multiply(self : Number, other : Number) -> Number
  result := new(Number)
  slim_value(result) := slim_value(self) * slim_value(other)

func plus(self : Number, other : Number) -> Number
  result := new(Number)
  slim_value(result) := slim_value(self) + slim_value(other)

These methods construct and return new Number objects with numeric values equal to the results of the respective operations carried out on the numeric values of the operand objects.