Symbol collisions. #2

Open
opened 2025-05-22 19:02:26 +00:00 by wryl · 0 comments
Owner

When compiling programs, it is possible for numeric literals to clash with symbolic ones. This can result in very odd bugs where a symbol like foo gets confused for the number 13 and vice-versa.

To avoid this, I propose that we expand numeric literals into pairs. 10 would expand into # 10, 12345 would expand into # 12345, etc. This would allow us to disambiguate between numeric literals and actual symbols.

In rules, to match a number, we can add the #variable syntax. That expands into # $variable, allowing you to explicitly match numbers.

This feature should be an optional processing pass, as nothing stops you from manually annotating your values. It is simply a convenient syntax.

This will break some existing code that handles numeric literals alongside symbols, but I have a feeling it'd be a worthy change. We can't rely on strings forever.

Example usage is shown below.

|:: do something with $a-symbol|
    :symbol: $a-symbol

|:: do something with #a-number|
    :number: #a-number

|::|
:: do something with foo
:: do something with bar
:: do something with 10
:: do something with 20

Expanding this syntax yields the following.

|:: do something with $a-symbol|
    :symbol: $a-symbol

|:: do something with # $a-number|
    :number: # $a-number

|::|
:: do something with foo
:: do something with bar
:: do something with # 10
:: do something with # 20
When compiling programs, it is possible for numeric literals to clash with symbolic ones. This can result in very odd bugs where a symbol like `foo` gets confused for the number `13` and vice-versa. To avoid this, I propose that we expand numeric literals into pairs. `10` would expand into `# 10`, `12345` would expand into `# 12345`, etc. This would allow us to disambiguate between numeric literals and actual symbols. In rules, to match a number, we can add the `#variable` syntax. That expands into `# $variable`, allowing you to explicitly match numbers. This feature should be an optional processing pass, as nothing stops you from manually annotating your values. It is simply a convenient syntax. This will break some existing code that handles numeric literals alongside symbols, but I have a feeling it'd be a worthy change. We can't rely on strings forever. Example usage is shown below. ``` |:: do something with $a-symbol| :symbol: $a-symbol |:: do something with #a-number| :number: #a-number |::| :: do something with foo :: do something with bar :: do something with 10 :: do something with 20 ``` Expanding this syntax yields the following. ``` |:: do something with $a-symbol| :symbol: $a-symbol |:: do something with # $a-number| :number: # $a-number |::| :: do something with foo :: do something with bar :: do something with # 10 :: do something with # 20 ```
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: wryl/serpens#2
No description provided.