3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 13:59:47 +01:00

compiler_vm: C removed unnecessary regex replace (#63)

This problem has previously been encountered. And my last solution was
not satisfactory.

It seems like there were 2 regex lines, one targeting `//` and another
targeting `/* */`.

Originally they were basically meant to perform this:

Search for

```c
;<COMMENT>;\n
```

and replacing it with

```c
;<COMMENT>
```

In e00ba2e62f I provided a patch to add
another `;` for `//` as it would eat the first `;` after the `<COMMENT>`

After thinking for some time, I came to the conclusion that the lines of
REGEX serve no purpose and should be removed.

For future reference:

e00ba2e62f was targeting this problem:

```c
printf("why is the last ; missing?"); // foo \n int a=42;
```

which generated something along the lines of

```c
    printf("why is the last ; missing?");
// foo
    int a = 42return 0;
```

Where it would strip a `;` from the first line after a `//` comment

And this commit additionally targeted:

```c
printf("foo\n");
//printf("bar\n");
printf("baz\n");
// only happens if the line ends with ; eg:
printf("hello\n");
// deny with ;
printf("world\n");
```
output:

    foo
    hello

expected:

    foo
    baz
    hello
    world

where it generated the code:

```c
    printf("foo\n");
//printf("bar\n" );printf("baz\n" );
// only happens if the line ends with ; eg:
    printf("hello\n");
// deny with ;printf("world\n" );
```

Thus this should fix #62 and the original problem
This commit is contained in:
0xACE 2021-11-03 21:31:07 +00:00 committed by GitHub
parent 4cf1e76169
commit 1dae484e3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -355,8 +355,6 @@ sub preprocess_code {
$self->{code} =~ s/^\s+//; $self->{code} =~ s/^\s+//;
$self->{code} =~ s/\s+$//; $self->{code} =~ s/\s+$//;
$self->{code} =~ s/;\s*;\n/;\n/gs; $self->{code} =~ s/;\s*;\n/;\n/gs;
$self->{code} =~ s/;(\s*\/\*.*?\*\/\s*);\n/;$1/gs;
$self->{code} =~ s/;(\s*\/\/.*?\s*);\n/;$1;/gs;
$self->{code} =~ s/(\{|})\n\s*;\n/$1\n/gs; $self->{code} =~ s/(\{|})\n\s*;\n/$1\n/gs;
$self->{code} =~ s/(?:\n\n)+/\n\n/g; $self->{code} =~ s/(?:\n\n)+/\n\n/g;