lisp-interpreter 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95797273f33bc2b8be5709474ab1495d38bfb342
4
- data.tar.gz: b16d5d97ee2d7d7d58b92685adb0fb7014bd3e4f
3
+ metadata.gz: 5cba48431d06d1753de8488615d06d0e96497e58
4
+ data.tar.gz: c746db86111666c4d8c176da69aa585e866d70a0
5
5
  SHA512:
6
- metadata.gz: 9f5bf08dda0d96dd254e1905b67e70c73d042dfaa866b69f9f7fce80ed5ef6bd05610b0cd249d6b20aa97a1b07e8d2f01226afcbe2661338ad38479ab4cc028a
7
- data.tar.gz: 811f88a1d5f579c6bc609a536be1d94eb514a115ddb94d7a86ae2190bcc769352b5d9e4d8903ecd44eae8e471e2f59fdaa32c9d40c59a83d5f08c643528f0de9
6
+ metadata.gz: 8cf8ea0df1f974639c2ff96afe4ca2fb76ff346638b34b47420369108dfec531a0893b712da39d655ea54078e15fbca1f06380ee3f981a47d8935747a847ca73
7
+ data.tar.gz: f249f299e0658dc49e7a53931747e3505c50c19bd72ab89a975e7018958fbb41abc012fa8a8b695b5a5d538031d1a8b1f9c68cbd7e8d7ac088b19feb9cd26535
@@ -37,7 +37,7 @@ module SchemeBooleans
37
37
  def if(other)
38
38
  raise arg_err_build 3, 0 if other.empty?
39
39
  expr, other = find_next_value other
40
- raise arg_err_build 3, other.size + 1 if other.size != 2
40
+ raise arg_err_build 3, other.size + 1 if other.size < 2
41
41
  if_helper expr, other
42
42
  end
43
43
  end
@@ -24,6 +24,3 @@ module ErrorMessages
24
24
  'Invalid data type, expected ' + exp.to_s + ' got ' + got.to_s
25
25
  end
26
26
  end
27
-
28
- class SchemeException < RuntimeError
29
- end
@@ -149,8 +149,7 @@ module SchemeLists
149
149
 
150
150
  def null?(other)
151
151
  raise arg_err_build 1, other.size if other.size != 1
152
- return '#f' unless other[0].to_s.list?
153
- other[0].to_s.size == 3 ? '#t' : '#f'
152
+ other[0] == '\'()' ? '#t' : '#f'
154
153
  end
155
154
 
156
155
  def length(other)
@@ -1,5 +1,5 @@
1
1
  require_relative 'object'
2
- require_relative 'stl_functions'
2
+ require_relative 'stl_constants'
3
3
  require_relative 'errors'
4
4
  require_relative 'numbers'
5
5
  require_relative 'strings'
@@ -13,32 +13,8 @@ module StlLoader
13
13
  def initialize
14
14
  @other = []
15
15
  @procs = {}
16
- @do_not_calculate = init_do_not_calculate_fn
17
- @reserved = init_reserved_fn
18
- set_reserved_keywords
19
- @functions = init_functions.dup
20
- init_predefined.each { |f| @functions[f] = f }
21
- end
22
-
23
- def init_do_not_calculate_fn
24
- DO_NOT_CALCULATE_FUNCTIONS
25
- end
26
-
27
- def init_functions
28
- SPECIAL_CHARACTER_FUNCTIONS
29
- end
30
-
31
- def init_predefined
32
- PREDEFINED_FUNCTIONS
33
- end
34
-
35
- def init_reserved_fn
36
- RESERVED_KEYWORDS
37
- end
38
-
39
- def set_reserved_keywords
40
- @reserved.each do |key, value|
41
- @procs[key.to_s] = value
42
- end
16
+ @functions = SPECIAL_CHARACTER_FUNCTIONS.dup
17
+ PREDEFINED_FUNCTIONS.each { |f| @functions[f] = f }
18
+ RESERVED_KEYWORDS.each { |key, value| @procs[key.to_s] = value }
43
19
  end
44
20
  end
@@ -10,25 +10,32 @@ module SchemeNumbersHelper
10
10
  other[0] == '(' ? (find_bracket_idx other, 0) + 1 : 1
11
11
  end
12
12
 
13
+ def rationalize_num(num)
14
+ [num.to_num.to_r.to_s]
15
+ end
16
+
13
17
  def num_denom_helper(other)
14
18
  if other.size == 1
15
- other = other[0].split('/')
19
+ other = rationalize_num other[0] if check_for_num other[0]
20
+ other[0].split('/')
16
21
  else
