How to resolve the algorithm Execute HQ9+ step by step in the Nanoquery programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Execute HQ9+ step by step in the Nanoquery programming language
Table of Contents
Problem Statement
Implement a HQ9+ interpreter or compiler.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Execute HQ9+ step by step in the Nanoquery programming language
Source code in the nanoquery programming language
import Nanoquery.IO
// a function to handle fatal errors
def fatal_error(errtext)
println "%" + errtext
println "usage: " + args[1] + " [filename.cp]"
exit(1)
end
// a function to perform '99 bottles of beer'
def bottles(n)
for bottles in range(n, 1, -1)
bottlestr = ""
if bottles = 1
bottlestr = "bottle"
else
bottlestr = "bottles"
end if
println (bottles + " " + bottlestr + " of beer on the wall")
println (bottles + " " + bottlestr + " of beer")
println "Take one down, pass it around."
if !(bottles = 2)
println (bottles - 1 + " bottles of beer on the wall.\n")
else
println "1 bottle of beer on the wall.\n"
end if
end for
end
// get a filename from the command line and read the file in
fname = null
source = null
try
fname = args[2]
source = new(Nanoquery.IO.File, fname).readAll()
catch
fatal_error("error while trying to read from specified file")
end
// define an int to be the accumulator
accum = 0
// interpreter the hq9+
for char in source
if char = "h"
println "hello world!"
else if char = "q"
println source
else if char = "9"
bottles(99)
else if char = "+"
accum += 1
end
end
You may also check:How to resolve the algorithm Anagrams step by step in the APL programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Fortran programming language
You may also check:How to resolve the algorithm Gapful numbers step by step in the Groovy programming language
You may also check:How to resolve the algorithm Semordnilap step by step in the Racket programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Delphi programming language