nydp 0.4.0 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +44 -0
  3. data/lib/lisp/core-010-precompile.nydp +13 -16
  4. data/lib/lisp/core-012-utils.nydp +21 -6
  5. data/lib/lisp/core-015-documentation.nydp +60 -19
  6. data/lib/lisp/core-017-builtin-dox.nydp +50 -39
  7. data/lib/lisp/core-020-utils.nydp +5 -5
  8. data/lib/lisp/core-030-syntax.nydp +103 -61
  9. data/lib/lisp/core-035-flow-control.nydp +18 -9
  10. data/lib/lisp/core-037-list-utils.nydp +36 -14
  11. data/lib/lisp/core-039-module.nydp +24 -0
  12. data/lib/lisp/core-040-utils.nydp +41 -23
  13. data/lib/lisp/core-041-string-utils.nydp +37 -9
  14. data/lib/lisp/core-042-date-utils.nydp +21 -1
  15. data/lib/lisp/core-043-list-utils.nydp +93 -67
  16. data/lib/lisp/core-045-dox-utils.nydp +5 -0
  17. data/lib/lisp/core-080-pretty-print.nydp +55 -17
  18. data/lib/lisp/core-090-hook.nydp +35 -1
  19. data/lib/lisp/core-100-utils.nydp +130 -28
  20. data/lib/lisp/core-110-hash-utils.nydp +61 -0
  21. data/lib/lisp/core-120-settings.nydp +46 -0
  22. data/lib/lisp/core-130-validations.nydp +51 -0
  23. data/lib/lisp/{core-060-benchmarking.nydp → core-900-benchmarking.nydp} +108 -5
  24. data/lib/lisp/tests/accum-examples.nydp +28 -1
  25. data/lib/lisp/tests/aif-examples.nydp +8 -3
  26. data/lib/lisp/tests/andify-examples.nydp +7 -0
  27. data/lib/lisp/tests/at-syntax-examples.nydp +17 -0
  28. data/lib/lisp/tests/best-examples.nydp +9 -0
  29. data/lib/lisp/tests/builtin-tests.nydp +19 -0
  30. data/lib/lisp/tests/case-examples.nydp +14 -0
  31. data/lib/lisp/tests/cdr-set-examples.nydp +6 -0
  32. data/lib/lisp/tests/date-examples.nydp +56 -1
  33. data/lib/lisp/tests/destructuring-examples.nydp +5 -5
  34. data/lib/lisp/tests/detect-examples.nydp +12 -0
  35. data/lib/lisp/tests/dp-examples.nydp +24 -0
  36. data/lib/lisp/tests/empty-examples.nydp +1 -1
  37. data/lib/lisp/tests/error-tests.nydp +4 -4
  38. data/lib/lisp/tests/filter-forms-examples.nydp +30 -0
  39. data/lib/lisp/tests/foundation-test.nydp +12 -0
  40. data/lib/lisp/tests/hash-examples.nydp +26 -2
  41. data/lib/lisp/tests/list-grep-examples.nydp +40 -0
  42. data/lib/lisp/tests/list-tests.nydp +58 -1
  43. data/lib/lisp/tests/map-hash-examples.nydp +11 -0
  44. data/lib/lisp/tests/mapreduce-examples.nydp +10 -0
  45. data/lib/lisp/tests/module-examples.nydp +10 -0
  46. data/lib/lisp/tests/multi-assign-examples.nydp +6 -0
  47. data/lib/lisp/tests/parser-tests.nydp +21 -0
  48. data/lib/lisp/tests/pretty-print-tests.nydp +16 -13
  49. data/lib/lisp/tests/set-difference-examples.nydp +8 -0
  50. data/lib/lisp/tests/set-intersection-examples.nydp +32 -0
  51. data/lib/lisp/tests/set-union-examples.nydp +24 -0
  52. data/lib/lisp/tests/settings-examples.nydp +40 -0
  53. data/lib/lisp/tests/sort-examples.nydp +8 -0
  54. data/lib/lisp/tests/string-tests.nydp +61 -1
  55. data/lib/lisp/tests/syntax-tests.nydp +5 -1
  56. data/lib/lisp/tests/to-integer-examples.nydp +16 -0
  57. data/lib/lisp/tests/validation-examples.nydp +15 -0
  58. data/lib/lisp/tests/zap-examples.nydp +12 -0
  59. data/lib/nydp.rb +13 -7
  60. data/lib/nydp/assignment.rb +10 -3
  61. data/lib/nydp/builtin.rb +1 -1
  62. data/lib/nydp/builtin/abs.rb +8 -0
  63. data/lib/nydp/builtin/cdr_set.rb +1 -6
  64. data/lib/nydp/builtin/date.rb +15 -1
  65. data/lib/nydp/builtin/error.rb +1 -1
  66. data/lib/nydp/builtin/handle_error.rb +1 -1
  67. data/lib/nydp/builtin/hash.rb +27 -45
  68. data/lib/nydp/builtin/inspect.rb +1 -1
  69. data/lib/nydp/builtin/plus.rb +10 -2
  70. data/lib/nydp/builtin/rand.rb +18 -0
  71. data/lib/nydp/builtin/random_string.rb +2 -2
  72. data/lib/nydp/builtin/ruby_wrap.rb +72 -0
  73. data/lib/nydp/builtin/set_intersection.rb +8 -0
  74. data/lib/nydp/builtin/set_union.rb +8 -0
  75. data/lib/nydp/builtin/string_match.rb +2 -2
  76. data/lib/nydp/builtin/string_pad_left.rb +7 -0
  77. data/lib/nydp/builtin/string_pad_right.rb +7 -0
  78. data/lib/nydp/builtin/string_replace.rb +1 -1
  79. data/lib/nydp/builtin/string_split.rb +1 -2
  80. data/lib/nydp/builtin/to_integer.rb +23 -0
  81. data/lib/nydp/builtin/to_string.rb +2 -9
  82. data/lib/nydp/builtin/type_of.rb +9 -6
  83. data/lib/nydp/closure.rb +0 -3
  84. data/lib/nydp/cond.rb +23 -1
  85. data/lib/nydp/context_symbol.rb +14 -6
  86. data/lib/nydp/core.rb +45 -33
  87. data/lib/nydp/core_ext.rb +54 -0
  88. data/lib/nydp/date.rb +37 -31
  89. data/lib/nydp/function_invocation.rb +34 -26
  90. data/lib/nydp/hash.rb +5 -6
  91. data/lib/nydp/helper.rb +41 -25
  92. data/lib/nydp/interpreted_function.rb +68 -40
  93. data/lib/nydp/literal.rb +1 -1
  94. data/lib/nydp/pair.rb +25 -9
  95. data/lib/nydp/parser.rb +8 -6
  96. data/lib/nydp/string_atom.rb +16 -22
  97. data/lib/nydp/symbol.rb +40 -27
  98. data/lib/nydp/symbol_lookup.rb +7 -7
  99. data/lib/nydp/tokeniser.rb +2 -2
  100. data/lib/nydp/truth.rb +17 -10
  101. data/lib/nydp/version.rb +1 -1
  102. data/lib/nydp/vm.rb +7 -2
  103. data/nydp.gemspec +2 -4
  104. data/spec/date_spec.rb +115 -22
  105. data/spec/embedded_spec.rb +12 -12
  106. data/spec/foreign_hash_spec.rb +14 -2
  107. data/spec/hash_non_hash_behaviour_spec.rb +7 -7
  108. data/spec/hash_spec.rb +24 -2
  109. data/spec/nydp_spec.rb +14 -2
  110. data/spec/parser_spec.rb +27 -16
  111. data/spec/rand_spec.rb +45 -0
  112. data/spec/spec_helper.rb +13 -1
  113. data/spec/symbol_spec.rb +31 -0
  114. data/spec/time_spec.rb +1 -1
  115. metadata +38 -37
  116. data/lib/nydp/builtin/car.rb +0 -7
  117. data/lib/nydp/builtin/cdr.rb +0 -7
  118. data/lib/nydp/builtin/cons.rb +0 -9
