lib/lisp/interpreter/list.rb in lisp-interpreter-0.3.1 vs lib/lisp/interpreter/list.rb in lisp-interpreter-0.3.2
- old
+ new
@@ -62,48 +62,55 @@
'\'(' + result + ')'
end
def get_cons_values(tokens)
result = get_k_arguments tokens, false, 2
- raise 'Too little arguments' if result.size != 2
+ raise arg_err_build 2, result.size if result.size != 2
result
end
def split_list_string(list)
result = list.split(/(\(|\)|\.)|\ /)
result.delete('')
result
end
def find_list_function_value(other)
- raise 'Incorrect number of arguments' if other.size != 1
+ raise arg_err_build 1, other.size if other.size != 1
raise 'Invalid data type' unless other[0].list?
split_list_as_string other[0].to_s
end
def split_list_as_string(list_as_string)
split_value = split_list_string list_as_string.to_s
no_eval_list split_value[2..-2]
end
def car_cdr_values(other)
- raise 'Incorrect number of arguments' if other.size != 1
+ raise arg_err_build 1, other.size if other.size != 1
return find_list_function_value other if other[0].list?
(split_list_string other[0].to_s)[2..-2] if other[0].pair?
end
def map_helper(lst, func)
return lst.map { |t| func.call(*t) } if func.is_a? Proc
lst.map { |t| send func, t }
end
+
+ def map_validate_helper(other)
+ raise arg_err_build 'at least 2', 0 if other.empty?
+ func, other = valid_function other
+ raise arg_err_build 'at least 2', 1 if other.empty?
+ [func, other]
+ end
end
# Scheme lists module
module SchemeLists
include SchemeListsHelper
def cons(other)
- raise 'Incorrect number of arguments' if other.size != 2
+ raise arg_err_build 2, other.size if other.size != 2
cons_helper other
end
def list(other)
build_list other
@@ -121,21 +128,21 @@
idx = value[1] == '.' ? 2 : 1
build_list value[idx..-1]
end
def list?(other)
- raise 'Incorrect number of arguments' if other.size != 1
+ raise arg_err_build 1, other.size if other.size != 1
other[0].to_s.list? ? '#t' : '#f'
end
def pair?(other)
- raise 'Incorrect number of arguments' if other.size != 1
+ raise arg_err_build 1, other.size if other.size != 1
other[0].to_s.pair? ? '#t' : '#f'
end
def null?(other)
- raise 'Incorrect number of arguments' if other.size != 1
+ raise arg_err_build 1, other.size if other.size != 1
return '#f' unless other[0].to_s.list?
other[0].to_s.size == 3 ? '#t' : '#f'
end
def length(other)
@@ -146,12 +153,10 @@
value = find_list_function_value other
build_list value.reverse
end
def map(other)
- raise 'Incorrect number of arguments' if other.empty?
- func, other = valid_function other
- raise 'Incorrect number of arguments' if other.empty?
+ func, other = map_validate_helper other
lst = find_all_values other
lst = lst.map { |t| find_list_function_value [t] }
lst = (equalize_lists lst).transpose
build_list map_helper lst, func
end