|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Robot_Steve 于 2021-11-10 21:50 编辑
最近闲来无事,想着办法“玩”
于是写了一个“编程语言”,至少满足了编程语言三大特性:
1. 变量
2. 函数
3. 关键字
注:部分东西没有加上,如:函数的参数不是一个变量
先上源码(GUI 由 tkinter 完成):import tkinter.messagebox as msg
from tkinter import *
root = Tk() # 创建主窗口
root.title("Title") # 设置标题
root.geometry("550x340+370+240") # 设置窗口大小
root.resizable(0, 0) # 设置窗口的尺寸不可改变
# 菜单
mb = Menu(root)
root.config(menu=mb)
# 定义 “主区块” 类
class WinMian:
def __init__(self):
self.home()
def home(self):
self.homefrome = Frame(root, bg="Black", width=530,height=340)
self.homefrome.place(x=0,y=0)
# 命令框
self.text = Text(self.homefrome, width=74, height=20, bg="LightSlateGray",
fg="White")
self.text.place(x=3,y=3)
self.jg = Text(self.homefrome,width=74,height=5, bg="LightSlateGray")
self.jg.place(x=3,y=270)
def run(): # 运行函数
commands_texts = (self.text.get("0.0","end").replace("\t","")).split("\n")
commands_texts.pop()
function_list = [] # 储存函数名
function_drit = {} # 储存函数相对应的参数
function_code_drit = {} # 储存函数相对应的代码
global_var = {}
local_var = {}
def RunMainStartFunction(infunction=False, code=None, functionname=None,
parmeter=None):
global functioncode, parmeters_lens
# print("Coommands",commands_text)
# 定义关键字
def hhh(name):
name_list = []
error = False
for w2 in name:
name_list.append(w2)
commandd = list(commands_text[0])
while name_list != []:
if name_list[0] == commandd[0]:
name_list.pop(0)
commandd.pop(0)
else:
msg.showerror("Error", "SyntaxError"+"\nThe Wrong: "+commands_texts[0])
error = True
break
# print(commandd)
return_list = []
if error == False:
string = ""
while commandd != []:
# print(commandd)
if commandd[0] != "^":
string = string + commandd[0]
commandd.pop(0)
if commandd == []:
return_list.append(string)
break
else:
return_list.append(string)
string = ""
commandd.pop(0)
if commandd == []:
break
return return_list
if infunction == True:
commands_text = code
if "pm:" in commands_text[0]:
r = hhh("pm:")
functionparmeter = function_drit[functionname]
get_parmeters_list = functionparmeter.keys()
parmeters_list = []
for w5 in get_parmeters_list:
parmeters_list.append(w5) # 将键(函数参数)存入列表
parmeters_list_lens = len(parmeters_list) # 函数参数长度
set_parmeters_lens = len(r) # 设置参数的长度
# print(parmeters_list, parmeters_list_lens, set_parmeters_lens)
if set_parmeters_lens == parmeters_list_lens: # 当长度符合条件时...
while r != []:
if r[0] == "None": # 当用户放弃设置当前参数时,恢复默认(None)
function_drit2 = function_drit[functionname]
function_drit2[parmeters_list[0]] = None
function_drit[functionname] = function_drit2
r.pop(0)
parmeters_list.pop(0)
else:
function_drit2 = function_drit[functionname]
function_drit2[parmeters_list[0]] = r[0]
function_drit[functionname] = function_drit2
r.pop(0)
parmeters_list.pop(0)
commands_text.pop(0)
else:
msg.showerror("Error", "ParameterSettingFailedError"+"\nThe Wrong: "+commands_texts[0])
functionparmeters = function_drit[functionname]
if functionparmeters != None:
get_parmeters = functionparmeters.keys()
parmeterslist = []
for w5 in get_parmeters:
parmeterslist.append(w5)
parmeters_lens = len(parmeterslist) # 参数长度
parmeter_lens = len(parmeter)
parmeterslist2 = parmeterslist.copy()
while parmeterslist2 != []:
if functionparmeters[parmeterslist2[0]] == None:
parmeterslist2.pop(0)
else:
parmeterslist2.pop(0)
parmeters_lens = parmeters_lens - 1
if parmeters_lens == parmeter_lens:
while parmeter != []:
functionparmeters[parmeterslist[0]] = parmeter[0]
parmeter.pop(0)
parmeterslist.pop(0)
else:
msg.showerror("Error", "SyntaxError"+"\nThe Wrong: "+commands_texts[0])
else:
# ...
commands_text = commands_texts.copy()
# 运行 函数
# print(commands_text)
true = "True"
false = "False"
while commands_text != []:
if "printf:" in commands_text[0]:
r = hhh("printf:")
string = r[0]
while "/b" in string: # 转移符捕捉
string_list = list(string)
hindex = string_list.index("/")
if string_list[hindex + 1] == "b":
string_list.pop(hindex)
string_list.pop(hindex)
string_list.insert(hindex, "\n")
import_str = ""
for w3 in string_list:
import_str = import_str + w3
string = import_str
# print(r)
if string in global_var:
if len(r) == 1:
self.jg.insert(END, str(global_var[string][1]) + "\n")
if len(r) == 2:
if r[1] in global_var:
self.jg.insert(END, str(global_var[string][1]) + str(global_var[r[1]][1]))
elif r[1] in local_var:
self.jg.insert(END, str(global_var[string][1]) + str(local_var[r[1]][1]))
else:
self.jg.insert(END, r[0] + r[1])
elif string in local_var:
yesglobal = local_var[string][2]
print(yesglobal)
if infunction == True:
if yesglobal == functionname or yesglobal == None:
if len(r) == 1:
self.jg.insert(END, str(local_var[string][1]) + "\n")
if len(r) == 2:
if r[1] in global_var:
self.jg.insert(END, str(local_var[string][1]) + str(global_var[r[1]][1]))
elif r[1] in local_var:
self.jg.insert(END, str(local_var[string][1]) + str(local_var[r[1]][1]))
else:
self.jg.insert(END, r[0] + r[1])
else:
msg.showerror("Error","NameError")
else:
if yesglobal == None:
if len(r) == 1:
self.jg.insert(END, str(local_var[string][1]) + "\n")
if len(r) == 2:
if r[1] in global_var:
self.jg.insert(END, str(local_var[string][1]) + str(global_var[r[1]][1]))
elif r[1] in local_var:
self.jg.insert(END, str(local_var[string][1]) + str(local_var[r[1]][1]))
else:
self.jg.insert(END, r[0] + r[1])
else:
msg.showerror("Error", "NameError")
else:
if len(r) == 1:
self.jg.insert(END,string+"\n")
if len(r) == 2:
if r[1] in global_var:
self.jg.insert(END, r[0] + str(global_var[r[1]][1]) )
elif r[1] in local_var:
yesglobal2 = local_var[r[1]][2]
if infunction == True:
if yesglobal2 == functionname or yesglobal2 == None:
self.jg.insert(END, r[0] + str(local_var[r[1]][1]))
else:
msg.showerror("Error", "NameError")
else:
self.jg.insert(END, r[0] + str(local_var[r[1]][1]))
else:
self.jg.insert(END,r[0]+r[1])
commands_text.pop(0)
elif "function:" in commands_text[0]:
r = hhh("function:")
try:
function_name = r[0] # 函数名
except:
msg.showerror("Error", "ParameterSettingFailedError"+"\nThe Wrong: "+commands_texts[0])
else:
r.pop(0)
function_parameter_drit = {} # 函数参数
have_parameter = False
if r == []:
# 如果没有设置参数,默认为 None
pass
else:
for w4 in r:
function_parameter_drit[w4] = None
have_parameter = True
commands_text.pop(0)
code = []
if commands_text[0] == "{":
commands_text.pop(0)
while commands_text[0] != "}":
code.append(commands_text[0].replace("\t",""))
commands_text.pop(0)
if commands_text == []:
msg.showerror("Error", "SyntaxError")
function_code_drit[function_name] = code
function_list.append(function_name)
if have_parameter == False:
function_drit[function_name] = None
else:
function_drit[function_name] = function_parameter_drit
# print(function_list,function_drit,function_code_drit)
else:
msg.showerror("Error", "SyntaxError"+"\nThe Wrong: "+commands_texts[0])
commands_text.pop(0)
elif "value:" in commands_text[0]:
h2 = hhh("value:")
if len(h2) == 4:
if h2[1] == "int":
try:
intnum = int(h2[2])
except:
msg.showerror("Error", "TypeError")
else:
if infunction == True:
if h2[3] == true:
global_var[h2[0]] = ["int",intnum,functionname]
elif h2[3] == false:
local_var[h2[0]] = ["int",intnum,functionname]
else:
msg.showerror("Error", "SyntaxError")
else:
if h2[3] == true:
global_var[h2[0]] = ["int",intnum,None]
elif h2[3] == false:
local_var[h2[0]] = ["int",intnum,None]
else:
msg.showerror("Error", "SyntaxError")
elif h2[1] == "str":
try:
strnum = str(h2[2])
except:
msg.showerror("Error", "TypeError")
else:
if infunction == True:
if h2[3] == true:
global_var[h2[0]] = ["str",strnum,functionname]
elif h2[3] == false:
local_var[h2[0]] = ["str",strnum,functionname]
else:
msg.showerror("Error", "SyntaxError")
else:
if h2[3] == true:
global_var[h2[0]] = ["str",strnum,None]
elif h2[3] == false:
local_var[h2[0]] = ["str",strnum,None]
else:
msg.showerror("Error", "SyntaxError")
elif h2[1] == "float":
try:
floatnum = float(h2[2])
except:
msg.showerror("Error", "TypeError")
else:
if infunction == True:
if h2[3] == true:
global_var[h2[0]] = ["float", floatnum, functionname]
elif h2[3] == false:
local_var[h2[0]] = ["float", floatnum, functionname]
else:
msg.showerror("Error", "SyntaxError")
else:
if h2[3] == true:
global_var[h2[0]] = ["float", floatnum, None]
elif h2[3] == false:
local_var[h2[0]] = ["float", floatnum, None]
else:
msg.showerror("Error", "SyntaxError")
else:
msg.showerror("Error", "ValueError")
else:
msg.showerror("Error", "SyntaxError")
commands_text.pop(0)
print(global_var, local_var)
elif "#" in commands_text[0]:
hhh("#") # 行注释
commands_text.pop(0)
elif commands_text[0] == "":
# 当用户为了美观不写任何代码时,忽略此行
commands_text.pop(0)
else:
function_name_check = function_list.copy()
if function_name_check != []:
self.have = False
while function_name_check != []:
if function_name_check[0] in commands_text[0]:
functioncode = function_code_drit[function_name_check[0]]
self.have = True
break
else:
function_name_check.pop(0)
if self.have != False:
r1 = hhh(function_name_check[0]+":")
RunMainStartFunction(True, functioncode,function_name_check[0],r1)
commands_text.pop(0)
else:
msg.showerror("Error", "NameError1"+"\nThe Wrong: "+commands_texts[0])
break
else:
msg.showerror("Error", "NameError"+"\nThe Wrong: "+commands_texts[0])
break
self.have = False
RunMainStartFunction()
# 创建文件菜单
file = Menu(mb, tearoff=False)
file.add_command(label="Open ", command="")
file.add_command(label="Save ", command="")
file.add_command(label="Run ", command=run)
file.add_separator()
file.add_command(label="Exit ", command=root.quit)
mb.add_cascade(label=" File ", menu=file)
WinMian()
mainloop()
运行后在最上边的文本文档里边儿输入此代码,可以看到解析的结果哟(可以加上缩进,[b]但注意一定要是缩进[/b]):value:e1^int^123^False
function:hhh^a^b
{
pm:None^abcdefg
printf:hello, world
value:ints^int^123^False
printf:ints
printf:e1
}
# ...
hhh:12
printf:========
function:exercise
{
value:e2^str^hello123456^False
printf:e2
printf:e1^e2
}
exercise:
你也可以由此源代码继续发展,获得一个真正的编程语言。
我有注释,你可以查看代码进行继续编写
定义关键字的函数: hhh()
我把它单独提炼了出来
代码:# 定义关键字
def hhh(name):
name_list = []
error = False
for w2 in name:
name_list.append(w2)
"""
commands_text 是一个列表是用户输入的内容。
commands_text = self.text.get("0.0","end").split("\n")
"""
commandd = list(commands_text[0])
while name_list != []:
if name_list[0] == commandd[0]:
name_list.pop(0)
commandd.pop(0)
else:
msg.showerror("Error", "SyntaxError"+"\nThe Wrong: "+commands_texts[0])
error = True
break
# print(commandd)
return_list = []
if error == False:
string = ""
while commandd != []:
# print(commandd)
if commandd[0] != "^": # 我在这儿以“^”作为分隔符
string = string + commandd[0]
commandd.pop(0)
if commandd == []:
return_list.append(string)
break
else:
return_list.append(string)
string = ""
commandd.pop(0)
if commandd == []:
break
return return_list # 返回值是一个列表
# 例如: printf:hello, world^12345
# 返回: ["hello, world", 12345]
|
|