简体   繁体   中英

Python - Only printing 'PRINT'

I am trying to make my own basic programming language. I have the following code in my smrlang.py file

from sys import *

tokens = []

def open_file(filename):
    data = open(filename, "r").read()
    return data

def smr(filecontents):
    tok = ""
    state = 0
    string = ""
    filecontents = list(filecontents)
    for char in filecontents:
        tok += char
        if tok == " ":
            if state == 0:
                tok = ""
            else:
                tok = " "
        elif tok == "PRINT":
            tokens.append("PRINT")
            tok = ""
        elif tok == "\"":
            if state == 0:
                state = 1
            elif state == 1:
                print("STRING")
                string = ""
                state = 0
        elif state == 1:
            string += tok
    print(tokens)
def run():
    data = open_file(argv[1])
    smr(data)
run()

And I have this in my one.smr file:

PRINT "HELLO WORLD"

The output should be something like PRINT STRING , but when I use the command python3 smrlang.py one.smr , the output is just PRINT . I am using Python 3

Debugging it in the head, I found the problem:

elif state == 1:
    string += tok

You don't reset the token here. It will be aababcabcd instead of abcd and recognizing \\ won't work (as it will be aababcabcd\\ ).

This also causes the token to just be everything and it will never print.

Try changing it to:

elif state == 1:
    string += tok
    tok = ""

Output after fix:

> py -3 temp.py temp.txt
STRING
['PRINT']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM