我仍然在编程语言项目中编写我的编程语言。打印完成,我做了输入,但它不起作用。这是我尝试输入的内容:
progress = []
command_libary = []
command_split = []
def split_command(command):
command2 = command.split()
for x in command2:
command_split.extend(x)
command_libary.extend(command)
def C05basic(command3):
x = 0
z = 0
split_command(command3)
if (command_split[z] == "p" and command_split[z + 1] == "r" and command_split[z + 2] == "i" and command_split[z + 3] == "n" and command_split[z + 4] == "t" and command_split[z + 5] == "("):
if (command_split[z + 6] == '"' or command_split[z + 6] == "'" or command_split[z + 6] == "`"):
for x in range(z + 7, len(command_split)):
if ((command_split[x] == '"' or command_split[x] == "'" or command_split[x] == "`") and command_split[x + 1] == ')'):
break
progress.extend(command_split[x])
print ("".join(progress))
z = z + 6 + len(progress)
progress.pop()
elif (command_split[z] == "i" and command_split[z + 1] == "n" and command_split[z + 2] == "p" and command_split[z + 3] == "u" and command_split[z + 4] == "t" and command_split[z + 5] == "("):
if (command_split[z + 6] == '"' or command_split[z + 6] == "'" or command_split[z + 6] == "`"):
for x in range(z + 7, len(command_split)):
if ((command_split[x] == '"' or command_split[x] == "'" or command_split[x] == "`") and command_split[x + 1] == ')'):
break
progress.extend(command_split[x])
input ("".join(progress))
z = z + 6 + len(progress)
progress.pop()
progress.pop()
command_split.pop()
C05basic("input('something')")
它给了我:
something #in input
但是当我使用时:
C05basic("print('something')")
C05basic("input('something')")
它给了我:
something
somethisomething'input('something #Not written by command input (, but by print)
Idk为什么......我做错了什么?
答案 0 :(得分:1)
<强>分析强>
您尚未指定期望的内容。但是,我认为你有一些解析命令的想法。为了跟进你正在做的事情,我在C05basic
顶部附近做了一个简单的调试插入:
def C05basic(command3):
x = 0
z = 0
split_command(command3)
print "TRACE command_split", command_split
print "TRACE progress", progress
我从两个案例得到的输出:
1命令:
TRACE command_split ['i', 'n', 'p', 'u', 't', '(', "'", 's', 'o', 'm', 'e', 't', 'h', 'i', 'n', 'g', "'", ')']
TRACE progress []
something
2个命令:
TRACE command_split ['p', 'r', 'i', 'n', 't', '(', "'", 't', 'h', 'i', 's', 's', 't', 'u', 'f', 'f', "'", ')']
TRACE progress []
thisstuff
TRACE command_split ['p', 'r', 'i', 'n', 't', '(', "'", 't', 'h', 'i', 's', 's', 't', 'u', 'f', 'f', "'", 'i', 'n', 'p', 'u', 't', '(', "'", 's', 'o', 'm', 'e', 't', 'h', 'i', 'n', 'g', "'", ')']
TRACE progress ['t', 'h', 'i', 's', 's', 't', 'u']
thisstuthisstuff'input('something
非常简单,您无需控制数据列表和下标。结果是你截断,连接,重叠,并且通常错误处理第二个输入。
<强>建议强>
学习增量编程。这里的部分问题是你在测试任何东西之前编码得太多了。相反,写几行,确保它们有效,然后再添加更多。
学习基本调试。查看这个可爱的debug博客以获取帮助。
在深入学习之前了解新语言的基本功能。例如,由于您还没有学习基本的字符串操作,因此您很难阅读。例如,你的第一个大的 if 语句可以通过一个简单的字符串比较,一个针对常量的切片来大大改进:
if command_split[z: z+5] == "print":
逐个字符比较浪费了大量的时间和视觉空间。