diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-01-12 15:04:47 -0500 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-01-12 15:04:47 -0500 |
| commit | 775625d162faab5f628872a0ca625f671a80d700 (patch) | |
| tree | 59c423f7242cfb3389465afd410cb796a48f6576 | |
| parent | d8033ff6cd612f8737f3a5fb2905482632488dda (diff) | |
Move error formatting code from repl.js to bqn.js
| -rw-r--r-- | docs/bqn.js | 23 | ||||
| -rw-r--r-- | docs/repl.js | 24 |
2 files changed, 24 insertions, 23 deletions
diff --git a/docs/bqn.js b/docs/bqn.js index db3085c0..cbb2802a 100644 --- a/docs/bqn.js +++ b/docs/bqn.js @@ -233,7 +233,28 @@ let fmt1 = run( )(list([type, decompose, glyph, fmtnum])); let fmt = x => fmt1(x).map(c=>c===0?" ":c).join(""); +let fmtErr = (s,e) => { + let r=e.src, w=e.message, loc=[]; + while (w&&w.loc||(r!=='!'&&w.sh&&w.sh[0]===2)) { + let is; [is,w]=w; + let n=is.sh?is.sh[0]:0, i=n?is[0]:is; + let pair=n&&is.sh.length>1; if (pair) n*=2; + let to=i=>s.slice(0,i).join('').split('\n').map(l=>Array.from(l)); + let ll=to(i), l=ll.length-1, j=ll[l].length, m=to()[l]; + let k=1,o=i-j,cl=j; while (k<n&&(cl=is[k]-o)<m.length) k++; + let ol=k<n; if (pair) { if (k%2) cl=m.length; else { k--; cl++; } } + let c=Array(cl).fill(0); c[j]=1; + for (let h=1;h<k;h++) c[is[h]-o+(pair?h%2:0)]=1; + if (pair) for (let h=1;h<cl;h++) c[h]^=c[h-1]; + let add = ['',m.join(''),c.map(t=>t?'^':' ').join('')]; + loc = add.concat(ol?['(and other lines)']:[], loc); + } + if (r==='!') w=w?fmt(w).replace(/^/gm,'! '):'! Error'; + else w=w.sh?w.join(''):w; + return [w].concat(loc).join('\n'); +} + if (typeof module!=='undefined') { - bqn.fmt=fmt; bqn.compile=compile; bqn.run=run; + bqn.fmt=fmt; bqn.fmtErr=fmtErr; bqn.compile=compile; bqn.run=run; module.exports=bqn; } diff --git a/docs/repl.js b/docs/repl.js index 2fc88caa..4fbbda8e 100644 --- a/docs/repl.js +++ b/docs/repl.js @@ -19,27 +19,6 @@ if (doc.count) { } let setExplain = e=>e; -let showErr = (s,e)=>{ - let r=e.src, w=e.message, loc=[]; - while (w&&w.loc||(r!=='!'&&w.sh&&w.sh[0]===2)) { - let is; [is,w]=w; - let n=is.sh?is.sh[0]:0, i=n?is[0]:is; - let pair=n&&is.sh.length>1; if (pair) n*=2; - let to=i=>s.slice(0,i).join('').split('\n').map(l=>Array.from(l)); - let ll=to(i), l=ll.length-1, j=ll[l].length, m=to()[l]; - let k=1,o=i-j,cl=j; while (k<n&&(cl=is[k]-o)<m.length) k++; - let ol=k<n; if (pair) { if (k%2) cl=m.length; else { k--; cl++; } } - let c=Array(cl).fill(0); c[j]=1; - for (let h=1;h<k;h++) c[is[h]-o+(pair?h%2:0)]=1; - if (pair) for (let h=1;h<cl;h++) c[h]^=c[h-1]; - let add = ['',m.join(''),c.map(t=>t?'^':' ').join('')]; - loc = add.concat(ol?['(and other lines)']:[], loc); - } - if (r==='!') w=w?fmt(w).replace(/^/gm,'! '):'! Error'; - else w=w.sh?w.join(''):w; - doc.rslt.classList.add('err'); - doc.rslt.textContent=[w].concat(loc).join('\n'); -} let repl = ()=>{ let s=Array.from(doc.code.value), src=str(s); doc.rslt.classList.remove('err'); @@ -51,7 +30,8 @@ let repl = ()=>{ setExplain(src,c); doc.rslt.textContent=fmt(run.apply(null,c)); } catch(e) { - showErr(src,e); + doc.rslt.classList.add('err'); + doc.rslt.textContent=fmtErr(src,e); } }, 0); } |