@@ -27,29 +27,29 @@ describe Nydp::Parser do
27
27
  it "should parse empty string" do
28
28
  expected = pair_list([sym('string-pieces'), Nydp::StringFragmentCloseToken.new('','')])
29
29
  actual = parse_string ""
30
- expect(actual).to eq Nydp::StringAtom.new ''
30
+ expect(actual).to eq ''
31
31
  end
32
32
 
33
33
  it "should parse external text" do
34
34
  actual = parse_string "a fluffy bunny!"
35
- expect(actual) .to eq Nydp::StringAtom.new "a fluffy bunny!"
35
+ expect(actual) .to eq "a fluffy bunny!"
36
36
  expect(actual.inspect).to eq '"a fluffy bunny!"'
37
37
  end
38
38
 
39
39
  it "should parse a string delimited by eof" do
40
40
  expected = pair_list([sym('string-pieces'), Nydp::StringFragmentCloseToken.new('a fluffy bunny!','a fluffy bunny!')])
41
41
  actual = parse_string "a fluffy bunny!"
42
- expect(actual) .to eq Nydp::StringAtom.new "a fluffy bunny!"
42
+ expect(actual) .to eq "a fluffy bunny!"
43
43
  expect(actual.inspect).to eq '"a fluffy bunny!"'
