Spydr06 / BCause

BCause (pronounced "because") is a compiler for the old B programming language (1969) for modern systems.
MIT License
54 stars 0 forks source link

Example from tutorial doesn't work. Why? #3

Open zeleniy opened 3 days ago

zeleniy commented 3 days ago

Hello! I get very first example from here and get en error:

$ ls
bcause  examples  libb.a  libb.o  LICENSE  Makefile  README.md  src
$ cat ~/b/putnumb.b
main() {
  auto a, b, c, sum;

  a = 1; b = 2; c = 3;
  sum = a+b+c;
  putnumb(sum);
}
$ ./bcause ~/b/putnumb.b
./bcause: error: undefined identifier ‘putnumb’

Why library function putnumb is undefined? What's wrong?

Spydr06 commented 2 days ago

first, you have to tell the compiler, that putnumb exists, you can do that by adding

extrn putnumb;

to the start of your main() function.

Secondly, you have to define putnumb somewhere, as this is not a function found in B's standard library. Maybe putnumb isn't listed in the B reference, in that case you're welcome to implement it yourself into src/libb/libb.c :D

Or you replace putnumb(sum) with printf("%d*n"). Then your program would look like:

main() {
    extrn printf;
    auto a, b, c, sum;

    a = 1; b = 2; c = 3;
    sum = a + b + c;
    printf("%d*n", sum);
}

I tested this and it seems to work as expected.