17
- _, temp = find_next_value other
18
- raise arg_err_build 1, 0 unless temp[0] == '/' || temp.empty?
19
- i = find_idx_numerators other
20
- other.delete_at(i)
22
+ other = other.map { |t| t.to_s.split(%r{(\/)}) }.flatten
23
+ other.delete('')
24
+ get_num_denom other
21
25
  end
22
- other
26
+ end
27
+
28
+ def num_denom_validator(temp)
29
+ raise arg_err_build 1, temp.size + 1 if (temp[0] != '/') && !temp.empty?
23
30
  end
24
31
 
25
32
  def get_num_denom(other)
26
- num, other = find_next_value other
27
- raise type_err '<number>', num.type unless check_for_num num
28
- return [num, 1] if other.empty?
29
- denom, other = find_next_value other
30
- raise arg_err_build 1, other.size unless other.empty?
31
- [num, denom]
33
+ x, temp = find_next_value other
34
+ num_denom_validator temp
35
+ return [x, 1] if temp.empty?
36
+ y, temp = find_next_value temp[1..-1]
37
+ num_denom_validator temp
38
+ [x, y]
32
39
  end
33
40
 
34
41
  def compare_value_arithmetic(other, oper)
@@ -122,18 +129,14 @@ module SchemeNumbers
122
129
 
123
130
  def numerator(other)
124
131
  raise arg_err_build 1, 0 if other.empty?
125
- other = num_denom_helper other
126
- result = (get_num_denom other)[0]
127
- raise type_err '<number>', result.type unless check_for_num result
128
- result.to_num
132
+ nums = num_denom_helper other
133
+ nums[0].to_num
129
134
  end
130
135
 
131
136
  def denominator(other)
132
137
  raise arg_err_build 1, 0 if other.empty?
133
- other = num_denom_helper other
134
- result = (get_num_denom other)[1]
135
- raise type_err '<number>', result.type unless check_for_num result
136
- result.to_num
138
+ nums = num_denom_helper other
139
+ nums[1].to_num
137
140
  end
138
141
 
139
142
  def abs(other)
@@ -1,12 +1,15 @@
1
1
  # redefine method in Object class
2
+ require 'bigdecimal'
3
+
4
+ # Object class
2
5
  class Object
3
6
  def number?
4
- to_f.to_s == to_s || to_i.to_s == to_s
7
+ match(/\A[-+]?[0-9]*\.?[0-9]+\Z/)
5
8
  end
6
9
 
7
10
  def to_num
8
- return to_f if to_f.to_s == to_s
9
- to_i if to_i.to_s == to_s
11
+ return to_f if to_s.include? '.'
12
+ to_i
10
13
  end
11
14
 
12
15
  def character?
@@ -1,4 +1,4 @@
1
- # STL functions
1
+ # STL constants
2
2
  module SchemeStl
3
3
  DO_NOT_CALCULATE_FUNCTIONS =
