buzz-language / buzz

👨‍🚀 buzz, A small/lightweight statically typed scripting language
https://buzz-lang.dev
MIT License
1.22k stars 34 forks source link

Compiler crashes after reporting not found symbol #196

Closed sneekyfoxx closed 12 months ago

sneekyfoxx commented 1 year ago

System Version

6.5.4-arch2-1

Description

Cannot run buzz file due to import failure. Buzz cannot find shared object files. If std is the only import then everything works fine.

Steps To Reproduce

git clone https://github.com/buzz-language/buzz && cd ./buzz/
cd ./mir/
git submodule update --init
make
cd ../

# for install without mimalloc
sudo zig build -Doptimize=ReleaseSafe -Dmimalloc=false install -p /usr/local

If the build succeeds, create a new file: test.buzz containing the code below

import "std";
import "os";

fun main([str] args) > void {
  if (args.len() == 0) {
    print("Hello, Buzz!");
  }
  else {
    os.buzzExit(1);
  }

  os.buzzExit(0);

Error Messages

./test.buzz:9:5: [E75] Compile error: `os` is not defined
     7 ╭─   }
     8 │    else {
     9 │      os.buzzExit(1);
       ┆      ╭─
       ┆      ╰─ `os` is not defined
    10 │    }
    11 ╰─   os.buzzExit(0);
thread 9965 panic: reached unreachable code
/home/sneekyfoxx/buzz/src/node.zig:0:0: 0x2d3d35 in generate (buzz)
/home/sneekyfoxx/buzz/src/node.zig:283:43: 0x2895ff in generate (buzz)
        _ = try self.expression.toByteCode(self.expression, codegen, breaks);
                                          ^
/home/sneekyfoxx/buzz/src/node.zig:6777:41: 0x2547b9 in generate (buzz)
            _ = try statement.toByteCode(statement, codegen, breaks);
                                        ^
/home/sneekyfoxx/buzz/src/node.zig:5768:43: 0x3306c0 in generate (buzz)
            _ = try else_branch.toByteCode(else_branch, codegen, breaks);
                                          ^
/home/sneekyfoxx/buzz/src/node.zig:6777:41: 0x2547b9 in generate (buzz)
            _ = try statement.toByteCode(statement, codegen, breaks);
                                        ^
/home/sneekyfoxx/buzz/src/node.zig:3614:48: 0x23d9f4 in generate (buzz)
            _ = try self.body.?.node.toByteCode(&self.body.?.node, codegen, breaks);
                                               ^
/home/sneekyfoxx/buzz/src/node.zig:4875:46: 0x273938 in generate (buzz)
        _ = try self.function.node.toByteCode(&self.function.node, codegen, breaks);
                                             ^
/home/sneekyfoxx/buzz/src/node.zig:6777:41: 0x2547b9 in generate (buzz)
            _ = try statement.toByteCode(statement, codegen, breaks);
                                        ^
/home/sneekyfoxx/buzz/src/node.zig:3614:48: 0x23d9f4 in generate (buzz)
            _ = try self.body.?.node.toByteCode(&self.body.?.node, codegen, breaks);
                                               ^
/home/sneekyfoxx/buzz/src/codegen.zig:93:50: 0x248a27 in runFile (buzz)
        const function = try root.node.toByteCode(&root.node, self, null);
                                                 ^
/home/sneekyfoxx/buzz/src/main.zig:300:12: 0x24d125 in main (buzz)
    runFile(
           ^
/home/sneekyfoxx/.local/lib/zig/std/start.zig:486:37: 0x24fe86 in main (buzz)
    std.os.argv = @as([*][*:0]u8, @ptrCast(c_argv))[0..@as(usize, @intCast(c_argc))];
                                    ^
???:?:?: 0x7f1ca5845ccf in ??? (libc.so.6)
Unwind information for `libc.so.6:0x7f1ca5845ccf` was not available, trace may be incomplete

[1]    9965 IOT instruction (core dumped)  buzz ./test.buzz

The code below works:

import "std";

fun main([str] args) > int {
  if (args.len() == 0) {
    print("Hello, Buzz!");
  }
  else {
    return 1;
  }

  return 0;
}
output: Hello, Buzz!
giann commented 1 year ago

The error stack is not normal but the initial error message is accurate. If you want the os import to be prefixed you have to write:

import "std";
import "os" as os;

fun main([str] args) > void {
  if (args.len() == 0) {
    print("Hello, Buzz!");
  } else {
    os.exit(1);
  }

  os.exit(0);
}

Without as, the imported symbols will be in the global namespace.

Also you don't need os.exit if you're directly in main: just change its signature to return an int and return the number you want.

giann commented 1 year ago

The libc thing is just zig telling you it can't unwind the stack further into it because there's no more debug symbols.

sneekyfoxx commented 1 year ago

Oh, ok thank you for the information. Your language has the potential to be better than most scripting languages as well. 👌🏾

sneekyfoxx commented 1 year ago

Is it just os.exit() or os.buzzExit()? Because I don't see exit in the standard library on the reference page on your website.

giann commented 1 year ago

Is it just os.exit() or os.buzzExit()? Because I don't see exit in the standard library on the reference page on your website.

The website is inaccurate the function is exit

I'll do a major update of the guide and reference for the 0.3.0 release.

In the meantime you can look at src/lib/*.buzz files to get the up to date API.

giann commented 12 months ago

@sneekyfoxx fixed the crash

sneekyfoxx commented 12 months ago

@giann I wanted to ask you whether you are going to implement a way for .buzz files to be converted to executables?

giann commented 12 months ago

Not really. Like most scripting language you can start your script with #!/usr/bin/env buzz and chmod +x yourscript.buzz.