Harp (Archived)

From Esolang
(Redirected from Harp)
Jump to navigation Jump to search
This article is not detailed enough and needs to be expanded. Please help us by adding some more information.

Harp is an esoteric programming language.

Structure

Harp is a language with one instruction. Every Harp program asks for an input at the beginning. Every instruction from then on has two parameters, A and B, and does this:

  • If last input is equal to A, or A is the empty string
  • Ask for input
  • Go to B
  • Otherwise
  • Execute the next instruction

Interpreter

-- split a string into a table by a delimiter
function split(inputstr, sep)
        local t = {}
		local i = 1
		for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
			if str and t and i then
				t[i] = str
				i = i + 1
			end
		end
        return t
end

-- see if the file exists
function file_exists(file)
  local f = io.open(file, "rb")
  if f then f:close() end
  return f ~= nil
end

-- get all lines from a file
function lines_from(file)
  if not file_exists(file) then return {} end
  lines = {}
  for line in io.lines(file) do
    lines[#lines + 1] = line
  end
  return lines
end

function ask()
	return io.read()
end

print("Type in the name of a file.")
local code = lines_from(ask())

local words = {}

for i,j in pairs(code) do
	words[i] = split(j, " ")
end

local input = ask()

local i = 1
while i <= #code do
	if input == "exit" then
		print("DEBUG:", "quitting...")
		return
	end
	if input == words[i][1] or words[i][1] == "" then
		print("DEBUG:", "goto", words[i][2])
		input = ask()
		i = tonumber(words[i][2]) - 1
	elseif not words[i][2] then
		print("DEBUG:", "goto", words[i][1])
		input = ask()
		i = tonumber(words[i][1]) - 1
	end
	i = i + 1
end