From 4b69cca60fd1256659b4738d8e7de72220493386 Mon Sep 17 00:00:00 2001 From: Julien Date: Fri, 16 Sep 2011 14:39:57 +0200 Subject: [PATCH] Initial commit --- README | 32 ++++++++++++++++++++++++ horrlang.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 README create mode 100755 horrlang.py diff --git a/README b/README new file mode 100644 index 0000000..9b00e20 --- /dev/null +++ b/README @@ -0,0 +1,32 @@ +Horrlang is an esoteric language, you should not try it. + +In Horrlang, memory and instruction are mixed, so you can use old instruction +as new memory places or do some metaprogramation rewriting your code as it run. + +Just like in LISP but ... without lists ... good luck ... + +So in Horrlang you have basically 2 pointers, a memory one and an instruction +one. Both starts at 0. The instruction one follow instructions that can move +the memory pointer, look at instruction set : + +Instruction set : + +Lx : Move memory head left x chars (defaults to 1) +Rx : Move memory head right x chars (defaults to 1) + +Nx : Goto the next char that is x +Px : Goto the prev char that is x +O : Output current value under memory head +I : Read from stdin to the current memory head location +K : Decrement current memory location +H : Increment current memory location +Jx : Jump to prev char X if current memory location != 0 + +The Hello World : +R9R9R9R4OROROROROROROROROROROROHello World. + +Writing a serie of P should like : +R5OP5P or R3OP3 + +Write 123456789 using a loop : +R9R3:ROHLKJ:81 diff --git a/horrlang.py b/horrlang.py new file mode 100755 index 0000000..7269483 --- /dev/null +++ b/horrlang.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys + + +class Machine: + def __init__(self, code): + self.mem = [ord(char) for char in code] + self.exc_ptr = 0 + self.mem_ptr = 0 + + def get_integer(self): + parameter = 0 + while chr(self.mem[self.exc_ptr + 1]) >= '0' \ + and chr(self.mem[self.exc_ptr + 1]) <= '9': + self.exc_ptr += 1 + parameter = parameter * 10 + (self.mem[self.exc_ptr] \ + - ord('0')) + if parameter == 0: + parameter = 1 + return parameter + + def L(self): + self.mem_ptr -= self.get_integer() + + def R(self): + self.mem_ptr += self.get_integer() + + def O(self): + sys.stdout.write(chr(self.mem[self.mem_ptr])) + + def I(self): + self.mem[self.mem_ptr] = ord(sys.stdin.read(1)) + + def N(self): + self.exc_ptr += 1 + search = self.mem[self.exc_ptr] + while self.mem[self.exc_ptr] != search: + self.exc_ptr += 1 + + def P(self): + search = self.mem[self.exc_ptr + 1] + self.exc_ptr -= 1 + while self.mem[self.exc_ptr] != search: + self.exc_ptr -= 1 + + def J(self): + if self.mem[self.mem_ptr] != ord('0'): + search = self.mem[self.exc_ptr + 1] + self.exc_ptr -= 1 + while self.mem[self.exc_ptr] != search: + self.exc_ptr -= 1 + + def H(self): + self.mem[self.mem_ptr] = self.mem[self.mem_ptr] + 1 + + def K(self): + self.mem[self.mem_ptr] = self.mem[self.mem_ptr] - 1 + + def run(self): + while True: + if self.exc_ptr >= len(self.mem): + sys.exit(0) + control = chr(self.mem[self.exc_ptr]) + if hasattr(self, control): + getattr(self, control)() + self.exc_ptr += 1 + +Machine(sys.argv[1]).run()