Commit Graph

49 Commits

Author SHA1 Message Date
Qi Xiao
959120162b pkg/eval: Use cmpd.StringLiteral in one more place.
Also remove an unused method that is now superseded by the cmpd package.
2021-03-19 23:18:42 +00:00
Qi Xiao
634d71dcba Extract utilities for compound nodes into pkg/parse/cmpd. 2021-02-24 00:02:47 +00:00
Qi Xiao
196eea21d4 Change module import path to src.elv.sh 2021-01-27 01:30:25 +00:00
Qi Xiao
37c3c0b0de Deprecate the use of "a=b" for non-temporary assignment in 0.15.
It is still the syntax for temporary assignment; only using it as a standalone
command is deprecated.
2021-01-19 23:28:34 +00:00
Qi Xiao
072677351b Introduce "var" and "set".
Assignment using "foo = bar" instead of "var foo = bar" or "set foo = bar" is
not yet deprecated, but will be soon.

This addresses #645.
2021-01-16 06:11:37 +00:00
Qi Xiao
22dc0e6a6b Move parsing of ordinary assignment form to the compile phase.
This will unblock the implementation of the "var" special command.

This addresses #645.
2021-01-16 01:48:51 +00:00
Qi Xiao
d84e9801b5 Don't compile the arguments of special commands twice.
This fixes #1204.
2021-01-09 23:33:24 +00:00
Qi Xiao
363b712f66 pkg/eval: Eliminate in-place mutations of *Ns.
This commit replaces scopeOp, the only remaining place that mutates *Ns in
place, with nsOp, which performs copy-on-write for *Ns.

As a result, the "eval" command no longer mutates the passed namespace. The
default namespace it uses is also changed to match the default of "-source" (an
amalgamated namespace from the local and upvalue scopes), making "-source $file"
equivalent to "eval (slurp <$file)", and formally deprecated.

Another result is that (*Evaler).Eval can now guard the mutation of the global
namespace with the mutex, making it concurrency-safe to execute multiple sources
that touch the global namespace.

This fixes #1137.
2021-01-09 15:02:15 +00:00
Qi Xiao
af906f1d76 pkg/eval: Change all the *Op types to return Exception instead of error.
Also change the variable name used to keep the Exception returned from "err" to
"exc".

This uncovers several error scenarios that were not returning Exception, and
would result in the absense of stack traces when such errors occur.
2021-01-09 00:25:32 +00:00
Qi Xiao
b77a052e7e pkg/eval: Make Exception an interface.
This change is a preparation step for refining all *Op types to return Exception
as the error.

Keeping Exception as a struct type will make such a change error-prone, since
a (*Exception)(nil) != error(nil), so if an *Op returns a nil *Exception, it
is not nil if the return value is stored in an error-typed variable.
2021-01-08 00:56:45 +00:00
Kurtis Rader
5564c8f754 Fix handling of unexpected errors
Doing something like the following is likely to result in too many open
files (assuming `ulimit -n` == 256) resulting in a panic:

    > fn fact [n]{ if (== $n 0) { put 1 } else { put (* $n (fact (- $n 1))) } }
    > fact 60
    panic: interface conversion: error is *os.SyscallError, not
    *eval.Exception

    goroutine 24161 [running]:
    github.com/elves/elvish/pkg/eval.(*pipelineOp).exec.func1(0x152a5a0,
    0xc000dca210, 0xc000a44e70, 0xc00057b540, 0xc001030590, 0x203000)
        github.com/elves/elvish/pkg/eval/compile_effect.go:153 +0x152
    created by github.com/elves/elvish/pkg/eval.(*pipelineOp).exec
        github.com/elves/elvish/pkg/eval/compile_effect.go:149 +0x225
    Exception: elvish exited with 2

