def equationToken(string,list_pt=None): sign=None equation=None equal=None if list_pt==None: list_pt=[] if string[0]=='-': sign=NetP('-') list_pt.append(sign) string=string[1:] [formula,string,list_pt]=formulaToken(string,list_pt) equation=formula if sign!=None: sign.con(None,formula) if string=='': return [equation,string,list_pt] elif string[0]=='=': equal=NetP('=') list_pt.append(equal) string=string[1:] [point,string,list_pt]=equationToken(string,list_pt) equal.con(equation,point) if string=='': return [equation,string,list_pt] raise Exception('Error: wrong equation format!')
def formulaToken(string,list_pt): [formula,string,list_pt]=variableToken(string,list_pt) if string=='': return [formula,string,list_pt] elif string[0]=='+' or string[0]=='-' or string[0]=='*' or string[0]=='/' or string[0]=='^': relation=NetP(string[0]) list_pt.append(relation) string=string[1:] [point,string,list_pt]=formulaToken(string,list_pt) relation.con(formula,point) return [formula,string,list_pt]
def inputToken(string,list_pt): [inputs,string,list_pt]=formulaToken(string,list_pt) last=inputs while string!='' and string[0]==',': relation=NetP('and') list_pt.append(relation) string=string[1:] [point,string,list_pt]=formulaToken(string,list_pt) relation.con(last,point) last=point return [inputs,string,list_pt]
def SmileiToken(code,title,list_pt=None): if list_pt==None: list_pt=[] point=NetP('smilei') point.m_text=title list_pt.append(point) code=re.sub(r'[ \t]','',code) code=re.sub(r'#.*\n','',code) while code!='': code=re.sub(r'^\n*','',code) if code=='': break else: [code,line_pt]=lineSmToken(code,list_pt) con=NetP('in') list_pt.append(con) con.con(point,line_pt) return [code,list_pt]
def register(self,dev_pt,mode=2): if dev_pt.m_dev==None: return else: device=dev_pt.m_dev if mode==0 or mode==2: if device not in self.m_outDev: self.m_outDev.append(device) device.m_inPool=self con=NetP('output') con.con(self.m_self,dev_pt) # self.m_sysPool.append(con) if mode==1 or mode==2: if device not in self.m_inDev: self.m_inDev.append(device) device.m_outPool=self con=NetP('input') con.con(self.m_self,dev_pt)
def lineSmToken(code,list_pt): nameFormat=r'^[\w\.]+' name=re.match(nameFormat,code).group() if name=='': raise Exception('Error! Invalid name of function or variable!') code=re.sub(nameFormat,'',code) line_pt=NetP(name) list_pt.append(line_pt) if code!='' and code[0]=='=': code=code[1:] [code,content]=conSmToken(code) if content=='': raise Exception('Error! Invalid assignment value!') line_pt.m_text=content elif code!='' and code[0]=='(': code=code[1:] code=re.sub(r'^\n*','',code) [code,var]=varSmToken(code,list_pt) con=NetP('in') list_pt.append(con) con.con(line_pt,var) while True: if code!='' and code[0]==',': code=code[1:] else: break code=re.sub(r'^\n*','',code) if code!='' and code[0]==')': break [code,var]=varSmToken(code,list_pt) con=NetP('in') list_pt.append(con) con.con(line_pt,var) code=re.sub(r'^\n*','',code) if code=='' or code[0]!=')': raise Exception('Error! Unbalanced bracket!') code=code[1:] return [code,line_pt]
def variableToken(string,list_pt): variable_name='' variable=None if string=='': raise Exception('Error: empty variable.') elif string[0]=='(': sign=None variable=NetP('括号') list_pt.append(variable) relation=NetP('in') list_pt.append(relation) string=string[1:] if string[0]=='-': sign=NetP('-') list_pt.append(sign) [point,string,list_pt]=formulaToken(string,list_pt) relation.con(variable,point) if sign!=None: sign.con(None,point) if string=='' or string[0]!=')': raise Exception('Error: unbalanced bracket!') else: string=string[1:] elif string[0].isdigit(): while True: if string!='' and string[0].isdigit(): variable_name+=string[0] string=string[1:] else: break variable=NetP(variable_name) list_pt.append(variable) elif string[0].isalpha(): while True: if string=='': break elif string[0].isdigit() or string[0].isalpha(): variable_name+=string[0] string=string[1:] else: break variable=NetP(variable_name) list_pt.append(variable) if string!='' and string[0]=='(': relation=NetP('in') list_pt.append(relation) string=string[1:] [inputs,string,list_pt]=inputToken(string,list_pt) relation.con(variable,inputs) if string=='' or string[0]!=')': raise Exception('Error: unbalanced brackets.') else: string=string[1:] return [variable,string,list_pt]