RETURN

From Esolang
Jump to navigation Jump to search

RETURN is an esoteric programming language made by Ben Russell (the third one by this author so far), which incorporates a new theory, in which all commands are blank functions, that call other blank functions, and the commands are called by the number of functions passed through a function. It is called RETURN because commands are executed depending on the return codes, effectively.

Specification

The memory allocation is the same as in Brainfuck: 8-bits, planar.

The only symbols used are the two standard brackets ( and ). Everything else is a comment.

Commands are executed by the number of bracket groups in the current bracket group, e.g.

(
 (()()())
 (()()()()())
 (()()())
)

would do this:

->
 subtract 1
 move pointer right
 subtract 1
subtract 1

Commands

1. Add 1
3. Subtract 1
5. Move pointer right
7. Move pointer left
9. Put character (optional)
11. Put number
13. Get character (optional)
15. Get number
17. While nonzero repeat what's in the next group of brackets
19. If nonzero skip next group of brackets
21. While zero repeat what's in the next group of brackets
23. If zero skip next group of brackets
25. Exit with return code 0
27. Exit with return code defined at pointer

All other numbers do nothing.

Examples

Hello World

Hello World!
By thematrixeatsyou.
(())(())(())(())(())(())(())(())
(()()()()()()()()()()()()()()()()())
(
  (()()()()())
  ((())(())(())(())(())(())(())(())(())())
  (()()()()())
  ((())(())(())(()))
  (()()()()())
  (
   (())(())(())(())(())
   (())(())(())(())(())
   (())(())(())()
  )
  (
   (()()()()()()())
   (()()()()()()())
   (()()()()()()())
  )
)
(()()()()())
(()()()()()()()()())
((()()()()())(()()()()())())
(
 (()()())
 (()()())
 ()()()()()()()
)
(((((((())))))))
(()()()()()()()()())
(()()()()()()()()())
(((())))
(()()()()()()()()())
(()()()()()()())
(()()()()()()()()())
(()()()()()()())
(((((((((((((((())))))))))))))))
(()()()()()()()()())
((()()()()())(()()()()())()()()()()()())
((((())))()()()()()()()())
(
 (()()())
 (()()())
 (()()())
)(
 (()()())
 (()()())
 ()()()()()()()
)
((
  (()()())
  (()()())
  (()()())
 )(
  (()()())
  (()()())
  (()()())
 )()()()()()()())
((()()()()()()())(())()()()()()()())
(()()()()())(()()()()())((((((((((()))))))))))
(()()()()()()()()())
((((())))()()()()()()()())
Long enough for you?

This can also be expressed without whitespace; it looks damn awful when it's taken out.

Python Interpreter

This interpreter takes the input file, strips all non-bracket characters, puts an "A" after every opening bracket, puts an "A" at the start, puts ".run(Tape())" at the end, and then runs it. "A" is a special class to parse the RETURN stream.

NOTE: This is not the original interpreter; that's probably long gone. If anything fails, please prod me. I coded this thing in about an hour. --Ben Russell 19:51, 6 February 2010 (UTC)

# Scary RETURN interpreter
# By Ben "GreaseMonkey" Russell, 2010
# Public domain.

import sys

global GETCHBUF
GETCHBUF = ""

def getch():
	while not GETCHBUF:
		GETCHBUF = raw_input("")
	
	c = GETCHBUF[0]
	GETCHBUF = GETCHBUF[1:]
	
	return c

def getint():
	v = 0
	while True:
		c = ord(getch()) - '0'
		if c >= 0 and c <= 9:
			v *= 10
			v += c
		else:
			break
	
	return v

class Tape:
	def __init__(self):
		self.l = [0]
		self.p = 0
	
	def left(self):
		self.p -= 1
		if self.p < 0:
			self.p = 0
	
	def right(self):
		self.p += 1
		while self.p >= len(self.l):
			self.l.append(0)
	
	def get(self):
		return self.l[self.p]

	def put(self, v):
		self.l[self.p] = int(v) & 0xFF

class A:
	def __init__(self, inv):
		self.cq = []
		self.cv = 0
		self.cp = None
		self.__call__(inv)
	
	def __call__(self, inv):
		if inv == self.__class__:
			self.cq.append(None)
		else:
			self.cq.append(inv)
			inv.cp = self
		
		self.cv += 1
		
		return self
	
	def run(self, tape, ci = 0, first = True):
		i = 0
		while i < len(self.cq):
			o = self.cq[i]
			if o:
				i = o.run(tape, i, False)
			i += 1
		
		if not first:
			i = ci
			q = self.cv
			if q == 1: # 1. Add 1 
				tape.put(tape.get()+1)
			elif q == 3: # 3. Subtract 1 
				tape.put(tape.get()-1)
			elif q == 5: # 5. Move pointer right 
				tape.right()
			elif q == 7: # 7. Move pointer left 
				tape.left()
			elif q == 9: # 9. Put character (optional) 
				sys.stdout.write(chr(tape.get()))
			elif q == 11: # 11. Put number 
				sys.stdout.write(tape.get())
			elif q == 13: # 13. Get character (optional) 
				tape.put(getch())
			elif q == 15: # 15. Get number 
				tape.put(getint())
			elif q == 17: # 17. While nonzero repeat what's in the next group of brackets
				i += 1
				o = self.cp.cq[i]
				while tape.get():
					if o:
						o.run(tape, False)
			elif q == 19: # 19. If nonzero skip next group of brackets 
				if not tape.get():
					i += 1
			elif q == 21: # 21. While zero repeat what's in the next group of brackets 
				i += 1
				o = self.cp.cq[i]
				while not tape.get():
					if o:
						o.run(tape, False)
			elif q == 23: # 23. If zero skip next group of brackets 
				if tape.get():
					i += 1
			elif q == 25: # 25. Exit with return code 0 
				sys.exit(0)
			elif q == 27: # 27. Exit with return code defined at pointer 
				sys.exit(tape.get())
		
			return i

fp = open(sys.argv[1],"r")
fdata = ""
for c in fp.read():
	if c == "(":
		fdata += "(A"
	elif c == ")":
		fdata += ")"

fdata = "A" + fdata + ".run(Tape())"

exec fdata

External resources

  • RETURN interpreter (dead link) this link is dead, if you've got the interpreter, I suggest you mirror it, otherwise, please prod me if the Python interpreter is crap --Ben Russell 19:51, 6 February 2010 (UTC)
    • This has been PD'd. Disregard the license, and do whatever you want with it. --Ben Russell 05:37, 12 August 2008 (UTC)