Splits

From Esolang
Jump to navigation Jump to search

Splits is an esoteric programming language invented by Dr Laurie Tratt in 2024.

Overview

The language uses reverse polish notation and supports literal integers, as well as operators +, -, * and comparison >=.

Every line is either a variable assignment, or a while loop start or end. While loop starts with while condition where condition is an expression evaluating to 1 or 0. While loop ends with keyword end.

Examples

Factorial

This program computes the factorial of number 5.

n = 5
r = 1
while n 1 >=
  r = r n *
  n = n 1 -
end

Implementation

Implementation in Python provided by the author.

import sys
class Ev:
  def ev(self, s):
    self.vars = {}
    lines = [x for x in s.split("\n") if x.strip() != ""]
    pc = 0
    while pc < len(lines):
      line = lines[pc]
      match line.split(maxsplit=1)[0]:
        case 'while':
          if self.ev_expr(line.split(maxsplit=1)[1]) == 1: pc += 1
          else:
            while lines[pc].split(maxsplit=1)[0] != 'end': pc += 1
            pc += 1
        case 'end':
          while lines[pc].split(maxsplit=1)[0] != 'while': pc -= 1
        case _:
          (name, _, expr) = line.split(maxsplit=2)
          self.vars[name] = self.ev_expr(expr)
          pc += 1
    print(self.vars)
  def ev_expr(self, s):
    toks = s.split()
    stack = []
    for tok in toks:
      if tok.isdigit(): stack.append(int(tok))
      elif tok in self.vars: stack.append(self.vars[tok])
      else:
        rhs = stack.pop()
        lhs = stack.pop()
        if tok == "+": stack.append(lhs + rhs)
        elif tok == "*": stack.append(lhs * rhs)
        elif tok == "-": stack.append(lhs - rhs)
        elif tok == ">=":
          if lhs >= rhs: stack.append(1)
          else: stack.append(0)
    return stack[0]
Ev().ev(open(sys.argv[1]).read())