GolfScript and IronRuby

| 1 Comment | No TrackBacks

As wonderfully useful as w0lf's online GolfScript tester is, I occasionally run up against its (necessary) limitations. As a result, I decided to try to get GolfScript running on a Windows machine with IronRuby and PowerShell. It required some small changes.

The first problem I ran into was that IronRuby's implementation of gets doesn't like nil for an argument. That's relatively easy to work around: File.read(ARGV[0]).

The second problem was stdin. When I run GolfScript under Linux I usually use "here strings", but PowerShell doesn't seem to support them. So I tried just supplying input from the terminal, and the entry of EOF seemed rather hit-and-miss. In the end I've decided to take a different approach and require a filename from which the input will be read.

The third problem stumped me, and I turned to w0lf for help. Due to a very odd bug in IronRuby, string string ? was always returning -1. w0lf's workaround is to append an empty string to the result of Array.pack.

Putting those three changes together, a patch to the official GolfScript interpreter is as follows:

--- golfscript.rb.orig
+++ golfscript.rb
@@ -272,7 +272,8 @@
                factory(to_s.inspect)
        end
        def to_s
-               @val.pack('C*')
+               # Workaround for IronRuby bug, thanks to w0lf
+               ''+@val.pack('C*')
        end
        def class_id; 2; end
        def coerce(b)
@@ -389,8 +390,16 @@
        end
        include Comparable
 end
-code=gets(nil)||''
-$_=$stdin.isatty ? '' : $stdin.read
+
+# IronRuby's implementation of gets is buggy
+code=File.read(ARGV[0])
+
+# PowerShell's handling of stdin will drive you nuts
+$_=''
+if (ARGV[1])
+       $_=File.read(ARGV[1])
+end
+
 $stack = [Gstring.new($_)]
 $var_lookup={}

I hope this helps someone.

No TrackBacks

TrackBack URL: http://www.cheddarmonk.org/golfscriptblog/cgi-bin/mt/mt-tb.cgi/7

1 Comment

The second problem was stdin. When I run GolfScript under Linux I usually use "here strings", but PowerShell doesn't seem to support them. So I tried just supplying input from the terminal, and the entry of EOF seemed rather hit-and-miss. In the end I've decided to take a different approach and require a filename from which the input will be read.


maryland lottery

Leave a comment

About this Entry

This page contains a single entry by Peter published on June 25, 2013 10:29 PM.

Code dissection: polynomial extrapolation was the previous entry in this blog.

Find recent content on the main index or look in the archives to find all content.