Skip to content

Floating-point parsing support in the std/parsing library #96

@maximecb

Description

@maximecb

A little while back, I spun off some of the lower-level parsing functions in the Plush parser into their own parsing library, so that other languages could reuse them. These functions basically do lexing, parsing of identifiers and string literals, things that are not overly specific to the Plush language.

One feature that is missing from the parsing library is the ability to parse floating-point numbers. I wrote a parseInt function, because that's relatively straightforward, but parsing numbers in general is trickier. The main reason why it's trickier is that all numbers start with a digit, and you don't know, when you begin parsing the number, if you are parsing a float or not.

The current Plush implementation solves this problem by accumulating the digits in a string. This, however, is kind of bad, because it causes a series of successive string concatenations. I would like to have an implementation that avoids this.

IMO, the best strategy is probably to note the current index in the input string being parsed when starting to parse a number, and scan until either a period is hit, or the end of the digits are found. Then get the chars as a substring, and delegate the actual parsing to the $str_to_i32 or $str_to_f32 instructions. Possibly, integer parsing could be done directly, to completely avoid the allocation caused by the substring call.

There are other details to keep in consideration, such that some values might be too large to be parsed as valid integers. This should result in an exception being thrown (see parseError function in the parsing library).

Lastly, I would like, for now, to force floats to end with the f char. For example, 3.2f. This is because we currently only support 32-bit floats, but may eventually support doubles.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions