-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add B programming language #8056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4880528
4b1c4dc
54781e0
fe3d1e8
276e311
6d4e06f
41f79a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -529,6 +529,14 @@ Awk: | |
| tm_scope: source.awk | ||
| ace_mode: text | ||
| language_id: 28 | ||
| B: | ||
| type: programming | ||
| color: "#da7666" | ||
| extensions: | ||
| - ".b" | ||
| tm_scope: source.c | ||
| ace_mode: text | ||
| language_id: 700792152 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some of these fields, like for tm_scope, it's worth considering if using the same values as the C programming language would be beneficial. You can check how the C syntax highlighing of a B file would look like using https://novalightshow.netlify.app/
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apart from the extrn keyword it looks pretty much the same as C |
||
| B (Formal Method): | ||
| type: programming | ||
| color: "#8aa8c5" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| array[5] 1, 2, 3, 4, 5; | ||
|
|
||
| sum(array, size){ | ||
| auto sum 0; | ||
| auto i 0; while(i < 5){ | ||
| sum =+ array[i]; | ||
| i++; | ||
| } | ||
| return(sum); | ||
| } | ||
|
|
||
| main(){ | ||
| extrn printf; | ||
| auto i 0; while(i < 5){ | ||
| printf("%d*n", array[i]); | ||
| i++; | ||
| } | ||
| printf("sum = %d*n", sum(array, 5)); | ||
| return(0); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| main() extrn printf; auto i 123; printf("%d*n", i); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| char(string, i){ | ||
| return(*(string + i) & 255); | ||
| } | ||
|
|
||
| lchar(string, i, char){ | ||
| *(string + i) = char & 255; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| main(){ | ||
| extrn initscr, endwin, curs_set, clear, mvprintw, refresh, getch; | ||
| auto k, x 1, y 1; | ||
|
|
||
| initscr(); | ||
| curs_set(0); | ||
|
|
||
| while(1){ | ||
| clear(); | ||
|
|
||
| mvprintw(0, 0, "Hello, from B!"); | ||
| mvprintw(y, x, "@"); | ||
|
|
||
| refresh(); | ||
|
|
||
| k = getch(); | ||
| if(k == 'q') break; | ||
| else if(k == 'w') y =- 1; | ||
| else if(k == 's') y =+ 1; | ||
| else if(k == 'a') x =- 1; | ||
| else if(k == 'd') x =+ 1; | ||
| } | ||
|
|
||
| endwin(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* https://github.com/bext-lang/b/blob/main/examples/duffs_device.b */ | ||
| /* // https://en.wikipedia.org/wiki/Duff%27s_device */ | ||
| duffs_device(s) { | ||
| extrn strlen, putchar; | ||
| auto n, count; | ||
| count = strlen(s); | ||
| n = (count + 7) / 8; | ||
| switch count%8 { | ||
| case 0: while(1) { putchar(*s++); | ||
| case 7: putchar(*s++); | ||
| case 6: putchar(*s++); | ||
| case 5: putchar(*s++); | ||
| case 4: putchar(*s++); | ||
| case 3: putchar(*s++); | ||
| case 2: putchar(*s++); | ||
| case 1: putchar(*s++); | ||
| putchar('|'); | ||
| putchar(10); | ||
| if (--n <= 0) return(0); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| main() { | ||
| duffs_device("The quick brown fox jumps over the lazy dog."); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /* https://www.nokia.com/bell-labs/about/dennis-m-ritchie/bref.html */ | ||
| /* This function replaces each upper case character in the input | ||
| string s by its lower case equivalent. It uses the fact that | ||
| the ascii alphabetic characters are contiguous. */ | ||
|
|
||
| lower(s) { | ||
| auto c,i; | ||
| i = -1; | ||
| while( (c=char(s,++i)) != '*e' ) | ||
| if( c >= 'A' & c <= 'Z' ) lchar(s~i~c-'A'+'a'); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* https://www.nokia.com/bell-labs/about/dennis-m-ritchie/kbman.html */ | ||
| /* The following function is a general formatting, printing, and | ||
| conversion subroutine. The first argument is a format string. | ||
| Character sequences of the form `%x' are interpreted and cause | ||
| conversion of type 'x' of the next argument, other character | ||
| sequences are printed verbatim. Thus | ||
|
|
||
| printf("delta is %d*n", delta); | ||
|
|
||
| will convert the variable delta to decimal (%d) and print the | ||
| string with the converted form of delta in place of %d. The | ||
| conversions %d-decimal, %o-octal, *s-string and %c-character | ||
| are allowed. | ||
|
|
||
| This program calls upon the function `printn'. (see section | ||
| 9.1) */ | ||
|
|
||
| printf(fmt, x1,x2,x3,x4,x5,x6,x7,x8,x9) { | ||
| extrn printn, char, putchar; | ||
| auto adx, x, c, i, j; | ||
|
|
||
| i= 0; /* fmt index */ | ||
| adx = &x1; /* argument pointer */ | ||
| loop : | ||
| while((c=char(fmt,i++) ) != `%') { | ||
| if(c == `*e') | ||
| return; | ||
| putchar(c); | ||
| } | ||
| x = *adx++; | ||
| switch c = char(fmt,i++) { | ||
|
|
||
| case `d': /* decimal */ | ||
| case `o': /* octal */ | ||
| if(x < O) { | ||
| x = -x ; | ||
| putchar('-'); | ||
| } | ||
| printn(x, c=='o'?8:1O); | ||
| goto loop; | ||
|
|
||
| case 'c' : /* char */ | ||
| putchar(x); | ||
| goto loop; | ||
|
|
||
| case 's': /* string */ | ||
| while(c=char(x, j++)) != '*e') | ||
| putchar(c); | ||
| goto loop; | ||
| } | ||
| putchar('%') ; | ||
| i--; | ||
| adx--; | ||
| goto loop; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* https://www.nokia.com/bell-labs/about/dennis-m-ritchie/kbman.html */ | ||
| /* The following function will print a non-negative number, n, to | ||
| the base b, where 2<=b<=10, This routine uses the fact that | ||
| in the ANSCII character set, the digits O to 9 have sequential | ||
| code values. */ | ||
|
|
||
| printn(n,b) { | ||
| extrn putchar; | ||
| auto a; | ||
|
|
||
| if(a=n/b) /* assignment, not test for equality */ | ||
| printn(a, b); /* recursive */ | ||
| putchar(n%b + '0'); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, we'd also include a rule for Limbo and Brainfuck (Bf).
There's already rules inside the heuristics.yml file that we can re-use:
and