Module: SchemeNumbers

Includes:
SchemeNumbersHelper
Included in:
Tokenizer
Defined in:
lib/lisp/interpreter/numbers.rb

Overview

Scheme numbers module

Instance Method Summary collapse

Methods included from SchemeNumbersHelper

#compare_value_arithmetic, #convert_to_num, #divide_number, #divide_special_convert, #find_idx_numerators, #get_num_denom, #get_one_arg_function, #num_denom_helper

Instance Method Details

#*(other) ⇒ Object



93
94
95
96
# File 'lib/lisp/interpreter/numbers.rb', line 93

def *(other)
  other = convert_to_num other
  other.reduce(1, :*)
end

#+(other) ⇒ Object



81
82
83
84
# File 'lib/lisp/interpreter/numbers.rb', line 81

def +(other)
  other = convert_to_num other
  other.reduce(0, :+)
end

#-(other) ⇒ Object



86
87
88
89
90
91
# File 'lib/lisp/interpreter/numbers.rb', line 86

def -(other)
  return 0 if other.empty?
  other = convert_to_num other
  return -other[0] if other.size == 1
  other[0] + other[1..-1].reduce(0, :-)
end

#/(other) ⇒ Object



98
99
100
101
102
103
# File 'lib/lisp/interpreter/numbers.rb', line 98

def /(other)
  raise 'Incorrect number of arguments' if other.empty?
  other = divide_special_convert other
  return (divide_number 1, other[0].to_num) if other.size == 1
  other[1..-1].inject(other[0]) { |res, t| divide_number res, t }
end

#<(other) ⇒ Object



65
66
67
# File 'lib/lisp/interpreter/numbers.rb', line 65

def <(other)
  compare_value_arithmetic other, '<'
end

#<=(other) ⇒ Object



73
74
75
# File 'lib/lisp/interpreter/numbers.rb', line 73

def <=(other)
  compare_value_arithmetic other, '<='
end

#>(other) ⇒ Object



69
70
71
# File 'lib/lisp/interpreter/numbers.rb', line 69

def >(other)
  compare_value_arithmetic other, '>'
end

#>=(other) ⇒ Object



77
78
79
# File 'lib/lisp/interpreter/numbers.rb', line 77

def >=(other)
  compare_value_arithmetic other, '>='
end

#abs(other) ⇒ Object



138
139
140
# File 'lib/lisp/interpreter/numbers.rb', line 138

def abs(other)
  (get_one_arg_function other).abs
end

#add1(other) ⇒ Object



142
143
144
# File 'lib/lisp/interpreter/numbers.rb', line 142

def add1(other)
  (get_one_arg_function other) + 1
end

#denominator(other) ⇒ Object



131
132
133
134
135
136
# File 'lib/lisp/interpreter/numbers.rb', line 131

def denominator(other)
  other = num_denom_helper other
  result = (get_num_denom other)[1]
  raise 'Invalid data type' unless check_for_number result
  result.to_num
end

#max(other) ⇒ Object



156
157
158
159
160
# File 'lib/lisp/interpreter/numbers.rb', line 156

def max(other)
  raise 'Incorrect number of arguments' if other.empty?
  other = convert_to_num other
  other.max
end

#min(other) ⇒ Object



150
151
152
153
154
# File 'lib/lisp/interpreter/numbers.rb', line 150

def min(other)
  raise 'Incorrect number of arguments' if other.empty?
  other = convert_to_num other
  other.min
end

#modulo(other) ⇒ Object



118
119
120
121
122
# File 'lib/lisp/interpreter/numbers.rb', line 118

def modulo(other)
  raise 'Incorrect number of arguments' if other.size != 2
  x, y = convert_to_num other
  x.modulo y
end

#numerator(other) ⇒ Object



124
125
126
127
128
129
# File 'lib/lisp/interpreter/numbers.rb', line 124

def numerator(other)
  other = num_denom_helper other
  result = (get_num_denom other)[0]
  raise 'Invalid data type' unless check_for_number result
  result.to_num
end

#quotient(other) ⇒ Object



105
106
107
108
109
110
# File 'lib/lisp/interpreter/numbers.rb', line 105

def quotient(other)
  raise 'Incorrect number of arguments' if other.size != 2
  x, y = convert_to_num other
  result = divide_number x, y
  result < 0 ? result.ceil : result.floor
end

#remainder(other) ⇒ Object



112
113
114
115
116
# File 'lib/lisp/interpreter/numbers.rb', line 112

def remainder(other)
  raise 'Incorrect number of arguments' if other.size != 2
  x, y = convert_to_num other
  (x.abs % y.abs) * (x / x.abs)
end

#sub1(other) ⇒ Object



146
147
148
# File 'lib/lisp/interpreter/numbers.rb', line 146

def sub1(other)
  (get_one_arg_function other) - 1
end