44
44
  end
45
45
 
46
46
  it "should parse a string with embedded code, delimited by eof" do
47
47
  x1 = sym('string-pieces')
48
48
  x2 = Nydp::StringFragmentToken.new('a fluffy bunny! ','a fluffy bunny! ~')
49
- x2 = Nydp::StringAtom.new(x2.string, x2)
49
+ x2 = x2.string
50
50
  x3 = sym('expr')
51
51
  x4 = Nydp::StringFragmentCloseToken.new(' a purple cow!',' a purple cow!')
52
- x4 = Nydp::StringAtom.new(x4.string, x4)
52
+ x4 = x4.string
53
53
 
54
54
  expected = pair_list([x1,x2,x3,x4])
55
55
  actual = parse_string "a fluffy bunny! ~expr a purple cow!"
@@ -59,15 +59,15 @@ describe Nydp::Parser do
59
59
  it "should parse a string with embedded code containing a nested string, delimited by eof" do
60
60
  n1 = sym(:foo)
61
61
  n2 = sym(:bar)
62
- n3 = Nydp::StringAtom.new 'an embedded bunny :)'
62
+ n3 = 'an embedded bunny :)'
63
63
  n4 = sym(:zop)
64
64
 
65
65
  x1 = sym('string-pieces')
66
66
  x2 = Nydp::StringFragmentToken.new('a fluffy bunny! ','a fluffy bunny! ~')
67
- x2 = Nydp::StringAtom.new(x2.string, x2)
67
+ x2 = x2.string
68
68
  x3 = pair_list [n1, n2, n3, n4]
69
69
  x4 = Nydp::StringFragmentCloseToken.new(' a purple cow!',' a purple cow!')
70
- x4 = Nydp::StringAtom.new(x4.string, x4)
70
+ x4 = x4.string
71
71
 
72
72
  expected = pair_list([x1,x2,x3,x4])
73
73
  actual = parse_string 'a fluffy bunny! ~(foo bar "an embedded bunny :)" zop) a purple cow!'
@@ -80,10 +80,10 @@ describe Nydp::Parser do
80
80
 
81
81
  s1 = sym('string-pieces')
82
82
  s2 = Nydp::StringFragmentToken.new('a rather ','a rather ~')
