Permalink
Browse files

Added HQ9F++

  • Loading branch information...
1 parent 6f26913 commit 6970f0d8f4dc24f14d0db4bf0ae4450ba213dc9f @gatesphere committed May 4, 2012
@@ -0,0 +1,123 @@
+HQ9F++ Interpreter
+==================
+Io Codejam - 20120504
+
+
+Today's codejam is a bit of a special double-feature, in that I both define an
+extension to a programming language and provide a complete implementation of it!
+
+So, the language is HQ9F++, which is an extension of HQ9++, which itself is an
+extension of HQ9+. HQ9F++ is identical in every way to HQ9++, except for the
+addition of a new command: `F`.
+
+`F` is defined as follows:
+
+ Command Function
+ ==========================================================
+ F Do the FizzBuzz problem for the range 1 - 100
+
+
+So, the HQ9F++ interpreter works in two modes: REPL and scripted.
+
+To invoke the REPL, do the following:
+
+ ./hq9fplusplus.io
+
+To run as a scripting engine, do this:
+
+ ./hq9fplusplus.io test.hq9fpp
+
+Demo Time!
+==========
+
+REPL:
+
+ $ ./hqf9plusplus.io
+ Welcome to HQ9F++, version May 2012. To exit, enter "!!exit"
+ HQ9F++> h
+ Hello World.
+ HQ9F++> q
+ q
+ HQ9F++> qq
+ qq
+ qq
+ HQ9F++> hqh
+ Hello World.
+ hqh
+ Hello World.
+ HQ9F++> +
+ HQ9F++> ++
+ HQ9F++> +++
+ HQ9F++> qq++q+h
+ qq++q+h
+ qq++q+h
+ qq++q+h
+ Hello World.
+ HQ9F++> 9
+ 100 bottles of beer on the wall,
+ 100 bottles of beer
+ take one down, pass it around,
+ 99 bottles of beer on the wall.
+
+ 99 bottles of beer on the wall,
+ 99 bottles of beer
+ take one down, pass it around,
+ 98 bottles of beer on the wall.
+
+ <...snip...>
+
+ 2 bottles of beer on the wall,
+ 2 bottles of beer
+ take one down, pass it around,
+ 1 bottle of beer on the wall.
+
+ 1 bottle of beer on the wall,
+ 1 bottle of beer
+ take one down, pass it around,
+ No more bottles of beer on the wall.
+
+ HQ9F++> f
+ 1
+ 2
+ Fizz
+ 4
+ Buzz
+ Fizz
+ 7
+ 8
+ Fizz
+ Buzz
+ 11
+ Fizz
+ 13
+ 14
+ FizzBuzz
+ <...snip...>
+ 97
+ 98
+ Fizz
+ Buzz
+ HQ9F++> !!exit
+ Goodbye.
+
+You will notice that the REPL has ReadLine capability, and saves a history file.
+
+Scripting engine:
+
+ $ cat test.hq9fpp
+ 9f+++hhqq
+
+ $ ./hqf9plusplus.io test.hq9fpp
+ 100 bottles of beer on the wall,
+ <...snip...>
+ No more bottles of beer on the wall.
+
+ 1
+ <...snip...>
+ Buzz
+ Hello World.
+ Hello World.
+ 9f+++hhqq
+ 9f+++hhqq
+
+Enjoy!
@@ -0,0 +1,148 @@
+#!/usr/bin/env/ io
+
+// HQ9F++ interpreter
+// Jacob Peck
+// HQ9F++ is an extension of HQ9++, which is an extension of HQ9+
+
+VM := Object clone do(
+ clone := method(self)
+ accumulator := 0
+
+ h := method(
+ writeln("Hello World.")
+ self
+ )
+
+ q := method(in,
+ writeln(in)
+ self
+ )
+
+ nine := method(
+ 100 repeat(i,
+ num := 100 - i
+ bottles := "bottles"
+ if(num == 1, bottles := "bottle")
+ nextnum := num - 1
+ bottles2 := "bottles"
+ if(nextnum == 1, bottles2 = "bottle")
+ if(nextnum == 0, nextnum = "No more")
+ writeln("#{num} #{bottles} of beer on the wall,\n#{num} #{bottles} of beer" interpolate)
+ writeln("take one down, pass it around,")
+ writeln("#{nextnum} #{bottles2} of beer on the wall.\n" interpolate)
+ )
+ self
+ )
+
+ f := method(
+ for(i, 1, 100,
+ out := i
+ if(i % 3 == 0, out = "Fizz")
+ if(i % 5 == 0, out = "Buzz")
+ if(i % 15 == 0, out = "FizzBuzz")
+ writeln(out)
+ )
+ self
+ )
+
+ plus := method(
+ self accumulator = self accumulator + 1
+ self
+ )
+
+ plusplus := method(
+ self extend plus plus
+ self
+ )
+
+ extend := method(
+ name := ""
+ 8 repeat(
+ r := Random value(65, 116) round
+ if(r > 90, r = r + 6)
+ name asMutable appendSeq(r asCharacter)
+ )
+ name asMessage := Object clone
+ self
+ )
+
+ initialize := method(
+ self accumulator = 0
+ )
+)
+
+REPL := Object clone do(
+ rl := ReadLine
+ hist_file := ".hq9fplusplus_history"
+ version := "May 2012"
+
+ initialize := method(
+ VM initialize
+ self rl prompt = "HQ9F++> "
+ self read_history
+ self welcome
+ self
+ )
+
+ read_history := method(
+ try(
+ self rl loadHistory(self hist_file)
+ )
+ )
+
+ save_history := method(
+ try(
+ self rl saveHistory(self hist_file)
+ )
+ )
+
+ welcome := method(
+ writeln("Welcome to HQ9F++, version #{version}. To exit, enter \"!!exit\"" interpolate)
+ self
+ )
+
+ goodbye := method(
+ writeln("Goodbye.")
+ )
+
+ run := method(file,
+ if(file != nil,
+ try(
+ f := File with(file) openForReading
+ self interpret(f contents)
+ )
+ ,
+ self initialize
+ loop(
+ in := self rl readLine(self rl prompt)
+ if(in asUppercase asMutable strip == "!!EXIT", break)
+ self interpret(in)
+ self rl addHistory(in)
+ )
+ self save_history
+ self goodbye
+ )
+ )
+
+ interpret := method(in,
+ pc := 0
+ while(pc < in size,
+ x := in at(pc)
+ if(x asCharacter asUppercase == "H", VM h)
+ if(x asCharacter asUppercase == "Q", VM q(in))
+ if(x asCharacter asUppercase == "9", VM nine)
+ if(x asCharacter asUppercase == "F", VM f)
+ if(x asCharacter asUppercase == "+",
+ if(in at(pc + 1) != nil and in at(pc + 1) asCharacter asUppercase == "+",
+ VM plusplus
+ pc = pc + 1
+ ,
+ VM plus
+ )
+ )
+ pc = pc + 1
+ )
+ )
+)
+
+REPL run(System args at(1))
@@ -0,0 +1 @@
+9f+++hhqq

0 comments on commit 6970f0d

Please sign in to comment.