hn-classics/_stories/1992/8533843.md

181 lines
6.5 KiB
Markdown
Raw Permalink Normal View History

---
created_at: '2014-10-30T16:14:32.000Z'
title: Arthur Whitney's One-page Interpreter (1992)
url: http://www.jsoftware.com/jwiki/Essays/Incunabulum
author: chrisdew
points: 59
story_text: ''
comment_text:
num_comments: 50
story_id:
story_title:
story_url:
parent_id:
created_at_i: 1414685672
_tags:
- story
- author_chrisdew
- story_8533843
objectID: '8533843'
2018-06-08 12:05:27 +00:00
year: 1992
---
2018-02-23 18:19:40 +00:00
[Source](http://code.jsoftware.com/wiki/Essays/Incunabulum "Permalink to Essays/Incunabulum - J Wiki")
# Essays/Incunabulum - J Wiki
# Essays/Incunabulum
From J Wiki
< [Essays][1]
Jump to: [navigation][2], [search][3]
One summer weekend in 1989, Arthur Whitney visited Ken Iverson at Kiln Farm and produced—on one page and in one afternoon—an interpreter fragment on the AT&T 3B1 computer. I studied this interpreter for about a week for its organization and programming style; and on Sunday, August 27, 1989, at about four o'clock in the afternoon, wrote the first line of code that became the implementation described in this document.
Arthur's one-page interpreter fragment is as follows:
typedef char C;typedef long I;
typedef struct a{I t,r,d[3],p[2];}*A;
#define P printf
#define R return
#define V1(f) A f(w)A w;
#define V2(f) A f(a,w)A a,w;
#define DO(n,x) {I i=0,_n=(n);for(;i<_n;++i){x;}}
I *ma(n){R(I*)malloc(n*4);}mv(d,s,n)I *d,*s;{DO(n,d[i]=s[i]);}
tr(r,d)I *d;{I z=1;DO(r,z=z*d[i]);R z;}
A ga(t,r,d)I *d;{A z=(A)ma(5+tr(r,d));z->t=t,z->r=r,mv(z->d,d,r);
R z;}
V1(iota){I n=*w->p;A z=ga(0,1,&n);DO(n,z->p[i]=i);R z;}
V2(plus){I r=w->r,*d=w->d,n=tr(r,d);A z=ga(0,r,d);
DO(n,z->p[i]=a->p[i]+w->p[i]);R z;}
V2(from){I r=w->r-1,*d=w->d+1,n=tr(r,d);
A z=ga(w->t,r,d);mv(z->p,w->p+(n**a->p),n);R z;}
V1(box){A z=ga(1,0,0);*z->p=(I)w;R z;}
V2(cat){I an=tr(a->r,a->d),wn=tr(w->r,w->d),n=an+wn;
A z=ga(w->t,1,&n);mv(z->p,a->p,an);mv(z->p+an,w->p,wn);R z;}
V2(find){}
V2(rsh){I r=a->r?*a->d:1,n=tr(r,a->p),wn=tr(w->r,w->d);
A z=ga(w->t,r,a->p);mv(z->p,w->p,wn=n>wn?wn:n);
if(n-=wn)mv(z->p+wn,z->p,n);R z;}
V1(sha){A z=ga(0,1,&w->r);mv(z->p,w->d,w->r);R z;}
V1(id){R w;}V1(size){A z=ga(0,0,0);*z->p=w->r?*w->d:1;R z;}
pi(i){P("%d ",i);}nl(){P("n");}
pr(w)A w;{I r=w->r,*d=w->d,n=tr(r,d);DO(r,pi(d[i]));nl();
if(w->t)DO(n,P("< ");pr(w->p[i]))else DO(n,pi(w->p[i]));nl();}
C vt[]="+{~<#,";
A(*vd[])()={0,plus,from,find,0,rsh,cat},
(*vm[])()={0,id,size,iota,box,sha,0};
I st[26]; qp(a){R a>='a'&&a<='z';}qv(a){R a<'a';}
A ex(e)I *e;{I a=*e;
if(qp(a)){if(e[1]=='=')R st[a-'a']=ex(e+2);a= st[ a-'a'];}
R qv(a)?(*vm[a])(ex(e+1)):e[1]?(*vd[e[1]])(a,ex(e+2)):(A)a;}
noun(c){A z;if(c<'0'||c>'9')R 0;z=ga(0,0,0);*z->p=c-'0';R z;}
verb(c){I i=0;for(;vt[i];)if(vt[i++]==c)R i;R 0;}
I *wd(s)C *s;{I a,n=strlen(s),*e=ma(n+1);C c;
DO(n,e[i]=(a=noun(c=s[i]))?a:(a=verb(c))?a:c);e[n]=0;R e;}
main(){C s[99];while(gets(s))pr(ex(wd(s)));}
* * *
Contributed by [Roger Hui][4]. From [_An Implementation of J][5]_, Appendix A: Incunabulum, 1992-01-27.
Retrieved from "[http://code.jsoftware.com/mediawiki/index.php?title=Essays/Incunabulum&oldid=8624][6]"
## Navigation menu
### Personal tools
* [Log in][7]
### Namespaces
* [Page][8]
* [Discussion][9]
### Variants
### Views
* [Read][10]
* [View source][11]
* [View history][12]
### More
### Search
### Navigation
* [New Users][13]
* [Guides][14]
* [System][15]
* [Showcase][16]
* [Library][17]
* [Community][18]
* [Recent changes][19]
* [Random page][20]
* [J wiki search][21]
### Tools
* [What links here][22]
* [Related changes][23]
* [Special pages][24]
* [Permanent link][25]
* [Page information][26]
### Print/export
* [Create a book][27]
* [Download as PDF][28]
* [Printable version][29]
* This page was last modified on 8 December 2008, at 10:45.
* [Privacy policy][30]
* [About J Wiki][31]
* [Disclaimers][32]
* ![Powered by MediaWiki][33]
[1]: http://code.jsoftware.com/wiki/Essays "Essays"
[2]: http://code.jsoftware.com#mw-head
[3]: http://code.jsoftware.com#p-search
[4]: http://code.jsoftware.com/wiki/User%3ARoger_Hui "User:Roger Hui"
[5]: http://code.jsoftware.com/wiki/Doc/An_Implementation_of_J "Doc/An Implementation of J"
[6]: http://code.jsoftware.com/mediawiki/index.php?title=Essays/Incunabulum&oldid=8624
[7]: /mediawiki/index.php?title=Special:UserLogin&returnto=Essays%2FIncunabulum "You are encouraged to log in; however, it is not mandatory [o]"
[8]: http://code.jsoftware.com/wiki/Essays/Incunabulum "View the content page [c]"
[9]: /mediawiki/index.php?title=Talk:Essays/Incunabulum&action=edit&redlink=1 "Discussion about the content page [t]"
[10]: http://code.jsoftware.com/wiki/Essays/Incunabulum
[11]: /mediawiki/index.php?title=Essays/Incunabulum&action=edit "This page is protected.
You can view its source [e]"
[12]: /mediawiki/index.php?title=Essays/Incunabulum&action=history "Past revisions of this page [h]"
[13]: http://code.jsoftware.com/wiki/Guides/GettingStarted
[14]: http://code.jsoftware.com/wiki/Guides
[15]: http://code.jsoftware.com/wiki/System
[16]: http://code.jsoftware.com/wiki/Showcase
[17]: http://code.jsoftware.com/wiki/Library
[18]: http://code.jsoftware.com/wiki/Community
[19]: http://code.jsoftware.com/wiki/Special%3ARecentChanges
[20]: http://code.jsoftware.com/wiki/Special%3ARandom
[21]: http://code.jsoftware.com/wiki/Special%3AJwikiSearch
[22]: http://code.jsoftware.com/wiki/Special%3AWhatLinksHere/Essays/Incunabulum "A list of all wiki pages that link here [j]"
[23]: http://code.jsoftware.com/wiki/Special%3ARecentChangesLinked/Essays/Incunabulum "Recent changes in pages linked from this page [k]"
[24]: http://code.jsoftware.com/wiki/Special%3ASpecialPages "A list of all special pages [q]"
[25]: /mediawiki/index.php?title=Essays/Incunabulum&oldid=8624 "Permanent link to this revision of the page"
[26]: /mediawiki/index.php?title=Essays/Incunabulum&action=info "More information about this page"
[27]: /mediawiki/index.php?title=Special:Book&bookcmd=book_creator&referer=Essays%2FIncunabulum
[28]: /mediawiki/index.php?title=Special:Book&bookcmd=render_article&arttitle=Essays%2FIncunabulum&oldid=8624&writer=rl
[29]: /mediawiki/index.php?title=Essays/Incunabulum&printable=yes "Printable version of this page [p]"
[30]: http://code.jsoftware.com/wiki/J%3APrivacy_policy "J:Privacy policy"
[31]: http://code.jsoftware.com/wiki/J%3AAbout "J:About"
[32]: http://code.jsoftware.com/wiki/J%3AGeneral_disclaimer "J:General disclaimer"
[33]: http://code.jsoftware.com/mediawiki/resources/assets/poweredby_mediawiki_88x31.png