Fixes #1208
2021-01-07 22:34:22 +00:00
Qi Xiao
2033600881 pkg/eval: Clean up Port related code. 2021-01-05 05:23:17 +00:00
Qi Xiao
6e8ecc8bd5 pkg/eval: Cleanups for reducing the API surface area number of files.
- Move NewEnvListVar to pkg/eval/vars.

- De-export GlobPattern, GlobFlag and ExternalCmd.

- Merge editor.go and chdir.go into eval.go, value_helper.go into compile_value.go.

- Remove eval_internal_test.go and replace it with a new test for $pid.
2021-01-05 04:33:52 +00:00
Qi Xiao
a2790af67a pkg/eval: Clean up the structure and methods of Evaler and Frame.
- Make Evaler mostly thread-safe. The only remaining thread-unsafe part is the
  modules field, which is more tricky than other fields.

- Remove the state and evalerScopes type, and move their fields into Evaler.

- Expose valuePrefix via a get method, and change PortsFromFiles to take the
  prefix instead of a *Evaler. Also expose a PortsFromStdFiles.

- Make Evaler a normal field of Frame, instead of an embedded field. This makes
  access to global states more explicit.
2021-01-05 00:22:09 +00:00
Qi Xiao
01612e291b pkg/eval: Clean up closure call implementation.
- Remove reliance on scopeOp, concentrating all the scope for creating the local
  scope in (*closure).call.

- Check options at the very beginning, and include all unsupported options in the
  error.
2021-01-04 13:46:00 +00:00
Qi Xiao
8613527450 pkg/eval: Deduplicate code for command head resolution. 2020-12-25 21:30:57 +00:00
Qi Xiao
6419f4524a Implement namespaces using slices instead of maps.
More related improvements and cleanups will be done in followup commits.

This fixes #1139.
2020-12-25 17:46:46 +00:00
Kurtis Rader
4df5e8e015 Use exported methods to access ports
Commit 734eb95 changed most uses of `fm.ports[x].` to a public method
that makes the intent clearer. This changes a couple of uses that were
overlooked by that prior change.
2020-12-25 01:11:33 +00:00
Qi Xiao
6f3b8a787b Minor fixup for last commit. 2020-11-07 19:02:44 +00:00
Kurtis Rader
b58b495d77 Fix hang reading value channel from a redirection
Fixes #600
Fixes #1010
2020-11-07 13:52:29 -05:00
Qi Xiao
734eb955e2 pkg/eval: Use exported methods to access ports from builtin functions. 2020-10-10 22:28:13 +01:00
Qi Xiao
bb122024dd pkg/eval: Move test framework into new evaltest package. 2020-09-03 06:51:21 +01:00
Qi Xiao
11898f763a pkg/util: Break into fsutil/ and logutil/. 2020-09-03 05:27:18 +01:00
Qi Xiao
0bbee3b5f4 pkg/eval: Fix the error message when using dynamic string as head. 2020-08-31 14:01:49 +01:00
Qi Xiao
b04ea12d48 pkg/eval: Refactor effectOp and valuesOp, remove lvaluesOp.
* Make effectOp and valuesOp interfaces - implementations now handle the
  range information themselves.

* Remove lvaluesOp; it is no longer used.
2020-08-22 19:57:11 +01:00
Qi Xiao
1e2ead56e2 pkg/eval: Rewrite the lvalue parsing code.
Also lift the restriction on rest variables and rest arguments - they may now
appear anywhere, as long as there is only one.
2020-08-20 13:15:00 +01:00
Kurtis Rader
28798ac053 Convert "XXX" to "TODO" comments
I stumbled across a comment that began with "XXX". It was clearly meant as
a "TODO" comment. This changes all such occurrences. However, a few "XXX"
comments are ambiguous and a better prefix might be "WARNING". The "TODO"
prefix at least ensures someone, eventually, looks into the situation
and either rewords the comment or fixes the problem. This change means
everyone can assume searching for "// TODO" will find all such comments
rather than requiring they also know to search for "// XXX".
2020-08-16 16:29:34 +01:00
Qi Xiao
c211679f0c Move all variable creations to start of lexical scope.
Commit 8c71635ca3 moved the creation to the start
of pipelines; the approach works for simplistic cases like "x = 1 | nop", but
fails in more complex cases, such as "nop (x = 1) | nop".

