kapusta 0.2.3 → 0.2.4

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
  SHA256:
3
- metadata.gz: 1a5fcd922c54b5fa491785e6087c4fc2f8e6de1a8d94deab10fca3f775518b9b
4
- data.tar.gz: d95f4bce9e6673104c44fc0ff281a705e796797490285cc50ef509b6b9fe5e97
3
+ metadata.gz: ef91159e2623e199fc391fa483440fa9f8ca76f273fbc31caeb0b07e891ee92c
4
+ data.tar.gz: 5468818f0ad4b0509a31635d886f3374009e3e8499a197d82f99da22c53e3a12
5
5
  SHA512:
6
- metadata.gz: cb5ba4420166be83a152c22bf56a12563efdb8cc64e54f230f6d299051761c5abc2329ae42dfc5e6127a721f66250b9804d6ec86a9b053dc36282599b2f8c986
7
- data.tar.gz: 334d34e9e47bdecf6a4aa35cc6e905770efb6913fa6c2e9f90b0f3b1a95915b9044c8e3f36c81778a865f91a6d43f675318bf1ae892e19292cb4a2d2de179685
6
+ metadata.gz: 7bcc51b164c21357c2aa2ef52e4e544cba0116ca8d8b6b1c02ecb55dc62697508a3cbb22d4c56a0a7cf0da77f53d7500827ecd2debe0760fec34ebe4d992be07
7
+ data.tar.gz: 40b1b709b5e083b94584424f9fb2ad7c3ec6908786ea80051173df8edaa380b9b5b76d0c485a6377f7e77bbed5799633501204d7a39acf0b919eb74197fabcf2
data/examples/counter.kap CHANGED
@@ -4,8 +4,7 @@
4
4
  (set (ivar n) start))
5
5
 
6
6
  (fn tick []
7
- (set (ivar n) (+ (ivar n) 1))
8
- (ivar n))
7
+ (set (ivar n) (+ (ivar n) 1)))
9
8
 
10
9
  (fn value []
11
10
  (ivar n))
@@ -1,6 +1,9 @@
1
1
  (fn length-of-last-word [s]
2
- (let [words (s.strip.split)]
3
- (: (. words -1) :length)))
2
+ (-> s
3
+ (: :strip)
4
+ (: :split)
5
+ (. -1)
6
+ (: :length)))
4
7
 
5
8
  (print (length-of-last-word "Hello World"))
6
9
  (print (length-of-last-word " fly me to the moon "))
@@ -1,7 +1,6 @@
1
1
  (fn palindrome? [s]
2
- (let [lower (s.downcase)
3
- normalized (lower.gsub (ruby "/[^a-z]/") "")]
4
- (= normalized (normalized.reverse))))
2
+ (let [normalized (-> s (: :downcase) (: :gsub (ruby "/[^a-z]/") ""))]
3
+ (= normalized (: normalized :reverse))))
5
4
 
6
5
  (print (palindrome? "racecar"))
7
6
  (print (palindrome? "A man, a plan, a canal: Panama"))
data/examples/pangram.kap CHANGED
@@ -1,9 +1,10 @@
1
1
  (fn pangram? [s]
2
- (let [lower (s.downcase)
3
- letters (lower.gsub (ruby "/[^a-z]/") "")
4
- chars (letters.chars)
5
- uniq (chars.uniq)]
6
- (= (uniq.length) 26)))
2
+ (= 26
3
+ (length (-> s
4
+ (: :downcase)
5
+ (: :gsub (ruby "/[^a-z]/") "")
6
+ (: :chars)
7
+ (: :uniq)))))
7
8
 
8
9
  (print (pangram? "The quick brown fox jumps over the lazy dog"))
9
10
  (print (pangram? "Hello, world"))
@@ -0,0 +1,13 @@
1
+ (fn pivot-index [nums]
2
+ (var total 0)
3
+ (each [n nums] (set total (+ total n)))
4
+ (var left 0)
5
+ (var found -1)
6
+ (each [i n (ipairs nums)]
7
+ (when (and (= found -1) (= left (- total left n))) (set found i))
8
+ (set left (+ left n)))
9
+ found)
10
+
11
+ (print (pivot-index [1 7 3 6 5 6]))
12
+ (print (pivot-index [1 2 3]))
13
+ (print (pivot-index [2 1 -1]))
@@ -106,22 +106,19 @@ module Kapusta
106
106
  if iter_expr.is_a?(List) && iter_expr.head.is_a?(Sym)
107
107
  case iter_expr.head.name
108
108
  when 'ipairs'
109
- value_var = temp('value')
110
- index_var = temp('index')
111
109
  body_env = env.child
112
- bind_code, body_env = emit_iteration_bindings(
113
- [[binding_pats[0], index_var], [binding_pats[1], value_var]], body_env
114
- )
110
+ value_var, value_bind = bind_iteration_param(binding_pats[1], 'value', body_env)
111
+ index_var, index_bind = bind_iteration_param(binding_pats[0], 'index', body_env)
112
+ bind_code = [index_bind, value_bind].compact.join("\n")
115
113
  body_code = yield(body_env)
