morse code

From Esolang
Jump to navigation Jump to search

morse code is an esolang created by kiken in 2023 (sort of) based on morse code. Because of how morse code works, it is compatible with brainfuck, but it just works... differently.

morse code
Paradigm(s) String-rewriting paradigm
Designed by User:kiken
Appeared in 2023
Computational class Unknown
Reference implementation here
File extension(s) .mos

Overview

morse code has three instructions, ., - and space. The following process is executed (where 'current instruction' and 'current character' refer to the first instruction):

if the current instruction is .:
if the next character is .:
move the current character in front of the next -
else if the next character is -:
move the current character in front of the next .
else:
delete the current character and continue
if the current instruction is -:
if the next character is .:
add a - in front of the next character and delete the current
else if the next character is -:
delete the next character and move the current character two spaces forward
else:
delete the current character
else:
halt

The remaining program is converted from morse code to letters then printed to the console. A double-space in morse code is equivalent to a space. Empty programs (including EOF) will halt.

Examples

Infinite loop

.-.

Here, . is moved in front of the -, yielding -... Then, - is deleted and another - is added to the front of the first ., once again yielding .-..

This is another infinite loop:

-..

It yields .-., which once again yields -...

Hello, world!

The first whitespace is necessary.

 .... . .-.. .-.. --- --..--  .-- --- .-. .-.. -.. -.-.--

Here, the program begins with neither . nor -, so the program halts and prints Hello, world!.

Computational class

Currently unknown. Laclale predicts that morse code is unusable for programming if excluding the morse-code converter.

Implementations

Python implementation

from sys import argv

# assuming file is in main.py, execute with: python3 main.py [filename]

def morse_code(code):
    yield_ = code
    index = 0

    morsetochar = {'.-': 'A', '-...': 'B', '-.-.': 'C', '-..': 'D', '.': 'E', '..-.': 'F', '--.': 'G', '....': 'H', '..': 'I', '.---': 'J', '-.-': 'K', '.-..': 'L', '--': 'M', '-.': 'N', '---': 'O', '.--.': 'P', '--.-': 'Q', '.-.': 'R', '...': 'S', '-': 'T', '..-': 'U', '...-': 'V', '.--': 'W', '-..-': 'X', '-.--': 'Y', '--..': 'Z', '.----': '1', '..---': '2', '...--': '3', '....-': '4', '.....': '5', '-....': '6', '--...': '7', '---..': '8', '----.': '9', '-----': '0', '.-.-.-': '.', '--..--': ',', '..--..': '?', '': ''}

    while yield_:
        if index+1 <= len(code) and len(code) >= 1:
            if code[index] == '.':
                if code[index+1] == '.': 
                    yield_ = yield_[1:yield_.index('-')] + '.' + yield_[yield_.index('-'):]
                elif code[index+1] == '-':
                    yield_ = yield_[1:yield_.index('.')] + '-' + yield_[yield_.index('.'):]
                else:
                    yield_ = yield_[1:]
                continue
            elif code[index] == '-':
                if code[index+1] == '.':
                    yield_ = yield_[1] + '-' + yield_[1:]
                elif code[index+1] == '-':
                    yield_ = yield_[0] + yield_[2:]
                    yield_ = yield_[:3] + yield_[0] + yield_[3:]
                else:
                    yield_ = yield_[1:]
                continue
            else:
                break
        else:
            break
        index += 1

    english_plain_text = ''
    morse_code = yield_

    current_char_morse_code = ''
    i = 0

    while i < len(morse_code) - 1:
        if morse_code[i] == ' ':
            if len(current_char_morse_code) == 0 and morse_code[i + 1] == ' ':
                english_plain_text += ' '
                i += 1
            else:
                english_plain_text += morsetochar[current_char_morse_code]
                current_char_morse_code = ''
        else:
            current_char_morse_code += morse_code[i]

        i += 1

    if len(current_char_morse_code) > 0:
        try:
            english_plain_text += morsetochar[current_char_morse_code]
        except KeyError:
            english_plain_text += '#'

    return english_plain_text

with open(argv[1]) as f:
    print(morse_code(f.read()))

Javascript implementation

cannot see

/*
  Javascript implementation
  execute with function 'morseCode'
*/

function morseCode(code) {
  let yield_ = code;
  let index = 0;

  let morsetochar = {
    ".-": "A",
    "-...": "B",
    "-.-.": "C",
    "-..": "D",
    ".": "E",
    "..-.": "F",
    "--.": "G",
    "....": "H",
    "..": "I",
    ".---": "J",
    "-.-": "K",
    ".-..": "L",
    "--": "M",
    "-.": "N",
    "---": "O",
    ".--.": "P",
    "--.-": "Q",
    ".-.": "R",
    "...": "S",
    "-": "T",
    "..-": "U",
    "...-": "V",
    ".--": "W",
    "-..-": "X",
    "-.--": "Y",
    "--..": "Z",
    ".----": "1",
    "..---": "2",
    "...--": "3",
    "....-": "4",
    ".....": "5",
    "-....": "6",
    "--...": "7",
    "---..": "8",
    "----.": "9",
    "-----": "0",
    ".-.-.-": ".",
    "--..--": ",",
    "..--..": "?",
    "": ""
  };

  while (yield_) {
    if (index + 1 <= code.length && code.length >= 1) {
      if (code[index] == ".") {
        if (code[index + 1] == ".") {
          yield_ =
            yield_.slice(1, yield_.indexOf("-")) +
            "." +
            yield_.slice(yield_.indexOf("-"));
        } else if (code[index + 1] == "-") {
          yield_ =
            yield_.slice(1, yield_.indexOf(".")) +
            "-" +
            yield_.slice(yield_.indexOf("."));
        } else {
          yield_ = yield_.slice(1);
        }
        continue;
      } else if (code[index] == "-") {
        if (code[index + 1] == ".") {
          yield_ = yield_.charAt(1) + "-" + yield_.slice(1);
        } else if (code[index + 1] == "-") {
          yield_ = yield_.charAt(0) + yield_.slice(2);
          yield_ = yield_.slice(0, 3) + yield_.charAt(0) + yield_.slice(3);
        } else {
          yield_ = yield_.slice(1);
        }
        continue;
      } else {
        break;
      }
    } else {
      break;
    }
    index += 1;
  }

  let englishPlainText = "";
  let morseCode = yield_;

  let currentCharMorseCode = "";
  let i = 0;

  while (i < morseCode.length - 1) {
    if (morseCode[i] == " ") {
      if (currentCharMorseCode.length == 0 && morseCode[i + 1] == " ") {
        englishPlainText += " ";
        i += 1;
      } else {
        englishPlainText += morsetochar[currentCharMorseCode];
        currentCharMorseCode = "";
      }
    } else {
      currentCharMorseCode += morseCode[i];
    }

    i += 1;
  }

  if (currentCharMorseCode.length > 0) {
    try {
      englishPlainText += morsetochar[currentCharMorseCode];
    } catch (err) {
      englishPlainText += "#";
    }
  }

  return englishPlainText;
}