The best kittens, technology, and video games blog in the world.

Saturday, May 12, 2007

OpenGL, YAML, JSON and other small things for RLisp

Do With Me As You Will.... by Mr. Greenjeans from flickr (CC-NC-SA)Some libraries for RLisp warranted a DSL, like Gtk and Unit testing, or even extending the implementation like regular expressions. But plenty of Ruby libraries just work, and are very convenient to use.

Things described here are available in examples/ directory in RLisp sources.


(ruby-require "yaml")

(let rlisp-value (list
1 2 "foo" (hash
hello: 42
world: 56

(let yaml-value [rlisp-value to_yaml])

(print "Original value:")
(print [rlisp-value inspect_lisp])
(print "As YAML:")
(print yaml-value)
(print "Parsed back:")
(print [[YAML load yaml-value] inspect_lisp])
In action:
Original value:
(1 2 "foo" {:hello=>42, :world=>56})
- 1
- 2
- foo
- :hello: 42
:world: 56

Parsed back:
(1 2 "foo" {:hello=>42, :world=>56})


(ruby-require "rubygems")
(ruby-require "json")

(let rlisp-value (list
1 2 "foo" (hash
hello: 42
world: 56

(let json-value [rlisp-value to_json])

(print "Original value:")
(print [rlisp-value inspect_lisp])
(print "As JSON:")
(print json-value)
(print "Parsed back:")
(print [[JSON parse json-value] inspect_lisp])

In action:
Original value:
(1 2 "foo" {:world=>56, :hello=>42})
Parsed back:
(1 2 "foo" {"world"=>56, "hello"=>42})

JSON changes symbols into strings, so some information is lost in the round-trip.

MD5 and SHA1

(ruby-require "digest/md5")
(ruby-require "digest/sha1")

(defun md5 (val) [Digest::MD5 hexdigest val])
(defun sha1 (val) [Digest::SHA1 hexdigest val])

(let val "Foo")
(print "MD5 of #{val} is #{(md5 val)}")
(print "SHA1 of #{val} is #{(sha1 val)}")
In action:
MD5 of Foo is 1356c67d7ad1638d816bfb822dd2c25d
SHA1 of Foo is 201a6b3053cc1422d2c3670b62616221d2290929


(ruby-require "opengl")
(ruby-require "glut")

(defun disp ()
[GL Color 0.0 0.0 1.0]
[GL Vertex 0 0]
[GL Color 0.0 1.0 0.0]
[GL Vertex 200 200]
[GL Color 1.0 0.0 0.0]
[GL Vertex 20 200]
[GL End]
[GL Flush]

(defun reshape (w h)
[GL Viewport 0 0 w h]
[GL LoadIdentity]
[GL Ortho 0 w 0 h -1 1]
[GL Scale 1 -1 1]
[GL Translate 0 (- h) 0]

[GLUT Init]
(let a [GLUT CreateWindow "single triangle"])
[GLUT DisplayFunc disp]
[GLUT ReshapeFunc reshape]
[GLUT MainLoop]
OpenGL is simply asking for some macros, but it will work fine without them too.

Filesystem access

(ruby-require "fileutils")

[FileUtils mkdir "foo"]
(system "ls -ld foo")
[FileUtils chmod 0750 "foo"]
(system "ls -ld foo")
[FileUtils rmdir "foo"]
In action:
drwx------ 2 taw taw 48 2007-05-12 11:19 foo
drwxr-x--- 2 taw taw 48 2007-05-12 11:19 foo

Other stuff

Most Ruby libraries should work with RLisp with little more than (ruby-require "lib").

A few may depend on things which are not well supported by RLisp yet. In such cases RLisp might need to change, or a bunch of macros providing a nice interfaces could be added. RLisp is very young, so everything is open to change.

No comments: