{"id":13779659,"url":"https://github.com/tlack/b-decoded","last_synced_at":"2025-03-17T02:31:18.233Z","repository":{"id":50500133,"uuid":"110335432","full_name":"tlack/b-decoded","owner":"tlack","description":"arthur whitney's b interpreter translated into a more traditional flavor of C","archived":false,"fork":false,"pushed_at":"2017-11-12T03:35:20.000Z","size":30,"stargazers_count":97,"open_issues_count":2,"forks_count":8,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-16T07:23:33.652Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tlack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-11T10:04:42.000Z","updated_at":"2025-03-09T01:48:05.000Z","dependencies_parsed_at":"2022-08-19T10:20:40.259Z","dependency_job_id":null,"html_url":"https://github.com/tlack/b-decoded","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlack%2Fb-decoded","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlack%2Fb-decoded/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlack%2Fb-decoded/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlack%2Fb-decoded/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tlack","download_url":"https://codeload.github.com/tlack/b-decoded/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243961734,"owners_count":20375297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T18:01:07.677Z","updated_at":"2025-03-17T02:31:17.966Z","avatar_url":"https://github.com/tlack.png","language":"C","funding_links":[],"categories":["C"],"sub_categories":[],"readme":"b Demistified, Volume 1, Abridged\n=================================\n\nThis is my attempt at understanding how the `b` programming language interpreter works.\n\n`b` is a systems-level programming language by Arthur Whitney, the creator of K, Q, and Kdb.\n\nHe has informally released it and sporadically updates the code at http://kparc.com/b/\n\nFrom his README:\n\n```\nfast c compiler (called b: isomorphic to c)\n\ncomputer language benchmarks: t.b and t.c\n\n         run  compile(ms) runsize\n     b   230     .06       1K\ngcc-O1   320   60.00       3K\ngcc-O2   230   90.00       3K\ngcc-O9   230  200.00       8K\n\nb generated code is fast. already matches the fastest gcc/llvm.\nb is fast. 1000 times faster than gcc/llvm/visualc. game changer.\nb is interactive. much better error reporting and development potential.\nb code is smaller(shrug?) and uses much less stack(shrug?).\n\nb parses and compiles a language isomorphic to c. \nsame types. same ops. same control. more compact. (no precedence)\n\nb : +-*%^\u0026|\u003c\u003e =  /  \\   $[c;t;f]\nc = +-*/^\u0026|\u003c\u003e == \u003c\u003c \u003e\u003e  if(c)t;else f;\n\nargtypes(for x,y,z) declared. \nresult and local types are automatic.\n\ne.g. binary search\n\nb: b[Ii]{h:#x;l:0;while(h\u003el)$[y\u003ex[i:/l+h];l:i+1;h:i];l}\nc: I b(I*x,I y){I h=x[-1],i,l=0;while(h\u003el)if(y\u003ex[i=l+h\u003e\u003e1])l=i+1;else h=i;R l;}\n\ntodo: \n\nalign, inline, unroll (we win)\narm, llvm, ..\nc89, c99, .. (not interesting right now since i already prefer b and there is no c code we care about)\n\nnotes:\nA.S kernel interface (map unmap ..)\na.c replaces libc (fp and pf need more work)\nb.c compiles b code\n \np() parse(this would change for c89 etc.  minor. the rest is the same.)\nd() to register, constant or global\ne() to register\nf() force\n..\n\n\nfor arm, llvm, etc. just need to replace:\njmp tst cmp o2\nret cll psh pop\n```\n\nArthur Style\n---------\n\nHere's an example of two lines of the `b` source code (from a.c, written in pretty much plain old C):\n\n```\nJ read(),write();I open(),close(),fstat(),munmap();S mmap();V exit();ZF ms(){J a,d;asm volatile(\"rdtsc\":\"=a\"(a),\"=d\"(d));R((d\u003c\u003c32)+a)*.58e-6;}\nV w2(S s){write(2,s,strlen(s));}ZS r2(S s){ZC b[256];R w2(s),b[read(0,b,256)-1]=0,b;}ZI rand(){ZJ j0=-314159;R j0=4294957665L*j0+(j0\u003e\u003e32);}\n```\n\nArthur is famous for his very dense programming style. Most C programmers would\nscream when seeing this code.\n\nIn his view (and others in the terse scene), it is much better to have\neverything in your application readable on the screen at once than to have\ngreat names for things or a lot of white space to comfort the first timer\nreader. \n\nTo them, once you've sufficiently studied that screen or two of code, you can\nunderstand all of it at the same time. If it's spread out over thousands of\nfiles, it's very difficult to understand all of it, which leads to bugs,\nunnecessary abstraction, and the need for advanced tooling just to work with\nyour own project's code.\n\nHe wants to see the code \"all at once\" so he can understand all of its behavior without paging around\nand shifting his focus to another tab, window, etc. To get there he makes a lot of tradeoffs in terms \nof the code formatting and naming conventions. He also, in `b`, creates a dense set of interlocking\nmacros and abstractions that can make the code very hard to follow.\n\nCritics and the uninitiated say that his code is like old school modem line\nnoise: random punctuation intermixed with bits of understandable code. I would\nsuggest that he's actually quite careful with the abstractions he chooses and\nthey are actually not always the most dense, highly compressed code structures\navailable to him. He chooses wisely and his code rewards deep study.\n\nArthur, by the way, says his code is like woodgrain, a more romantic view. \n\nAbout this repo\n------\n\nHere I have reworked parts of the code so they are easier to understand by a\nprogrammer of the traditional school of code formatting, including comments\nexplaining my presumption of the intent of a bit of code.\n\nFor what it's worth, I don't pretend to truly understand `b`. \n\nI've also gone through and added some comments here and there to help the reader\nunderstand how it works.\n\nI am just sorta exploding all the parts here and reworking them for easier\nunderstanding. I've erred on the side of too much white space and commenting in\nmy adaptation (as presented here) because I think the \"guts\" of it are so\ninteresting and complex they deserve to be presented all alone.\n\nNote that `b` doesn't actually compile for me right now, so I'm flyin blind.\nThis is more for study and curiosity, and perhaps a window into how Arthur's\nmind works, than to compile and use. Check out the Kparc master repo which\nmight be closer to being usable.\n\nI do not track changes from the `b` master repo in this repo.\n\nLegal\n-----\n\nI do not claim copyright on this code. I have uploaded this to GitHub for\neducational purposes only. If you have a question about this code's license or\nyou want to use it in any way, contact Arthur Whitney directly.\n\nNotes\n-----\n\nTo understand the code, you must understand the macros in `a.h` first. These are critical in higher\nlevel portions of the code, especially that in `b.c`.\n\nMisc notes, no particular order:\n\n* General conventions:\n\t* Types are abbreviated to single letters:\n\t  * `f` float\n\t\t* `i` int\n\t\t* `j` long\n\t\t* `c` byte\n\t\t* `s` string \n\t\t* `k` internal b value (name probably comes from Q's k.h)\n  * `x` generally is a locally-defined variable that contains the argument we are operating on\n  * Likewise, these same functions are defined for `y`, `r`, and `z`.\n* Values:\n\t* An elements length is stored as an int in the item *before* the pointer, so:\n\t* `xn` calls `xI[-1]` where:\n\t\t* `xI` basically does `(int*)x`\n\t\t* `xJ`, `xF`, \n\t* There are other, related functions (implemented as macros in `a.h`):\n\t\t* `xm` accesses x[-8], which is the memory allocated to the value\n\t\t* `xr` accesses x[-7], which is the ref count\n\t\t* `xu` accesses x[-6], which I think are the attributes \n\t\t* `xt` accesses x[-5], which is the type\n* Common functions:\n\t* `j2` joins two items into a list.\n\t* `nt` is the size in bytes of each of the types\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlack%2Fb-decoded","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftlack%2Fb-decoded","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlack%2Fb-decoded/lists"}