8
2
Seem like GolfScript wins all of these. So you can't beat them, join them.
Write an Self-Contained Golfscript Interpreter
I'm using the definition of self-contained to mean, a single program:- So no passing the buck to some external program to do the work for you.
Test Cases:
Strings and blocks are represented as lists of ASCII codes. Types are not checked with these tests, but should be right anyway.
test("[50] [60]+", [[50, 60]]);
test("{a} {b}+", [[97, 32, 98]]);
test("'a' 'b'+", [[97, 98]]);
test("' ' 0+", [[32, 48]]);
test("' ' [50]+", [[32, 50]]);
test("{a} 0+", [[97, 32, 48]]);
test("{a} [50]+", [[97, 32, 53, 48]]);
test("5 ~", [-6]);
test('"1 2+"~', [3]);
test('{1 2+}~', [3]);
test('[1 2 3]~', [1, 2, 3]);
test('1`', [[49]]);
test("[1 [2] 'asdf']`", [[91, 49, 32, 91, 50, 93, 32, 34, 97, 115, 100, 102, 34, 93]]);
test('"1"`', [[34, 49, 34]]);
test("{1}`", [[123, 49, 125]]);
test("0!", [1]);
test("[]!", [1]);
test("{}!", [1]);
test("''!", [1]);
test("5!", [0]);
test("[[]]!", [0]);
test("{{}}!", [0]);
test("'asdf'!", [0]);
test("1 2 3 4 @", [1, 3, 4, 2]);
test("1 # 2", [1]);
test("1 2 3 4 5 1 $", [1, 2, 3, 4, 5, 4]);
test("'asdf' $", [[97, 100, 102, 115]]);
test("[5 4 3 1 2]{-1*}$", [[5, 4, 3, 2, 1]]);
test("5 7 +", [12]);
test("'a'{b}+", [[97, 32, 98]]);
test("[1 2 3][4 5]+", [[1, 2, 3, 4, 5]]);
test("1 2-3+", [1, -1]);
test("1 2 -3+", [1, -1]);
test("1 2- 3+", [2]);
test("[5 2 5 4 1 1][1 2]-", [[5, 5, 4]]);
test("2 4*", [8]);
test("2 {2*} 5*", [64]);
test("[1 2 3]2*", [[1, 2, 3, 1, 2, 3]]);
test("3'asdf'*", [[97,115,100,102,97,115,100,102,97,115,100,102]]);
test("[1 2 3]' '*", [[49, 32, 50, 32, 51]]);
test("[1 2 3][4]*", [[1,4,2,4,3]]);
test("'asdf'' '*", [[97,32,115,32,100,32,102]]);
test("[1 [2] [3 [4 [5]]]]' '*", [[49, 32, 2, 32, 3, 4, 5]]);
test("[1 [2] [3 [4 [5]]]][6 7]*", [[1, 6, 7, 2, 6, 7, 3, [4, [5]]]]);
test("[1 2 3 4]{+}*", [10]);
test("'asdf'{+}*", [414]);
test("7 3 /", [2]);
test("[1 2 3 4 2 3 5][2 3]/", [[[1], [4], [5]]]);
test("[1 2 3 4 5] 2/", [[[1, 2], [3, 4], [5]]]);
test("0 1 {10<} { .@+ } /", [8, [1, 1, 2, 3, 5, 8]]);
test("[1 2 3]{1+}/", [2, 3, 4]);
test("7 3 %", [1]);
test("'assdfs' 's'%", [[[97], [100, 102]]]);
test("'assdfs' 's'/", [[[97], [], [100, 102], []]]);
test("[1 2 3 4 5] 2%", [[1, 3, 5]]);
test("[1 2 3 4 5] -1%", [[5, 4, 3, 2, 1]]);
test("[1 2 3] {1+}%", [[2, 3, 4]]);
test("5 3 |", [7]);
test("[5 5 1] [1 3] |", [[5, 1, 3]]);
test("5 3 &", [1]);
test("[1 1 2 2][1 3]&", [[1]]);
test("5 3 ^", [6]);
test("[1 1 2 2][1 3]^", [[2, 3]]);
test("1 2 [\\]", [[2, 1]]);
test("1 2 3 \\", [1, 3, 2]);
test("1 2 3; ", [1, 2]);
test("3 4 <", [1]);
test('"asdf" "asdg" <', [1]);
test("[1 2 3] 2 <", [[1, 2]]);
test("{asdf} -1 <", [[97, 115, 100]]);
test("3 4 >", [0]);
test('"asdf" "asdg" >', [0]);
test("[1 2 3] 2 >", [[3]]);
test("{asdf} -1 >", [[102]]);
test("3 4 =", [0]);
test('"asdf" "asdg" =', [0]);
test("[1 2 3] 2 =", [3]);
test("{asdf} -1 =", [102]);
test("3,", [[0,1,2]]);
test("10,,", [10]);
test("10,{3%},", [[1, 2, 4, 5, 7, 8]]);
test("1 2 .", [1,2,2]);
test("2 8?", [256]);
test(" 5 [4 3 5 1] ?", [2]);
test(" 6 [4 3 5 1] ?", [-1]);
test("[1 2 3 4 5 6] {.* 20>} ?", [5]);
test("5(", [4]);
test("[1 2 3](", [[2, 3], 1]);
test("5)", [6]);
test("[1 2 3])", [[1, 2], 3]);
test("5 {1 0/} or", [5]);
test("5 {1 1+} and", [2]);
test("0 [3] xor", [[3]]);
test("2 [3] xor", [0]);
test("5{1-..}do", [4, 3, 2, 1, 0, 0]);
test("5{.}{1-.}while", [4, 3, 2, 1, 0, 0]);
test("5{.}{1-.}until", [5]);
test("1 2 3 if", [2]);
test("0 2 {1.} if", [1, 1]);
test("[[1 2 3][4 5 6][7 8 9]]zip", [[[1, 4, 7], [2, 5, 8], [3, 6, 9]]]);
test("[1 1 0] 2 base", [6]);
test("6 2 base", [[1, 1, 0]]);
4Warning to anyone who would try this: it's a very big task if you're not using a language closely related to Ruby. – Peter Taylor – 2012-09-03T19:47:27.927
@PeterTaylor Where would you say the best documentation is for someone attempting this task? Is it the golfscript.com site, or would it be better to just dig into the golfscript.rb source itself? – Gareth – 2012-09-03T21:05:58.457
2Would you get extra points for writing it in Golfscript? – Mr Lister – 2012-09-03T21:54:12.777
@Gareth, http://www.golfscript.com/golfscript/builtin.html and the source.
– Peter Taylor – 2012-09-04T06:39:31.1973You know, for a task this complex the question-asker really ought to post a good set of test cases... – Peter Taylor – 2012-09-04T06:40:06.113
4Do we have to support ruby's string evaluation, like
"The time is #{Time.now}"
? How about arbitrary precision numbers ? – copy – 2012-09-04T14:41:55.780