ToFunction

From Esolang
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.

ToFunction is a programming language that uses only 3 commands and only uses a string variable type by User:EsolangerII

Syntax

There are only 3 syntax in ToFunction.

Function

It starts with a "define". and needed a function.

define "inputvalue" => "outputvalue";

The First string ('x' => x) is an input value. The second string (x => 'x') is an output value.

When "applied", each character or string is replaced with its corresponding "definition". It checks every combination of characters until there are no more combinations

Input

starts with "input"

input "hello";

comments

starts with //

//Hello!

Output

The output infinitely recurs when the output limit is not set, or the previous output and the current output are different. If a limit is set, and when the output is directly equal to the limit, then it stops. setting limit:

limit "1";

Step-By-Step Exmaples

define "x" => "Qwerty";

This rule changes every x to Qwerty.

input "x x xx";

The rule is applied to matching patterns from left to right.

//output in step 1 : Qwerty x xx
//output in step 2 : Qwerty Qwerty xx
//output in step 3 : Qwerty Qwerty Qwertyx
//output in step 4 : Qwerty Qwerty QwertyQwerty
define "En" => "E";

This rule erases an n that immediately follows an E. If the input is En, it will become E right away.

input "En";
//output in step 1 : E
//stops

If the input is Enn, it will still become E within a single term by chaining steps.

input "Enn";
//output in step 1 : En (there is still En)
//output in step 2 : E
define "nE" = "E";

This works same as En => E

Step vs Term

step: A single instance of text substitution. A step occurs when the interpreter finds the first match for the current rule and replaces it. Because a rule must be applied repeatedly until it is exhausted, multiple steps can occur consecutively for the same rule before the interpreter moves on to the next rule in a term. term: A single complete pass through the entire set of defined rules. During a term, each rule is evaluated sequentially from top to bottom. For each rule, the interpreter must repeatedly apply the transformation until no more matches for that specific rule exist in the current string (always scanning from the beginning of the string after each substitution). Only after a rule is completely exhausted does the interpreter move to the next rule in the list.

Examples

Simple Examples

Hello World

input "Hello, World!";
limit "Hello, World!";

Duplicator

define "x" => "xx";
input "x";
//output in term 1 : xx
//output in term 2 : xxxx
//output in term 3 : xxxxxxxx
//...

Dotmaker

define "x" => ".x";
input "x";
//output in term 1 : .x
//output in term 2 : ..x
//output in term 3 : ...x
//...

Looper

//Example of order matters
define "x" => "z";
define "y" => "x";
define "z" => "y";
input "x";
//output in term 1 : y
//output in term 2 : x
//output in term 3 : y
//...

Looper(wrong)

//Example of order matters
define "x" => "z";
define "z" => "y";
define "y" => "x";
input "x";
//output in term 1 : x
//output in term 2 : x
//...

Dotmaker in two term(looper + dotmaker)

define "x" => "z";
define "y" => "x";
define "z" => "y";
define "xE" => ".xE";
input "xE";
//output in term 1 : yE
//output in term 2 : .xE
//output in term 3 : .yE
//output in term 4 : ..xE
//...

Swapper(binary)

define "C0" => "1C";
define "C1" => "0C";
//output in term 1 : 0C01
//output in term 2 : 010C
//stops

Complex Examples

Addition

//makes a number to x
define "1" => "x";
define "2" => "xx";
define "3" => "xxx";
define "4" => "xxxx";
//delete spaces that are near the plus sign
define "+ " => "+";
define " +" => "+";
//adds a number and pushes the plus sign
define "x+x" => "xx+";
//space works as a timer
define " E" => "E";
//it deletes the plus sign
define "+E" => "E";
//change x to numeral form and set limits.
define "xxxxxxxxE" => "8E";
limit "8E";
define "xxxxxxxE" => "7E";
limit "7E";
define "xxxxxxE" => "6E";
limit "6E";
define "xxxxxE" => "5E";
limit "5E";
define "xxxxE" => "4E";
limit "4E";
define "xxxE" => "3E";
limit "3E";
define "xxE" => "2E";
limit "2E";
define "xE" => "1E";
limit "1E";
//input
input "1 + 2  E";
//output in term 1 : x+xx  E
//output in term 2 : 3E
//stops

Multiplication

//timer + "*" eraser preset
define "P" => "ff  E";
//defining numbers
//added A, B, C, D to find what number is behind "*"
define "1" => "xA";
define "2" => "xxB";
define "3" => "xxxC";
define "4" => "xxxxD";
//same pupose at addition
define "* " => "*";
define " *" => "*";
//multiplication.
//copying number behind and move "*" to front
define "A*x" => "xA*";
define "B*x" => "xxB*";
define "C*x" => "xxxC*";
define "D*x" => "xxxxD*";
//erasing "*"
define "A*f" => "";
define "B*f" => "";
define "C*f" => "";
define "D*f" => "";
define "xAf" => "";
define "xBf" => "";
define "xCf" => "";
define "xDf" => "";
//same purpose as addition
define " E" => "E";
//same pupose as addition
define "xxxxxxxxxxxxxxxxE" => "16E";
limit "16E";
define "xxxxxxxxxxxxxxxE" => "15E";
limit "15E";
define "xxxxxxxxxxxxxxE" => "14E";
limit "14E";
define "xxxxxxxxxxxxxE" => "13E";
limit "13E";
define "xxxxxxxxxxxxE" => "12E";
limit "12E";
define "xxxxxxxxxxxE" => "11E";
limit "11E";
define "xxxxxxxxxxE" => "10E";
limit "10E";
define "xxxxxxxxxE" => "9E";
limit "9E";
define "xxxxxxxxE" => "8E";
limit "8E";
define "xxxxxxxE" => "7E";
limit "7E";
define "xxxxxxE" => "6E";
limit "6E";
define "xxxxxE" => "5E";
limit "5E";
define "xxxxE" => "4E";
limit "4E";
define "xxxE" => "3E";
limit "3E";
define "xxE" => "2E";
limit "2E";
define "xE" => "1E";
limit "1E";
input "3 * 4P";
//output in term 1 : xxxxxxC*xxf  E
//output in term 2 : xxxxxxxxxC*xf E
//output in term 3 : 12E

Interpreters/Compilers

Lua

Python

Used AI(it might wrong)

import re

class ToFunctionInterpreter:
    def __init__(self):
        self.rules = []
        self.state_string = ""

    def parse_and_run(self, source_code):
        self.rules = []
        self.state_string = ""
        
        for line in source_code.split('\n'):
            line = line.strip()
            if not line or line.startswith("//"):
                continue
                
            define_match = re.match(r'define\s+"(.*?)"\s*=>\s*"(.*?)";', line)
            if define_match:
                self.rules.append((define_match.group(1), define_match.group(2)))
                continue
                
            input_match = re.match(r'input\s+"(.*?)";', line)
            if input_match:
                self.state_string = input_match.group(1)
                continue

        term_changed = True
        
        while term_changed:
            term_changed = False
            
            for target, replacement in self.rules:
                while target in self.state_string:
                    self.state_string = self.state_string.replace(target, replacement, 1)
                    term_changed = True
                    
        return self.state_string

if __name__ == "__main__":
    tofunction_code = """
    define "a" => "b";
    define "b" => "c";
    
    input "a b a";
    """
    
    interpreter = ToFunctionInterpreter()
    print(interpreter.parse_and_run(tofunction_code))

Interpreter needed. Feel free to contribute if you can implement one