4
4
  %w[
@@ -73,6 +73,7 @@ module ValueFinder
73
73
  val = (predefined_method_caller [var])
74
74
  return val unless val.nil?
75
75
  valid = valid_var var
76
+ var = var.to_num.to_s if check_for_num var
76
77
  valid ? var : (raise unbound_symbol_err var)
77
78
  end
78
79
 
@@ -22,7 +22,7 @@ class Parser
22
22
  print 'zakichan> ' if @env_type == Environment::PROD
23
23
  token = ''
24
24
  until (validate_token token).nil? && token != ''
25
- crr_input = STDIN.gets.chomp + ' '
25
+ crr_input = STDIN.gets.chomp
26
26
  token << crr_input
27
27
  break if crr_input == ''
28
28
  end
@@ -78,7 +78,7 @@ class Parser
78
78
  end
79
79
 
80
80
  def parse(token)
81
- return read_file token if token.start_with? 'ghci'
81
+ return read_file token if (token.start_with? 'ghci ') && token.size > 4
82
82
  token_error = validate_token token
83
83
  result =
84
84
  if token_error.nil?
@@ -66,7 +66,7 @@ class Tokenizer
66
66
 
67
67
  def call_predefined_method(m_name, arr)
68
68
  return special_check_proc m_name, arr if m_name.is_a? Proc
69
- if @do_not_calculate.include? m_name
69
+ if DO_NOT_CALCULATE_FUNCTIONS.include? m_name
70
70
  send m_name.to_s, arr[2..-2]
71
71
  elsif !m_name.nil?
72
72
  values = find_all_values arr[2..-2]
@@ -1,5 +1,5 @@
1
1
  module Lisp
2
2
  module Interpreter
3
- VERSION = '0.5.2'.freeze
3
+ VERSION = '0.5.3'.freeze
4
4
  end
5
5
  end
@@ -64,12 +64,11 @@ RSpec.describe Lisp::Interpreter do
64
64
  expect(@p.parse('#\invalid')).to eq unbound_symbol_err '#\invalid'
65
65
  end
66
66
 
67
- it 'can parse integers' do
67
+ it 'can parse numbers' do
68
68
  expect(@p.parse('1')).to eq '1'
69
- end
70
-
71
- it 'can parse floats' do
69
+ expect(@p.parse('0123')).to eq '123'
72
70
  expect(@p.parse('1.5')).to eq '1.5'
71
+ expect(@p.parse('0.500')).to eq '0.5'
73
72
  end
74
73
 
75
74
  it 'can parse strings' do
@@ -269,9 +268,9 @@ RSpec.describe Lisp::Interpreter do
269
268
  end
270
269
 
271
270
  it 'returns the numerator of <q> when <q> is float' do
272
- expect(@p.parse('(numerator 5.3)')).to eq 5.3
273
- expect(@p.parse('(numerator 1.5)')).to eq 1.5
274
- expect(@p.parse('(numerator 0.7)')).to eq 0.7
271
+ expect(@p.parse('(numerator 5.3)')).to eq 5_967_269_506_265_907
272
+ expect(@p.parse('(numerator 1.5)')).to eq 3
273
+ expect(@p.parse('(numerator 0.7)')).to eq 3_152_519_739_159_347
275
274
  end
276
275
 
277
276
  it 'returns the numerator of <q> when <q> is rational' do
@@ -289,9 +288,9 @@ RSpec.describe Lisp::Interpreter do
289
288
  end
290
289
 
291
290
  it 'returns the denominator of <q> when <q> is float' do
292
- expect(@p.parse('(denominator 5.3)')).to eq 1
293
- expect(@p.parse('(denominator 1.5)')).to eq 1
294
- expect(@p.parse('(denominator 0.7)')).to eq 1
291
+ expect(@p.parse('(denominator 5.3)')).to eq 1_125_899_906_842_624
292
+ expect(@p.parse('(denominator 1.5)')).to eq 2
293
+ expect(@p.parse('(denominator 0.7)')).to eq 4_503_599_627_370_496
295
294
  end
296
295
 
297
296
  it 'returns the denominator of <q> when <q> is rational' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lisp-interpreter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zaki Petrov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-09 00:00:00.000000000 Z
11
+ date: 2017-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,7 +71,6 @@ files:
71
71
  - bin/start.rb
72
72
  - bin/zakichan.bat
73
73
  - lib/lisp/interpreter.rb
74
- - lib/lisp/interpreter/checker.rb
75
74
  - lib/lisp/interpreter/core/boolean.rb
76
75
  - lib/lisp/interpreter/core/errors.rb
77
76
  - lib/lisp/interpreter/core/functional.rb
@@ -79,7 +78,7 @@ files:
79
78
  - lib/lisp/interpreter/core/loader.rb
80
79
  - lib/lisp/interpreter/core/numbers.rb
81
80
  - lib/lisp/interpreter/core/object.rb
82
- - lib/lisp/interpreter/core/stl_functions.rb
81
+ - lib/lisp/interpreter/core/stl_constants.rb
83
82
  - lib/lisp/interpreter/core/strings.rb
84
83
  - lib/lisp/interpreter/helpers/checker.rb
85
84
  - lib/lisp/interpreter/helpers/validator.rb
@@ -1,44 +0,0 @@
1
- # Check if variable is specific type
2
- module SchemeChecker
3
- def check_for_bool(token)
4
- return true if token.boolean?
5
- is_instance_var = check_instance_var token
6
- return (check_for_bool get_var token) if is_instance_var
7
- false
8
- end
9
-
10
- def check_for_string(token)
11
- return true if token.string?
12
- is_instance_var = check_instance_var token
13
- return (check_for_string get_var token) if is_instance_var
14
- false
15
- end
16
-
17
- def check_for_num(token)
18
- return true if token.to_s.number?
19
- is_instance_var = check_instance_var token
20
- return (check_for_num get_var token) if is_instance_var
21
- false
22
- end
23
-
24
- def check_for_quote(token)
25
- return true if token[0].quote?
26
- is_instance_var = check_instance_var token
27
- return (check_for_num get_var token) if is_instance_var
28
- false
29
- end
30
-
31
- def check_for_symbol(var)
32
- var = var.join('') if var.is_a? Array
33
- return true if var.character?
34
- is_instance_var = check_instance_var var
35
- return (check_for_character get_var var) if is_instance_var
36
- false
37
- end
38
-
39
- def check_instance_var(var)
40
- return false if var.is_a? Proc
41
- return false unless valid_var_name var
42
- @procs.key? var.to_s
43
- end
44
- end