User:I am islptng/Draft

From Esolang
Jump to navigation Jump to search

fCompiler is a language designed by islptng.

This is still a work in progress. It may be changed in the future.
fCompiler
Designed by User:I am islptng
Appeared in 2023
Computational class Metalanguage
Reference implementation Python
Influenced by Subleq
File extension(s) .txt

Intro

When you play some games that require you to write large assembly programs, won't you feel bad?
This is what that come to my mind when I was playing the game "Turing Complete" and writting Subleq programs.

However, users must build up an environment by themselves -- There's NOT any predefined functions!
This allows for flexibility -- It, unlike those who can only compile to machine code, can compile to anything if you want!

Syntax

Keywords

Initially, there's only 2 keywords: "def" and "const".

"const"'s usage:

const a 1

Defines a constant named "a" with value 1. "def"'s usage:

def name ->
   ; some code here
<>

Defines a function. You can use "%1%" to represent the 1st argument, "%2%" to represent the 2nd argument, and so on.
%u will be replaced by a unique ID that, on each function call, returns different value. The ids are "no1uid","no2uid"...

Comments

We have only one form of comments: ;. From the semicolon, to the next line break, is comment.

Compile to...

It depends on what you write after an exclamation mark. for example, !Hello, world! causes the interpreter to print "Hello, world!".

Examples

Hello, world!

!Hello, world!

Interpreter

src = []

while True:
	try:
		ins = input().split(";")[0].strip()
		if ins == "": continue
		src.append(ins)
	except: break
def repsplit(x):
	if x[0] == "!": return [x]
	while "  " in x: x = x.replace("  "," ")
	return x.split()
src = [repsplit(i) for i in src]
def recsubst(x,f,t):
	if x == f: return t
	if not isinstance(x,list): return x
	return [recsubst(i,f,t) for i in x]

functions = {}
keywords = {}

class struct:
	def __init__(self,code):
		self.code = code
		if code[0][0] == "def":
			functions[code[0][1]] = self.code[1:]
		elif code[0][0] == "key":
			curindex = code[0][0]
			res = {curindex:[]}
			for i in code[1:]:
				if i[0] == "<-":
					try: curindex = i[1]
					except: curindex = "<>"
					res[curindex] = []
				else: res[curindex].append(i)
	__str__ = lambda s: "<"+str(s.code)+">"
	__repr__ = __str__
	def __call__(self,uid):
		try: kwd = keywords[self.code[0][0]]
		except: raise SyntaxError("Unknown keyword!")
		res = self.code
		for i in kwd: res = recsubst(res,i,kwd[i])
		for i in range(1,len(self.code[0])):
			res = recsubst(res,"%"+str(i)+"%",self.code[0][i])
		return res

def rewrite(n):
	res = []
	substruct = []
	depth = 0
	for i in n:
		if i[-1] == "->":
			depth += 1
		elif i[0] == "<>":
			depth -= 1
			if depth == 0:
				n = struct([substruct[0]]+rewrite(substruct[1:]))
				if n.code[0][0] in ["def","key"]: continue
				res.append(n)
				substruct = []
				continue
		if depth > 0: substruct.append(i)
		else: res.append(i)
	if depth != 0: raise SyntaxError("Unmatched -> and <>")
	return res

print(rewrite(src),functions,keywords)