Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 19th, 2006, 7:30 AM   #21
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Marvin
Your not all knowing. I think your wrong on this one.
Then give a reasoned argument why you think I'm wrong, otherwise your argument is based upon irrational belief, which is a tactic that works decidedly better for religions than it does for programming.

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.
Arevos is offline   Reply With Quote
Old Jul 19th, 2006, 11:20 AM   #22
gryfang
Programmer
 
gryfang's Avatar
 
Join Date: May 2006
Location: Ohio
Posts: 36
Rep Power: 0 gryfang is on a distinguished road
Send a message via AIM to gryfang
Quote:
Originally Posted by Arevos
... but just a basic interpreter for Befunge-93 code programmed in C. ...I hope you don't mind me having a go at this myself, gryfang.
Befunge is such a follow-the-finger language it isn't funny, so Go ahead, My first step is to make a tutorial though (what sense is it to tell the computer how to do it if I can't tell others how to do it).

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
gryfang is offline   Reply With Quote
Old Jul 19th, 2006, 6:30 PM   #23
Indigno
Professional Programmer
 
Indigno's Avatar
 
Join Date: Dec 2005
Location: Anywhere non-productive
Posts: 267
Rep Power: 0 Indigno is an unknown quantity at this point
Send a message via AIM to Indigno Send a message via MSN to Indigno Send a message via Yahoo to Indigno
Quote:
Originally Posted by Marvin
If i were Bush i would stop listening to the american jews who seem to control US foreign policy at the moment.
Now why the hell did you bring the jews into this one?
Indigno is offline   Reply With Quote
Old Jul 19th, 2006, 6:48 PM   #24
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
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
uman is offline   Reply With Quote
Old Jul 19th, 2006, 6:50 PM   #25
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 19th, 2006, 6:51 PM   #26
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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()
I could probably make it a little neater if I used decorators, functions and currying instead of lambdas, but there you go. As far as I know, this is a standard's compliant Befunge-93 interpreter, but it seems like most of the documentation for it is now for Funge-98, so it's difficult to be sure.
Arevos is offline   Reply With Quote
Old Jul 19th, 2006, 7:26 PM   #27
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Jul 19th, 2006, 7:31 PM   #28
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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()
Arevos is offline   Reply With Quote
Old Jul 19th, 2006, 8:14 PM   #29
Samuaijack
Programmer
 
Samuaijack's Avatar
 
Join Date: Jul 2006
Location: using Earth.Africa.Egypt.Cairo;
Posts: 76
Rep Power: 3 Samuaijack is on a distinguished road
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.
Samuaijack is offline   Reply With Quote
Old Jul 19th, 2006, 8:19 PM   #30
Indigno
Professional Programmer
 
Indigno's Avatar
 
Join Date: Dec 2005
Location: Anywhere non-productive
Posts: 267
Rep Power: 0 Indigno is an unknown quantity at this point
Send a message via AIM to Indigno Send a message via MSN to Indigno Send a message via Yahoo to Indigno
To me it seems like Marvin is just trying to hate something and just chose Befunge and Jews.
Indigno is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC