mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 15:22:43 +00:00 
			
		
		
		
	 1a4ac3531f
			
		
	
	
		1a4ac3531f
		
	
	
	
	
		
			
			This patchset allows a match expression to have a list of names for its
glob parts, which are assigned to the matched values in the body of the
match.
For example,
```sh
stuff=foobarblahblah/target_{1..30}
for $stuff {
    match $it {
        */* as (dir sub) {
            echo "doing things with $sub in $dir"
            make -C $dir $sub # or whatever...
        }
    }
}
```
With this, match expressions are now significantly more powerful!
		
	
			
		
			
				
	
	
		
			83 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
| #!/bin/Shell
 | |
| 
 | |
| result=no
 | |
| match hello {
 | |
|     he* { result=yes }
 | |
|     * { result=fail }
 | |
| };
 | |
| 
 | |
| test "$result" = yes || echo invalid result $result for normal string match, single option && exit 1
 | |
| 
 | |
| result=no
 | |
| match hello {
 | |
|     he* | f* { result=yes }
 | |
|     * { result=fail }
 | |
| };
 | |
| 
 | |
| test "$result" = yes || echo invalid result $result for normal string match, multiple options && exit 1
 | |
| 
 | |
| result=no
 | |
| match (well hello friends) {
 | |
|     (* *) { result=fail }
 | |
|     (* * *) { result=yes }
 | |
|     * { result=fail }
 | |
| };
 | |
| 
 | |
| test "$result" = yes || echo invalid result $result for list match && exit 1
 | |
| 
 | |
| result=no
 | |
| match yes as v {
 | |
|     () { result=fail }
 | |
|     (*) { result=yes }
 | |
|     * { result=$v }
 | |
| };
 | |
| 
 | |
| test "$result" = yes || echo invalid result $result for match with name && exit 1
 | |
| 
 | |
| result=no
 | |
| # $(...) is a list, $(echo) should be an empty list, not an empty string
 | |
| match $(echo) {
 | |
|     * { result=fail }
 | |
|     () { result=yes }
 | |
| };
 | |
| 
 | |
| test "$result" = yes || echo invalid result $result for list subst match && exit 1
 | |
| 
 | |
| result=no
 | |
| # "$(...)" is a string, "$(echo)" should be an empty string, not an empty list
 | |
| match "$(echo)" {
 | |
|     * { result=yes }
 | |
|     () { result=fail }
 | |
| };
 | |
| 
 | |
| test "$result" = yes || echo invalid result $result for string subst match && exit 1
 | |
| 
 | |
| match (foo bar) {
 | |
|     (f? *) as (x y) {
 | |
|         result=fail
 | |
|     }
 | |
|     (f* b*) as (x y) {
 | |
|         if [ "$x" = oo -a "$y" = ar ] {
 | |
|             result=yes
 | |
|         } else {
 | |
|             result=fail
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| test "$result" = yes || echo invalid result $result for subst match with name && exit 1
 | |
| 
 | |
| match (foo bar baz) {
 | |
|     (f? * *z) as (x y z) {
 | |
|         result=fail
 | |
|     }
 | |
|     (f* b* *z) as (x y z) {
 | |
|         if [ "$x" = oo -a "$y" = ar -a "$z" = ba ] {
 | |
|             result=yes
 | |
|         } else {
 | |
|             result=fail
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| test "$result" = yes || echo invalid result $result for subst match with name 2 && exit 1
 |