Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/linguist/heuristics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ disambiguations:
- language: LTspice Symbol
pattern: '^SymbolType[ \t]'
- language: Asymptote
- extensions: ['.b']
rules:
- language: B
pattern: '^\s*(?:extrn|auto|goto)\b'
negative_pattern: '^\s*(?:implement|include|load|ref)\b'

Copy link
Copy Markdown
Contributor

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:

  - language: Brainfuck
    pattern: '(>\+>|>\+<)'

and

  - language: Limbo
    pattern: '^\w+\s*:\s*module\s*\{'

- language: Brainfuck
pattern: '(>\+>|>\+<)'
- language: Limbo
pattern: '^\w+\s*:\s*module\s*\{'
- extensions: ['.bas']
rules:
- language: B4X
Expand Down
8 changes: 8 additions & 0 deletions lib/linguist/languages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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/

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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"
Expand Down
20 changes: 20 additions & 0 deletions samples/B/array.b
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);
}
1 change: 1 addition & 0 deletions samples/B/carrying_statement.b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main() extrn printf; auto i 123; printf("%d*n", i);
7 changes: 7 additions & 0 deletions samples/B/char.b
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;
}
25 changes: 25 additions & 0 deletions samples/B/curses.b
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();
}
26 changes: 26 additions & 0 deletions samples/B/duffs_device.b
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.");
}
11 changes: 11 additions & 0 deletions samples/B/lower.b
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');
}
55 changes: 55 additions & 0 deletions samples/B/printf.b
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;
}
14 changes: 14 additions & 0 deletions samples/B/printn.b
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');
}
Loading