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

Fix the 'ouble' bug #2300

Open
wants to merge 1 commit into
base: NewParser2020
Choose a base branch
from
Open

Conversation

dc03
Copy link
Contributor

@dc03 dc03 commented May 31, 2022

Adding onto the commit message, here is the relevant state of the compiler when parsing the following code, where the ^ represents the variable pos in collect_variables() and code is printed before synt each time:

buf = buffer_create(32000, buffer_grow, 4);
local double i = 0.0;
local double j = 0.5;
FOUND LOCAL/GLOBAL KEYWORD:
{buf=buffer_create(32000,buffer_grow,4);localdoublei=0.0;localdoublej=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);LLLLLttttttn=000;LLLLLttttttn=000;};
                                        ^
Declared double i as local
WHILE DECLARING:
{buf=buffer_create(32000,buffer_grow,4);i=0.0;localdoublej=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);n=000;LLLLLttttttn=000;};
                                             ^
ERASE FROM CODE: i
dec_name: i
AFTER ERASING INIT:
{buf=buffer_create(32000,buffer_grow,4);i=0.0;localdoublej=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);n=000;LLLLLttttttn=000;};
                                              ^
FOUND LOCAL/GLOBAL KEYWORD:
{buf=buffer_create(32000,buffer_grow,4);i=0.0;localdoublej=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);n=000;LLLLLttttttn=000;};
                                               ^
Declared ouble j as local
WHILE DECLARING:
{buf=buffer_create(32000,buffer_grow,4);i=0.0;lj=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);n=000;Ln=000;};
                                                    ^
ERASE FROM CODE: j
dec_name: j
AFTER ERASING INIT:
{buf=buffer_create(32000,buffer_grow,4);i=0.0;lj=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);n=000;Ln=000;};
                                                     ^

After erasing the initializer of the variable i, the pos is set at the first l of local instead of the semicolon, because of which it skips to the o of local when it starts parsing j because of the pos++ at the end of the loop. Therefore, by setting pos to be one character behind, it leads to the following state:

FOUND LOCAL/GLOBAL KEYWORD:
{buf=buffer_create(32000,buffer_grow,4);localdoublei=0.0;localdoublej=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);LLLLLttttttn=000;LLLLLttttttn=000;};
                                        ^
Declared double i as local
WHILE DECLARING:
{buf=buffer_create(32000,buffer_grow,4);i=0.0;localdoublej=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);n=000;LLLLLttttttn=000;};
                                             ^
ERASE FROM CODE: i
dec_name: i
AFTER ERASING INIT:
{buf=buffer_create(32000,buffer_grow,4);i=0.0;localdoublej=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);n=000;LLLLLttttttn=000;};
                                             ^
FOUND LOCAL/GLOBAL KEYWORD:
{buf=buffer_create(32000,buffer_grow,4);i=0.0;localdoublej=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);n=000;LLLLLttttttn=000;};
                                              ^
Declared double j as local
WHILE DECLARING:
{buf=buffer_create(32000,buffer_grow,4);i=0.0;j=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);n=000;n=000;};
                                                   ^
ERASE FROM CODE: j
dec_name: j
AFTER ERASING INIT:
{buf=buffer_create(32000,buffer_grow,4);i=0.0;j=0.5;};
{nnn=nnnnnnnnnnnnn(00000,nnnnnnnnnnn,0);n=000;n=000;};
                                                   ^

Thus pos points to the right character at the end of parsing the initializer and the bug is fixed.

This bug occurs when the `pos` variable in `collect_variables()`
(`collect_variables.cpp:57`) is set one character too far after erasing
an initializer. This leads to the output getting mangled because of the
skipped character.

For example, consider the script
```
local double i;
local double j;
```

This will lead to the output being
```
double i;
ouble j;
```

Because after parsing `i`, the `pos` will be on the second character of
`local`, i.e. it will see `ocal`, and the way the parser is coded makes
it so that it ends up skipping the first `d` of `double`.

To fix this, just set `pos` to be one character behind when erasing an
initializer.
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.

None yet

1 participant