Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

PHAWN Interpreter (See related posts)

// description of your code here

import sys
import time

debug = 0
source = []
stack = []
px = 0
py = 0
pd = 1
mode = 0
register = 0

def Initialize():
    
    global source
    global px
    global py
    
    path = raw_input("PHAWN source file: ")
    
    try: file = open(path, "r")
    except IOError:
        print "Failed to open file!"
        return 0
    else: print "File opened successfully!"
    
    for line in file:
        source.append(line.strip("\n"))
        try: px = line.index("$")
        except: continue
        else: py = source.index(line.strip("\n"))
        
    return 1

def Run():
    
    global debug
    global source
    global stack
    global px
    global py
    global pd
    global mode
    global register
    
    try: instruction = source[py][px]
    except:
        print "--- Pointer moved into an unknowned region."
        return 0
    
    if debug == 1: print "Instr:", instruction, " Pos:(", px, ",", py, ")", " Dir:", pd
    
    oy = py
    
    if instruction == "#":
        return 0
    elif instruction == ">":
        if pd > 0:
            stack.append(register)
        elif len(stack) > 0:
            register = stack.pop()
    elif instruction == "<":
        if pd < 0:
            stack.append(register)
        elif len(stack) > 0:
                register = stack.pop()
    elif instruction == "}":
        if pd > 0:
            Input()
        else:
            Output()
    elif instruction == "{":
        if pd < 0:
            Input()
        else:
            Output()
    elif instruction == "/":
        if pd > 0:
            px += 1
            while (source[py][px].isdigit() and source[py][px+1].isdigit()):
                px += 1
    elif instruction == "\\":
        if pd < 0:
            px -= 1
            while (source[py][px].isdigit() and source[py][px-1].isdigit()):
                px -= 1
    elif instruction == "^":
        py -= 1
    elif instruction == "_":
        py += 1
    elif instruction == "~":
        if len(stack) > 1:
            if stack[len(stack) - 1] > stack[len(stack) - 2]:
                py -= 1
            else:
                py += 1
    elif instruction == "+":
        if len(stack) > 1:
            val1 = stack.pop()
            val2 = stack.pop()
            stack.append(val1 + val2)
    elif instruction == "-":
        if len(stack) > 1:
            val1 = stack.pop()
            val2 = stack.pop()
            stack.append(val2 - val1)
    elif instruction == "&":
        mode = 1 - mode
    elif instruction == "%":
        if len(stack) > 1:
            val1 = stack.pop()
            val2 = stack.pop()
            stack.append(val1)
            stack.append(val2)
    elif instruction.isdigit() == True:
        ReadInteger()      
        
    if pd > 0 and px + pd > len(source[py]) - 1:
        pd = -1
    if pd < 0 and px + pd < 0:
        pd = 1
    
    if oy == py:
        px += pd
    
    return 1
    
def Input():
    
    global stack
    
    data = raw_input("Input: ")
    if data.isdigit() == False:
        stack.append(ord(data[0]))
    else:
        stack.append((int(data)))
        
def Output():
    
    global mode
    
    if len(stack) > 0:
        data = stack.pop()
        if mode == 0:
            print data
        else:
            print chr(data)
            
def ReadInteger():
    
    global source
    global stack
    global px
    global py
    global pd
    global register
    
    data = source[py][px]
    
    if pd > 0:
        while (source[py][px + 1].isdigit() == True):
            try:
                px += 1
                data += source[py][px]
            except: break
    else:
        while (source[py][px - 1].isdigit() == True):
            try:
                px -= 1
                data = source[py][px] + data
            except: break
            
    register = int(data)
            
    if px > len(source[py]) - 1:
        px = len(source[py]) - 1
    if px < 0:
        px = 0
        
debug = raw_input("Debug Mode? (1 = Yes / 0 = No) ")
if debug.isdigit() == True: debug = int(debug)

if Initialize() == 1:
    start = time.clock()
    while(Run() == 1):
        if debug == 1:
            print "Stack: ", stack
            raw_input("Pressed <Enter> To Continue.")
        else: continue
    print "Executed in ", time.clock() - start, " seconds."
else: print "--- Exited without execution."    
raw_input("Press <Enter> to exit.") 

You need to create an account or log in to post comments to this site.