Stoppt die Vorratsdatenspeicherung! Jetzt klicken &handeln! Willst du auch an der Aktion teilnehmen? Hier findest du alle relevanten Infos
  und Materialien:

Skip to content


Parser generation with Ruby

I’ve started to play a little with Treetop, a parser generator library for Ruby.
Why? you might ask. While I’ve been dreaming of my own programming languages and designing them for quite some time now, I never really managed to actually get beyond the design-phase. I’ve come up with several language designs in the past, each time getting closer to something that I think really could work (which doesn’t mean, that it actually could be useful or ‘good’ – if that even makes sense).
While I’ve been looking at many different languages in the past, I’ve found the dynamic, object-oriented and functional high-level languages to be most interesting & fun to work with. Naturally, after working with Ruby alot, I’ve started looking a little bit at Smalltalk and how it differs to Ruby.

Inspired by what Smalltalk and Ruby have to give, I started designing my own language, heavily influenced by those two great, very dynamic, truly object-oriented programming languages and came up with a quite simple language named blockd.

The name is based upon the main principle of the language: Everything is an object, and in the end, all code is defined within blocks – that are ruby / smalltalk like blocks. Naming these blocks is done via simple assignment. So instead of having a huge amount of keywords, the language itself is pretty simple, because all we really need is a way to describe assignments, instantiate objects and easily create blocks to pass to methods or use in an assignment.

So in contrast to Ruby, where there are procs, lambdas and blocks, blockd only knows blocks, which basically behave like Smalltalk like blocks – they simply are anonymous procedures / blocks of code (or functions, if you want to call them that – although not pure as in Haskell).

The syntax is a mix of Ruby’s and Smalltalk’s with some minor tweaks I thought would be cool (or not? I don’t know ;) ).
To give you an example of how the language looks, here is a small snippet of some sample code, that actually doesn’t work yet but might do so in the future :)

?Download sample.blk
# this is just for demonstration
# there probably won't be the need to require the console module from the
# standard library every time you want to print something to the screen ;)
System require: "console"
 
# we support ruby-style blocks with do ... end and curly braces { ... }
File open: "test.txt" mode: "w" do |f|
  f puts: "what's up, dog?!"
  f puts: "crazy shit, yo!"
  f puts: "hahaha!!"
end
 
10 to: 0 do |i|
  Console puts: i
end
 
i = 0
(i < 10) while_true: do |i|
  Console puts: i
  i incr
end
 
numbers = [1,2,3,4,5] select: {|i| i < 3}
numbers each: do |i|
  puts i
end
 
(1 .. 100) each: {|i| Console puts: i}
 
squares = (1 .. 100) collect: {|i| i * i}
 
# define a square method
square = { |x|
  x * x
}
 
# or like this:
abs = { |num|
  (num > 0) if_true {
    return num
  }
 
  num * -1
}

I’m still tweaking the syntax and haven’t decided upon all things completely yet, but I guess I’ve found the pretty basic “feel” of it.

While I haven’t shown you any code that defines a class or modules, it still does show a little of the languages syntax. I’ve started the parser today and it successfully parses the code above, meaning, that since the syntax isn’t too complicated, I expect to be able to parse all the remaining stuff (classes & modules and some minor other things) pretty soon. I then intend to start writing the actual implementation – an interpreter written in Ruby. While I could come up with my own runtime, class library etc, I want to make use of the Ruby infrastructure as much as possible. For one reason, it makes it easier to actually implement something useful, on the other hand, since the language is very close to Ruby’s object model and semantics, I hope I can use most of Ruby’s code directly. This also would mean, that the interpreter could also run on other Ruby implementations like JRuby, IronRuby etc. And maybe, some day, it could also run natively compiled with the Ruby compiler I’ve been working on recently as well – but that’s still far into the future, since the compiler isn’t finished yet.

I guess that’s it for now. More to come soon :)

Posted in General. Tagged with , , , , , , , , , .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.


Ihr Browser versucht gerade eine Seite aus dem sogenannten Internet auszudrucken. Das Internet ist ein weltweites Netzwerk von Computern, das den Menschen ganz neue Möglichkeiten der Kommunikation bietet.

Da Politiker im Regelfall von neuen Dingen nichts verstehen, halten wir es für notwendig, sie davor zu schützen. Dies ist im beidseitigen Interesse, da unnötige Angstzustände bei Ihnen verhindert werden, ebenso wie es uns vor profilierungs- und machtsüchtigen Politikern schützt.

Sollten Sie der Meinung sein, dass Sie diese Internetseite dennoch sehen sollten, so können Sie jederzeit durch normalen Gebrauch eines Internetbrowsers darauf zugreifen. Dazu sind aber minimale Computerkenntnisse erforderlich. Sollten Sie diese nicht haben, vergessen Sie einfach dieses Internet und lassen uns in Ruhe.

Die Umgehung dieser Ausdrucksperre ist nach §95a UrhG verboten.

Mehr Informationen unter www.politiker-stopp.de.