Module: SchemeStringsHelper

Included in:
SchemeStrings
Defined in:
lib/lisp/interpreter/strings.rb,
lib/lisp/interpreter/core/strings.rb

Overview

Helper functions for SchemeStrings

Instance Method Summary collapse

Instance Method Details

#arg_function_validator(other, vars = 1) ⇒ Object



39
40
41
42
43
44
# File 'lib/lisp/interpreter/strings.rb', line 39

def arg_function_validator(other, vars = 1)
  raise arg_err_build other.size, vars if other.size != vars
  result = other[0..vars - 1].all? { |v| check_for_string v }
  raise 'Invalid data type' unless result
  result
end

#build_as_string_helper(other, idx) ⇒ Object



14
15
16
17
# File 'lib/lisp/interpreter/strings.rb', line 14

def build_as_string_helper(other, idx)
  value = other[0..idx].join(' ').gsub('( ', '(').gsub(' )', ')')
  [value, other[idx + 1..-1]]
end

#build_character(char) ⇒ Object



30
31
32
# File 'lib/lisp/interpreter/strings.rb', line 30

def build_character(char)
  '#\\' + (char == ' ' ? 'space' : char)
end

#build_next_value_as_string(other) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/lisp/interpreter/strings.rb', line 19

def build_next_value_as_string(other)
  idx = find_idx_for_list other
  if other[0] == '('
    build_as_string_helper other, idx
  elsif other[0..1].join == '\'('
    [(get_raw_value other[0..idx]), other[idx + 1..-1]]
  else
    [other[0], other[1..-1]]
  end
end

#find_delimeter(other) ⇒ Object



9
10
11
12
# File 'lib/lisp/interpreter/strings.rb', line 9

def find_delimeter(other)
  return ' ' if other.nil?
  other[1..-2]
end

#remove_carriage(str) ⇒ Object



34
35
36
37
# File 'lib/lisp/interpreter/strings.rb', line 34

def remove_carriage(str)
  str = str[1..-2]
  str.gsub('\n', '').gsub('\r', '').gsub('\t', '').strip.squeeze(' ')
end

#string_join_helper(other, dilimeter) ⇒ Object



46
47
48
49
50
# File 'lib/lisp/interpreter/strings.rb', line 46

def string_join_helper(other, dilimeter)
  values = split_list_as_string other.to_s
  delim_result = find_delimeter dilimeter
  '"' + (values.join delim_result) + '"'
end

#strjoin_validate(other) ⇒ Object



52
53
54
55
# File 'lib/lisp/interpreter/strings.rb', line 52

def strjoin_validate(other)
  raise arg_err_build '[1, 2]', other.size unless other.size.between? 1, 2
  raise 'Invalid data type' unless other[0].to_s.list?
end

#substring_builder(str, from, to) ⇒ Object



3
4
5
6
7
# File 'lib/lisp/interpreter/strings.rb', line 3

def substring_builder(str, from, to)
  result = (str[1..-2])[from..(to.nil? ? -1 : to - 1)]
  return '""' if result.nil?
  '"' + result + '"'
end

#substring_validator(from, to) ⇒ Object



57
58
59
60
61
# File 'lib/lisp/interpreter/core/strings.rb', line 57

def substring_validator(from, to)
  valid = (check_for_num from) && (to.nil? || (check_for_num to))
  type = [from, to].first { |t| t.type if t.type != 'number' }
  raise data_type_err '<number>', type unless valid
end