83
- s2 = Nydp::StringAtom.new(s2.string, s2)
83
+ s2 = s2.string
84
84
  s3 = pair_list [e1, e2]
85
85
  s4 = Nydp::StringFragmentCloseToken.new(' bunny :)',' bunny :)"')
86
- s4 = Nydp::StringAtom.new(s4.string, s4)
86
+ s4 = s4.string
87
87
 
88
88
  n1 = sym(:foo)
89
89
  n2 = sym(:bar)
@@ -92,10 +92,10 @@ describe Nydp::Parser do
92
92
 
93
93
  x1 = sym('string-pieces')
94
94
  x2 = Nydp::StringFragmentToken.new('a fluffy bunny! ','a fluffy bunny! ~')
95
- x2 = Nydp::StringAtom.new(x2.string, x2)
95
+ x2 = x2.string
96
96
  x3 = pair_list [n1, n2, n3, n4]
97
97
  x4 = Nydp::StringFragmentCloseToken.new(' a purple cow!',' a purple cow!')
98
- x4 = Nydp::StringAtom.new(x4.string, x4)
98
+ x4 = x4.string
99
99
 
100
100
  expected = pair_list([x1,x2,x3,x4])
101
101
  actual = parse_string "a fluffy bunny! ~(foo bar \"a rather ~(describe bunny) bunny :)\" zop) a purple cow!"
@@ -9,7 +9,7 @@ describe Nydp::Hash do
9
9
  describe "hash set" do
10
10
  it "returns a new Nydp hash" do
11
11
  k = Nydp::Symbol.mk "keysym", ns
12
- v = Nydp::StringAtom.new "foobar"
12
+ v = "foobar"
13
13
  args = pair_list [ahash, k, v]
14
14
  Nydp::Builtin::HashSet.instance.invoke vm, args
15
15
 
@@ -29,7 +29,7 @@ describe Nydp::Hash do
29
29
 
30
30
  Nydp::Builtin::HashGet.instance.invoke vm, pair_list(args)
31
31
 
32
- expect(vm.args.pop).to eq Nydp::StringAtom.new("avalue")
32
+ expect(vm.args.pop).to eq "avalue"
33
33
  end
34
34
 
35
35
  it "converts ruby nil to nydp value" do
@@ -84,5 +84,17 @@ describe Nydp::Hash do
84
84
  expect(vm.args.pop).to eq pair_list [sym("k0"), sym("k1")]
85
85
  end
86
86
  end
87
+
88
+ describe "hash-slice" do
89
+ it "returns a new hash containing only the given keys from the old hash" do
90
+ ahash[:k0] = 42
91
+ ahash[:k1] = 84
92
+ args = [ahash, pair_list([sym("k0"), sym("k1")])]
93
+
94
+ Nydp::Builtin::HashSlice.instance.invoke vm, pair_list(args)
95
+
96
+ expect(vm.args.pop).to eq({ k0: 42, k1: 84 })
97
+ end
98
+ end
87
99
  end
88
100
  end
@@ -20,15 +20,15 @@ describe Nydp::Hash do
20
20
  args = [ ahash, k ]
21
21
 
22
22
  Nydp::Builtin::HashGet.instance.invoke vm, pair_list(args)
23
- expect(vm.args.pop).to eq Nydp::StringAtom.new("hello there")
23
+ expect(vm.args.pop).to eq "hello there"
24
24
  end
25
25
 
26
26
  it "converts string keys to method names" do
27
- k = Nydp::StringAtom.new "b"
27
+ k = "b"
28
28
  args = [ ahash, k ]
29
29
 
30
30
  Nydp::Builtin::HashGet.instance.invoke vm, pair_list(args)
31
- expect(vm.args.pop).to eq Nydp::StringAtom.new("hello there")
31
+ expect(vm.args.pop).to eq "hello there"
32
32
  end
33
33
 
34
34
  it "returns nil for unavailable methods" do
