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

Support for regex and =~ operator #250

Open
KBeDevel opened this issue Jun 27, 2024 · 7 comments
Open

Support for regex and =~ operator #250

KBeDevel opened this issue Jun 27, 2024 · 7 comments
Labels
bug Something isn't working compiler

Comments

@KBeDevel
Copy link

Bash example:

line=" aaaaaab"

pattern="[[:space:]]*(a)?b"

if [[ $line =~ $pattern ]]; then
  echo "Matches"
fi

Source: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Conditional-Constructs

Is there a way or plan to implement this with Amber?

The following code:

main (args) {
  let line = " aaaaaab"

  let pattern = "[[:space:]]*(a)?b"

  if line =~ pattern {
    echo "Matches"
  }
}

Does not compile:

 ERROR 
Identifier 'if' is a reserved keyword
at example.ab:6:3


5| 
6|   if text =~ pattern {
7|     echo "Matches"

Using Amber 0.3.3-alpha

@Mte90 Mte90 added the bug Something isn't working label Jun 28, 2024
@b1ek
Copy link
Member

b1ek commented Jun 28, 2024

that's not an feature that is amber's responsibility to implement.

bash's if works like this:

if (command with args); then
    echo the command's exit code was 0;
fi

therefore if [[ ... ]] is not a bash thing. [[ is a command that is usually installed and behaves mostly the same between platforms.

to do this in amber correctly, you need to do this:

if unsafe $[[ "a" ~= "\w" ]]$ {
    echo "a is a letter"
}

@b1ek b1ek closed this as completed Jun 28, 2024
@b1ek
Copy link
Member

b1ek commented Jun 28, 2024

[[ probably should be added as a builtin tho. but that should be discussed in the issue about builtins

@Ph0enixKM
Copy link
Member

Ph0enixKM commented Jul 1, 2024

We could add Regex to standard library

@Mte90
Copy link
Member

Mte90 commented Jul 17, 2024

A line of code:

if unsafe $[[ {packed_file} ~= ".tar." ]]$ {

Generate:

__AMBER_VAL_2=$([[ ${packed_file} ~= ".tar." ]]);

That fails in bash

@Mte90
Copy link
Member

Mte90 commented Jul 17, 2024

This is right:

 if $[[ {packed_file} = *".tar."* ]]$ {

that generates:

__AMBER_VAL_2=$([[ ${packed_file} = *".tar."* ]]);

The problem is that doens't return anything so the check is always true.

Instead should generate a code like:

if [[ ${packed_file} = *".tar."* ]]; then

In this way works

@Mte90 Mte90 reopened this Jul 17, 2024
@Mte90
Copy link
Member

Mte90 commented Jul 17, 2024

I suggest for regex to use grep that works everywhere and will avoid the situation I mentioned.

@Mte90 Mte90 added the compiler label Jul 19, 2024
@Mte90
Copy link
Member

Mte90 commented Sep 6, 2024

I am not sure if #453 fix this ticket as is a stdlib implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler
Projects
None yet
Development

No branches or pull requests

4 participants