Module: SchemeStringsHelper

Included in:
SchemeStrings
Defined in:
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



35
36
37
38
39
40
# File 'lib/lisp/interpreter/core/strings.rb', line 35

def arg_function_validator(other, vars = 1)
  raise arg_err_build other.size, vars if other.size != vars
  res = other[0..vars - 1].reject(&:string?)
  raise type_err '<string>', res[0].type unless res.empty?
  res
end

#build_as_string_helper(other, idx) ⇒ Object



14
15
16
17
# File 'lib/lisp/interpreter/core/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_next_value_as_string(other) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/lisp/interpreter/core/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/core/strings.rb', line 9

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

#remove_carriage(str) ⇒ Object



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

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

#string_join_helper(other, dilimeter) ⇒ Object



42
43
44
45
46
# File 'lib/lisp/interpreter/core/strings.rb', line 42

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



48
49
50
51
# File 'lib/lisp/interpreter/core/strings.rb', line 48

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

#substring_builder(str, from, to) ⇒ Object



3
4
5
6
7
# File 'lib/lisp/interpreter/core/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



53
54
55
56
57
# File 'lib/lisp/interpreter/core/strings.rb', line 53

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