Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
rubyish - handle revised nqp::for iteration on hashes
nqp::for on a hash object now returns an iterator object. nqp::iter_key and
nqp::iter_val are used to extract the key and value respectively.
  • Loading branch information
dwarring committed Sep 2, 2014
1 parent 883e264 commit d1f23dd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
9 changes: 8 additions & 1 deletion examples/rubyish/README.md
Expand Up @@ -27,7 +27,7 @@ Implemented:
- statement modifiers `if` `unless`, `while`, `until` e.g.: `puts 42 if true`
- basic arrays and hashes
- for loops on arrays: `for val in [10, 20, 30] do puts val end`
- for loops on hash keys: `h = {'a'=>10, 'b'=>20}; for k in h do puts h{k} end`
- for loops on hash pairs: `h = {'a'=>10, 'b'=>20}; for kv in h do puts value k end`
- lambda blocks/closures: `def make_counter(n,incr) ; n-=incr; lambda { n += incr }; end`
- lightweight eRuby like templating, see [template.rbi](examples-rubyish/template.rbi)
- heredocs, literal `<<EOF ... EOF` and interpolating `<<"END" ... END`
Expand Down Expand Up @@ -59,6 +59,13 @@ Strings and truth values are Perlish rather than Rubyish:
- hash dereferencing is via angle braces: `puts fruit<apples>` or
curlies `puts fruit{'bananas'}`

hash iteration is by pairs. The `key` and `value` built-ins are used for dereferencing
```
for item in {"apples" => 20, "bananas" => 35, "potatos" => 12}
puts" #{key item} are #{value item} cents per Kg"
end
```

nqp op-codes can be called like regular functions. E.g.
```
puts nqp::if(2+2 == 4, 'yup', 'nope' )
Expand Down
7 changes: 5 additions & 2 deletions examples/rubyish/rubyish.nqp
Expand Up @@ -71,8 +71,8 @@ grammar Rubyish::Grammar is HLL::Grammar {
[ <stmt=.stmtish>? ] *%% [<.separator>|<stmt=.template-chunk>]
}

token stmtish {
<stmt> [:s<hs> <modifier> <EXPR>]?
token stmtish {:s<hs>
<stmt> [ <modifier> <EXPR>]?
}

token modifier {if|unless|while|until}
Expand Down Expand Up @@ -147,6 +147,9 @@ grammar Rubyish::Grammar is HLL::Grammar {
%builtins := nqp::hash(
'abort', 'die',
'exit', 'exit',
'key', 'iterkey_s',
'delete', 'deletekey',
'value', 'iterval',
'print', 'print',
'puts', 'say',
'sleep', 'sleep',
Expand Down
10 changes: 5 additions & 5 deletions examples/rubyish/t/hashs.t
Expand Up @@ -7,19 +7,19 @@ h_idx = 'c'
puts "#{h{h_idx} == 30? 'ok' : 'nok'} 3 - h{c_idx}"
puts "#{h{'d'} == 100? 'ok' : 'nok'} 4 - h{'d'}"

nqp::deletekey(h, 'd')
delete(h, 'd')

for key in h do
puts "ok #{ h{key} / 10 + 4 } - hash key iteration (#{key})"
for kv in h do
puts "ok #{value(kv) / 10 + 4 } - hash key iteration (#{key kv})"
end

h<d> = ['nok','ok']
puts "#{ h<d>[1] } 8 - HoA"

def test_hash_args(x, h_args)
puts "ok #{ x } - fixed arg"
for k in h_args do
puts "ok #{ h_args{k} } - hash slurpy arg (#{k})"
for kv in h_args do
puts "ok #{ value kv } - hash slurpy arg (#{key kv})"
end
end

Expand Down

0 comments on commit d1f23dd

Please sign in to comment.