Best JavaScript code snippet using playwright-internal
complier.js
Source:complier.js  
1//==============================================================================2// complier.js - no indexing3//==============================================================================4//==============================================================================5// overall6//==============================================================================7function compile (rules)8 {var code = '';9  code += prettycompile(rules);10  code += prettyprogram(rules);11  return code}12//==============================================================================13// normalize14//==============================================================================15function normalize (rules)16 {var out = [];17  for (var i=0; i<rules.length; i++)18      {out.push(normalizerule(rules[i]))};19  return out}20function normalizerule (rule)21 {if (symbolp(rule)) {return seq('rule',rule,true)};22  if (rule[0]==='transition') {return rule};23  if (rule[0]==='definition') {return rule};24  var newrule= seq('rule',rule[1]);25  for (var i=2; i<rule.length; i++)26      {newrule.push(normalizesubgoal(rule[i]))};27  return newrule}28function normalizesubgoal (subgoal)29 {if (symbolp(subgoal)) {return subgoal};30  if (subgoal[0]==='matches')31     {var query = seq('matches',subgoal[1],subgoal[2]);32      var result = seq('cons',newvar(),listify(subgoal.slice(3)));33      return seq('evaluate',query,result)};34  if (subgoal[0]==='submatches')35     {var query = seq('submatches',subgoal[1],subgoal[2]);36      var result = seq('cons',subgoal[3],newvar());37      return seq('evaluate',query,result)};38  if (builtinp(subgoal[0]))39     {return seq('evaluate',subgoal.slice(0,-1),subgoal[subgoal.length-1])};40  if (mathp(subgoal[0]))41     {return seq('evaluate',subgoal.slice(0,-1),subgoal[subgoal.length-1])};42  if (listop(subgoal[0]))43     {return seq('evaluate',subgoal.slice(0,-1),subgoal[subgoal.length-1])};44  if (subgoal[0]==='map')45     {return seq('evaluate',subgoal.slice(0,-1),subgoal[subgoal.length-1])};46  if (subgoal[0]==='setofall')47     {return seq('evaluate',subgoal.slice(0,-1),subgoal[subgoal.length-1])};48  if (subgoal[0]==='countofall')49     {return seq('evaluate',subgoal.slice(0,-1),subgoal[subgoal.length-1])};50  return subgoal}51//==============================================================================52// compile53//==============================================================================54//------------------------------------------------------------------------------55// prettycompile56//------------------------------------------------------------------------------57function prettycompile (rules)58 {var code = '';59  var views = getviewsfromrules(rules);60  for (var i=0; i<views.length; i++)61      {code += prettyjs(compileoneview(views[i],rules));62       code += prettyjs(compileallview(views[i],rules))};63  var atoms = getatomsfromrules(rules);64  for (var i=0; i<atoms.length; i++)65      {code += prettyjs(compileoneatomlist(atoms[i],rules));66       code += prettyjs(compileallatomlist(atoms[i],rules))};67  var bases = getbasesfromrules(rules);68  for (var i=0; i<bases.length; i++)69      {code += prettyjs(compileonebase(bases[i]));70       code += prettyjs(compileallbase(bases[i]))};71  return code}72//------------------------------------------------------------------------------73// compileoneview74//------------------------------------------------------------------------------75function compileoneview (view,rules)76 {var data = indexees(view,rules);77  var code = seq('function',single(view),seq('query','al','facts','rules'));78  code.push(seq('bind','answer','false'));79  for (var i=0; i<data.length; i++)80      {var rule = data[i];81       if (atomp(rule)) {rule = seq('rule',rule,'true')};82       code.push(compileoneviewrule(rule))};83  code.push(seq('return','answer'));84  return code}85function compileoneviewrule (rule)86 {var hlist = vars(rule[1]);87  var blist = {};88  var code = seq('block');89  code.push(seq('bind','head',codify(rule[1],[],{})));90  for (var i=2; i<rule.length; i++)91      {code.push(seq('bind','sub'+(i-1),codify(rule[i],[],{})))};92  code.push(seq('bind','bl',seq('obj')));93  code.push(seq('bind','ol',seq('seq')));94  var cond = seq('vnify','query','al','head','bl','ol');95  var subcode = seq('block');96  subcode.push(compileoneviewsubgoals(rule,2,hlist,blist));97  subcode.push(seq('backup','ol'));98  code.push(seq('if',cond,subcode));99  return code};100function compileoneviewsubgoals (rule,n,hlist,blist)101 {if (n>=rule.length)102     {var result = codify(rule[1],hlist,blist);103      return seq('block',seq('bind','answer',result))};104  return compileoneviewsubgoal(rule,n,hlist,blist)}105function compileoneviewsubgoal (rule,n,hlist,alist)106 {if (symbolp(rule[n]))107     {return compileoneviewatom(rule,n,hlist,alist)};108  if (rule[n][0]==='same')109     {return compileoneviewsame(rule,n,hlist,alist)};110  if (rule[n][0]==='distinct')111     {return compileoneviewdistinct(rule,n,hlist,alist)};112  if (rule[n][0]==='evaluate')113     {return compileoneviewevaluate(rule,n,hlist,alist)};114  if (rule[n][0]==='true' && rule[n].length===3)115     {return compileoneviewtrue(rule,n,hlist,alist)};116  if (rule[n][0]==='not')117     {return compileoneviewnot(rule,n,hlist,alist)};118  if (compgroundp(rule[n],alist))119     {return compileoneviewbound(rule,n,hlist,alist)};120  if (n===rule.length-1)121     {return compileoneviewlast(rule,n,hlist,alist)};122  return compileoneviewdb(rule,n,hlist,alist)}123function compileoneviewatom (rule,n,hlist,alist)124 {if (rule[n]==='true') {return compileoneviewsubgoals(rule,n+1,hlist,alist)};125  if (rule[n]==='false') {return 'false'};126  var subroutine = single(rule[n]);127  var cond = seq(subroutine,kwotify(rule[n]),'bl','facts','rules');128  return seq('if',cond,compileoneviewsubgoals(rule,n+1,hlist,alist))}129function compileoneviewsame (rule,n,hlist,alist)130 {var x = codify(rule[n][1],hlist,alist);131  var y = codify(rule[n][2],hlist,alist);132  var cond = seq('equalp',x,y);133  var code = compileoneviewsubgoals(rule,n+1,hlist,alist);134  return seq('if',cond,code)}135function compileoneviewdistinct (rule,n,hlist,alist)136 {var x = codify(rule[n][1],hlist,alist);137  var y = codify(rule[n][2],hlist,alist);138  var cond = seq('equalp',x,y);139  var code = compileoneviewsubgoals(rule,n+1,hlist,alist);140  return seq('if',seq('not',cond),code)}141function compileoneviewevaluate (rule,n,hlist,alist)142 {var subvar = 'sub'+(n-1);143  var datavar = 'l'+(n-1);144  var subgoal = seq('companswerx',subvar,'bl','facts','rules');145  var block = seq('block');146  block.push(seq('bind',datavar,subgoal));147  compilematch(rule[n],datavar,hlist,alist,'ol');148  block.push(seq('if',datavar,compileoneviewsubgoals(rule,n+1,hlist,alist)));149  return block}150function compileoneviewtrue (rule,n,hlist,alist)151 {var subvar = 'sub'+(n-1);152  var datavar = 'l'+(n-1);153  var indvar = 'i'+(n-1);154  var olvar = 'ol'+(n-1);155  var source = seq('sub','datasets',kwotify(rule[n][2]));156  var subgoal = seq(plural(operator(rule[n][1])),seq('sub',subvar,'1'),'bl',source,'rules');157  var block = seq('block');158  block.push(seq('bind',datavar,subgoal));159  compilematch(rule[n][1],seq('sub',datavar,indvar),hlist,alist,olvar);160  var bind = seq('maatchify',seq('sub',subvar,'1'),'bl',seq('sub',datavar,indvar),'bl',olvar);161  var code = compileoneviewsubgoals(rule,n+1,hlist,alist);162  var inner = seq('block');163  inner.push(seq('bind',olvar,seq('seq')));164  inner.push(bind);165  inner.push(code);166  inner.push(seq('backup',olvar));167  block.push(seq('loop',indvar,'0',datavar+'.length',inner));168  return block}169function compileoneviewnot (rule,n,hlist,blist)170 {var cond = compilecall(rule[n][1],hlist,blist); 171  var code = compileoneviewsubgoals(rule,n+1,hlist,blist);172  return seq('if',seq('not',cond),code)}173function compileoneviewbound (rule,n,hlist,alist)174 {var subroutine = single(operator(rule[n]));175  var cond = seq(subroutine,codify(rule[n],hlist,alist),'bl','facts','rules');176  return seq('if',cond,compileoneviewsubgoals(rule,n+1,hlist,alist))}177function compileoneviewlast (rule,n,hlist,alist)178 {var subvar = 'sub'+(n-1);179  var datavar = 'l'+(n-1);180  var subgoal = seq(single(operator(rule[n])),subvar,'bl','facts','rules');181  var block = seq('block');182  block.push(seq('bind',datavar,subgoal));183  compilematch(rule[n],datavar,hlist,alist,'ol');184  block.push(seq('if',datavar,compileoneviewsubgoals(rule,n+1,hlist,alist)));185  return block}186function compileoneviewdb (rule,n,hlist,alist)187 {var subvar = 'sub'+(n-1);188  var datavar = 'l'+(n-1);189  var indvar = 'i'+(n-1);190  var olvar = 'ol'+(n-1);191  var subgoal = seq(plural(operator(rule[n])),subvar,'bl','facts','rules');192  var block = seq('block');193  block.push(seq('bind',datavar,subgoal));194  compilematch(rule[n],seq('sub',datavar,indvar),hlist,alist,olvar);195  var bind = seq('maatchify',subvar,'bl',seq('sub',datavar,indvar),'bl',olvar);196  var code = compileoneviewsubgoals(rule,n+1,hlist,alist);197  var inner = seq('block');198  inner.push(seq('bind',olvar,seq('seq')));199  inner.push(bind);200  inner.push(code);201  inner.push(seq('backup',olvar));202  inner.push(seq('if','answer','break'));203  block.push(seq('loop',indvar,'0',datavar+'.length',inner));204  return block}205//------------------------------------------------------------------------------206// compileallview207//------------------------------------------------------------------------------208function compileallview (view,rules)209 {var data = indexees(view,rules);210  var code = seq('function',plural(view),seq('query','al','facts','rules'));211  code.push(seq('bind','answers',seq('seq')));212  for (var i=0; i<data.length; i++)213      {var rule = data[i];214       if (atomp(rule)) {rule = seq('rule',rule,'true')};215       code.push(compileallviewrule(rule))};216  code.push(seq('return','answers'));217  return code}218function compileallviewrule (rule)219 {var hlist = vars(rule[1]);220  var blist = {};221  var code = seq('block');222  code.push(seq('bind','head',codify(rule[1],[],{})));223  for (var i=2; i<rule.length; i++)224      {code.push(seq('bind','sub'+(i-1),codify(rule[i],[],{})))};225  code.push(seq('bind','bl',seq('obj')));226  code.push(seq('bind','ol',seq('seq')));227  var cond = seq('vnify','query','al','head','bl','ol');228  var subcode = seq('block');229  subcode.push(compileallviewsubgoals(rule,2,hlist,blist));230  subcode.push(seq('backup','ol'));231  code.push(seq('if',cond,subcode));232  return code};233function compileallviewsubgoals (rule,n,hlist,blist)234 {if (n>=rule.length)235     {var result = codify(rule[1],hlist,blist);236      return seq('block',seq('answers.push',result))};237  return compileallviewsubgoal(rule,n,hlist,blist)}238function compileallviewsubgoal (rule,n,hlist,alist)239 {if (symbolp(rule[n]))240     {return compileallviewatom(rule,n,hlist,alist)};241  if (rule[n][0]==='same')242     {return compileallviewsame(rule,n,hlist,alist)};243  if (rule[n][0]==='distinct')244     {return compileallviewdistinct(rule,n,hlist,alist)};245  if (rule[n][0]==='evaluate')246     {return compileallviewevaluate(rule,n,hlist,alist)};247  if (rule[n][0]==='true' && rule[n].length===3)248     {return compileallviewtrue(rule,n,hlist,alist)};249  if (rule[n][0]==='not')250     {return compileallviewnot(rule,n,hlist,alist)};251  if (compgroundp(rule[n],alist))252     {return compileallviewbound(rule,n,hlist,alist)};253  return compileallviewdb(rule,n,hlist,alist)}254function compileallviewatom (rule,n,hlist,alist)255 {if (rule[n]==='true') {return compileallviewsubgoals(rule,n+1,hlist,alist)};256  if (rule[n]==='false') {return 'false'};257  var subroutine = single(rule[n]);258  var cond = seq(subroutine,kwotify(rule[n]),'bl','facts','rules');259  return seq('if',cond,compileallviewsubgoals(rule,n+1,hlist,alist))}260function compileallviewsame (rule,n,hlist,alist)261 {var x = codify(rule[n][1],hlist,alist);262  var y = codify(rule[n][2],hlist,alist);263  var cond = seq('equalp',x,y);264  var code = compileallviewsubgoals(rule,n+1,hlist,alist);265  return seq('if',cond,code)}266function compileallviewdistinct (rule,n,hlist,alist)267 {var x = codify(rule[n][1],hlist,alist);268  var y = codify(rule[n][2],hlist,alist);269  var cond = seq('equalp',x,y);270  var code = compileallviewsubgoals(rule,n+1,hlist,alist);271  return seq('if',seq('not',cond),code)}272function compileallviewevaluate (rule,n,hlist,alist)273 {var subvar = 'sub'+(n-1);274  var datavar = 'l'+(n-1);275  var subgoal = seq('companswerx',subvar,'bl','facts','rules');276  var block = seq('block');277  block.push(seq('bind',datavar,subgoal));278  compilematch(rule[n],datavar,hlist,alist,'ol');279  block.push(seq('if',datavar,compileallviewsubgoals(rule,n+1,hlist,alist)));280  return block}281function compileallviewtrue (rule,n,hlist,alist)282 {var subvar = 'sub'+(n-1);283  var datavar = 'l'+(n-1);284  var indvar = 'i'+(n-1);285  var olvar = 'ol'+(n-1);286  var source = seq('sub','datasets',kwotify(rule[n][2]));287  var subgoal = seq(plural(operator(rule[n][1])),seq('sub',subvar,'1'),'bl',source,'rules');288  var block = seq('block');289  block.push(seq('bind',datavar,subgoal));290  compilematch(rule[n][1],seq('sub',datavar,indvar),hlist,alist,olvar);291  var bind = seq('maatchify',seq('sub',subvar,'1'),'bl',seq('sub',datavar,indvar),'bl',olvar);292  var code = compileallviewsubgoals(rule,n+1,hlist,alist);293  var inner = seq('block');294  inner.push(seq('bind',olvar,seq('seq')));295  inner.push(bind);296  inner.push(code);297  inner.push(seq('backup',olvar));298  block.push(seq('loop',indvar,'0',datavar+'.length',inner));299  return block}300function compileallviewnot (rule,n,hlist,blist)301 {var cond = compilecall(rule[n][1],hlist,blist);302  var code = compileallviewsubgoals(rule,n+1,hlist,blist);303  return seq('if',seq('not',cond),code)}304function compileallviewbound (rule,n,hlist,alist)305 {var subroutine = single(operator(rule[n]));306  var cond = seq(subroutine,codify(rule[n],hlist,alist),'bl','facts','rules');307  return seq('if',cond,compileallviewsubgoals(rule,n+1,hlist,alist))}308function compileallviewdb (rule,n,hlist,alist)309 {var subvar = 'sub'+(n-1);310  var datavar = 'l'+(n-1);311  var indvar = 'i'+(n-1);312  var olvar = 'ol'+(n-1);313  var subgoal = seq(plural(operator(rule[n])),subvar,'bl','facts','rules');314  var block = seq('block');315  block.push(seq('bind',datavar,subgoal));316  compilematch(rule[n],seq('sub',datavar,indvar),hlist,alist,olvar);317  var bind = seq('maatchify',subvar,'bl',seq('sub',datavar,indvar),'bl',olvar);318  var code = compileallviewsubgoals(rule,n+1,hlist,alist);319  var inner = seq('block');320  inner.push(seq('bind',olvar,seq('seq')));321  inner.push(bind);322  inner.push(code);323  inner.push(seq('backup',olvar));324  block.push(seq('loop',indvar,'0',datavar+'.length',inner));325  return block}326//------------------------------------------------------------------------------327function compilecall (subgoal,hlist,blist)328 {if (symbolp(subgoal))329     {return compilecallatom(subgoal,hlist,blist)};330  if (subgoal[0]==='same')331     {return compilecallsame(subgoal,hlist,blist)};332  if (subgoal[0]==='distinct')333     {return compilecalldistinct(subgoal,hlist,blist)};334  if (subgoal[0]==='evaluate')335     {return compilecallevaluate(subgoal,hlist,blist)};336  if (subgoal[0]==='true' && subgoal.length===3)337     {return compilecalltrue(subgoal,hlist,blist)};338  return compilecalldb(subgoal,hlist,blist)}339function compilecallatom (subgoal,hlist,blist)340 {return seq('$' + subgoal + '$',seq('seq'),'facts','rules')}341function compilecallsame (subgoal,hlist,blist)342 {var x1 = codify(subgoal[1],[],blist);343  var x2 = codify(subgoal[2],[],blist);344  return seq('equalp',x1,x2)}345function compilecalldistinct (subgoal,hlist,blist)346 {var x1 = codify(subgoal[1],[],blist);347  var x2 = codify(subgoal[2],[],blist);348  return seq('not',seq('equalp',x1,x2))}349function compilecallevaluate (subgoal,hlist,blist)350 {var query = codify(subgoal,hlist,blist);351  return seq('companswerx',query,'bl','facts','rules')}352function compilecalltrue (subgoal,hlist,blist)353 {var source = seq('sub','datasets',kwotify(subgoal[2]));354  var subroutine = single(operator(subgoal[1]));355  var query = codify(subgoal[1],hlist,blist);356  return seq(subroutine,query,seq('seq'),source,'rules')}357function compilecalldb (subgoal,hlist,blist)358 {var subroutine = single(operator(subgoal));359  var query = codify(subgoal,hlist,blist);360  return seq(subroutine,query,seq('seq'),'facts','rules')}361//------------------------------------------------------------------------------362function compilecalls (subgoal,hlist,blist)363 {if (symbolp(subgoal))364     {return compilecallsdb(subgoal,hlist,blist)};365  if (subgoal[0]==='true' && subgoal.length===3)366     {return compilecallstrue(subgoal,hlist,blist)};367  return compilecallsdb(subgoal,hlist,blist)}368function compilecallstrue (subgoal,hlist,blist)369 {var source = seq('sub','datasets',kwotify(subgoal[2]));370  var subroutine = plural(operator(subgoal[1]));371  var query = codify(subgoal[1],hlist,blist);372  return seq(subroutine,query,seq('seq'),source,'rules')}373function compilecallsdb (subgoal,hlist,blist)374 {var subroutine = plural(operator(subgoal));375  var query = codify(subgoal,hlist,blist);376  return seq(subroutine,query,seq('seq'),'facts','rules')}377//------------------------------------------------------------------------------378// compileoneatomlist379//------------------------------------------------------------------------------380function compileoneatomlist (view,rules)381 {var name = single(view);382  var params = ['query','al','facts','rules'];383  var dataname = static(view);384  var code = ['baseanswerx','query','al',dataname];385  return seq('function',name,params,seq('return',code))}386//------------------------------------------------------------------------------387// compileallatomlist388//------------------------------------------------------------------------------389function compileallatomlist (view,rules)390 {var name = plural(view);391  var params = ['query','al','facts','rules'];392  var dataname = static(view);393  var code = ['baseanswers','query','al',dataname];394  return seq('function',name,params,seq('return',code))}395//------------------------------------------------------------------------------396// compileonebase397//------------------------------------------------------------------------------398function compileonebase (rel)399 {var params = ['query','al','facts','rules'];400  return seq('function',single(rel),params,401             seq('return',seq('dataanswerx').concat(params)))}402//------------------------------------------------------------------------------403// compileallbase404//------------------------------------------------------------------------------405function compileallbase (rel)406 {var params = ['query','al','facts','rules'];407  return seq('function',plural(rel),params,408             seq('return',seq('dataanswers').concat(params)))}409//==============================================================================410// program411//==============================================================================412//------------------------------------------------------------------------------413// prettyprogram414//------------------------------------------------------------------------------415function prettyprogram (rules)416 {var code = '';417  var views = getviewsfromrules(rules);418  for (var i=0; i<views.length; i++)419      {var arity = getarity(views[i],rules);420       if (arity===0)421          {code += prettyjs(programoneviewbound(views[i],rules))};422       if (arity===1)423          {code += prettyjs(programoneviewbound(views[i],rules));424           code += prettyjs(programoneviewf(views[i],rules));425           code += prettyjs(programallviewf(views[i],rules))};426       if (arity===2)427          {code += prettyjs(programoneviewbound(views[i],rules));428           code += prettyjs(programoneviewbf(views[i],rules));429           code += prettyjs(programoneviewfb(views[i],rules));430           code += prettyjs(programoneviewfree(views[i],rules));431           code += prettyjs(programallviewbf(views[i],rules));432           code += prettyjs(programallviewfb(views[i],rules));433           code += prettyjs(programallviewfree(views[i],rules))};434       if (arity>2)435          {code += prettyjs(programoneviewbound(views[i],rules));436           code += prettyjs(programoneviewfree(views[i],rules));437           code += prettyjs(programallviewfree(views[i],rules))}};438  var atoms = getatomsfromrules(rules);439  for (var i=0; i<atoms.length; i++)440      {var arity = getarity(atoms[i],rules);441       if (arity===0)442          {code += prettyjs(programoneatombound(atoms[i],rules))};443       if (arity===1)444          {code += prettyjs(programoneatombound(atoms[i],rules))445           code += prettyjs(programoneatomf(atoms[i],rules));446           code += prettyjs(programallatomf(atoms[i],rules))};447       if (arity===2)448          {code += prettyjs(programoneatombound(atoms[i],rules))449           code += prettyjs(programoneatombf(atoms[i],rules));450           code += prettyjs(programoneatomfb(atoms[i],rules));451           code += prettyjs(programoneatomfree(atoms[i],rules));452           code += prettyjs(programallatombf(atoms[i],rules));453           code += prettyjs(programallatomfb(atoms[i],rules));454           code += prettyjs(programallatomfree(atoms[i],rules))};455       if (arity>2)456          {code += prettyjs(programoneatombound(atoms[i],rules));457           code += prettyjs(programoneatomfree(atoms[i],rules));458           code += prettyjs(programallatomfree(atoms[i],rules))};459       code += prettyjs(programatomdata(atoms[i],rules))};460  var bases = getbasesfromrules(rules);461  for (var i=0; i<bases.length; i++)462      {var arity = getarity(bases[i],rules);463       if (arity===0)464          {code += prettyjs(programonebasebound(bases[i],rules))};465       if (arity===1)466          {code += prettyjs(programonebasebound(bases[i],rules));467           code += prettyjs(programonebasef(bases[i],rules));468           code += prettyjs(programallbasef(bases[i],rules))};469       if (arity===2)470          {code += prettyjs(programonebasebound(bases[i],rules));471           code += prettyjs(programonebasebf(bases[i],rules));472           code += prettyjs(programonebasefb(bases[i],rules));473           code += prettyjs(programonebasefree(bases[i],rules));474           code += prettyjs(programallbasebf(bases[i],rules));475           code += prettyjs(programallbasefb(bases[i],rules));476           code += prettyjs(programallbasefree(bases[i],rules))};477       if (arity===3)478          {code += prettyjs(programonebasebound(bases[i],rules));479           code += prettyjs(programonebasefree(bases[i],rules));480           code += prettyjs(programallbaseffb(bases[i],rules));481           code += prettyjs(programallbasefree(bases[i],rules))};482       if (arity>3)483          {code += prettyjs(programonebasebound(bases[i],rules));484           code += prettyjs(programonebasefree(bases[i],rules));485           code += prettyjs(programallbasefree(bases[i],rules))}};486  return code}487//------------------------------------------------------------------------------488// programoneview cases489//------------------------------------------------------------------------------490function programoneviewbound (view,rules)491 {var arity = getrulearity(view,rules);492  var dataset = static(view);493  var subroutine = specialn(view,arity);494  var params = [];495  for (var i=0; i<arity; i++) {params.push('x' + (i+1))};496  params.push('facts');497  params.push('rules');498  var code = seq('function',subroutine,params);499  var data = indexees(view,rules);500  for (var i=0; i<data.length; i++)501      {var rule = data[i];502       if (atomp(rule)) {rule = seq('rule',rule,'true')};503       var blist = {};504       var cond = [];505       for (var j=0; j<params.length-2; j++)506           {cond.push(skinnymatch(params[j],rule[1][j+1],blist))};507       cond = programands(cond);508       var subcode = programoneviewsubgoals(rule,2,blist,true);509       code.push(seq('if',cond,subcode))};510  code.push(seq('return','false'));511  return code}512function programoneviewf (view,rules)513 {var subroutine = special(view,'f');514  var code = seq('function',subroutine,seq('facts','rules'));515  var data = indexees(view,rules);516  for (var i=0; i<data.length; i++)517      {var rule = data[i];518       if (atomp(rule)) {rule = seq('rule',rule,'true')};519       var blist = {};520       code.push(programoneviewsubgoals(rule,2,blist,rule[1][1]))};521  code.push(seq('return','false'));522  return code}523function programoneviewbf (view,rules)524 {var subroutine = special(view,'bf');525  var code = seq('function',subroutine,seq('x1','facts','rules'));526  var data = indexees(view,rules);527  for (var i=0; i<data.length; i++)528      {var rule = data[i];529       if (atomp(rule)) {rule = seq('rule',rule,'true')};530       var blist = {};531       var cond = skinnymatch('x1',rule[1][1],blist);532       var subcode = programoneviewsubgoals(rule,2,blist,rule[1][2]);533       code.push(seq('if',cond,subcode))};534  code.push(seq('return','false'));535  return code}536function programoneviewfb (view,rules)537 {var subroutine = special(view,'fb');538  var code = seq('function',subroutine,seq('x2','facts','rules'));539  var data = indexees(view,rules);540  for (var i=0; i<data.length; i++)541      {var rule = data[i];542       if (atomp(rule)) {rule = seq('rule',rule,'true')};543       var blist = {};544       var cond = skinnymatch('x2',rule[1][2],blist);545       var subcode = programoneviewsubgoals(rule,2,blist,rule[1][1]);546       code.push(seq('if',cond,subcode))};547  code.push(seq('return','false'));548  return code}549function programoneviewfree (view,rules)550 {var arity = getrulearity(view,rules);551  var subroutine = special(view,makesequence('f',arity));552  var code = seq('function',subroutine,seq('facts','rules'));553  var data = indexees(view,rules);554  for (var i=0; i<data.length; i++)555      {var rule = data[i];556       if (atomp(rule)) {rule = seq('rule',rule,'true')};557       var blist = {};558       code.push(programoneviewsubgoals(rule,2,blist,rule[1]))};559  code.push(seq('return','false'));560  return code}561function programoneviewsubgoals (rule,n,blist,out)562 {if (n>=rule.length)563     {if (typeof(out)==='boolean') {return seq('return','true')};564      return seq('return',codify(out,[],blist))};565  return programoneviewsubgoal(rule,n,blist,out)}566function programoneviewsubgoal (rule,n,blist,out)567 {if (symbolp(rule[n]))568     {return programoneviewsubatom(rule,n,blist,out)};569  if (rule[n][0]==='same')570     {return programoneviewsubsame(rule,n,blist,out)};571  if (rule[n][0]==='distinct')572     {return programoneviewsubdistinct(rule,n,blist,out)};573  if (rule[n][0]==='evaluate')574     {return programoneviewsubevaluate(rule,n,blist,out)};575  if (rule[n][0]==='true' && rule[n].length===3)576     {return programoneviewsubtrue(rule,n,blist,out)};577  if (rule[n][0]==='not')578     {return programoneviewsubnot(rule,n,blist,out)};579  if (compgroundp(rule[n],blist))580     {return programoneviewsubbound(rule,n,blist,out)};581  if (rule[n].length-1===1 && compfp(rule[n],blist))582     {return programoneviewsubf(rule,n,blist,out)};583  if (rule[n].length-1===2 && compbfp(rule[n],blist))584     {return programoneviewsubbf(rule,n,blist,out)};585  if (rule[n].length-1===2 && compfbp(rule[n],blist))586     {return programoneviewsubfb(rule,n,blist,out)};587  //if (rule[n].length-1===3 && compffbp(rule[n],blist))588  //   {return programoneviewsubffb(rule,n,blist,out)};589  if (compfreep(rule[n],blist))590     {return programoneviewsubfree(rule,n,blist,out)};591  if (n===rule.length-1)592     {return programoneviewsublast(rule,n,blist,out)};593  return programoneviewsubdb(rule,n,blist,out)}594function programoneviewsubatom (rule,n,blist,out)595 {if (rule[n]==='true') {return programoneviewsubgoals(rule,n+1,blist,out)};596  if (rule[n]==='false') {return 'false'};597  var subroutine = specialn(rule[n],0);598  var cond = seq(subroutine,'facts','rules');599  return seq('if',cond,programoneviewsubgoals(rule,n+1,blist,out))}600function programoneviewsubsame (rule,n,blist,out)601 {var x = codify(rule[n][1],[],blist);602  var y = codify(rule[n][2],[],blist);603  var cond = seq('equalp',x,y);604  var code = programoneviewsubgoals(rule,n+1,blist,out);605  return seq('if',cond,code)}606function programoneviewsubdistinct (rule,n,blist,out)607 {var x = codify(rule[n][1],[],blist);608  var y = codify(rule[n][2],[],blist);609  var cond = seq('equalp',x,y);610  var code = programoneviewsubgoals(rule,n+1,blist,out);611  return seq('if',seq('not',cond),code)}612function programoneviewsubevaluate (rule,n,blist,out)613 {var datavar = 'l'+(n-1);614  var query = codify(rule[n][1],[],blist);615  var subgoal = seq('compvalue',query,'facts','rules')616  var cond = skinnymatch(datavar,rule[n][2],blist);617  var block = seq('block');618  block.push(seq('bind',datavar,subgoal));619  block.push(seq('if',cond,programoneviewsubgoals(rule,n+1,blist,out)));620  return block}621function programoneviewsubtrue (rule,n,blist,out)622 {var source = seq('sub','datasets',kwotify(rule[n][2]));623  var datavar = 'l'+(n-1);624  var indvar = 'i'+(n-1);625  var subgoal = compilecalls(rule[n],[],blist);626  skinnymatch(seq('sub',datavar,indvar),rule[n][1],blist);627  var code = programoneviewsubgoals(rule,n+1,blist,out)628  var block = seq('block');629  block.push(seq('bind',datavar,subgoal));630  block.push(seq('loop',indvar,'0',datavar+'.length',code));631  return block}632function programoneviewsubnot (rule,n,blist,out)633 {var cond = programcall(rule[n][1],blist);634  var code = programoneviewsubgoals(rule,n+1,blist,out);635  return seq('if',seq('not',cond),code)}636function programoneviewsubbound (rule,n,blist,out)637 {var subroutine = specialn(operator(rule[n]),rule[n].length-1);638  var cond = seq(subroutine);639  for (var i=1; i<rule[n].length; i++)640      {cond.push(skinnyplug(rule[n][i],blist))}641  cond.push('facts');642  cond.push('rules');643  return seq('if',cond,programoneviewsubgoals(rule,n+1,blist,out))}644function programoneviewsubf (rule,n,blist,out)645 {var datavar = 'l'+(n-1);646  var indvar = 'i'+(n-1);647  var subroutine = specials(operator(rule[n]),'f');648  var subcode = seq(subroutine,'facts','rules');649  skinnymatch(seq('sub',datavar,indvar),rule[n][1],blist);650  var code = programoneviewsubgoals(rule,n+1,blist,out)651  var block = seq('block');652  block.push(seq('bind',datavar,subcode));653  block.push(seq('loop',indvar,'0',datavar+'.length',code));654  return block}655function programoneviewsubbf (rule,n,blist,out)656 {var datavar = 'l'+(n-1);657  var indvar = 'i'+(n-1);658  var subroutine = specials(operator(rule[n]),'bf');659  var query = codify(rule[n][1],[],blist);660  var subcode = seq(subroutine,query,'facts','rules');661  skinnymatch(seq('sub',datavar,indvar),rule[n][2],blist);662  var code = programoneviewsubgoals(rule,n+1,blist,out)663  var block = seq('block');664  block.push(seq('bind',datavar,subcode));665  block.push(seq('loop',indvar,'0',datavar+'.length',code));666  return block}667function programoneviewsubfb (rule,n,blist,out)668 {var datavar = 'l'+(n-1);669  var indvar = 'i'+(n-1);670  var subroutine = specials(operator(rule[n]),'fb');671  var query = codify(rule[n][2],[],blist);672  var subcode = seq(subroutine,query,'facts','rules');673  skinnymatch(seq('sub',datavar,indvar),rule[n][1],blist);674  var code = programoneviewsubgoals(rule,n+1,blist,out)675  var block = seq('block');676  block.push(seq('bind',datavar,subcode));677  block.push(seq('loop',indvar,'0',datavar+'.length',code));678  return block}679function programoneviewsubffb (rule,n,blist,out)680 {var datavar = 'l'+(n-1);681  var indvar = 'i'+(n-1);682  var subroutine = specials(operator(rule[n]),'ffb');683  var query = codify(rule[n][3],[],blist);684  var subcode = seq(subroutine,query,'facts','rules');685  skinnymatch(seq('sub',datavar,indvar),rule[n],blist);686  var code = programoneviewsubgoals(rule,n+1,blist,out)687  var block = seq('block');688  block.push(seq('bind',datavar,subcode));689  block.push(seq('loop',indvar,'0',datavar+'.length',code));690  return block}691function programoneviewsubfree (rule,n,blist,out)692 {var arity = rule[n].length-1;693  var datavar = 'l'+(n-1);694  var indvar = 'i'+(n-1);695  var subroutine = specials(operator(rule[n]),makesequence('f',arity));696  var subcode = seq(subroutine,'facts','rules');697  skinnymatch(seq('sub',datavar,indvar),rule[n],blist);698  var code = programoneviewsubgoals(rule,n+1,blist,out)699  var block = seq('block');700  block.push(seq('bind',datavar,subcode));701  block.push(seq('loop',indvar,'0',datavar+'.length',code));702  return block}703function programoneviewsublast (rule,n,blist,out)704 {var datavar = 'l'+(n-1);705  var subgoal = compilecall(rule[n],[],blist);706  skinnymatch(datavar,rule[n],blist);707  var code = programoneviewsubgoals(rule,n+1,blist,out)708  var block = seq('block');709  block.push(seq('bind',datavar,subgoal));710  block.push(seq('if',datavar,code));711  return block}712function programoneviewsubdb (rule,n,blist,out)713 {var datavar = 'l'+(n-1);714  var indvar = 'i'+(n-1);715  var subgoal = compilecalls(rule[n],[],blist);716  skinnymatch(seq('sub',datavar,indvar),rule[n],blist);717  var code = programoneviewsubgoals(rule,n+1,blist,out)718  var block = seq('block');719  block.push(seq('bind',datavar,subgoal));720  block.push(seq('loop',indvar,'0',datavar+'.length',code));721  return block}722//------------------------------------------------------------------------------723function programcall (subgoal,blist)724 {if (symbolp(subgoal))725     {return programcallatom(subgoal,blist)};726  if (subgoal[0]==='same')727     {return programcallsame(subgoal,blist)};728  if (subgoal[0]==='distinct')729     {return programcalldistinct(subgoal,blist)};730  if (subgoal[0]==='evaluate')731     {return programcallevaluate(subgoal,blist)};732  if (subgoal[0]==='true' && subgoal.length===3)733     {return programcalltrue(subgoal,blist)};734  if (compgroundp(subgoal,blist))735     {return programcallbound(subgoal,blist)};736  if (subgoal.length-1===1 && compfp(subgoal,blist))737     {return programcallf(subgoal,blist)};738  if (subgoal.length-1===2 && compbfp(subgoal,blist))739     {return programcallbf(subgoal,blist)};740  if (subgoal.length-1===2 && compfbp(subgoal,blist))741     {return programcallfb(subgoal,blist)};742  if (subgoal.length-1===3 && compffbp(subgoal,blist))743     {return programcallffb(subgoal,blist)};744  if (compfreep(subgoal,blist))745     {return programcallfree(subgoal,blist)};746  return programcalldb(subgoal,blist)}747function programcallatom (subgoal,blist)748 {return seq('$' + subgoal + '$$','facts','rules')}749function programcallsame (subgoal,blist)750 {var x1 = codify(subgoal[1],[],blist);751  var x2 = codify(subgoal[2],[],blist);752  return seq('equalp',x1,x2)}753function programcalldistinct (subgoal,blist)754 {var x1 = codify(subgoal[1],[],blist);755  var x2 = codify(subgoal[2],[],blist);756  return seq('not',seq('equalp',x1,x2))}757function programcallevaluate (subgoal,blist)758 {var query = codify(subgoal,[],blist);759  return seq('companswerx',query,'bl','facts','rules')}760function programcalltrue (subgoal,blist)761 {var source = seq('sub','datasets',kwotify(subgoal[2]));762  var subroutine = single(operator(subgoal[1]));763  var query = codify(subgoal[1],[],blist);764  return seq(subroutine,query,seq('seq'),source,'rules')}765function programcallbound (subgoal,blist)766 {var arity = subgoal.length-1;767  var subroutine = special(operator(subgoal),makesequence('b',arity));768  var code = [subroutine];769  for (var i=1; i<subgoal.length; i++)770      {code.push(codify(subgoal[i],[],blist))}771  code.push('facts');772  code.push('rules');773  return code}774function programcallf (subgoal,blist)775 {var subroutine = '$' + operator(subgoal) + '$f$';776  return seq(subroutine,'facts','rules')}777function programcallbf (subgoal,blist)778 {var subroutine = '$' + operator(subgoal) + '$bf$';779  var query = codify(subgoal[1],[],blist);780  return seq(subroutine,query,'facts','rules')}781function programcallfb (subgoal,blist)782 {var subroutine = '$' + operator(subgoal) + '$fb$';783  var query = codify(subgoal[2],[],blist);784  return seq(subroutine,query,'facts','rules')}785function programcallffb (subgoal,blist)786 {var subroutine = '$' + operator(subgoal) + '$ffb$';787  var query = codify(subgoal[3],[],blist);788  return seq(subroutine,query,'facts','rules')}789function programcallfree (subgoal,blist)790 {var arity = subgoal.length-1;791  var subroutine = special(operator(subgoal),makesequence('f',arity))792  var code = [subroutine];793  for (var i=1; i<subgoal.length; i++)794      {code.push(codify(subgoal[i],[],blist))}795  code.push('facts');796  code.push('rules');797  return code}798function programcalldb (subgoal,blist)799 {var subroutine = single(operator(subgoal));800  var query = codify(subgoal,[],blist);801  return seq(subroutine,query,seq('seq'),'facts','rules')}802//------------------------------------------------------------------------------803// programallview cases804//------------------------------------------------------------------------------805function programallviewf (view,rules)806 {var subroutine = specials(view,'f');807  var code = seq('function',subroutine,seq('facts','rules'));808  code.push(seq('bind','answers',seq('seq')));809  var data = indexees(view,rules);810  for (var i=0; i<data.length; i++)811      {var rule = data[i];812       if (atomp(rule)) {rule = seq('rule',rule,'true')};813       var blist = {};814       code.push(programallviewsubgoals(rule,2,blist,1))};815  code.push(seq('return','answers'));816  return code}817function programallviewbf (view,rules)818 {var subroutine = specials(view,'bf');819  var code = seq('function',subroutine,seq('x1','facts','rules'));820  code.push(seq('bind','answers',seq('seq')));821  var data = indexees(view,rules);822  for (var i=0; i<data.length; i++)823      {var rule = data[i];824       if (atomp(rule)) {rule = seq('rule',rule,'true')};825       var blist = {};826       var cond = skinnymatch('x1',rule[1][1],blist);827       code.push(seq('if',cond,programallviewsubgoals(rule,2,blist,2)))};828  code.push(seq('return','answers'));829  return code}830function programallviewfb (view,rules)831 {var subroutine = specials(view,'fb');832  var code = seq('function',subroutine,seq('x2','facts','rules'));833  code.push(seq('bind','answers',seq('seq')));834  var data = indexees(view,rules);835  for (var i=0; i<data.length; i++)836      {var rule = data[i];837       if (atomp(rule)) {rule = seq('rule',rule,'true')};838       var blist = {};839       var cond = skinnymatch('x2',rule[1][2],blist);840       code.push(seq('if',cond,programallviewsubgoals(rule,2,blist,1)))};841  code.push(seq('return','answers'));842  return code}843function programallviewfree (view,rules)844 {var arity = getrulearity(view,rules);845  var subroutine = specials(view,makesequence('f',arity));846  var code = seq('function',subroutine,seq('facts','rules'));847  code.push(seq('bind','answers',seq('seq')));848  var data = indexees(view,rules);849  for (var i=0; i<data.length; i++)850      {var rule = data[i];851       if (atomp(rule)) {rule = seq('rule',rule,'true')};852       var blist = {};853       code.push(programallviewsubgoals(rule,2,blist,rule[1]))};854  code.push(seq('return','answers'));855  return code}856function programallviewsubgoals (rule,n,blist,out)857 {if (n>=rule.length)858     {if (typeof(out)==='number') {var result = codify(rule[1][out],[],blist)}859         else {var result = codify(out,[],blist)};860      return seq('answers.push',result)};861  return programallviewsubgoal(rule,n,blist,out)}862function programallviewsubgoal (rule,n,blist,out)863 {if (symbolp(rule[n]))864     {return programallviewsubatom(rule,n,blist,out)};865  if (rule[n][0]==='same')866     {return programallviewsubsame(rule,n,blist,out)};867  if (rule[n][0]==='distinct')868     {return programallviewsubdistinct(rule,n,blist,out)};869  if (rule[n][0]==='evaluate')870     {return programallviewsubevaluate(rule,n,blist,out)};871  if (rule[n][0]==='true' && rule[n].length===3)872     {return programallviewsubtrue(rule,n,blist,out)};873  if (rule[n][0]==='not')874     {return programallviewsubnot(rule,n,blist,out)};875  if (compgroundp(rule[n],blist))876     {return programallviewsubbound(rule,n,blist,out)};877  if (rule[n].length-1===1 && compfp(rule[n],blist))878     {return programallviewsubf(rule,n,blist,out)};879  if (rule[n].length-1===2 && compbfp(rule[n],blist))880     {return programallviewsubbf(rule,n,blist,out)};881  if (rule[n].length-1===2 && compfbp(rule[n],blist))882     {return programallviewsubfb(rule,n,blist,out)};883  //if (rule[n].length-1===3 && compffbp(rule[n],blist))884  //   {return programallviewsubffb(rule,n,blist,out)};885  if (compfreep(rule[n],blist))886     {return programallviewsubfree(rule,n,blist,out)};887  return programallviewsubdb(rule,n,blist,out)}888function programallviewsubatom (rule,n,blist,out)889 {if (rule[n]==='true') {return programallviewsubgoals(rule,n+1,blist,out)};890  if (rule[n]==='false') {return 'false'};891  var subroutine = specialn(rule[n],0);892  var cond = seq(subroutine,'facts','rules');893  return seq('if',cond,programallviewsubgoals(rule,n+1,blist,out))}894function programallviewsubsame (rule,n,blist,out)895 {var x = codify(rule[n][1],[],blist);896  var y = codify(rule[n][2],[],blist);897  var cond = seq('equalp',x,y);898  var code = programallviewsubgoals(rule,n+1,blist,out);899  return seq('if',cond,code)}900function programallviewsubdistinct (rule,n,blist,out)901 {var x = codify(rule[n][1],[],blist);902  var y = codify(rule[n][2],[],blist);903  var cond = seq('equalp',x,y);904  var code = programallviewsubgoals(rule,n+1,blist,out);905  return seq('if',seq('not',cond),code)}906function programallviewsubevaluate (rule,n,blist,out)907 {var datavar = 'l'+(n-1);908  var query = codify(rule[n][1],[],blist);909  var subgoal = seq('compvalue',query,'facts','rules')910  var cond = skinnymatch(datavar,rule[n][2],blist);911  var block = seq('block');912  block.push(seq('bind',datavar,subgoal));913  block.push(seq('if',cond,programallviewsubgoals(rule,n+1,blist,out)));914  return block}915function programallviewsubtrue (rule,n,blist,out)916 {var source = seq('sub','datasets',kwotify(rule[n][2]));917  var datavar = 'l'+(n-1);918  var indvar = 'i'+(n-1);919  var subgoal = compilecalls(rule[n],[],blist);920  skinnymatch(seq('sub',datavar,indvar),rule[n][1],blist);921  var code = programallviewsubgoals(rule,n+1,blist,out)922  var block = seq('block');923  block.push(seq('bind',datavar,subgoal));924  block.push(seq('loop',indvar,'0',datavar+'.length',code));925  return block}926function programallviewsubnot (rule,n,blist,out)927 {var cond = programcall(rule[n][1],blist);928  var code = programallviewsubgoals(rule,n+1,blist,out);929  return seq('if',seq('not',cond),code)}930function programallviewsubbound (rule,n,blist,out)931 {var subroutine = specialn(operator(rule[n]),rule[n].length-1);932  var cond = seq(subroutine);933  for (var i=1; i<rule[n].length; i++)934      {cond.push(skinnyplug(rule[n][i],blist))}935  cond.push('facts');936  cond.push('rules');937  return seq('if',cond,programallviewsubgoals(rule,n+1,blist,out))}938function programallviewsubf (rule,n,blist,out)939 {var datavar = 'l'+(n-1);940  var indvar = 'i'+(n-1);941  var subroutine = specials(operator(rule[n]),'f');942  var subcode = seq(subroutine,'facts','rules');943  skinnymatch(seq('sub',datavar,indvar),rule[n][1],blist);944  var code = programallviewsubgoals(rule,n+1,blist,out)945  var block = seq('block');946  block.push(seq('bind',datavar,subcode));947  block.push(seq('loop',indvar,'0',datavar+'.length',code));948  return block}949function programallviewsubbf (rule,n,blist,out)950 {var datavar = 'l'+(n-1);951  var indvar = 'i'+(n-1);952  var subroutine = specials(operator(rule[n]),'bf');953  var query = codify(rule[n][1],[],blist);954  var subcode = seq(subroutine,query,'facts','rules');955  skinnymatch(seq('sub',datavar,indvar),rule[n][2],blist);956  var code = programallviewsubgoals(rule,n+1,blist,out)957  var block = seq('block');958  block.push(seq('bind',datavar,subcode));959  block.push(seq('loop',indvar,'0',datavar+'.length',code));960  return block}961function programallviewsubfb (rule,n,blist,out)962 {var datavar = 'l'+(n-1);963  var indvar = 'i'+(n-1);964  var subroutine = specials(operator(rule[n]),'fb');965  var query = codify(rule[n][2],[],blist);966  var subcode = seq(subroutine,query,'facts','rules');967  skinnymatch(seq('sub',datavar,indvar),rule[n][1],blist);968  var code = programallviewsubgoals(rule,n+1,blist,out)969  var block = seq('block');970  block.push(seq('bind',datavar,subcode));971  block.push(seq('loop',indvar,'0',datavar+'.length',code));972  return block}973function programallviewsubffb (rule,n,blist,out)974 {var datavar = 'l'+(n-1);975  var indvar = 'i'+(n-1);976  var subroutine = specials(operator(rule[n]),'ffb');977  var query = codify(rule[n][3],[],blist);978  var subcode = seq(subroutine,query,'facts','rules');979  skinnymatch(seq('sub',datavar,indvar),rule[n],blist);980  var code = programallviewsubgoals(rule,n+1,blist,out)981  var block = seq('block');982  block.push(seq('bind',datavar,subcode));983  block.push(seq('loop',indvar,'0',datavar+'.length',code));984  return block}985function programallviewsubfree (rule,n,blist,out)986 {var arity = rule[n].length-1;987  var datavar = 'l'+(n-1);988  var indvar = 'i'+(n-1);989  var subroutine = specials(operator(rule[n]),makesequence('f',arity));990  var subcode = seq(subroutine,'facts','rules');991  skinnymatch(seq('sub',datavar,indvar),rule[n],blist);992  var code = programallviewsubgoals(rule,n+1,blist,out)993  var block = seq('block');994  block.push(seq('bind',datavar,subcode));995  block.push(seq('loop',indvar,'0',datavar+'.length',code));996  return block}997function programallviewsubdb (rule,n,blist,out)998 {var datavar = 'l'+(n-1);999  var indvar = 'i'+(n-1);1000  var subgoal = compilecalls(rule[n],[],blist);1001  skinnymatch(seq('sub',datavar,indvar),rule[n],blist);1002  var code = programallviewsubgoals(rule,n+1,blist,out)1003  var block = seq('block');1004  block.push(seq('bind',datavar,subgoal));1005  block.push(seq('loop',indvar,'0',datavar+'.length',code));1006  return block}1007//------------------------------------------------------------------------------1008function programcalls (subgoal,blist)1009 {if (symbolp(subgoal)) {return programcallatom(subgoal,blist)};1010  if (compgroundp(subgoal,blist)) {return programcallsbound(subgoal,blist)};1011  var subroutine = plural(operator(subgoal));1012  var query = codify(subgoal,[],blist);1013  return seq(subroutine,query,seq('seq'),'facts','rules')}1014function programcallsatom (subgoal,blist)1015 {return seq('$' + subgoal + '$$','facts','rules')}1016function programcallsbound (subgoal,blist)1017 {var subroutine = specialn(subgoal[0],subgoal.length-1);1018  var code = [subroutine];1019  for (var i=1; i<subgoal.length; i++)1020      {code.push(codify(subgoal[i],blist))}1021  code.push('facts');1022  code.push('rules');1023  return code}1024function programcallsbf (subgoal,blist)1025 {var subroutine = '$$' + operator(subgoal) + '$bf$$';1026  var query = codify(subgoal[1],[],blist);1027  return seq(subroutine,query,'facts','rules')}1028function programcallsfb (subgoal,blist)1029 {var subroutine = '$$' + operator(subgoal) + '$fb$$';1030  var query = codify(subgoal[2],[],blist);1031  return seq(subroutine,query,'facts','rules')}1032function programcallsffb (subgoal,blist)1033 {var subroutine = '$$' + operator(subgoal) + '$ffb$$';1034  var query = codify(subgoal[3],[],blist);1035  return seq(subroutine,query,'facts','rules')}1036function programcallsff (subgoal,blist)1037 {var subroutine = '$$' + operator(subgoal) + '$ff$$';1038  return seq(subroutine,'facts','rules')}1039//------------------------------------------------------------------------------1040// programoneatom cases1041//------------------------------------------------------------------------------1042function programoneatombound (rel,rules)1043 {var arity = getfactarity(rel,rules);1044  var dataset = static(rel);1045  var subroutine = '$' + rel + '$' + makesequence('b',arity) + '$';1046  var params = [];1047  for (var i=0; i<arity; i++) {params.push('x' + (i+1))};1048  var code = seq('block');1049  code.push(seq('bind','data',seq('baseindexees',kwotify(rel),dataset)));1050  for (var i=0; i<arity; i++)1051      {code.push(seq('bind','dum',seq('baseindexps',params[i],dataset)));1052  var cond = seq('and','dum',seq('less','dum.length','data.length'));1053  code.push(seq('if',cond,seq('block',seq('bind','data','dum'))))};1054  var cond = [];1055  for (var i=0; i<arity; i++)1056      {cond.push(seq('equalp',seq('sub',seq('sub','data','i'),i+1),params[i]))};1057  cond = programands(cond);1058  var inner = seq('if',cond,seq('return','true'));1059  code.push(seq('loop','i','0','data.length',inner));1060  code.push(seq('return','false'));1061  return seq('function',subroutine,params,code)}1062//------------------------------------------------------------------------------1063function programoneatomf (rel,rules)1064 {var dataset = static(rel);1065  var subroutine = special(rel,'f');1066  var params = seq('facts','rules');1067  var code = seq('function',subroutine,params);1068  code.push(seq('bind','data',seq('baseindexees',kwotify(rel),dataset)));1069  var cond = seq('greater','data.length','0');1070  code.push(seq('if',cond,seq('return',seq('sub',seq('sub','data','0'),'1'))));1071  code.push(seq('return','false'));1072  return code}1073//------------------------------------------------------------------------------1074function programoneatombf (rel,rules)1075 {var dataset = static(rel);1076  var subroutine = special(rel,'bf');1077  var params = seq('x1','facts','rules');1078  var code = seq('function',subroutine,params);1079  code.push(seq('bind','data',seq('baseindexees',kwotify(rel),dataset)));1080  code.push(seq('bind','dum',seq('baseindexps','x1',dataset)));1081  var cond = seq('and','dum',seq('less','dum.length','data.length'));1082  code.push(seq('if',cond,seq('block',seq('bind','data','dum'))));1083  var cond =  seq('equalp',seq('sub',seq('sub','data','i'),'1'),'x1');1084  var result = seq('sub',seq('sub','data','i'),'2');1085  var subcode = seq('if',cond,seq('return',result));1086  code.push(seq('loop','i','0','data.length',subcode));1087  code.push(seq('return','false'));1088  return code}1089//------------------------------------------------------------------------------1090function programoneatomfb (rel,rules)1091 {var dataset = static(rel);1092  var subroutine = special(rel,'fb');1093  var params = seq('x2','facts','rules');1094  var code = seq('function',subroutine,params);1095  code.push(seq('bind','data',seq('baseindexees',kwotify(rel),dataset)));1096  code.push(seq('bind','dum',seq('baseindexps','x2','facts')));1097  var cond = seq('and','dum',seq('less','dum.length','data.length'));1098  var cond =  seq('equalp',seq('sub',seq('sub','data','i'),'2'),'x2');1099  var result = seq('sub',seq('sub','data','i'),'1');1100  var subcode = seq('if',cond,seq('return',result));1101  code.push(seq('loop','i','0','data.length',subcode));1102  code.push(seq('return','false'));1103  return code}1104//------------------------------------------------------------------------------1105function programoneatomfree (rel,rules)1106 {var arity = getrulearity(rel,rules);1107  var dataset = static(rel);1108  var subroutine = special(rel,makesequence('f',arity));1109  var params = seq('facts','rules');1110  var code = seq('function',subroutine,params);1111  code.push(seq('bind','data',seq('baseindexees',kwotify(rel),dataset)));1112  var cond = seq('greater','data.length','0');1113  code.push(seq('if',cond,seq('return',seq('sub','data','0'))));1114  code.push(seq('return','false'))1115  return code}1116//------------------------------------------------------------------------------1117// programallatomlist cases1118//------------------------------------------------------------------------------1119function programallatomf (rel,rules)1120 {var dataset = static(rel);1121  var subroutine = specials(rel,'f');1122  var params = seq('facts','rules');1123  var code = seq('function',subroutine,params);1124  var method = seq('dot',seq('indexees',kwotify(rel),dataset),'map');1125  //var arg = seq('transform','x',seq('sub','x','1'));1126  var arg = 'arg1';1127  code.push(seq('return',seq('method',method,arg)));1128  return code}1129//------------------------------------------------------------------------------1130function programallatombf (rel,rules)1131 {var dataset = static(rel);1132  var subroutine = specials(rel,'bf');1133  var params = seq('x1','facts','rules');1134  var code = seq('function',subroutine,params);1135  code.push(seq('bind','data',seq('baseindexees',kwotify(rel),dataset)));1136  code.push(seq('bind','dum',seq('baseindexps','x1',dataset)));1137  var cond = seq('and','dum',seq('less','dum.length','data.length'));1138  code.push(seq('if',cond,seq('block',seq('bind','data','dum'))));1139  var cond =  seq('equalp',seq('sub',seq('sub','data','i'),'1'),'x1');1140  var result = seq('sub',seq('sub','data','i'),'2');1141  var subcode = seq('if',cond,seq('answers.push',result));1142  code.push(seq('bind','answers',seq('seq')));1143  code.push(seq('loop','i','0','data.length',subcode));1144  code.push(seq('return','answers'));1145  return code}1146//------------------------------------------------------------------------------1147function programallatomfb (rel,rules)1148 {var dataset = static(rel);1149  var subroutine = specials(rel,'fb');1150  var params = seq('x2','facts','rules');1151  var code = seq('function',subroutine,params);1152  code.push(seq('bind','data',seq('baseindexees',kwotify(rel),dataset)));1153  code.push(seq('bind','dum',seq('baseindexps','x2',dataset)));1154  var cond = seq('and','dum',seq('less','dum.length','data.length'));1155  code.push(seq('if',cond,seq('block',seq('bind','data','dum'))));1156  var cond =  seq('equalp',seq('sub',seq('sub','data','i'),'2'),'x2');1157  var result = seq('sub',seq('sub','data','i'),'1');1158  var subcode = seq('if',cond,seq('answers.push',result));1159  code.push(seq('bind','answers',seq('seq')));1160  code.push(seq('loop','i','0','data.length',subcode));1161  code.push(seq('return','answers'));1162  return code}1163//------------------------------------------------------------------------------1164function programallatomfree (rel,rules)1165 {var arity = getrulearity(rel,rules);1166  var dataset = static(rel);1167  var subroutine = specials(rel,makesequence('f',arity));1168  var params = seq('facts','rules');1169  var code = seq('function',subroutine,params);1170  code.push(seq('return',seq('baseindexees',kwotify(rel),dataset)));1171  return code}1172//------------------------------------------------------------------------------1173// programonebase cases1174//------------------------------------------------------------------------------1175function programonebasebound (rel,rules)1176 {var arity = getarity(rel,rules);1177  var subroutine = '$' + rel + '$' + makesequence('b',arity) + '$';1178  var params = [];1179  for (var i=0; i<arity; i++) {params.push('x' + (i+1))};1180  params.push('facts');1181  var code = seq('function',subroutine,params);1182  code.push(seq('bind','data','facts'));1183  var cond = [seq('eq',seq('sub',seq('sub','data','i'),0),kwotify(rel))];1184  for (var i=0; i<arity; i++)1185      {cond.push(seq('equalp',seq('sub',seq('sub','data','i'),i+1),params[i]))};1186  var inner = seq('if',programands(cond),seq('return','true'));1187  code.push(seq('loop','i','0','data.length',inner));1188  code.push(seq('return','false'));1189  return code}1190//------------------------------------------------------------------------------1191function programonebasef (rel,rules)1192 {var subroutine = special(rel,'f');1193  var params = seq('facts','rules');1194  var code = seq('function',subroutine,params);1195  code.push(seq('bind','data','facts'));1196  var cond = seq('eq',seq('sub',seq('sub','data','i'),0),kwotify(rel));1197  var inner = seq('if',cond,seq('return','true'));1198  code.push(seq('loop','i','0','data.length',inner));1199  code.push(seq('return','false'));1200  return code}1201//------------------------------------------------------------------------------1202function programonebasebf (rel,rules)1203 {var subroutine = special(rel,'bf');1204  var params = seq('x1','facts','rules');1205  var code = seq('function',subroutine,params);1206  code.push(seq('bind','data','facts'));1207  var cond =1208    seq('and',1209        seq('eq',seq('sub',seq('sub','data','i'),0),kwotify(rel)),1210        seq('equalp',seq('sub',seq('sub','data','i'),1),'x1'));1211  var result = seq('sub',seq('sub','data','i'),2);1212  var subcode = seq('if',cond,seq('return',result));1213  code.push(seq('bind','answers',seq('seq')));1214  code.push(seq('loop','i','0','data.length',subcode));1215  code.push(seq('return','false'));1216  return code}1217//------------------------------------------------------------------------------1218function programonebasefb (rel,rules)1219 {var subroutine = special(rel,'fb');1220  var params = seq('x2','facts','rules');1221  var code = seq('function',subroutine,params);1222  code.push(seq('bind','data','facts'));1223  var cond = 1224    seq('and',1225        seq('eq',seq('sub',seq('sub','data','i'),0),kwotify(rel)),1226        seq('equalp',seq('sub',seq('sub','data','i'),2),'x2'));1227  var result = seq('sub',seq('sub','data','i'),1);1228  var subcode = seq('if',cond,seq('return',result));1229  code.push(seq('bind','answers',seq('seq')));1230  code.push(seq('loop','i','0','data.length',subcode));1231  code.push(seq('return','false'));1232  return code}1233//------------------------------------------------------------------------------1234function programonebasefree (rel,rules)1235 {var arity = getarity(rel,rules);1236  var subroutine = special(rel,makesequence('f',arity));1237  var params = seq('facts','rules');1238  var code = seq('function',subroutine,params);1239  code.push(seq('bind','data','facts'));1240  var cond = seq('eq',seq('sub',seq('sub','data','i'),0),kwotify(rel));1241  var inner = seq('if',cond,seq('return',seq('sub','data','i')));1242  code.push(seq('loop','i','0','data.length',inner));1243  code.push(seq('return','false'))1244  return code}1245//------------------------------------------------------------------------------1246// programallbase cases1247//------------------------------------------------------------------------------1248function programallbasef (rel,rules)1249 {var subroutine = specials(rel,'f');1250  var params = seq('facts','rules');1251  var code = seq('function',subroutine,params);1252  var cond = seq('eq',seq('sub',seq('sub','data','i'),0),kwotify(rel));1253  var result = seq('sub',seq('sub','data','i'),1);1254  var subcode = seq('if',cond,seq('answers.push',result));1255  code.push(seq('bind','answers',seq('seq')));1256  code.push(seq('loop','i','0','data.length',subcode));1257  code.push(seq('return','answers'));1258  return code}1259//------------------------------------------------------------------------------1260function programallbasebf (rel,rules)1261 {var subroutine = specials(rel,'bf');1262  var params = seq('x1','facts','rules');1263  var code = seq('function',subroutine,params);1264  code.push(seq('bind','data','facts'));1265  var cond =1266    seq('and',1267        seq('eq',seq('sub',seq('sub','data','i'),0),kwotify(rel)),1268        seq('equalp',seq('sub',seq('sub','data','i'),1),'x1'));1269  var result = seq('sub',seq('sub','data','i'),2);1270  var subcode = seq('if',cond,seq('answers.push',result));1271  code.push(seq('bind','answers',seq('seq')));1272  code.push(seq('loop','i','0','data.length',subcode));1273  code.push(seq('return','answers'));1274  return code}1275//------------------------------------------------------------------------------1276function programallbasefb (rel,rules)1277 {var subroutine = specials(rel,'fb');1278  var params = seq('x2','facts','rules');1279  var code = seq('function',subroutine,params);1280  code.push(seq('bind','data','facts'));1281  var cond =1282    seq('and',1283        seq('eq',seq('sub',seq('sub','data','i'),0),kwotify(rel)),1284        seq('equalp',seq('sub',seq('sub','data','i'),2),'x2'));1285  var result = seq('sub',seq('sub','data','i'),1);1286  var subcode = seq('if',cond,seq('answers.push',result));1287  code.push(seq('bind','answers',seq('seq')));1288  code.push(seq('loop','i','0','data.length',subcode));1289  code.push(seq('return','answers'));1290  return code}1291//------------------------------------------------------------------------------1292function programallbaseffb (rel,rules)1293 {var subroutine = specials(rel,'ffb');1294  var params = seq('x3','facts','rules');1295  var code = seq('function',subroutine,params);1296  code.push(seq('bind','data','facts'));1297  var cond =1298    seq('and',1299        seq('eq',seq('sub',seq('sub','data','i'),0),kwotify(rel)),1300        seq('equalp',seq('sub',seq('sub','data','i'),3),'x3'));1301  var subcode = seq('if',cond,seq('answers.push',seq('sub','data','i')));1302  code.push(seq('bind','answers',seq('seq')));1303  code.push(seq('loop','i','0','data.length',subcode));1304  code.push(seq('return','answers'));1305  return code}1306//------------------------------------------------------------------------------1307function programallbasefree (rel,rules)1308 {var arity = getarity(rel,rules);1309  var subroutine = specials(rel,makesequence('f',arity));1310  var params = seq('facts','rules');1311  var code = seq('function',subroutine,params);1312  var cond = seq('eq',seq('sub',seq('sub','data','i'),0),kwotify(rel));1313  var result = seq('sub','data','i');1314  var subcode = seq('if',cond,seq('answers.push',result));1315  code.push(seq('bind','answers',seq('seq')));1316  code.push(seq('loop','i','0','data.length',subcode));1317  code.push(seq('return','answers'));1318  return code}1319//------------------------------------------------------------------------------1320// programatomdata1321//------------------------------------------------------------------------------1322function programatomdata (view,rules)1323 {var data = indexees(view,rules);1324  var name = static(view);1325  var code = ['defineindex',['readdata','`' + grindem(data) + '`']];1326  return seq('bind',name,code)}1327//==============================================================================1328// Miscellaneous1329//==============================================================================1330//------------------------------------------------------------------------------1331// getmyviews1332// getviewsfromrules1333// getatomsfromrules1334// getbasesfromrules1335//------------------------------------------------------------------------------1336function getmyviews (data)1337 {var views = seq();1338  for (var i=0; i<data.length; i++)1339      {var datum =  data[i];1340       if (datum==='true' || datum==='false') {continue};1341       if (symbolp(datum)) {adjoin(datum,views); continue};1342       if (datum[0]==='definition') {continue};1343       if (datum[0]==='transition') {continue};1344       if (datum[0]==='rule') {adjoin(operator(datum),views); continue};1345       adjoin(datum[0],views)};1346  return views}1347//------------------------------------------------------------------------------1348function getviewsfromrules (rules)1349 {var views = getmyviews(rules);1350  var tables = seq();1351  for (var i=0; i<views.length; i++)1352      {if (!atomlistp(views[i],rules)) {tables = adjoin(views[i],tables)}};1353  return tables}1354//------------------------------------------------------------------------------1355function getatomsfromrules (rules)1356 {var views = getmyviews(rules);1357  var tables = seq();1358  for (var i=0; i<views.length; i++)1359      {if (views[i]==='definition') {continue};1360       if (views[i]==='transition') {continue};1361       if (atomlistp(views[i],rules)) {tables = adjoin(views[i],tables)}};1362  return tables}1363function atomlistp (view,rules)1364 {var data = indexees(view,rules);1365  for (var i=0; i<data.length; i++)1366      {if (!atomp(data[i])) {return false}};1367  return true}1368function atomp (rule)1369 {return (symbolp(rule) || rule[0]!=='rule')};1370//------------------------------------------------------------------------------1371function getbasesfromrules (rules)1372 {var bases = seq();1373  var views = getmyviews(rules);1374  for (var i=0; i<rules.length; i++)1375      {bases = getbasesexp(rules[i],views,bases)};1376  return bases}1377function getbasesexp (rule,views,bases)1378 {if (symbolp(rule))1379     {if (!find(rule,views)) {return adjoin(rule,bases)};1380      return bases};1381  if (rule[0]==='same') {return bases};1382  if (rule[0]==='distinct') {return bases};1383  if (rule[0]==='evaluate') {return bases};1384  if (rule[0]==='true' && rule.length===3) {return bases};1385  if (rule[0]==='not') {return getbasesexp(rule[1],views,bases)};1386  if (rule[0]==='and' || rule[0]==='or')1387     {for (var i=1; i<rule.length; i++)1388          {bases = getbasesexp(rule[i],views,bases)};1389      return bases};1390  if (rule[0]==='definition') {return bases};1391  if (rule[0]==='rule')1392     {for (var i=2; i<rule.length; i++)1393          {bases = getbasesexp(rule[i],views,bases)};1394      return bases};1395  if (rule[0]==='transition') {return bases};1396  if (find(rule[0],views)) {return bases};1397  return adjoin(rule[0],bases)}1398function basep (rel,rules)1399 {return (indexees(rel,rules).length===0)}1400//------------------------------------------------------------------------------1401// compilegroundp1402// compilealmostp1403//------------------------------------------------------------------------------1404function compgroundp (x,blist)1405 {if (varp(x)) {return blist[x]!==undefined};1406  if (symbolp(x)) {return true};1407  for (var i=1; i<x.length; i++)1408      {if (!compgroundp(x[i],blist)) {return false}};1409  return true}1410function compfp (subgoal,blist)1411 {if (!varp(subgoal[1])) {return false};1412  if (blist[subgoal[1]]!==undefined) {return false};1413  return true}1414function compbfp (subgoal,blist)1415 {if (!compgroundp(subgoal[1],blist)) {return false};1416  if (!varp(subgoal[2])) {return false};1417  if (blist[subgoal[2]]!==undefined) {return false};1418  return true}1419function compfbp (subgoal,blist)1420 {if (!varp(subgoal[1])) {return false};1421  if (blist[subgoal[1]]!==undefined) {return false};1422  if (!compgroundp(subgoal[2],blist)) {return false};1423  return true}1424function compffbp (subgoal,blist)1425 {if (!varp(subgoal[1])) {return false};1426  if (blist[subgoal[1]]!==undefined) {return false};1427  if (!varp(subgoal[2])) {return false};1428  if (blist[subgoal[2]]!==undefined) {return false};1429  if (!compgroundp(subgoal[3],blist)) {return false};1430  return true}1431function compfreep (subgoal,blist)1432 {for (var i=1; i<subgoal.length; i++)1433      {if (!varp(subgoal[i])) {return false};1434       if (blist[subgoal[i]]!==undefined) {return false};1435       if (subgoal.indexOf(subgoal[i],i+1)>0) {return false}};1436  return true}1437//------------------------------------------------------------------------------1438// compilematch1439//------------------------------------------------------------------------------1440function compilematch (x,y,hlist,alist,ol)1441 {if (varp(x))1442     {if (alist[x]) {return seq('equalp',alist[x],y)};1443      if (find(x,hlist))1444         {alist[x] = y;;1445          return seq('maatchify',kwotify(x),'bl',y,'bl',ol)};1446      alist[x] = y;1447      return 'true'};1448  if (symbolp(x)) {return seq('eq',kwotify(x),y)};1449  var code = [];1450  for (var i=0; i<x.length; i++)1451      {code.push(compilematch(x[i],seq('sub',y,i),hlist,alist,ol))};1452  code = programands(code);1453  return code}1454//------------------------------------------------------------------------------1455// skinnymatch1456//------------------------------------------------------------------------------1457function skinnymatch (x,y,blist)1458 {if (varp(y))1459     {if (blist[y]!==undefined) {return seq('equalp',blist[y],x)};1460      blist[y] = x;1461      return 'true'};1462  if (symbolp(y)) {return seq('eq',x,kwotify(y))};1463  var cond = seq(seq('not',seq('symbolp',x)));1464  for (var i=0; i<y.length; i++)1465      {cond.push(skinnymatch(seq('sub',x,i),y[i],blist))};1466  return programands(cond)}1467function skinnyplug (x,blist)1468 {if (varp(x))1469     {if (blist[x]) {return blist[x]};1470      return kwotify(x)};1471  if (symbolp(x)) {return kwotify(x)};1472  var exp = seq('seq');1473  for (var i=0; i<x.length; i++)1474      {exp.push(skinnyplug(x[i],blist))};1475  return exp}1476//------------------------------------------------------------------------------1477// sundry1478//------------------------------------------------------------------------------1479function single (s)1480 {return '$' + s + '$'}1481function plural (s)1482 {return '$$' + s + '$$'}1483function static (s)1484 {return '$' + s + '$data$'}1485function special (rel,suffix)1486 {return '$' + rel + '$' + suffix + '$'}1487function specials (rel,suffix)1488 {return '$$' + rel + '$' + suffix + '$$'}1489function specialn (rel,n)1490 {var out = '$' + rel + '$';1491  for (var i=0; i<n; i++) {out += 'b'};1492  out += '$'; 1493  return out}1494function codify (x,hlist,alist)1495 {if (varp(x))1496     {if (alist[x]) {return alist[x]};1497      if (find(x,hlist)) {return seq('pluug',kwotify(x),'bl','bl')};1498      return kwotify(x)};1499  if (symbolp(x)) {return kwotify(x)};1500  var exp = seq('seq');1501  for (var i=0; i<x.length; i++)1502      {exp.push(codify(x[i],hlist,alist))};1503  return exp}1504function kwotify (x)1505 {return ("'" + x + "'")}1506function makesequence (char,n)1507 {var out = '';1508  for (var i=0; i<n; i++)1509      {out = out.concat([char])};1510  return out}1511function getarity (rel,rules)1512 {for (var i=0; i<rules.length; i++)1513      {var dum = getarityexp(rel,rules[i]);1514       if (dum) {return dum}};1515  return 0}1516function getarityexp (rel,x)1517 {if (symbolp(x)) {if (x===rel) {return 0} else {return false}};1518  if (x[0]===rel) {return x.length-1};1519  for (var i=1; i<x.length; i++)1520      {var dum = getarityexp(rel,x[i]);1521       if (dum) {return dum}};1522  return false}1523function programands (s)1524 {if (s.length===0) {return 'true'};1525  if (s.length===1) {return s[0]};1526  var out = ['and'];1527  for (var i=0; i<s.length; i++)1528      {if (s[i]==='true') {continue};1529       if (s[i]==='false') {return 'false'};1530       out.push(s[i])};1531  if (out.length===1) {return 'true'};1532  if (out.length===2) {return out[1]};1533  return out}1534//==============================================================================1535// grind1536//==============================================================================1537//------------------------------------------------------------------------------1538// prettyjs1539//------------------------------------------------------------------------------1540function prettyjs (x)1541 {return '\r' + prettifyjs(x,0) + '\r'}1542function prettifyjs (x,n)1543 {if (symbolp(x)) {return x};1544  if (x[0]==='seq') {return '[' + grindjsargs(x.slice(1)) + ']'};1545  if (x[0]==='obj') {return '{' + grindjsargs(x.slice(1)) + '}'};1546  if (x[0]==='dot') {return grindjs(x[1]) + '.' + grindjs(x[2])};1547  if (x[0]==='sub') {return grindjs(x[1]) + '[' + x[2] + ']'};1548  if (x[0]==='bind') {return "var " + x[1] + " = " + grindjs(x[2])};1549  if (x[0]==='method') {return grindjs(x[1]) + grindjsarglist(x.slice(2))};1550  if (x[0]==='transform') {return grindjs(x[1]) + ' => ' + grindjs(x[2])};1551  if (x[0]==='less') {return grindjs(x[1]) + '<' + grindjs(x[2])};1552  if (x[0]==='greater') {return grindjs(x[1]) + '>' + grindjs(x[2])};1553  if (x[0]==='eq') {return grindjs(x[1]) + '===' + grindjs(x[2])};1554  if (x[0]==='if')1555     {var answer = 'if (' + grindjs(x[1]) + ")\r";1556      answer += grindindent(n+3) + prettifyjs(x[2],n+3);1557      return answer};1558  if (x[0]==='not') {return "!" + grindjs(x[1])};1559  if (x[0]==='and')1560     {var answer = '(';1561      for (var i=1; i<x.length-1; i++)1562          {answer += grindjs(x[i]) + ' && '};1563      answer += grindjs(x[x.length-1]) + ')';1564      return answer};1565  if (x[0]==='return') {return "return " + grindjs(x[1])};1566  if (x[0]==='block')1567     {var answer = '{';1568      if (x.length>1) {answer += prettifyjs(x[1],n+1) + ';'};1569      for (var i=2; i<x.length; i++)1570          {answer += '\r' + grindindent(n+1) + prettifyjs(x[i],n+1) + ';'};1571      answer += '}';1572      return answer};1573  if (x[0]==='loop')1574     {var answer = 'for ';1575      answer += '(var ' + x[1] + '=' + x[2] + '; ';1576      answer += x[1] + '<' + x[3] + '; ';1577      answer += x[1] + '++)\r';1578      answer += grindindent(n+4) + prettifyjs(x[4],n+4);1579      return answer}1580  if (x[0]=== 'function')1581     {var answer = 'function ' + x[1] + ' ' + grindjsarglist(x[2]) + '\r';1582      answer += grindindent(n+1) + '{';1583      if (x.length>3) {answer += prettifyjs(x[3],n+2)};1584      for (var i=4; i<x.length; i++)1585          {answer += ';\r' + grindindent(n+2) + prettifyjs(x[i],n+2)};1586      answer += '}';1587      return answer}1588  return x[0] + grindjsarglist(x.slice(1))}1589//------------------------------------------------------------------------------1590// grindjs1591//------------------------------------------------------------------------------1592function grindjs (x)1593 {if (symbolp(x)) {return x};1594  if (x[0]==='seq') {return '[' + grindjsargs(x.slice(1)) + ']'};1595  if (x[0]==='obj') {return '{' + grindjsargs(x.slice(1)) + '}'};1596  if (x[0]==='dot') {return grindjs(x[1]) + '.' + grindjs(x[2])};1597  if (x[0]==='sub') {return grindjs(x[1]) + '[' + x[2] + ']'};1598  if (x[0]==='bind') {return "var " + x[1] + " = " + grindjs(x[2])};1599  if (x[0]==='method') {return grindjs(x[1]) + grindjsarglist(x.slice(2))};1600  if (x[0]==='transform') {return grindjs(x[1]) + ' => ' + grindjs(x[2])};1601  if (x[0]==='less') {return grindjs(x[1]) + '<' + grindjs(x[2])};1602  if (x[0]==='greater') {return grindjs(x[1]) + '>' + grindjs(x[2])};1603  if (x[0]==='eq') {return grindjs(x[1]) + '===' + grindjs(x[2])};1604  if (x[0]==='if') {return "if (" + grindjs(x[1]) + ")\r" + grindjs(x[2])};1605  if (x[0]==='not') {return "!" + grindjs(x[1])};1606  if (x[0]==='and')1607     {var answer = '';1608      for (var i=1; i<x.length-1; i++)1609          {answer += grindjs(x[i]) + ' && '};1610      answer += grindjs(x[x.length-1]);1611      return answer};1612  if (x[0]==='return') {return "return " + grindjs(x[1])};1613  if (x[0]==='block')1614     {var answer = '{';1615      for (var i=1; i<x.length-1; i++)1616          {answer += grindjs(x[i]) + '; '};1617      answer += grindjs(x[x.length-1]) + '}';1618      return answer};1619  if (x[0]==='loop')1620     {var answer = 'for ';1621      answer += '(var ' + x[1] + '=' + x[2] + '; ';1622      answer += x[1] + '<' + x[3] + '; ';1623      answer += x[1] + '++)\r'1624      answer += grindjs(x[4]);1625      return answer}1626  if (x[0]=== 'function')1627     {var answer = 'function ' + x[1] + ' ' + grindjsarglist(x[2]) + '\r';1628      answer += '{';1629      for (var i=3; i<x.length-1; i++)1630          {answer += grindjs(x[i]) + '; '};1631      answer += grindjs(x[i]) + '}';1632      return answer}1633  return x[0] + grindjsarglist(x.slice(1))}1634function grindjsarglist (p)1635 {var exp = '(';1636  if (p.length>0) {exp += grindjs(p[0])};1637  for (var i=1; i<p.length; i++)1638      {exp = exp + ',' + grindjs(p[i])}1639  exp += ')';1640  return exp}1641function grindjsargs (l)1642 {var exp = '';1643  if (l.length>0) {exp += grindjs(l[0])};1644  for (var i=1; i<l.length; i++)1645      {exp = exp + ',' + grindjs(l[i])}1646  return exp}1647function grindindent (n)1648 {var out = '';1649  for (var i=0; i<n; i++) {out += ' '};1650  return out}1651//------------------------------------------------------------------------------1652// End of Script...routes-loader.js
Source:routes-loader.js  
...22    if (err) return cb(err);23    this.matches = matches;24    this.acc = [];25    this.indent = 0;26    this.compileMatch();27    this.push(''); // add a space between the functions28    this.compileResolve();29    cb(null, this.acc.join('\n'));30  }31};32Routes.prototype.glob = function(cb) {33  glob(this.dir + '/**/*' + this.ext, {mark: true}, function(err, files) {34    if (err) return cb(err);35    cb(null, files.reduce(this.handleFile.bind(this), []));36  }.bind(this));37};38Routes.prototype.handleFile = function(acc, file) {39  var pathname = Path.basename(Path.dirname(file));40  var isDefault = pathname === '__default__';...bindings.js
Source:bindings.js  
...180                //#endif181            }182            183            //always give a function, no async calls (could also just error on execution)184            c[name] = ppc.lm.compileMatch(s); 185            186            //#ifdef __WITH_AML_BINDINGS187            c[name].hasAml = hasAml;188            //#endif189            190            return c;191        }192        193        for (name in this) {194            if (name == "each")195                continue;196            197            rules  = this[name];198            if (rules.dataType != ppc.ARRAY)199                continue;200            201            s = [], hasAml = false;202            for (var rule, i = 0, l = rules.length; i < l; i++) {203                if (!(rule = rules[i]).match && !rule.value)204                    continue;205206                s.push(rule.match, rule.value);207                //#ifdef __WITH_AML_BINDINGS208                if (!hasAml && rule.value)209                    hasAml = rule.hasaml || rule.value.indexOf("<a:") > -1;210                //#endif211            }212            213            //always give a function, no async calls (could also just error on execution)214            c[name] = ppc.lm.compileMatch(s); 215            216            //#ifdef __WITH_AML_BINDINGS217            c[name].hasAml = hasAml;218            //#endif219        }220221        this.$isCompiled = true;222        223        return c;224    },225    226    getRuleIndex : function(name, index) {227        var rule = this[name][index];228        if (rule.value) {
...compileMatch.mjs
Source:compileMatch.mjs  
1const PRELUDE = `// * * * * * * * * * * * * * * * * BEGIN PRELUDE * * * * * * * * * * * * * * * * //2function node (name) {3  let c = (class extends Array {})4  Object.defineProperty(c, 'name', { value: name })5  return new c()6}7function matchRegex (origtext, regex) {8  let matches = origtext.match(regex)9  if (matches !== null) {10    let text = origtext.slice(matches[0].length)11    return [matches[0], text]12  } else {13    return [null, origtext]14  }15}16function matchWhitespace (text) {17  let token = node('WHITESPACE')18  let _token, _text = text19  // Consume (or backtrack)20  ;[_token, _text] = matchRegex(_text, /^(\\s*(--[^\\n]*\\n)?)*/)21  if (_token === null) { return [null, text] } else { _token && token.push(_token) }22  return [token, _text]23}24const matchJustIdentifier = (text) => {25  let _token, _text = text26  ;[_token, _text] = matchRegex(_text, /^[a-zA-Z_]\\w*/)27  if (_token === null) { return [null, text] } else { return [_token, _text] }28}29function matchJustNumber (origtext) {30  let matches = origtext.match(/^[0-9]+/)31  if (matches !== null) {32    let text = origtext.slice(matches[0].length)33    return [['NUMBER', matches[0]], text]34  } else {35    return [null, origtext]36  }37}38const matchJustString = (origtext) => {39  if (origtext.startsWith("'")) {40    // search for ending quote41    let i = 142    i = origtext.indexOf("'", i)43    while (i + 1 < origtext.length && origtext[i+1] === "'") {44      // keep searching45      i = origtext.indexOf("'", i + 2)46      if  (i === -1) {47        return [null, origtext]48      }49    }50    return [origtext.slice(1, i).replace(/''/g, "'"), origtext.slice(i+1)]51  }52  return [null, origtext]53}54function _matchSTRING (text) {55  let token = node('STRING')56  let _token, _text = text57  // Consume (or backtrack)58  ;[_token, _text] = matchWhitespace(_text)59  if (_token !== null) _token = false // Discard literal60  if (_token === null) { return [null, text] } else { _token && token.push(_token) }61  // Consume (or backtrack)62  ;[_token, _text] = matchJustString(_text)63  if (_token === null) { return [null, text] } else { _token && token.push(_token) }64  return [token, _text]65}66function _matchID (text) {67  let token = node('ID')68  let _token, _text = text69  // Consume (or backtrack)70  ;[_token, _text] = matchWhitespace(_text)71  if (_token !== null) _token = false // Discard literal72  if (_token === null) { return [null, text] } else { _token && token.push(_token) }73  // Consume (or backtrack)74  ;[_token, _text] = matchJustIdentifier(_text)75  if (_token === null) { return [null, text] } else { _token && token.push(_token) }76  return [token, _text]77}78function _matchLITERAL (text, literal) {79  let token = node('LITERAL')80  let _token, _text = text81  ;[_token, _text] = matchWhitespace(_text)82  if (_text.startsWith(literal)) {83    token.push(literal)84    return [token, _text.slice(literal.length)]85  } else {86    return [null, text]87  }88}89// * * * * * * * * * * * * * * * * END PRELUDE * * * * * * * * * * * * * * * * //90`91export { compileMatch }92function compileMatch (ast) {93  let text = PRELUDE94  let indent = 095  function out (line) {96    text += ' '.repeat(indent * 2) + line + '\n'97  }98  function start (line = '') {99    text += ' '.repeat(indent * 2) + line100  }101  function mid (line = '') {102    text += line103  }104  function end (line = '') {105    text += line + '\n'106  }107  function call (ast) {108    fns(ast.constructor.name)(...ast)109  }110  const fns = {111    SYNTAX (id, rules) {112      start(`export { match`)113      call(id)114      end(` }`)115      end()116      call(rules)117    },118    ID (value) {119      mid(value)120    },121    RULES (...rules) {122      for (let rule of rules) {123        call(rule)124      }125    },126    RULE (id, exp) {127      start(`function match`)128      call(id)129      end(`} (text) {`)130      indent++131      out(`let _token, _text = text`)132      end()133      call(exp)134      out(`return [token, _text]`)135      indent--136      out(`}`)137      end()138    },139    EXP (alt) {140      call(alt)141    },142    ALT (...seqs) {143      if (seqs.length === 1) {144        call(seqs[0])145      } else {146        out(`{`)147        indent++148        out(`let __text = _text`)149        out(`for (let i = 0; i <= ${seqs.length}; i++) {`)150        indent++151        out(`let breakFor = false`)152        out(`switch (i) {`)153        indent++154        for (let seq of seqs) {155          out(`case ${i}: {`)156          indent++157          call(seq)158          out(`breakFor = true`)159          indent--160          out(`}`)161        }162        out(`case ${seqs.length}: {`)163        indent++164        out(`// Backtrack`)165        out(`return [null, text]`)166        indent--167        out(`}`)168        indent--169        out(`}`)170        indent--171        out(`_text = __text`)172        indent--173        out(`}`)174      }175    },176    TYPE (alt, id) {177      start(`let token = node('`)178      call(id)179      end(`}')`)180      call(alt)181    },182    SEQ (...terms) {183      if (terms.length === 1) {184        call(terms[0])185      } else {186        let first = terms.shift()187        out(`// Consume (or backtrack)`)188        call(first)189        out(`if (_token === null) { return [null, text] } else { _token && token.push(_token) }`)190        end()191        for (let term of terms) {192          out(`// Consume (or throw)`)193          call(term)194          out(`if (_token === null) { throw new Error(text) } else { _token && token.push(_token) }`)195          end()196        }197      }198    },199    ID2 (value) {200      start(`;[_token, _text] = match`)201      mid(value)202      end(`(_text)`)203    },204    STRING (value) {205      start(`;[_token, _text] = _matchLITERAL(_text, '`)206      mid(value)207      end(`')`)208      out(`if (_token !== null) _token = false // Discard literal`)209    },210    LITERAL (value) {211      if (value === '.ID') {212        out(`;[_token, _text] = _matchID(_text)`)213      }214    },215    REPEAT (term) {216      out(`// Consume 0 or more times`)217      out(`while (true) {`)218      end()219      indent++220      call(term)221      out(`if (_token === null) { break } else { _token && token.push(_token) }`)222      end()223      indent--224      out(`}`)225      end()226    }227  }228  call(ast)229  return text...compiler.js
Source:compiler.js  
...7      return compileSeries(node.subExpressions);8    case "SUB_EXPRESSION":9      return compileSeries(node.subExpressionItems);10    case "MATCH":11      return compileMatch(node);12    case "GROUP":13      return compileGroup(node);14    case "QUANTIFIER":15      return compileQuantifier(node);16  }17  throw new Error(`unsupported node ${node.kind}`);18}19// a is the input token, b is the expression token20// if b is a function, pass a to b and return the result21// return true if a is contains b22const tokensMatch = (a, b) => {23  if (a == null || b == null) {24    return false;25  }...validation.js
Source:validation.js  
...64        /*var rules = this.$rules[name];65        //@todo Shouldn't allow async calls..., should always give a function66        for (var rule, i = 0, l = rules.length; i < l; i++) {67            var rule = rules[i];68            if ((rule[1] || (rule[1] = (rule[5] = apf.lm.compileMatch(rule[0]))[0] ||apf.K))(xmlNode)) 69                return rule;70        }*/7172        var id = apf.xmldb.nodeConnect(apf.xmldb.getXmlDocId(xmlNode), xmlNode.nodeType == 1 ? xmlNode : xmlNode.parentNode);73        for (var i = 0, l = this.$rules.length; i < l; i++) {74            if (xmlNode.ownerDocument.selectSingleNode("(.//" + this.$rules[i][0].split("|").join("|.//") + ")[@" + apf.xmldb.xmlIdTag + "='" + id + "']"))75                return this.$rules[i][1];76        }77    }78    79    this.validate = function(xmlNode, checkRequired, validityState){80        var rule = this.getRule(xmlNode);81        if (!rule) return validityState;82        
...compiler_extensions.js
Source:compiler_extensions.js  
...53/**54 * `match` to match a single expression for readability55 * @type {CompilerExt}56 */57export function compileMatch(mode, _parent) {58  if (!mode.match) return;59  if (mode.begin || mode.end) throw new Error("begin & end are not supported with match");60  mode.begin = mode.match;61  delete mode.match;62}63/**64 * provides the default 1 relevance to all modes65 * @type {CompilerExt}66 */67export function compileRelevance(mode, _parent) {68  // eslint-disable-next-line no-undefined69  if (mode.relevance === undefined) mode.relevance = 1;...compileMatch.spec.mjs
Source:compileMatch.spec.mjs  
...7describe('compiler', () => {8  const ast = matchSYNTAX(demo)[0]9  it("compiler hasn't broken", () => {10    expect(true).toBe(true)11    let compiled = compileMatch(ast)12    // console.log(compiled)13    expect(compiled).toBe(parser)14  })...Using AI Code Generation
1const { compileMatch } = require('playwright/lib/utils/utils');2const { match } = require('playwright/lib/utils/matchers');3const { compileMatch } = require('playwright/lib/utils/utils');4const { match } = require('playwright/lib/utils/matchers');5const { compileMatch } = require('playwright/lib/utils/utils');6const { match } = require('playwright/lib/utils/matchers');7const { compileMatch } = require('playwright/lib/utils/utils');8const { match } = require('playwright/lib/utils/matchers');9const { compileMatch } = require('playwright/lib/utils/utils');10const { match } = require('playwright/lib/utils/matchers');11const { compileMatch } = require('playwright/lib/utils/utils');12const { match } = require('playwright/lib/utils/matchers');13const { compileMatch } = require('playwright/lib/utils/utils');14const { match } = require('playwright/lib/utils/matchers');15const { compileMatch } = require('playwright/lib/utils/utils');16const { match } = require('playwright/lib/utils/matchers');17const { compileMatch } = require('playwright/lib/utils/utils');18const { match } = require('playwright/lib/utils/matchers');19const { compileMatch } = require('playwright/lib/utils/utils');20const { match } = require('playwright/lib/utils/matchers');21const { compileMatch } = require('playwright/lib/utils/utils');22const { match } = require('playwright/lib/utils/matchers');23const { compileMatch } = require('playwright/lib/utils/utils');24const { match } = require('playwright/lib/utils/matchers');25const { compileMatch } =Using AI Code Generation
1const { compileMatch } = require('playwright/lib/utils/utils');2const { match } = require('playwright/lib/utils/utils');3const { parseURL } = require('playwright/lib/utils/utils');4const { parseDomain } = require('playwright/lib/utils/utils');5const { parsePath } = require('playwright/lib/utils/utils');6const { parseQuery } = require('playwright/lib/utils/utils');7const { parseFragment } = require('playwright/lib/utils/utils');8const parsedUrl = parseURL(url);9const parsedPattern = parseURL(pattern);10const compiledMatch = compileMatch(parsedPattern);11const result = match(compiledMatch, parsedUrl);12const domain = parseDomain(url);13const path = parsePath(url);14const query = parseQuery(url);15const fragment = parseFragment(url);16console.log(result);17console.log(domain);18console.log(path);19console.log(query);20console.log(fragment);21{ query: 'playwright' }Using AI Code Generation
1const { compileMatch } = require('playwright/lib/utils/utils');2const match = compileMatch('**/test.js');3const { compileMatch } = require('playwright/lib/utils/utils');4const match = compileMatch('**/test.js');5const { compileMatch } = require('playwright/lib/utils/utils');6const match = compileMatch('**/test.js');7const { compileMatch } = require('playwright/lib/utils/utils');8const match = compileMatch('**/test.js');9const { compileMatch } = require('playwright/lib/utils/utils');10const match = compileMatch('**/test.js');11const { compileMatch } = require('playwright/lib/utils/utils');12const match = compileMatch('**/test.js');13const { compileMatch } = require('playwright/lib/utils/utils');14const match = compileMatch('**/test.js');15const { compileMatch } = require('playwright/lib/utils/utils');16const match = compileMatch('**/test.js');17console.log(match('/home/user/test.tsUsing AI Code Generation
1const { compileMatch } = require('playwright/lib/utils/utils');2const { Page } = require('playwright/lib/server/page');3const { Frame } = require('playwright/lib/server/frame');4const { compileMatch } = require('playwright/lib/utils/utils');5const { Page } = require('playwright/lib/server/page');6const { Frame } = require('playwright/lib/server/frame');7const { compileMatch } = require('playwright/lib/utils/utils');8const { Page } = require('playwright/lib/server/page');9const { Frame } = require('playwright/lib/server/frame');10const { compileMatch } = require('playwright/lib/utils/utils');11const { Page } = require('playwright/lib/server/page');12const { Frame } = require('playwright/lib/server/frame');13const { compileMatch } = require('playwright/lib/utils/utils');14const { Page } = require('playwright/lib/server/page');15const { Frame } = require('playwright/lib/server/frame');16const { compileMatch } = require('playwright/lib/utils/utils');17const { Page } = require('playwright/lib/server/page');18const { Frame } = require('playwright/lib/server/frame');19const { compileMatch } = require('playwright/lib/utils/utils');20const { Page } = require('playwright/lib/server/page');21const { Frame } = require('playwright/lib/server/frame');22const { compileMatch } = require('playwright/lib/utils/utils');23const { Page } = require('playwright/lib/server/page');24const { Frame } = require('playwright/lib/server/frame');25const { compileMatch } = require('playwright/lib/utils/utils');26const { Page } = require('playwright/lib/server/page');27const { Frame } = require('playwright/lib/server/frame');28const { compileMatch } = require('playwright/lib/utils/utils');29const { Page } = requireUsing AI Code Generation
1const { compileMatch } = require('playwright/lib/server/utils');2const { parse } = require('url');3const path = '/a/b/c/d';4const compiled = compileMatch(path);5const parsed = parse(url);6console.log(compiled(parsed.pathname));7const { compileMatch } = require('playwright/lib/server/utils');8const { parse } = require('url');9const path = '/a/b/c/d';10const compiled = compileMatch(path);11const parsed = parse(url);12console.log(compiled(parsed.pathname));13const { compileMatch } = require('playwright/lib/server/utils');14const { parse } = require('url');15const path = '/a/b/c/d';16const compiled = compileMatch(path);17const parsed = parse(url);18console.log(compiled(parsed.pathname));19const { compileMatch } = require('playwright/lib/server/utils');20const { parse } = require('url');21const path = '/a/b/c/d';22const compiled = compileMatch(path);23const parsed = parse(url);24console.log(compiled(parsed.pathname));25const { compileMatch } = require('playwright/lib/server/utils');26const { parse } = require('url');27const path = '/a/b/c/d';28const compiled = compileMatch(path);29const parsed = parse(url);30console.log(compiled(parsed.pathname));31const { compileMatch } = require('playwright/lib/server/utils');32const { parse } = require('url');33const path = '/a/b/c/d';Using AI Code Generation
1const { compileMatch } = require('playwright/lib/utils/selectorParser');2const matcher = compileMatch('css=div >> text=Hello');3const element = document.querySelector('div');4const { compileSelector } = require('playwright/lib/utils/selectorParser');5const selector = compileSelector({name: 'text', args: 'Hello'}, 'css=div');6const { parseSelector } = require('playwright/lib/utils/selectorParser');7const parsedSelector = parseSelector('css=div >> text=Hello');8const { parseSelectorList } = require('playwright/lib/utils/selectorParser');9const parsedSelectorList = parseSelectorList('css=div >> text=Hello, css=div >> text=World');10const { serializeSelector } = require('playwright/lib/utils/selectorParser');11const serializedSelector = serializeSelector({name: 'text', args: 'Hello'}, 'css=div');12const { serializeSelectorList } = require('playwright/lib/utils/selectorParser');13const serializedSelectorList = serializeSelectorList([{ name: 'text', args: 'Hello', body: 'css=div' }, { name: 'text', args: 'World', body: 'css=div' }]);LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
