Skip to content

Improve type vs variable detection #226

Open
@chqrlie

Description

@chqrlie

The current parser cannot fully disambiguate some expressions that may both be a type and an expression:

  • In sizeof(A[10]), A can be a type and the expression evaluates to the size of an array of 10 A, or A is a global constant array and the expression is the size of an element of this array.

  • when parsing statements, an imperfect heuristic is used to distinguish declarations from expressions: a * b; could be a multiplication whose result is not used or a definition of b as a pointer to a. Case could be used to disambiguate, but I would prefer not to have to rely on case alone.

There is a simple solution to this problem: we could perform 2 full passes on every source file:

  • the first pass would serve only to detect type names: it would parse only type definitions for type names and would use an accelerated tokenizer to parse other statements and bracketed contents. This should be very fast.
  • the second pass would perform the current parsing, but identifiers would no longer be ambiguous, type names would be known and this would simplify the parsing as well as the analysis phases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions