Sembly

From Esolang
Jump to navigation Jump to search

Sembly is a cell-based esolang similar to brainfuck except the cells are binary - meaning they can only hold a single bit: 0 or 1, and only uses 7 parameter-less functions, it is likely turing complete.

Functions

  • inp - inputs a single 1 or 0 bit to the cell at the pointer
  • out - outputs the cell at the pointer
  • left - shifts pointer left
  • right - shifts pointer right
  • flip - logical NOT on the cell at the pointer (0 becomes 1, or 1 becomes 0)
  • loop - enters loop if cell at the pointer is 0, and continues looping until the cell at the pointer equals 1
  • end - ends loop

Note: the loop function is used for both loops and conditionals/if-statements.

Examples

A Truth Machine

inp
out
flip
loop
flip
out
flip
end

Nor Gate

inp
right
inp
loop
left
loop
right
right
flip
left
left
flip
end
right
flip
end
right
out

And Gate

inp
right
inp
flip
loop
left
flip
loop
right
right
flip
left
left
flip
end
right
flip
end
right
out

Interpreter

Interpreter made in Python (not strictly tested)

Note: Requires making a file in the same directory by default called 'program.txt'

import os

def skip(i, program):
  depth=0
  for x in range(i+1, len(program)):
    if 'end' in program[x]:
      if depth==0:
        return x+1
      else:
        depth -= 1
    if 'loop' in program[x]:
      depth += 1
  
def cont(i, program):
  depth=0
  for x in range(i-1, -1, -1):
    if 'loop' in program[x]:
      if depth==0:
        return x
      else:
        depth-=1
    if 'end' in program[x]:
      depth += 1

array = [0, 0, 0]
clipboard = 0
pos = 0

program = open(f'program.txt', 'r').readlines() 

i = 0
while True:
  
  try:
    line = program[i]
  except:
    break
  
  if 'inp' in line:
    array[pos] = int(input('> '))
  
  elif 'out' in line:
    print(array[pos])
  
  elif 'left' in line:
    pos -= 1
  
  elif 'right' in line:
    array.append(0)
    pos += 1
    
  elif 'loop' in line:
    if array[pos] != 0:
      i = skip(i, program)
      continue
    
  elif 'flip' in line:
    array[pos] = 1-array[pos] 

  elif 'end' in line:
    i = cont(i, program)
    continue

  i += 1

Notes

Any problems or queries with anything to do with this language please put here: