Skip to content

Commit

Permalink
[oil-language] Implement overlooked != operator
Browse files Browse the repository at this point in the history
Caught by Danilo Spinella.

- Add test cases from the other examples.
- Language FAQ: Explain limited proc / shell function return values.

Thanks for the feedback!
  • Loading branch information
Andy Chu committed Jan 8, 2021
1 parent d5a31dc commit 1c3de58
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
16 changes: 16 additions & 0 deletions doc/oil-language-faq.md
Expand Up @@ -16,6 +16,7 @@ Old and new constructs exist side-by-side. New constructs have fewer
<div id="toc">
</div>


## What's the difference between `$(dirname $x)` and `$len(x)` ?

Superficially, both of these syntaxes take an argument `x` and return a
Expand All @@ -29,6 +30,21 @@ string. But they are different:

(Note: command subs may be optimized later, as `ksh` does.)

## How can I return rich values from shell functions / Oil `proc`s?

There are two primary ways:

- Print the "returned" data to `stdout`. Retrieve it with a command sub like
`$(myproc)` or a pipeline like `myproc | read --line`.
- Use an "out param" with [setref]($oil-help:setref).

Oil may grow true functions with the `func` keyword at some point. However,
that must be done carefully, as a `proc` composes with processes, but a `func`
doesn't.

Send us feedback if this doesn't make sense, or if you want a longer
explanation.

## Why doesn't a raw string work here: `${array[r'\']}` ?

Oil has two array index syntax:
Expand Down
3 changes: 3 additions & 0 deletions oil_lang/expr_eval.py
Expand Up @@ -350,8 +350,11 @@ def EvalExpr(self, node):
result = left >= right
elif op.id == Id.Arith_LessEqual:
result = left <= right

elif op.id == Id.Arith_DEqual:
result = left == right
elif op.id == Id.Arith_NEqual:
result = left != right

elif op.id == Id.Expr_In:
result = left in right
Expand Down
55 changes: 55 additions & 0 deletions spec/oil-user-feedback.test.sh
@@ -0,0 +1,55 @@
# Oil user feedback

# From Zulip:
#
# https://oilshell.zulipchat.com/#narrow/stream/121540-oil-discuss/topic/Experience.20using.20oil

#### setvar

# This seems to work as expected?

proc get_opt(arg, :out) {
setvar out = $arg
}
var a = ''
get_opt a 'lol'
echo hi
## status: 1
## STDOUT:
## END

#### != operator
var a = 'bar'

# NOTE: a != foo is idiomatic)
if ($a != 'foo') {
echo 'not equal'
}

if ($a != 'bar') {
echo 'should not get here'
}

## STDOUT:
not equal
## END


#### Regex Literal

# This seems to work as expected?

proc get_opt(arg, :out) {
setref out = $(write -- $arg | cut -d'=' -f2)
}

var variant = ''
var arg = '--variant=foo'
if ( arg ~ / '--variant=' <word> / ) {
get_opt $arg :variant
echo variant=$variant
}

## STDOUT:
variant=foo
## END
5 changes: 5 additions & 0 deletions test/spec.sh
Expand Up @@ -980,6 +980,11 @@ oil-interactive() {
$OIL_LIST "$@"
}

oil-user-feedback() {
sh-spec spec/oil-user-feedback.test.sh --osh-failures-allowed 0 \
$OIL_LIST "$@"
}

ble-idioms() {
sh-spec spec/ble-idioms.test.sh --osh-failures-allowed 0 \
$BASH $ZSH $MKSH $BUSYBOX_ASH $OSH_LIST "$@"
Expand Down

0 comments on commit 1c3de58

Please sign in to comment.