The correct place to hoist variable creations is the lexical scope, and this
commit implements this approach.

This fixes #623.
2020-04-26 19:05:16 +01:00
Qi Xiao
c767f92d01 Support compile-time deprecation.
This fixes #898.
2020-04-26 13:14:51 +01:00
Qi Xiao
21f5a6f7c3 pkg/parse: Turn most public methods of Node into functions.
This make the godoc of the parse package much cleaner and removes some
boilerplate.
2020-04-25 18:55:44 +01:00
Qi Xiao
5cdff82883 pkg/eval: Fix stack trace, and add tests. 2020-04-18 00:28:48 +01:00
Qi Xiao
6597d38834 pkg/eval: Test more error cases. 2020-04-13 16:29:01 +01:00
Qi Xiao
a36a372624 pkg: Fix small issues found by staticcheck. 2020-04-13 13:56:10 +01:00
Qi Xiao
64e282c828 pkg/eval: Replace ValuesUnwrapper with simple functions.
Also add structured BadValue error type to pkg/eval/errs.
2020-04-13 13:27:55 +01:00
Qi Xiao
07a9e781a6 pkg/eval: Report detailed ArityMismatch errors. 2020-04-13 00:47:33 +01:00
Qi Xiao
c65e970951 pkg/eval: ComposeExceptionsFromPipeline -> makePipelineError. 2020-04-10 01:25:55 +01:00
Qi Xiao
096ee354d6 pkg/eval: Simplify API for output capture. 2020-04-10 01:12:25 +01:00
Qi Xiao
af6b523e02 pkg/eval: Remove Frame.{eval,Call}. 2020-04-09 21:01:14 +01:00
Qi Xiao
ffa9727f14 pkg/eval: Remove Frame.srcRange.
Maintaining a state of current range is error-prone. Instead, always keep track
of range information explicitly.
2020-04-09 20:44:09 +01:00
Qi Xiao
730bd969d8 pkg/eval: Fix position information in spacey assignments.
Also add more tests against spacey assignments.
2020-04-02 00:29:18 +01:00
Qi Xiao
f49cf9f80e pkg/eval: More tests, especially against error cases.
Also fix a bug in handling redirections from invalid types.

This addresses #944.
2020-03-31 04:58:42 +01:00
Qi Xiao
8c71635ca3 pkg/eval: Move all assignments to the start of the pipeline they belong in.
This avoids race conditions of accessing the local scope. The test case
"x = 1", "put $x | y = (all)" used to contain a race condition but no longer
does.

This addresses #73.
2020-03-28 23:49:30 +00:00
Qi Xiao
72234e70fa [cleanup] pkg/eval: Replace frame.{begin,end} with a Ranging. 2020-03-28 22:03:42 +00:00
Qi Xiao
3abfc42a8e [cleanup] pkg/eval: Remove boilerplate.go. 2020-03-28 21:33:55 +00:00
Qi Xiao
e6b7d06e80 [cleanup] pkg/eval: Remove compiler.errorf and compiler.compiling.
All errors are now written using compiler.errorpf.
2020-03-28 21:33:55 +00:00
Qi Xiao
11919e83ec [cleanup] pkg/eval: use diag.Ranging in op types. 2020-03-28 21:33:55 +00:00
Qi Xiao
5550819ac0 pkg/eval: Check FD when using it as redir source.
This fixes #788.
2020-01-12 00:42:18 +00:00
Qi Xiao
64b104f533 Support dynamic command names for unambiguous external commands.
This fixes #764.
2020-01-11 18:57:39 +00:00
Qi Xiao
e45fdf7228 Move all libraries to new pkg/. 2019-12-23 20:00:59 +00:00