07.04.2009

Numb Ers

So, today we will look at numbers in Babedepupi!

In the last post we used numbers as if they were a construct of the language, i.e. 432 would stand for fourhundredthirtytwo. While it's convenient, it's a special construct. Which is stupid and ugly!
Indeed we want numbers to be real objects. We can look at numbers being just sets with a certain number of elements. So we define:

UnaryNumber LinkedList
+ number
number iterate
add self

- number
number iterate
remove
* number
number iterate
+ self
print

iterate
result = result + "1"


print just prints the number in an unary representation. You might wonder, what self is. It is indeed the equivalent to the self of Smalltalk. While it looks like a keyword, we will see that we can define it in the language (?). This notation is of course quite unhandy, so we want at least to define binary numbers. We will bootstrap them with the UnaryNumbers. (A change to the syntax from the last time is that the last line of the method is the return value, so no need for a methodName = ... -statement (thx toon). Also I have to rethink blocks. Now they are just indendations without parameters. It would be cool to bootstrap as such that we don't need special syntax for classes and methods. But let's now close this parenthesis).

One UnaryNumber
new
UnaryNumber new
list add
0
BinaryNumber new
1
BinaryNumber new + one

0 = UnaryNumber new
1 = One new

BinaryNumber UnaryNumber
print
result = "0"
iterate
space = ""
result [ result count ] = "0" ?
result [ result count ] = "1"
else
result += "0"
realResult = ""
space = ""
result iterate
realResult = space + item
space = " "
0
* 1 + 1
1
* 1 + 1 + 1


Now we can redefine 0 and 1:

1 = BinaryNumber new  + 1
0 = BinaryNumber new


And finally we can do arithmetics with binary numbers:

1 0 1 0 1 0 +  1 0 1 1


Similarly to the above procedure we can implement decimal numbers. I won't do that here, because I'm lazy.

As I got from the comments from the last post, some people think, parentheses are so great (yeah, I mean you). We can add working parenthesis to our language by just defining them in the mother of all classes, Object:

Object
( expr )
expr


We have to introduce new syntax, naminly prefixes for methods. Notice the delay after the (. It designates a prefix. The next argument (here expr) is always self! So in this statement we just return self. With this definition we can write:

( ( 1 0 0 1 +  1 1 1 ) *  1 0 )


Now how do we model strings? We could do the same thing like with numbers, just defining characters as global variables and being able to concatenate them (for instance 'a' 'b' would be the messaga 'b' sent to the variable 'a', obviously representing the character a). But I don't see a purely object oriented way of representing characters themselves - at most as numbers (?).

Next time, we will bootstrap the system, or something. You know that crazy stuff with the meta thing! And the fireworks.

That's all, have a good N 1 0 0 0.

PS: Performance? What?