Module: RubyLisp::Util
- Defined in:
- lib/rubylisp/util.rb,
lib/rubylisp/types.rb
Class Method Summary collapse
- .assert_arg_type(sexp, arg_number, arg_type) ⇒ Object
- .assert_at_least_n_args(sexp, num_args) ⇒ Object
- .assert_number_of_args(sexp, num_args) ⇒ Object
- .hash_map(values) ⇒ Object
- .list(values) ⇒ Object
- .list?(x) ⇒ Boolean
- .map?(x) ⇒ Boolean
- .sequential?(x) ⇒ Boolean
- .vec(values) ⇒ Object
- .vector(*values) ⇒ Object
- .vector?(x) ⇒ Boolean
Class Method Details
.assert_arg_type(sexp, arg_number, arg_type) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rubylisp/util.rb', line 25 def assert_arg_type sexp, arg_number, arg_type fn = sexp[0] fn_name = fn.value arg = if arg_number == 'last' sexp.last else sexp[arg_number] end arg_description = if arg_number == 'last' 'The last argument' else "Argument ##{arg_number}" end arg_types = if arg_type.class == Array arg_type else [arg_type] end expected = case arg_types.count when 1 arg_types.first when 2 arg_types.join(' or ') else last_arg_type = arg_types.pop arg_types.join(', ') + " or #{last_arg_type}" end if arg_types.none? {|type| arg.is_a? type} raise RuntimeError, "#{arg_description} to `#{fn_name}` must be a " + "#{expected}; got a #{arg.class}." end end |
.assert_at_least_n_args(sexp, num_args) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/rubylisp/util.rb', line 15 def assert_at_least_n_args sexp, num_args fn, *args = sexp fn_name = fn.value unless args.count >= num_args raise RuntimeError, "Wrong number of arguments passed to `#{fn_name}`; " + "got #{args.count}, expected at least #{num_args}." end end |
.assert_number_of_args(sexp, num_args) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/rubylisp/util.rb', line 5 def assert_number_of_args sexp, num_args fn, *args = sexp fn_name = fn.value unless args.count == num_args raise RuntimeError, "Wrong number of arguments passed to `#{fn_name}`; " + "got #{args.count}, expected #{num_args}." end end |
.hash_map(values) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/rubylisp/types.rb', line 123 def hash_map(values) if values.size.odd? raise ParseError, "A hash-map must contain an even number of forms." else Hamster::Hash[values.each_slice(2).to_a] end end |
.list(values) ⇒ Object
135 136 137 |
# File 'lib/rubylisp/types.rb', line 135 def list(values) values.to_list end |
.list?(x) ⇒ Boolean
139 140 141 |
# File 'lib/rubylisp/types.rb', line 139 def list?(x) x.is_a? Hamster::List end |
.map?(x) ⇒ Boolean
131 132 133 |
# File 'lib/rubylisp/types.rb', line 131 def map?(x) x.is_a? Hamster::Hash end |
.sequential?(x) ⇒ Boolean
155 156 157 |
# File 'lib/rubylisp/types.rb', line 155 def sequential?(x) list?(x) || vector?(x) end |
.vec(values) ⇒ Object
143 144 145 |
# File 'lib/rubylisp/types.rb', line 143 def vec(values) Hamster::Vector.new(values) end |