116
114
  header = "#{emit_expr(iter_expr.items[1], env, current_scope)}" \
117
115
  ".each_with_index do |#{value_var}, #{index_var}|"
118
116
  return iteration_block(header, bind_code, body_code)
119
117
  when 'pairs'
120
- key_var = temp('key')
121
- value_var = temp('value')
122
118
  body_env = env.child
123
- bind_code, body_env = emit_iteration_bindings([[binding_pats[0], key_var], [binding_pats[1], value_var]],
124
- body_env)
119
+ key_var, key_bind = bind_iteration_param(binding_pats[0], 'key', body_env)
120
+ value_var, value_bind = bind_iteration_param(binding_pats[1], 'value', body_env)
121
+ bind_code = [key_bind, value_bind].compact.join("\n")
125
122
  body_code = yield(body_env)
126
123
  header = "#{emit_expr(iter_expr.items[1], env, current_scope)}.each do |#{key_var}, #{value_var}|"
127
124
  return iteration_block(header, bind_code, body_code)
@@ -130,11 +127,10 @@ module Kapusta
130
127
 
131
128
  coll_code = emit_expr(iter_expr, env, current_scope)
132
129
  if binding_pats.length == 1
133
- value_var = temp('value')
134
130
  body_env = env.child
135
- bind_code, body_env = emit_iteration_bindings([[binding_pats[0], value_var]], body_env)
131
+ value_var, bind_code = bind_iteration_param(binding_pats[0], 'value', body_env)
136
132
  body_code = yield(body_env)
137
- iteration_block("#{coll_code}.each do |#{value_var}|", bind_code, body_code)
133
+ iteration_block("#{coll_code}.each do |#{value_var}|", bind_code || '', body_code)
138
134
  else
139
135
  parts_var = temp('parts')
140
136
  body_env = env.child
@@ -145,6 +141,17 @@ module Kapusta
145
141
  end
146
142
  end
147
143
 
144
+ def bind_iteration_param(pattern, fallback_name, env)
145
+ if pattern.is_a?(Sym) && !pattern.dotted?
146
+ ruby_name = pattern.name == '_' ? '_' : define_local(env, pattern.name)
147
+ [ruby_name, nil]
148
+ else
149
+ tmp = temp(fallback_name)
150
+ bind_code, _new_env = emit_iteration_bindings([[pattern, tmp]], env)
151
+ [tmp, bind_code.empty? ? nil : bind_code]
152
+ end
153
+ end
154
+
148
155
  def iteration_block(header, bind_code, body_code)
149
156
  [header, indent(join_code(bind_code, body_code)), 'end'].join("\n")
150
157
  end
@@ -189,11 +196,9 @@ module Kapusta
189
196
  end
190
197
 
191
198
  def emit_sequence_value_assignment(target_var, body_code)
192
- <<~RUBY.chomp
193
- #{target_var} = begin
194
- #{indent(body_code)}
195
- end
196
- RUBY
199
+ return "#{target_var} = #{body_code}" unless body_code.include?("\n")
200
+
201
+ ["#{target_var} = begin", indent(body_code), 'end'].join("\n")
197
202
  end
198
203
  end
199
204
  end
@@ -491,7 +491,7 @@ module Kapusta
491
491
  code.match?(/\A\$[a-zA-Z_]\w*\z/) ||
492
492
  code.match?(/\A[A-Z]\w*(?:::[A-Z]\w*)*\z/) ||
493
493
  code.match?(/\A[a-z_]\w*[!?=]?\([^()\n]*\)\z/) ||
494
- code.match?(/\A\d+(?:\.\d+)?\z/) ||
494
+ code.match?(/\A-?\d+(?:\.\d+)?\z/) ||
495
495
  code.match?(/\A[a-z_]\w*(?:\.[a-z_]\w*[!?=]?(?:\([^()\n]*\))?|\[[^\[\]]*\])+\z/) ||
496
496
  code.match?(/\A[A-Z]\w*(?:::[A-Z]\w*)*(?:\.[a-z_]\w*[!?=]?(?:\([^()\n]*\))?|\[[^\[\]]*\])+\z/) ||
497
497
  code.match?(/\A:[a-zA-Z_]\w*[!?=]?\z/) ||
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kapusta
4
- VERSION = '0.2.3'
4
+ VERSION = '0.2.4'
5
5
  end
@@ -280,6 +280,10 @@ RSpec.describe 'examples' do
280
280
  OUT
281
281
  end
282
282
 
283
+ it 'pivot-index.kap' do
284
+ expect(run_example('pivot-index.kap')).to eq("3\n-1\n0\n")
285
+ end
286
+
283
287
  it 'points.kap' do
284
288
  expect(run_example('points.kap')).to eq(<<~OUT)
285
289
  "origin"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kapusta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgenii Morozov
@@ -68,6 +68,7 @@ files:
68
68
  - examples/pangram.kap
69
69
  - examples/pcall.kap
70
70
  - examples/pipeline.kap
71
+ - examples/pivot-index.kap
71
72
  - examples/plus-one.kap
72
73
  - examples/points.kap
73
74
  - examples/primes.kap