@@ -42,7 +42,7 @@ describe Nydp::Hash do
42
42
  end
43
43
 
44
44
  describe "unfriendly non-hash" do
45
- let(:ahash) { Nydp::StringAtom.new "this here ain't no hash, hombre" }
45
+ let(:ahash) { "this here ain't no hash, hombre" }
46
46
 
47
47
  def cleanup_err_msg txt
48
48
  txt.gsub(/at \/.*:in `builtin_invoke'/, '<error info>')
@@ -51,7 +51,7 @@ describe Nydp::Hash do
51
51
  describe "hash set" do
52
52
  it "does nothing, returns its value" do
53
53
  k = Nydp::Symbol.mk "keysym", ns
54
- v = Nydp::StringAtom.new "foobar"
54
+ v = "foobar"
55
55
  args = pair_list [ahash, k, v]
56
56
 
57
57
  begin
@@ -66,7 +66,7 @@ with args
66
66
  keysym
67
67
  \"foobar\""
68
68
 
69
- expect(cleanup_err_msg error.cause.message).to eq "hash-set: Not a hash: Nydp::StringAtom"
69
+ expect(cleanup_err_msg error.cause.message).to eq "_nydp_get : not settable: keysym on String"
70
70
  end
71
71
  end
72
72
 
@@ -86,7 +86,7 @@ with args
86
86
  \"this here ain't no hash, hombre\"
87
87
  keysym"
88
88
 
89
- expect(cleanup_err_msg error.cause.message).to eq "hash-get: Not a hash: Nydp::StringAtom"
89
+ expect(cleanup_err_msg error.cause.message).to eq "_nydp_get : not gettable: keysym on String"
90
90
  end
91
91
  end
92
92
  end
@@ -16,7 +16,7 @@ describe Nydp::Hash do
16
16
 
17
17
  it "converts ruby string key to nydp string key" do
18
18
  hash = Nydp::Hash.new
19
- hash[Nydp::StringAtom.new "boo"] = 42
19
+ hash["boo"] = 42
20
20
 
21
21
  rhash = hash.to_ruby
22
22
  expect(rhash["boo"]).to eq 42
@@ -123,7 +123,7 @@ describe Nydp::Hash do
123
123
  describe "hash set" do
124
124
  it "does nothing, returns its value" do
125
125
  k = Nydp::Symbol.mk "keysym", ns
126
- v = Nydp::StringAtom.new "foobar"
126
+ v = "foobar"
127
127
  args = pair_list [ahash, k, v]
128
128
  Nydp::Builtin::HashSet.instance.invoke vm, args
129
129
 
@@ -143,4 +143,26 @@ describe Nydp::Hash do
143
143
  end
144
144
  end
145
145
  end
146
+
147
+ describe "hash-slice" do
148
+ it "returns a new hash containing only the given keys from the old hash" do
149
+ hash = Nydp::Hash.new
150
+ sfoo = sym "foo"
151
+ sbar = sym "bar"
152
+ syak = sym "yak"
153
+ szeb = sym "zeb"
154
+
155
+ h = Nydp::Hash.new
156
+
157
+ h[sfoo] = 16
158
+ h[sbar] = 42
159
+ h[szeb] = 99
160
+
161
+ args = [h, pair_list([sbar, syak, szeb])]
162
+
163
+ Nydp::Builtin::HashSlice.instance.invoke vm, pair_list(args)
164
+
165
+ expect(vm.args.pop).to eq({ sbar => 42, szeb => 99 })
166
+ end
167
+ end
146
168
  end
@@ -22,7 +22,7 @@ describe Nydp do
22
22
  end
23
23
 
24
24
  it "should add strings" do
25
- expect(run '(+ "hello" " " "world")').to eq Nydp::StringAtom.new("hello world")
25
+ expect(run '(+ "hello" " " "world")').to eq "hello world"
26
26
  end
27
27
 
28
28
  it "should add Pairs" do
@@ -46,7 +46,7 @@ describe Nydp do
46
46
  end
47
47
 
48
48
  it "should convert items to strings" do
49
- expect(run "(to-string 3.1415)").to eq Nydp::StringAtom.new("3.1415")
49
+ expect(run "(to-string 3.1415)").to eq "3.1415"
50
50
  end
51
51
 
52
52
  it "should compare integers" do
@@ -112,4 +112,16 @@ describe Nydp do
112
112
  expect(run code).to eq 17
113
113
  end
114
114
  end
115
+
116
+ describe "proc from ruby object" do
117
+ it "invokes a proc like a builtin function" do
118
+ Nydp::Symbol.mk(:tt, ns).assign(TestThing.new(42, 720, 9699690))
119
+
120
+ one_thing = run "((hash-get tt 'one_thing) 24)"
121
+ expect(one_thing).to eq(42 + 24)
122
+
123
+ two_things = run "((hash-get tt 'two_things) 60 2)"
124
+ expect(two_things).to eq(60 + (2 * 720))
125
+ end
126
+ end
115
127
  end
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Nydp::Parser do
4
+ let(:empty) { Nydp::Symbol.mk :"", ns }
4
5
  let(:aa) { Nydp::Symbol.mk :aa, ns }
5
6
  let(:a) { Nydp::Symbol.mk :a, ns }
6
7
  let(:b) { Nydp::Symbol.mk :b, ns }
@@ -9,6 +10,7 @@ describe Nydp::Parser do
9
10
  let(:zz) { Nydp::Symbol.mk :zz, ns }
10
11
  let(:foo) { Nydp::Symbol.mk :foo, ns }
11
12
  let(:bar) { Nydp::Symbol.mk :bar, ns }
13
+ let(:foobar) { Nydp::Symbol.mk :foobar, ns }
12
14
  let(:zab) { Nydp::Symbol.mk :zab, ns }
13
15
  let(:quote) { Nydp::Symbol.mk :quote, ns }
14
16
  let(:quasiquote) { Nydp::Symbol.mk :quasiquote, ns }
@@ -19,6 +21,7 @@ describe Nydp::Parser do
19
21
  let(:dotsyn) { Nydp::Symbol.mk :"dot-syntax", ns }
20
22
  let(:cocosyn) { Nydp::Symbol.mk :"colon-colon-syntax", ns }
21
23
  let(:colosyn) { Nydp::Symbol.mk :"colon-syntax", ns }
24
+ let(:atsyn) { Nydp::Symbol.mk :"at-syntax", ns }
22
25
  let(:string_pieces) { Nydp::Symbol.mk :"string-pieces", ns }
23
26
 
24
27
  it "should return a stream of tokens" do
@@ -72,7 +75,7 @@ describe Nydp::Parser do
72
75
  s2 = Nydp::StringFragmentCloseToken.new "hello there", '"hello there"'
73
76
 
74
77
  x1 = 1
75
- x2 = Nydp::StringAtom.new "hello there"
78
+ x2 = "hello there"
76
79
  x3 = 3
77
80
 
78
81
  expected = pair_list [x1, x2, x3]
@@ -82,7 +85,7 @@ describe Nydp::Parser do
82
85
 
83
86
  it "should parse a string" do
84
87
  x1 = sym 'join'
85
- x2 = Nydp::StringAtom.new " - "
88
+ x2 = " - "
86
89
  x3 = 1
87
90
  x4 = 2
88
91
  x5 = 3
@@ -96,7 +99,7 @@ describe Nydp::Parser do
96
99
  s2 = Nydp::StringFragmentCloseToken.new "hello (1 2 3) there", '"hello (1 2 3) there"'
97
100
 
98
101
  x1 = 1
99
- x2 = Nydp::StringAtom.new "hello (1 2 3) there"
102
+ x2 = "hello (1 2 3) there"
100
103
  x3 = 3
101
104
 
102
105
  expected = pair_list [x1, x2, x3]
@@ -108,7 +111,7 @@ describe Nydp::Parser do
108
111
  s2 = Nydp::StringFragmentCloseToken.new "hello there \"jimmy\"", '"hello there \"jimmy\""'
109
112
 
110
113
  x1 = 1
111
- x2 = Nydp::StringAtom.new "hello there \"jimmy\""
114
+ x2 = "hello there \"jimmy\""
112
115
  x3 = 3
113
116
 
114
117
  expected = pair_list [x1, x2, x3]
@@ -117,7 +120,7 @@ describe Nydp::Parser do
117
120
  end
118
121
 
119
122
  it "should handle escaped tabs and newlines inside a string" do
120
- expected = Nydp::StringAtom.new "hello\tworld\nnice day"
123
+ expected = "hello\tworld\nnice day"
121
124
  parsed = parse "\"hello\\tworld\\nnice day\""
122
125
  expect(parsed).to eq expected
123
126
  end
@@ -156,6 +159,14 @@ describe Nydp::Parser do
156
159
  expect(parse "foo::bar").to eq pair_list([cocosyn, foo, bar])
157
160
  end
158
161
 
162
+ it "should parse an at symbol" do
163
+ expect(parse "foo@bar").to eq pair_list([atsyn, foo, bar])
164
+ end
165
+
166
+ it "should parse a prefix-at symbol" do
167
+ expect(parse "@foobar").to eq pair_list([atsyn, empty, foobar])
168
+ end
169
+
159
170
  it "should parse a colon-symbol within a colon-colon within a dotted symbol" do
160
171
  expected = parse "(colon-colon-syntax (colon-syntax (dot-syntax aa foo) foo) (colon-syntax bar (dot-syntax bar zz)))"
161
172
  expect(parse "aa.foo:foo::bar:bar.zz").to eq expected
@@ -204,7 +215,7 @@ describe Nydp::Parser do
204
215
  end
205
216
 
206
217
  it "retains otherwise unidentified list prefixes" do
207
- expect(parse "%wong(bar)").to eq pair_list([prefix_list, Nydp::StringAtom.new("%wong"), pair_list([bar])])
218
+ expect(parse "%wong(bar)").to eq pair_list([prefix_list, "%wong", pair_list([bar])])
208
219
  end
209
220
 
210
221
  it "should do some complicated unquote stuff with lists" do
@@ -246,24 +257,24 @@ describe Nydp::Parser do
246
257
  end
247
258
 
248
259
  it "parses a simple string" do
249
- expect(parse '"foo"').to eq Nydp::StringAtom.new("foo")
260
+ expect(parse '"foo"').to eq "foo"
250
261
  end
251
262
 
252
263
  it "parses a string with a simple interpolation" do
253
- str = Nydp::StringAtom.new("foo ")
254
- empty = Nydp::StringAtom.new("")
264
+ str = "foo "
265
+ empty = ""
255
266
  expect(parse '"foo ~foo"').to eq pair_list([string_pieces, str, foo, empty])
256
267
  end
257
268
 
258
269
  it "parses a string with a more complex interpolation" do
259
- strf = Nydp::StringAtom.new("foo ")
260
- strb = Nydp::StringAtom.new(" bar")
270
+ strf = "foo "
271
+ strb = " bar"
261
272
  expect(parse '"foo ~(foo bar) bar"').to eq pair_list([string_pieces, strf, pair_list([foo, bar]), strb])
262
273
  end
263
274
 
264
275
  it "parses a string with an interpolation containing a nested interpolation" do
265
- strf = Nydp::StringAtom.new("foo ")
266
- strb = Nydp::StringAtom.new(" bar")
276
+ strf = "foo "
277
+ strb = " bar"
267
278
 
268
279
  nested = pair_list [string_pieces, strf, foo, strb]
269
280
  expr = pair_list [foo, nested, bar]
@@ -272,7 +283,7 @@ describe Nydp::Parser do
272
283
  end
273
284
 
274
285
  it "parses a string with only an interpolation" do
275
- empty = Nydp::StringAtom.new("")
286
+ empty = ""
276
287
  expect(parse '"~foo"').to eq pair_list([string_pieces, empty, foo, empty])
277
288
  end
278
289
 
@@ -281,7 +292,7 @@ describe Nydp::Parser do
281
292
  ; here's a comment
282
293
  (zab))
283
294
  "
284
- c1 = pair_list([comment, Nydp::StringAtom.new("here's a comment")])
295
+ c1 = pair_list([comment, "here's a comment"])
285
296
  fbar = pair_list([bar])
286
297
  fzab = pair_list([Nydp::Symbol.mk(:zab, ns)])
287
298
  fdef = Nydp::Symbol.mk(:def, ns)
@@ -311,7 +322,7 @@ NYDP
311
322
  expect(args[1]).to eq sym("args")
312
323
  expect(args[2]).to be_nil
313
324
  expect(parsed[2].cdr.cdr).to eq sym("body")
314
- expect(parsed[3].to_a).to eq [sym("comment"), Nydp::StringAtom.new("define a new function in the global namespace")]
325
+ expect(parsed[3].to_a).to eq [sym("comment"), "define a new function in the global namespace"]
315
326
  expect(parsed[4].to_a).to eq [sym("chapter"), sym("nydp-core")]
316
327
  end
317
328
  end
@@ -0,0 +1,45 @@
1
+ require "spec_helper"
2
+
3
+ describe Nydp::Builtin::Rand do
4
+ let(:vm) { Nydp::VM.new(ns) }
5
+
6
+ let(:randf) { Nydp::Builtin::Rand.instance }
7
+
8
+ def get_rand *args
9
+ randf.invoke vm, pair_list(args)
10
+ vm.pop_args(1).first
11
+ end
12
+
13
+ describe "zero args" do
14
+ it "returns values between 0 and 1" do
15
+ numbers = (0..100).map { |i| get_rand }
16
+ expect(numbers.all? { |n| n >= 0 && n < 1 })
17
+ avg = numbers.reduce &:+
18
+ expect(avg).to be_between 40, 60 # with high probability
19
+ distinct = Set.new numbers
20
+ expect(distinct.count).to be > 90
21
+ end
22
+ end
23
+
24
+ describe "one arg" do
25
+ it "returns values between 0 and arg" do
26
+ numbers = (0..200).map { |i| get_rand 10 }
27
+ expect(numbers.all? { |n| n >= 0 && n < 10 })
28
+ avg = numbers.reduce &:+
29
+ expect(avg).to be_between 800, 1200 # with high probability
30
+ distinct = Set.new numbers
31
+ expect(distinct.count).to eq 10
32
+ end
33
+ end
34
+
35
+ describe "two arg" do
36
+ it "returns values between arg 0 and arg 1" do
37
+ numbers = (0..200).map { |i| get_rand 10, 20 }
38
+ expect(numbers.all? { |n| n >= 10 && n < 20 })
39
+ avg = numbers.reduce &:+
40
+ expect(avg).to be_between 2800, 3200 # with high probability
41
+ distinct = Set.new numbers
42
+ expect(distinct.count).to eq 10
43
+ end
44
+ end
45
+ end
@@ -37,5 +37,17 @@ class TestThing
37
37
  "(TestThing #{a.inspect} #{b.inspect})"
38
38
  end
39
39
 
40
- def _nydp_safe_methods ; %i{ a b } ; end
40
+ def one_thing x
41
+ x + a
42
+ end
43
+
44
+ def two_things x, y
45
+ x + (y * b)
46
+ end
47
+
48
+ def _nydp_get name
49
+ name = name.to_s.to_sym
50
+ return send(name) if name == :a || name == :b
51
+ return method(name) if name == :one_thing || name == :two_things
52
+ end
41
53
  end