Harp

From Esolang
Jump to: navigation, search
This article is a stub, which means that it is not detailed enough and needs to be expanded. Please help us by adding some more information.

The Harp programming language is an esoteric programming language by User:Rdococ. It aims to combine the worlds of logic and programming, in a new and unique way.

Structure

Harp is a one-instruction language. At the beginning of the program, it asks for input. Then, every instruction - which has two parameters A and B, and for which the syntax is simply the two parameters, separated by spaces - goes like this:

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

Computational Class

It is designed to be a finite state automaton, but you never know...

Interpreter

Harp interpreter in Lua. Copy into a text document and save as a lua file. Place it in the same directory as the file you want to run, enter the file's name when it asks, and then you can run the program. Output will tell you when the interpreter jumps to a different line. If you type 'exit', the program will be forced to quit, but that's only for safety purposes. The 'exit' feature shouldn't be there in an official interpreter release.

-- 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