Expansion

From Esolang
Jump to navigation Jump to search

Expansion is an esolang invented by User:None1.

Syntax

source_string1=target_string1
source_string2=target_string2
...
source_stringn=target_stringn
*memory_string

Every source string must be surrounded by square brackets, and except the beginning and the end, there must be no square brackets, that means:

[]
[a]

Are valid source strings, but:

[
a]
]]
[b]c]
[]]

Are invalid source strings.

The only memory is the memory string, then, each turn, every substring surounded by brackets is replaced with the replacements, for example:

[b]=[a]
[a]=b[a]
*[a][b][c]

The memory string is initially [a][b][c], but will become b[a][a][c] in the first turn.

When no replacements can be performed, execution stops and the memory string is printed.

Examples

Hello, World!

*Hello, World!

Truth Machine

[1]=1[1]
[0]=0
*[input]

You replace input with the input (0 or 1), actually, this doesn't print anything (because the program never halts) when input is 1, but appends 1 to the memory string infinitely.

A+B Problem

[0 0]=0
[0 1]=1
[0 2]=2
[0 3]=3
[0 4]=4
[0 5]=5
[0 6]=6
[0 7]=7
[0 8]=8
[0 9]=9
[1 0]=1
[1 1]=2
[1 2]=3
[1 3]=4
[1 4]=5
[1 5]=6
[1 6]=7
[1 7]=8
[1 8]=9
[1 9]=10
[2 0]=2
[2 1]=3
[2 2]=4
[2 3]=5
[2 4]=6
[2 5]=7
[2 6]=8
[2 7]=9
[2 8]=10
[2 9]=11
[3 0]=3
[3 1]=4
[3 2]=5
[3 3]=6
[3 4]=7
[3 5]=8
[3 6]=9
[3 7]=10
[3 8]=11
[3 9]=12
[4 0]=4
[4 1]=5
[4 2]=6
[4 3]=7
[4 4]=8
[4 5]=9
[4 6]=10
[4 7]=11
[4 8]=12
[4 9]=13
[5 0]=5
[5 1]=6
[5 2]=7
[5 3]=8
[5 4]=9
[5 5]=10
[5 6]=11
[5 7]=12
[5 8]=13
[5 9]=14
[6 0]=6
[6 1]=7
[6 2]=8
[6 3]=9
[6 4]=10
[6 5]=11
[6 6]=12
[6 7]=13
[6 8]=14
[6 9]=15
[7 0]=7
[7 1]=8
[7 2]=9
[7 3]=10
[7 4]=11
[7 5]=12
[7 6]=13
[7 7]=14
[7 8]=15
[7 9]=16
[8 0]=8
[8 1]=9
[8 2]=10
[8 3]=11
[8 4]=12
[8 5]=13
[8 6]=14
[8 7]=15
[8 8]=16
[8 9]=17
[9 0]=9
[9 1]=10
[9 2]=11
[9 3]=12
[9 4]=13
[9 5]=14
[9 6]=15
[9 7]=16
[9 8]=17
[9 9]=18
*[A B]

Replace A and B to the two numbers you want to add, they must be one-digit.

Print 1048576 A's

[0]=A
[1]=[0][0]
[2]=[1][1]
[3]=[2][2]
[4]=[3][3]
[5]=[4][4]
[6]=[5][5]
[7]=[6][6]
[8]=[7][7]
[9]=[8][8]
[10]=[9][9]
[11]=[10][10]
[12]=[11][11]
[13]=[12][12]
[14]=[13][13]
[15]=[14][14]
[16]=[15][15]
[17]=[16][16]
[18]=[17][17]
[19]=[18][18]
*[19]

Implementations

Python

import sys,re
code=sys.stdin.read().strip().split('\n')
memstr=code.pop()[1:]
rules={}
def replacement(x):
    t=x.string[slice(*x.span())][1:-1]
    if t in rules:
        return rules[t]
    return x.string[slice(*x.span())]
for i in code:
    if not re.match('\\[[^\\]]*?\\]=(.*)',i):
        raise SyntaxError('Invalid code')
    beg=i[i.find('[')+1:i.find(']')]
    end=i[i.find('=')+1:]
    rules[beg]=end
while 1:
    if not re.search('\\[.*?\\]',memstr):
        print(memstr)
        break
    memstr=re.sub('\\[.*?\\]',replacement,memstr)
    #print(memstr) # Decomment this line to see memory string

See also