![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Moving on, this talk about Befunge interpreters has got me rather interested in building one myself. Nothing as involved as gryfang's planned extensions to the language, but just a basic interpreter for Befunge-93 code programmed in C. I figure it would also be a good chance to use a Duff's Device, something which I've always wanted to do. I hope you don't mind me having a go at this myself, gryfang. |
|
|
|
|
|
|
#22 | |
|
Programmer
|
Quote:
To conceptulize (can't spell) I would break it down into these chunks to work with Flow: > < v ^ ? # Control: | _ (in the specs it pops if True, but doesn't pop for False (0)) Stack: $ \ : " (stringmode) and 0-9 Operations: + - * / % ! ` IO: . , & ~ Code: g p and of course @ (seriously though) Each chunk has it's similarities
__________________
-------------------- LOAD "*" ,8,1 God bless - Gryfang |
|
|
|
|
|
|
#23 | |
|
Professional Programmer
|
Quote:
|
|
|
|
|
|
|
#24 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
Wow Marvin stop freaking out.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#25 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Marvin, honestly, you need to take your meds, or whatever. If you feel you must deviate from the purposes of the forum, there are others you can visit during those periods.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#26 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Here's my Befunge-93 interpreter. I originally aimed to program it in C, but I realised that by the time I got half way, I'd be bored and wouldn't complete it. So I constructed one in Python instead:
import sys
import string
import random
from operator import *
class State:
normal, string, skip, end = range(4)
class Interpreter:
def __init__(self, file):
self.code = [line.strip('\n') for line in file]
self.stack = []
self.delta = (1, 0)
self.pointer = (0, 0)
self.state = State.normal
up, down, left, right = (0, -1), (0, 1), (-1, 0), (1, 0)
pop, push, set_delta = self._pop, self._push, self._set_delta
set_state = self._set_state
self.instructions = {
'+' : lambda : push( add(*pop(2)) ),
'-' : lambda : push( sub(*reversed(pop(2))) ),
'*' : lambda : push( mul(*pop(2)) ),
'/' : lambda : push( div(*reversed(pop(2))) ),
'%' : lambda : push( mod(*reversed(pop(2))) ),
'!' : lambda : push( int(not_(*pop())) ),
'`' : lambda : push( int(gt(*reversed(pop(2)))) ),
'>' : lambda : set_delta( right ),
'<' : lambda : set_delta( left ),
'^' : lambda : set_delta( up ),
'v' : lambda : set_delta( down ),
'?' : lambda : set_delta( random.choice([up, down, left, right]) ),
'_' : lambda : pop() == 0 and set_delta(right) or set_delta(left),
'|' : lambda : pop() == 0 and set_delta(down) or set_delta(up),
'"' : lambda : set_state(State.string),
':' : lambda : push(*self.stack[-1:]),
'\\': lambda : push(*reversed(pop(2))),
'$' : pop,
'.' : lambda : sys.stdout.write( str(int(*pop())) ),
',' : lambda : sys.stdout.write( chr(*pop()) ),
'#' : lambda : set_state(State.skip),
'p' : lambda : self.set(reversed(pop(2)), pop()),
'g' : lambda : self.get(reversed(pop(2))),
'&' : lambda : push( int(raw_input()) ),
'~' : lambda : push( ord(raw_input()) ),
'@' : lambda : set_state(State.end)
}
def run(self):
while self.state != State.end:
self.evaluate(self.get(self.pointer))
self.next()
def evaluate(self, char):
if self.state == State.string:
if char != '"':
self._push(ord(char))
else:
self.state = State.normal
else:
if char in string.digits:
self._push(int(char))
elif char != ' ':
self.instructions[char]()
def next(self):
if self.state != State.end:
(x, y), (dx, dy) = self.pointer, self.delta
self.goto(((x + dx), (y + dy)))
if self.state == State.skip:
self.goto(((x + dx), (y + dy)))
def goto(self, (x, y)):
y = y % len(self.code)
x = x % len(self.code[y])
self.pointer = (x, y)
def _pop(self, amount = 1):
return [self.__pop_single() for i in range(amount)]
def __pop_single(self):
if not self.stack: return 0
return self.stack.pop()
def _push(self, *items):
self.stack.extend(items)
def _set_delta(self, delta):
self.delta = delta
return True
def set(self, (x, y), value):
self.code[y][x] = value
def get(self, (x, y)):
return self.code[y][x]
def _set_state(self, state):
self.state = state
if __name__ == '__main__':
Interpreter(sys.stdin).run() |
|
|
|
|
|
#27 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Marvin (or Nemesis, or whoever the hell you are), I take extreme offense to your comment. Especially being Jewish myself.
It is important to stay objective and critical in national and international policies. Israeli citizens do it, and I'd hope that the rest of the world does to. Blindly taking sides is one of the worst possible courses of action. Debate on any of Israel's policies is encouraged (just not here, as it is a programming forum). Your comment, on the other hand, is entirely different. Disagreement with policy should never excuse hatred. You are implying a national conspiracy by Jews. I hope you realize how similar your opinions resemble those of Hitler. Thanks, that will be all.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#28 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
In case anyone's interested, and because this probably doesn't warrent its own thread, here's a Python implementation of Brainf*ck:
import sys
class Commands(type):
def __new__(cls, name, bases, members):
members['commands'] = dict(
(m.__doc__, m) for m in members.values() if m.__doc__
)
return type.__new__(cls, name, bases, members)
class Interpreter:
__metaclass__ = Commands
def __init__(self, file):
self.code = [c for c in file.read() if c in self.commands.keys()]
self.memory = {}
self.pointer = 0
self.instr = 0
def run(self):
while self.instr < len(self.code):
self.commands[self.code[self.instr]](self)
self.instr += 1
def inc(self):
">"
self.pointer += 1
def dec(self):
"<"
self.pointer -= 1
def inc_value(self):
"+"
try:
self.memory[self.pointer] += 1
except:
self.memory[self.pointer] = 1
def dec_value(self):
"-"
try:
self.memory[self.pointer] -= 1
except:
self.memory[self.pointer] = -1
def output(self):
"."
sys.stdout.write(chr(self.memory[self.pointer]))
def input(self):
","
self.memory[self.pointer] = ord(sys.stdin.read(1))
def start_loop(self):
"["
if self.memory[self.pointer] == 0:
self.instr = self.code.index("]", self.instr) + 1
def end_loop(self):
"]"
if self.memory[self.pointer] != 0:
for i in xrange(self.instr, 0, -1):
if self.code[i] == "[":
self.instr = i
break
if __name__ == '__main__':
Interpreter(sys.stdin).run() |
|
|
|
|
|
#29 |
|
Programmer
Join Date: Jul 2006
Location: using Earth.Africa.Egypt.Cairo;
Posts: 76
Rep Power: 3
![]() |
can we please close the jewish thing because i wont stay silent for too long if this continued.
and now for Arevos and gryfang goodluck on what you are doing guys. |
|
|
|
|
|
#30 |
|
Professional Programmer
|
To me it seems like Marvin is just trying to hate something and just chose Befunge and Jews.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| If you had it to do all over again, which language? | peace_of_mind | Coder's Corner Lounge | 24 | Jan 13th, 2008 6:06 PM |
| The C programming Language (2nd Edition) | nnxion | Book Reviews | 10 | Jul 6th, 2007 4:29 PM |
| Language display in program | Prm753 | C++ | 3 | May 30th, 2006 6:45 PM |
| More languages? | UnKnown X | Coder's Corner Lounge | 27 | Dec 18th, 2005 4:06 PM |