Warning: Work in progress! Leave feedback on Zulip or Github if you'd like this doc to be updated.

Interpreter State

The Oil project has a single interpreter that supports both the OSH and Oil languages.

In other words, It's useful to think of Unix shell in historical layers:

Table of Contents
Example
Design Goals
Simplify and Rationalize bash
Add New Features and Types
High Level Description
Memory Is a Stack
Enviroment Variables Become Global Variables
Functions and Variables Are Separate
Variable Name Lookup with "Dynamic Scope"
Limitations of Arrays And Compound Data Structures
Integers and Coercion
Unix fork() Has Copy-On-Write Semantics
Key Data Types
cell
value
cmd_value for shell builtins
Printing State
Shell Builtins
pp in Oil
Modifying State
Oil Keywords
Shell Assignment Builtins: declare/typeset, readonly, export
unset
Other Builtins
Links
Appendix: Bash Issues
Strings and Arrays Are Confused
Indexed Arrays and Associative Arrays Are Confused
Empty and Unset Are Confused

Example

Shell has many syntaxes for the same semantics, which can be confusing. For example, in bash, these four statements do similar things:

$ foo='bar'
$ declare -g foo=bar
$ x='foo=bar'; typeset $x
$ printf -v foo bar

$ echo $foo
bar

In addition Oil, adds JavaScript-like syntax:

var foo = 'bar'

Oil's syntax can express more data types, but it may also confuse new users.

So the sections below describe the shell from a semantic perspective, which should help users reason about their programs.

Quick tip: Use the pp builtin to inspect shell variables.

Design Goals

Simplify and Rationalize bash

POSIX shell has a fairly simple model: everything is a string, and "$@" is a special case.

Bash adds many features on top of POSIX, including arrays and associative arrays. Oil implements those features, and a few more.

However, it also significantly simplifies the model.

A primary difference is mentioned in Known Differences:

In other words, Oil "salvages" the confusing semantics of bash and produces something simpler, while still being very compatible.

Add New Features and Types

TODO

High Level Description

Memory Is a Stack

Enviroment Variables Become Global Variables

On initialization, environment variables like PYTHONPATH=. are copied into the shell's memory as global variables, with the export flag set.

Global variables are stored in the first stack frame, i.e. the one at index 0.

Functions and Variables Are Separate

There are two distinct namespaces. For example:

foo() {
  echo 'function named foo'
}
foo=bar   # a variable; doesn't affect the function

Variable Name Lookup with "Dynamic Scope"

OSH has it, but Oil limits it.

Limitations of Arrays And Compound Data Structures

Shell is a value-oriented language.

Example:

declare -a myarray=("${other_array[@]}")   # shell

var myarray = %( @other_array )            # Oil

Reason: There's no Garbage collection.

Integers and Coercion

Unix fork() Has Copy-On-Write Semantics

See the Process Model document.

Key Data Types

TODO: core/runtime.asdl

cell

TODO

value

Undef, Str, Sequential/Indexed Arrays, Associative Array

cmd_value for shell builtins

Another important type:

  assign_arg = (lvalue lval, value? rval, int spid)

  cmd_value =
    Argv(string* argv, int* arg_spids, command__BraceGroup? block)
  | Assign(builtin builtin_id,
           string* argv, int* arg_spids,
           assign_arg* pairs)

Printing State

Shell Builtins

Oil supports various shell and bash operations to view the interpretr state.

pp in Oil

Pretty prints a cell.

This is cleaner!

TODO: What about functions

Modifying State

Oil Keywords

TODO: See Oil Keywords doc.

Shell Assignment Builtins: declare/typeset, readonly, export

...

unset

You can't unset an array in OSH? But you can in bash.

Other Builtins

Links

Appendix: Bash Issues

Strings and Arrays Are Confused

Horrible

a=('1 2' 3)
b=(1 '2 3')  # two different elements

[[ $a == $b ]]
[[ ${a[0]} == ${b[0]} ]]

[[ ${a[@]} == ${b[@]} ]]

Associative arrays and being undefined

Indexed Arrays and Associative Arrays Are Confused

Empty and Unset Are Confused


Generated on Sat Jan 23 00:06:31 PST 2021