MiniStringFuck

From Esolang
Jump to navigation Jump to search

MiniStringFuck (or MSF-) is an esoteric programming language with only two commands, operating on one cell.

Instructions

Instruction Action
+ Increment the memory cell. If it reaches 256, wrap to 0.
. Output the character in the cell.

Examples

Hello, World!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++.+++.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.

Alphabet (uppercase)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.

Golfing

To golf code you need to input the code to this program that does the job for you.

Implementation

Here is an implementation in Python 2.7.12.

# coding=utf-8
""" MiniStringFuck interpreter by Έρικ Κωνσταντόπουλος, © 2001-2015 """

# Let's import the required modules
from sys import argv, stdout

# Let's get teh code!
code_ = open(argv[1])
code = code_.read()

# Free some memory! Not required, but I would recommend it.
# Un-comment the below line to disable memory freeing.
#"""
del stdin
#""" # Never tamper with this line!

# Initialize the cell
cell0 = 0

# Code fastener; not really required.
# Un-comment the line below to disable it, although I recommend to leave it enabled.
#"""
for i in xrange(len(code)):
    while code[i:i + 256] == '+' * 256:  # We don't need 256 +s in a row...
        code = code[:i] + code[i + 256:] # ...so we trash them!
#""" # Please do not remove or un-comment this line!

# Interpreter
for i in code:
    if i == '+':    # +
        cell0 += 1 # Increments the cell.
    
    cell0 %= 256 # There are no ASCII characters over 255, so we wrap.
    if i == '.':                  # .
        stdout.write(chr(cell0)) # Outputs the character in the cell.

code_.close()

Here's an implementation in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace MiniStringFuck_Interpreter
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(args[0]);
            string code = sr.ReadToEnd();
            code = code.Replace(((char)13).ToString(), "");

            int cell = 0;

            foreach (char c in code)
            {
                if (c == '+')
                {
                    if (cell == 255)
                    {
                        cell = 0;
                    }
                    else
                    {
                        cell++;
                    }
                }
                else if (c == '.')
                {
                    Console.Write((char)cell);
                }
            }

            Console.ReadLine();
        }
    }
}

Miscellaneous

Generator

# coding=utf-8
""" MiniStringFuck generator by Έρικ Κωνσταντόπουλος, © 2006-2013 """

# Module importing
from sys import stdout, stdin, stderr

# String getting
string = stdin.read()

# Memory freeing #1
#""" # Uncomment this line to disable!
del stdin
#""" # Never modify line 12.

# String checking (ASCII only)
for i in string:
    if ord(i) not in range(256):
         stderr.write('Your string contains non-ASCII characters.')
         exit()

# Memory freeing #2
#""" # Uncomment this line to disable!
del stderr
#""" # Never modify line 23.

# Character storing
char = 0

# Code generating
for i in string:
    while chr(char) != i:
        char += 1
        char %= 256
        stdout.write('+')
    stdout.write('.')

Optimizer

# coding=utf-8
""" MiniStringFuck optimizer by Έρικ Κωνσταντόπουλος, © 2001-2004 """

# Module imports
from sys import argv, stdout, stdin

file = open(argv[1]) if len(argv) >= 2 else stdin
if len(argv) >= 3: out = open(argv[2], 'w')
elif file != stdin: out = open(argv[1] + '_cmp', 'w')
else: out = stdout
# Free precious RAM!!
del stdout
code = file.read()

# Memory freeing
del stdin

# Main
for i in xrange(len(code)):
    while code[i] not in '+.':
        brtoggle = False
        if i == len(code) - 1:
            continue_ = ''
            brtoggle = True
        else:
            continue_ = code[i + 1:]
        code = code[:i] + continue_
        if brtoggle:
            break
    if i == len(code) - 1:
        break

code = '.' + code
while code[-1] != '.':
    code = code[:-1]
code = code[1:]

while '+' * 256 in code:
    code = code.replace('+' * 256, '')

#File O
out.write(code)

See also