Skip to content
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

Add support to _ in number literals #185

Conversation

mcbattirola
Copy link
Contributor

@mcbattirola mcbattirola commented Sep 23, 2023

This PR adds support for _ in number literals (int and float) (#163).

The underscore must be between the digits, not at the beginning or end, like in Zig and Go.

Since Zig already has this feature, we don't have to ignore the _ character and can leverage Zig's std.fmt.parseInt/std.fmt.parseFloat.

On hex and binary literals, it works like this, _ after the b or x is not valid, mimicking Zig's behavior, so 0b_000 and 0x_fff are not valid:

test.buzz:28:16: [E78] Syntax error: '_' must be between digits
    26 ╭─   assert(0x0_0_F == 0x00F, message: "hex ok");
    27 │
    28 │    int testx1 = 0b_000;
       ┆                 ╭─
       ┆                 ╰─ '_' must be between digits
    29 │    int testx2 = 0x_fff;
    30 ╰─   |int testx2 = 0x0F_;


I've added some simple test case in tests/001-basic-types.buzz. Please let me know if I should add more extensive tests.

This file is also compiling and running as expected:

import "std";

fun main([str] args) > void {
  int a = 9_9_9;
  int b = 1;
  int c = 1;

  int d = 0;

  float f = 1.0_0;
  float g = 1_0.0_0;

  assert(a + b == 1_000, message: "Sum OK");

  assert(1200.0_01 == 1_200.001, message: "Float sum OK");

  assert(1_0 == 10, message: "1_0 == 10 OK");

  assert(1_0.9 == 10.9, message: "1_0.9 == 10.9 OK");

  assert(0b0_0_1 == 0b001, message: "binary OK");

  assert(0x0_0_F == 0x00F, message: "hex ok");
}

I've also tried a modified version of the example on the website:

import "std";

| 👨‍🚀 buzz is a simple unambiguous scripting language
object Person {
    str name,
    int age = 1_8,

    fun growUp() > int {
        this.age = this.age + 1;

        return this.age;
    }
}

fun main([str] args) > void {
    Person me = Person{
        name = "Giann",
        age = 3_6,
    };

    print("Hello I'm {me.name} and I'm {me.age}.");
    me.growUp();
    print("Hello I'm {me.name} and now I'm {me.age}.");
}

It prints the expected:

Hello I'm Giann and I'm 36.
Hello I'm Giann and now I'm 37.

Please let me know if I'm missing something or should do something differently. I'd be happy to update this PR if requested.

Closes #163

@mcbattirola mcbattirola marked this pull request as draft September 23, 2023 02:14
@mcbattirola mcbattirola changed the title Mcbattirola/allow underscore on number literal Add support to _ in number literals Sep 23, 2023
@giann giann self-requested a review September 23, 2023 02:24
@mcbattirola mcbattirola marked this pull request as ready for review September 23, 2023 02:28
Copy link
Collaborator

@giann giann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.

  • See issue I pointed out about trailing _
  • Please squash your commits and amend the git log to follow https://www.conventionalcommits.org/en/v1.0.0/
  • Test invalid number literals. To do so add a test in tests/compile_errors. Start the test with a comment that matches the expected error message.

src/scanner.zig Outdated Show resolved Hide resolved
@mcbattirola mcbattirola force-pushed the mcbattirola/allow_underscore_on_number_literal branch from 699338d to ae4a582 Compare September 23, 2023 14:45
@mcbattirola mcbattirola force-pushed the mcbattirola/allow_underscore_on_number_literal branch from ae4a582 to 3a342b8 Compare September 23, 2023 15:11
@mcbattirola
Copy link
Contributor Author

Thanks @giann, I've fixed the trailing _ and squashed the commits.

I've also implemented _ in binary and hex literals, as it felt wrong to allow _ in base 10 literals and not on hex/bin. Please let me know if you'd prefer those to be in a separate PR.

@giann giann merged commit f6707ba into buzz-language:main Sep 23, 2023
3 checks passed
@giann
Copy link
Collaborator

giann commented Sep 23, 2023

Thanks!

@mcbattirola mcbattirola deleted the mcbattirola/allow_underscore_on_number_literal branch September 23, 2023 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use _ in number literals
2 participants