Execute HQ9+/Ursa

From Rosetta Code
Execute HQ9+/Ursa is part of RCHQ9+. You may find other members of RCHQ9+ at Category:RCHQ9+.
Execute HQ9+/Ursa is an implementation of HQ9+. Other implementations of HQ9+.

This is an HQ9+ interpreter written in Standard Ursa. It expects the filename of an HQ9+ source file as a command-line argument. <lang ursa>#

  1. hq9+ interpreter in ursa
  1. check if a file was provided on the command line; if not, stop

if (> (size args) 2) out "error: no file provided" endl console end if

  1. safely open the file by creating it first, then read it

decl file f f.create args<1> f.open args<1> decl string hq9 set hq9 (f.readall) f.close

  1. interpret the hq9+
  2. define an int to be the accumlator (even though this isn't technically
  3. an accumulator)

decl int accum for (decl int i) (< i (size hq9)) (inc i) if (= (lower hq9) "h") out "hello world!" endl console end if if (= (lower hq9) "q") out hq9 console end if if (= hq9 "9") decl int bottles decl string bottlestr

for (set bottles 99) (> bottles 0) (dec bottles) if (= bottles 1) set bottlestr "bottle" else set bottlestr "bottles" end if

out bottles " " bottlestr " of beer on the wall" endl console out bottles " " bottlestr " of beer" endl console out "Take one down, pass it around." endl console

if (not (= bottles 2)) out (int (- bottles 1)) " bottles of beer on the wall." endl endl console else

               		out (int (- bottles 1)) " bottle of beer on the wall." endl endl console

end if end for end if if (= hq9 "+") set accum (int (+ accum 1)) end if end for</lang>