<s> """<STR_LIT>""" <EOL> import sys <EOL> import semanticCommand <EOL> import generativeDefiner <EOL> import conceptualHierarchy <EOL> import grammarRule <EOL> import ellyChar <EOL> import ellyBits <EOL> class Code ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , lstg ) : <EOL> """<STR_LIT>""" <EOL> self . lstg = lstg <EOL> self . indx = <NUM_LIT:0> <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . indx >= len ( self . lstg ) : <EOL> return semanticCommand . Gretn <EOL> else : <EOL> x = self . lstg [ self . indx ] <EOL> self . indx += <NUM_LIT:1> <EOL> return x <EOL> def skip ( self ) : <EOL> """<STR_LIT>""" <EOL> self . indx = self . lstg [ self . indx ] <EOL> BAD = - <NUM_LIT> <EOL> LSTJ = '<STR_LIT:U+002C>' <EOL> PNAM = '<STR_LIT>' <EOL> space = { u'<STR_LIT:\n>' : u'<STR_LIT>' , u'<STR_LIT:\r>' : u'<STR_LIT>' , u'<STR_LIT:U+0020>' : ellyChar . NBS , u'<STR_LIT:\t>' : u'<STR_LIT>' } <EOL> def despace ( strs ) : <EOL> """<STR_LIT>""" <EOL> lstg = [ ] <EOL> for c in strs : <EOL> if c in space : <EOL> lstg . append ( space [ c ] ) <EOL> else : <EOL> lstg . append ( c ) <EOL> return u'<STR_LIT>' . join ( lstg ) <EOL> class GenerativeProcedure ( object ) : <EOL> """<STR_LIT>""" <EOL> _error = "<STR_LIT>" <EOL> def __init__ ( self , syms , defs ) : <EOL> """<STR_LIT>""" <EOL> self . logic = generativeDefiner . compileDefinition ( syms , defs ) <EOL> def __unicode__ ( self ) : <EOL> """<STR_LIT>""" <EOL> generativeDefiner . showCode ( self . logic ) <EOL> def doRun ( self , cntx , phrs ) : <EOL> """<STR_LIT>""" <EOL> phs = phrs <EOL> while True : <EOL> if self . run ( cntx , phs ) : <EOL> break <EOL> phs . krnl . bias = BAD <EOL> phs . order ( ) <EOL> if phs . krnl . bias == BAD : <EOL> return False <EOL> if phs . krnl . ctxc != conceptualHierarchy . NOname : cntx . cncp = phs . krnl . ctxc <EOL> if phs . alnk != None : <EOL> mnb = phs . krnl . bias - <NUM_LIT:1> <EOL> phi = phs <EOL> rls = [ ] <EOL> while phi != None : <EOL> if phi . krnl . bias >= mnb and not phi . krnl . rule in rls : <EOL> rls . append ( phi . krnl . rule ) <EOL> phi = phi . alnk <EOL> ru = rls [ <NUM_LIT:0> ] <EOL> rls = rls [ <NUM_LIT:1> : ] <EOL> if len ( rls ) > <NUM_LIT:0> : <EOL> if ru . bias == <NUM_LIT:0> : <EOL> ru . bias = - <NUM_LIT:1> <EOL> else : <EOL> for ru in rls : <EOL> ru . bias = <NUM_LIT:0> <EOL> return True <EOL> def run ( self , cntx , phrs , pnam = None ) : <EOL> """<STR_LIT>""" <EOL> if self . logic == None : <EOL> return True <EOL> if phrs == None : <EOL> print >> sys . stderr , '<STR_LIT>' , self <EOL> return False <EOL> cntx . pushStack ( ) <EOL> if pnam != None : <EOL> cntx . defineLocalVariable ( PNAM , pnam ) <EOL> code = Code ( self . logic ) <EOL> status = True <EOL> while status : <EOL> op = code . next ( ) <EOL> if op < <NUM_LIT:0> : break <EOL> if op == semanticCommand . Gnoop : <EOL> pass <EOL> elif op == semanticCommand . Gretn : <EOL> break <EOL> elif op == semanticCommand . Gfail : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> status = False <EOL> elif op == semanticCommand . Gleft : <EOL> if phrs . krnl . lftd == None : <EOL> print >> sys . stderr , '<STR_LIT>' , phrs <EOL> status = False <EOL> else : <EOL> status = phrs . krnl . lftd . krnl . rule . gens . doRun ( cntx , phrs . krnl . lftd ) <EOL> elif op == semanticCommand . Grght : <EOL> if phrs . krnl . rhtd == None : <EOL> if phrs . krnl . lftd == None : <EOL> print >> sys . stderr , '<STR_LIT>' , phrs <EOL> status = False <EOL> status = phrs . krnl . lftd . krnl . rule . gens . doRun ( cntx , phrs . krnl . lftd ) <EOL> else : <EOL> status = phrs . krnl . rhtd . krnl . rule . gens . doRun ( cntx , phrs . krnl . rhtd ) <EOL> elif op == semanticCommand . Gblnk : <EOL> cntx . insertCharsIntoBuffer ( u'<STR_LIT:U+0020>' ) <EOL> elif op == semanticCommand . Glnfd : <EOL> cntx . insertCharsIntoBuffer ( u'<STR_LIT>' ) <EOL> elif op == semanticCommand . Gsplt : <EOL> cntx . splitBuffer ( ) <EOL> elif op == semanticCommand . Gback : <EOL> cntx . backBuffer ( ) <EOL> elif op == semanticCommand . Gmrge : <EOL> cntx . mergeBuffers ( ) <EOL> elif op == semanticCommand . Gchng : <EOL> ts = code . next ( ) <EOL> ss = code . next ( ) <EOL> cntx . mergeBuffersWithReplacement ( ts , ss ) <EOL> elif ( op == semanticCommand . Gchck or <EOL> op == semanticCommand . Gnchk ) : <EOL> sens = ( op == semanticCommand . Gchck ) <EOL> ids = code . next ( ) <EOL> ref = code . next ( ) <EOL> val = cntx . getLocalVariable ( ids ) <EOL> if ( val in ref ) == sens : <EOL> code . next ( ) <EOL> continue <EOL> code . skip ( ) <EOL> elif op == semanticCommand . Gchkf : <EOL> refr = code . next ( ) <EOL> dblb = phrs . krnl . semf . compound ( ) <EOL> if ellyBits . check ( dblb , refr ) : <EOL> code . next ( ) <EOL> continue <EOL> code . skip ( ) <EOL> elif op == semanticCommand . Gskip : <EOL> code . skip ( ) <EOL> elif op == semanticCommand . Gvar : <EOL> var = code . next ( ) <EOL> val = code . next ( ) <EOL> cntx . defineLocalVariable ( var , val ) <EOL> elif op == semanticCommand . Gpeek : <EOL> var = code . next ( ) <EOL> sns = code . next ( ) <EOL> val = cntx . peekIntoBuffer ( nxtb = sns ) <EOL> cntx . setLocalVariable ( var , val ) <EOL> elif ( op == semanticCommand . Gset or <EOL> op == semanticCommand . Gextl or <EOL> op == semanticCommand . Gextr ) : <EOL> var = code . next ( ) <EOL> if op == semanticCommand . Gset : <EOL> val = code . next ( ) <EOL> elif op == semanticCommand . Gextl : <EOL> val = cntx . extractCharsFromBuffer ( code . next ( ) , <NUM_LIT:1> ) <EOL> else : <EOL> val = cntx . extractCharsFromBuffer ( code . next ( ) , <NUM_LIT:0> ) <EOL> cntx . setLocalVariable ( var , val ) <EOL> elif ( op == semanticCommand . Ginsr or <EOL> op == semanticCommand . Ginsn ) : <EOL> var = code . next ( ) <EOL> s = cntx . getLocalVariable ( var ) <EOL> if s != '<STR_LIT>' : <EOL> if op == semanticCommand . Ginsn : <EOL> cntx . insertCharsIntoBuffer ( s , <NUM_LIT:1> ) <EOL> else : <EOL> cntx . insertCharsIntoBuffer ( s , <NUM_LIT:0> ) <EOL> elif op == semanticCommand . Gshft : <EOL> cntx . moveCharsBufferToBuffer ( code . next ( ) ) <EOL> elif op == semanticCommand . Gdele : <EOL> cntx . deleteCharsFromBuffer ( code . next ( ) ) <EOL> elif op == semanticCommand . Gdelt : <EOL> s = code . next ( ) <EOL> t = code . next ( ) <EOL> if t > <NUM_LIT:0> : <EOL> cntx . deleteCharsInBufferTo ( s ) <EOL> else : <EOL> cntx . deleteCharsInBufferFrom ( s ) <EOL> elif op == semanticCommand . Gstor : <EOL> var = code . next ( ) <EOL> nde = code . next ( ) <EOL> val = cntx . getDeletion ( ) <EOL> if abs ( nde ) >= len ( val ) : <EOL> val = [ ] <EOL> elif nde > <NUM_LIT:0> : <EOL> val = val [ : - nde ] <EOL> elif nde < <NUM_LIT:0> : <EOL> val = val [ - nde : ] <EOL> cntx . setLocalVariable ( var , val ) <EOL> elif op == semanticCommand . Gfnd : <EOL> cntx . findCharsInBuffer ( code . next ( ) , code . next ( ) ) <EOL> elif op == semanticCommand . Gpick : <EOL> var = code . next ( ) <EOL> s = cntx . getLocalVariable ( var ) <EOL> dct = code . next ( ) <EOL> if not s in dct : <EOL> if '<STR_LIT>' in dct : <EOL> s = '<STR_LIT>' <EOL> else : <EOL> continue <EOL> cntx . insertCharsIntoBuffer ( dct [ s ] ) <EOL> elif op == semanticCommand . Gappd : <EOL> s = code . next ( ) <EOL> cntx . insertCharsIntoBuffer ( s ) <EOL> elif op == semanticCommand . Gget : <EOL> var = code . next ( ) <EOL> gvn = code . next ( ) <EOL> cntx . setLocalVariable ( var , cntx . getGlobalVariable ( gvn ) ) <EOL> elif op == semanticCommand . Gput : <EOL> var = code . next ( ) <EOL> gvn = code . next ( ) <EOL> cntx . setGlobalVariable ( gvn , cntx . getLocalVariable ( var ) ) <EOL> elif op == semanticCommand . Gassg : <EOL> dst = code . next ( ) <EOL> src = code . next ( ) <EOL> cntx . setLocalVariable ( dst , cntx . getLocalVariable ( src ) ) <EOL> elif op == semanticCommand . Gque : <EOL> dst = code . next ( ) <EOL> src = code . next ( ) <EOL> cno = code . next ( ) <EOL> val = cntx . getLocalVariable ( src ) <EOL> if len ( val ) == <NUM_LIT:0> : <EOL> if cno == <NUM_LIT:0> : continue <EOL> it = '<STR_LIT>' <EOL> elif cno <= <NUM_LIT:0> : <EOL> it = cntx . getLocalVariable ( dst ) + val <EOL> else : <EOL> if cno > len ( val ) : <EOL> cno = len ( val ) <EOL> it = val [ : cno ] <EOL> cntx . setLocalVariable ( src , val [ cno : ] ) <EOL> cntx . setLocalVariable ( dst , it ) <EOL> elif op in [ semanticCommand . Gunio , semanticCommand . Gintr , semanticCommand . Gcomp ] : <EOL> dst = code . next ( ) <EOL> src = code . next ( ) <EOL> dv = cntx . getLocalVariable ( dst ) <EOL> sv = cntx . getLocalVariable ( src ) <EOL> lstd = [ ] if len ( dv ) == <NUM_LIT:0> else dv . split ( LSTJ ) <EOL> lsts = [ ] if len ( sv ) == <NUM_LIT:0> else sv . split ( LSTJ ) <EOL> ls = [ ] <EOL> if op == semanticCommand . Gunio : <EOL> for x in lsts : <EOL> if not x in lstd : <EOL> lstd . append ( x ) <EOL> ls = lstd <EOL> elif op == semanticCommand . Gintr : <EOL> for x in lsts : <EOL> if x in lstd : <EOL> ls . append ( x ) <EOL> else : <EOL> for x in lstd : <EOL> if not x in lsts : <EOL> ls . append ( x ) <EOL> js = LSTJ . join ( ls ) <EOL> cntx . setLocalVariable ( dst , js ) <EOL> elif op == semanticCommand . Gobtn : <EOL> tok = cntx . getNthTokenInListing ( phrs . krnl . posn ) <EOL> cntx . insertCharsIntoBuffer ( tok . toUnicode ( ) ) <EOL> elif op == semanticCommand . Gcapt : <EOL> c = cntx . extractCharsFromBuffer ( ) <EOL> if c != '<STR_LIT>' : <EOL> cntx . insertCharsIntoBuffer ( c . upper ( ) , <NUM_LIT:1> ) <EOL> elif op == semanticCommand . Gucpt : <EOL> c = cntx . extractCharsFromBuffer ( ) <EOL> if c != '<STR_LIT>' : <EOL> cntx . insertCharsIntoBuffer ( c . lower ( ) , <NUM_LIT:1> ) <EOL> elif op == semanticCommand . Gtrce : <EOL> cat = cntx . syms . getSyntaxTypeName ( phrs . krnl . typx ) <EOL> brn = <NUM_LIT:1> if isinstance ( phrs . krnl . rule , grammarRule . ExtendingRule ) else <NUM_LIT:2> <EOL> pnam = cntx . getLocalVariable ( PNAM ) <EOL> fm = u'<STR_LIT>' <EOL> fs = fm . format ( phrs . krnl . posn , cat , phrs . krnl . rule . seqn , brn ) <EOL> sys . stderr . write ( fs ) <EOL> sys . stderr . flush ( ) <EOL> cntx . printStatus ( pnam ) <EOL> elif op == semanticCommand . Gshow : <EOL> vr = code . next ( ) <EOL> ms = code . next ( ) <EOL> st = despace ( cntx . getLocalVariable ( vr ) ) <EOL> fm = u'<STR_LIT>' <EOL> fs = fm . format ( phrs . krnl . seqn , ms , vr , st ) <EOL> sys . stderr . write ( fs ) <EOL> sys . stderr . write ( '<STR_LIT:\n>' ) <EOL> sys . stderr . flush ( ) <EOL> elif op == semanticCommand . Gview : <EOL> nc = code . next ( ) <EOL> cbl , nbl = cntx . viewBufferDivide ( nc ) <EOL> fm = u'<STR_LIT>' <EOL> st = fm . format ( phrs . krnl . seqn , str ( cbl ) , str ( nbl ) ) <EOL> sys . stderr . write ( st ) <EOL> sys . stderr . flush ( ) <EOL> elif op == semanticCommand . Gproc : <EOL> name = code . next ( ) <EOL> if name == '<STR_LIT>' : continue <EOL> proc = cntx . getProcedure ( name ) <EOL> if proc == None : <EOL> print >> sys . stderr , '<STR_LIT>' , name <EOL> status = False <EOL> else : <EOL> status = proc . run ( cntx , phrs , name ) <EOL> else : <EOL> print >> sys . stderr , GenerativeProcedure . _error , '<STR_LIT>' , op <EOL> status = False <EOL> cntx . popStack ( ) <EOL> return status <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import ellyDefinitionReader <EOL> import procedureTestFrame <EOL> from generativeDefiner import showCode <EOL> frame = procedureTestFrame . ProcedureTestFrame ( ) <EOL> phr = frame . phrase <EOL> ctx = frame . context <EOL> stb = ctx . syms <EOL> args = sys . argv [ <NUM_LIT:1> : ] if len ( sys . argv ) > <NUM_LIT:1> else [ '<STR_LIT>' ] <EOL> for srcn in args : <EOL> ctx . clearLocalStack ( ) <EOL> ctx . clearBuffers ( ) <EOL> print "<STR_LIT>" , srcn <EOL> inp = ellyDefinitionReader . EllyDefinitionReader ( srcn ) <EOL> if inp . error != None : <EOL> print >> sys . stderr , "<STR_LIT>" , srcn <EOL> print >> sys . stderr , inp . error <EOL> continue <EOL> for ln in inp . buffer : <EOL> print ln <EOL> print '<STR_LIT>' <EOL> gp = GenerativeProcedure ( stb , inp ) <EOL> print '<STR_LIT>' <EOL> if gp . logic != None : <EOL> showCode ( gp . logic ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> continue <EOL> res = gp . doRun ( ctx , phr ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' , res <EOL> print '<STR_LIT>' <EOL> frame . showBuffers ( ) <EOL> print "<STR_LIT>" <EOL> print len ( args ) , '<STR_LIT>' </s>
