ToFunction

From Esolang
Jump to navigation Jump to search

ToFunction is a programming language that inspired by Thue, and 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
//...

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

def run_tofunction(code):
    rules = []
    limit = None
    input_str = ""
    
    lines = code.split('\n')
    clean_code = ""
    for line in lines:
        if '//' in line:
            line = line[:line.find('//')]
        clean_code += line + " "
        
    statements = clean_code.split(';')
    for stmt in statements:
        stmt = stmt.strip()
        if not stmt:
            continue
        if stmt.startswith("define"):
            match = re.search(r'define\s+"(.*?)"\s*(?:=>|=)\s*"(.*?)"', stmt)
            if match:
                rules.append((match.group(1), match.group(2)))
        elif stmt.startswith("input"):
            match = re.search(r'input\s+"(.*?)"', stmt)
            if match:
                input_str = match.group(1)
        elif stmt.startswith("limit"):
            match = re.search(r'limit\s+"(.*?)"', stmt)
            if match:
                limit = match.group(1)

    current_str = input_str
    prev_term_str = None
    term_count = 1

    print(f"Initial: {current_str}")

    while current_str != prev_term_str and term_count <= 1000:
        if limit is not None and current_str == limit:
            break
            
        prev_term_str = current_str
        
        for k, v in rules:
            idx = 0
            while True:
                idx = current_str.find(k, idx)
                if idx == -1:
                    break
                current_str = current_str[:idx] + v + current_str[idx+len(k):]
                print(f"[Term {term_count}] Step (Rule '{k}' => '{v}'): {current_str}")
                idx += len(v)
                
                if limit is not None and current_str == limit:
                    break
            if limit is not None and current_str == limit:
                break
        
        print(f"=== Term {term_count} Result: {current_str} ===")
        
        if limit is not None and current_str == limit:
            break
            
        term_count += 1

    if term_count > 1000:
        print("Term limit 1000 exceeded. Stopping.")

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