- store grid as ints

- fix put and get
This commit is contained in:
Wim Pomp
2022-11-24 00:45:11 +01:00
parent 656a9f96f8
commit 91462bea57
3 changed files with 17 additions and 15 deletions

View File

@@ -75,10 +75,11 @@ class Grid(dict):
self._ip[1] = self.wrap(value, 1) self._ip[1] = self.wrap(value, 1)
def __getitem__(self, key): def __getitem__(self, key):
return self.get(key, ' ') return self.get(key, ord(' '))
def __setitem__(self, key, value): def __setitem__(self, key, value):
super().__setitem__(tuple(self.wrap(k, i) for i, k in enumerate(key)), value) super().__setitem__(tuple(self.wrap(k, i) for i, k in enumerate(key)),
ord(value) if isinstance(value, str) else value)
def __repr__(self): def __repr__(self):
lines = [] lines = []
@@ -87,7 +88,7 @@ class Grid(dict):
lines.append([]) lines.append([])
while len(lines[y]) <= x: while len(lines[y]) <= x:
lines[y].append(' ') lines[y].append(' ')
lines[y][x] = value lines[y][x] = chr(value) if 32 <= value <= 126 or 161 <= value <= 255 else chr(164)
lines = [''.join(line) for line in lines] lines = [''.join(line) for line in lines]
if self.cursor: if self.cursor:
return '\n'.join(lines[:self.y] + return '\n'.join(lines[:self.y] +
@@ -157,7 +158,9 @@ class Befunge(Grid):
return self return self
def __next__(self): def __next__(self):
return self.step() if self.step().terminated:
raise StopIteration
return self
def run(self): def run(self):
for _ in self: for _ in self:
@@ -208,17 +211,15 @@ class Befunge(Grid):
pass pass
def step(self, n=1): def step(self, n=1):
if self.terminated:
raise StopIteration
m = 0 m = 0
while m < n: while m < n:
if self.string: if self.string:
if self.op == '"': if self.op == ord('"'):
self.string = False self.string = False
else: else:
self.stack.push(ord(self.op)) self.stack.push(self.op)
elif self.op in self.operations: elif chr(self.op) in self.operations:
match self.op: match chr(self.op):
case '+': case '+':
self.stack.push(self.stack.pop() + self.stack.pop()) self.stack.push(self.stack.pop() + self.stack.pop())
case '-': case '-':
@@ -268,16 +269,15 @@ class Befunge(Grid):
case '#': case '#':
self.advance() self.advance()
case 'p': case 'p':
self[self.stack.pop(-2), self.stack.pop()] = chr(self.stack.pop()) self[self.stack.pop(-2), self.stack.pop()] = self.stack.pop(-3)
case 'g': case 'g':
self.stack.push(ord(self[self.stack.pop(-2), self.stack.pop()])) self.stack.push(self[self.stack.pop(-2), self.stack.pop()])
case '&': case '&':
self.stack.push(int(self.input())) self.stack.push(int(self.input()))
case '~': case '~':
self.stack.push(ord(self.input())) self.stack.push(ord(self.input()))
case '@': case '@':
self.terminated = True self.terminated = True
break
case ' ': case ' ':
pass pass
case op: case op:
@@ -285,7 +285,7 @@ class Befunge(Grid):
else: else:
raise OperatorException(self.op) raise OperatorException(self.op)
self.advance() self.advance()
if not (not self.string and self.op == ' '): if not (not self.string and self.op == ord(' ')):
m += 1 m += 1
self.steps += 1 self.steps += 1
return self return self

View File

@@ -0,0 +1,2 @@
&:>00p1-::v
^ *g00 _g.25*,@

View File

@@ -1,7 +1,7 @@
import os import os
import setuptools import setuptools
version = '2022.11.0' version = '2022.11.1'
with open('README.md', 'r') as fh: with open('README.md', 'r') as fh:
long_description = fh.read() long_description = fh.read()