Wsrb is a language to write whitespace easily. It is a subset of Ruby syntactically. It is almost a subset of Ruby semantically.
# test.ws.rb
def calc(x, y)
x + y
end
put_as_number(calc(1, 2))
$ akaza wsrb test.ws.rb > test.ws
$ akaza exec test.ws # => 3
Wsrb has methods that are not included in Ruby.
get_as_char
get_as_number
put_as_char
put_as_number
Wsrb does not distinguish between character and integer, like C language.
You can define method at the top level.
def put_3_times(ch)
put_as_char ch
put_as_char ch
put_as_char ch
end
put_3_times('a') # => aaa
It only allows argument without default value. It also does not allow keyword argument, rest argument, and so on.
Arguments number is not checked.
You can define instance methods for existing classes, that are Array
, Hash
and Integer
.
class Hash
def fetch(key)
self[key]
end
end
More examples: https://github.com/pocke/akaza/blob/master/lib/akaza/ruby2ws/prelude.rb That is built-in methods definitions with the extending class feature.
Note: You cannot extend other classes, such as TrueClass
.
You can use Integer and Character literal. Character literal is a String literal, but it has only one character. And character will be converted to Integer that is based on code point implicitly.
put_as_char 'A' # => A
put_as_number 'A' # => 65
put_as_number 42 # => 42
You can use Array
Must not access to out of range of array.
x = [1, 2, 3]
put_as_number x[0]
It support only a few methods.
push
unshift
[]
- It does not support negative index. Use
array[array.size - 1]
instead ofarray[-1]
. (Implementing pull request is welcome!)
- It does not support negative index. Use
[]=
- It works only with existing index. Use
Array#push
to add a value to not initialized place.
- It works only with existing index. Use
first
size
You can use Hash.
x = {
1 => 2,
3 => 4,
}
x[5] = 6
put_as_number x[1] # => 2
x[100] # => nil
It supports only a few methods.
[]
[]=
You can use local variables.
x = 1
def foo
x = 2
put_as_number x
end
foo # => 2
put_as_number x # => 1
You can use if
and unless
.
x = 0
if x == 0
put_as_number x
else
put_as_number 2
end
put_as_number 3 unless x < 0
You can use while
.
x = -10
while x < 0
put_as_number x
x = x + 1
end
You can use operators, that are +
, -
, *
, /
and %
.
put_as_number 1 + 2
put_as_number 1 - 2
put_as_number 1 * 2
put_as_number 4 / 2
put_as_number 10 % 3
You can use raise
method to raise an exception.
raise "This is error message"
Program will exit by raise
method with error message.
But the exit status is 0
.
An Array object uses three heap.
- address to the first item
- Array size
- capacity
It is implemented with Hash table and use chaining to resolve collision.
A Hash object uses one heap.
- address to the first item
An item uses three heaps.
- key
- value
- address to the next chain
On initialize, it reserve HASH_SIZE * 3
heaps. And it sets NONE
to all keys.
Value and address is not initialized. Do not access these heaps before initializing.