Skip to content

Commit

Permalink
[tea] Move it to its own directory.
Browse files Browse the repository at this point in the history
Fix and refactor main program.
  • Loading branch information
Andy Chu committed Nov 1, 2020
1 parent 8f82981 commit 81a79a5
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 69 deletions.
48 changes: 3 additions & 45 deletions bin/oil.py
Expand Up @@ -71,6 +71,7 @@ def _tlog(msg):
from frontend import parse_lib
from osh import builtin_misc
from pylib import os_path
from tea import tea_main
from tools import deps
from tools import osh2oil
from tools import readlink
Expand Down Expand Up @@ -202,49 +203,6 @@ def OshCommandMain(argv):
return 0


def TeaMain(argv):
# type: (str, List[str]) -> int
arena = alloc.Arena()
try:
script_name = argv[1]
arena.PushSource(source.MainFile(script_name))
except IndexError:
arena.PushSource(source.Stdin())
f = sys.stdin
else:
try:
f = open(script_name)
except IOError as e:
stderr_line("tea: Couldn't open %r: %s", script_name,
posix.strerror(e.errno))
return 2

aliases = {} # Dummy value; not respecting aliases!

loader = pyutil.GetResourceLoader()
oil_grammar = pyutil.LoadOilGrammar(loader)

# Not used in tea, but OK...
opt0_array = state.InitOpts()
no_stack = None # type: List[bool] # for mycpp
opt_stacks = [no_stack] * option_i.ARRAY_SIZE # type: List[List[bool]]
parse_opts = optview.Parse(opt0_array, opt_stacks)

# parse `` and a[x+1]=bar differently
parse_ctx = parse_lib.ParseContext(arena, parse_opts, aliases, oil_grammar)

line_reader = reader.FileLineReader(f, arena)

try:
parse_ctx.ParseTeaModule(line_reader)
status = 0
except error.Parse as e:
ui.PrettyPrintError(e, arena)
status = 2

return status


# TODO: Hook up these applets and all valid applets to completion
# APPLETS = ['osh', 'osh', 'oil', 'readlink']

Expand Down Expand Up @@ -309,8 +267,8 @@ def AppBundleMain(argv):
loader, line_input)

elif main_name == 'tea':
main_argv = arg_r.Rest()
return TeaMain(main_argv)
arg_r.Next()
return tea_main.Main(arg_r)

# For testing latency
elif main_name == 'true':
Expand Down
10 changes: 10 additions & 0 deletions frontend/flag_def.py
Expand Up @@ -270,3 +270,13 @@ def _AddShellOptions(spec):
# --verbose?
FORK_SPEC = OilFlags('fork', typed=True)
FORKWAIT_SPEC = OilFlags('forkwait', typed=True)

#
# Tea
#

TEA_MAIN_SPEC = OilFlags('tea_main', typed=True)
TEA_MAIN_SPEC.Flag('-n', args.Bool) # Parse
TEA_MAIN_SPEC.Flag('-c', args.String) # Command snippet
TEA_MAIN_SPEC.Flag('-translate', args.Bool)

18 changes: 6 additions & 12 deletions oil_lang/run.sh
Expand Up @@ -16,18 +16,6 @@ parse-all-osh() {
| xargs -n 1 -- bin/osh -n
}

tea-files() {
find oil_lang/testdata -name '*.tea'
}

parse-all-tea() {
# Parse with the Oil binary
tea-files | xargs -n 1 -- bin/oil -O parse_tea -n

# Standalone tea parser
tea-files | xargs -n 1 -- bin/tea
}

all-passing() {
for prog in oil_lang/testdata/*.{sh,osh}; do
echo $prog
Expand Down Expand Up @@ -70,4 +58,10 @@ demo() {
bin/osh oil_lang/testdata/assign.osh
}

travis() {
parse-all-osh
all-passing
}


"$@"
2 changes: 1 addition & 1 deletion oil_lang/testdata/hello.osh
Expand Up @@ -23,7 +23,7 @@ var list2 = mylist + [4, 5]
echo $list2

# This is a more explicit representation.
repr list2
pp .cell list2

str1='shell string' # traditinoal shell-style assignment

Expand Down
10 changes: 0 additions & 10 deletions oil_lang/testdata/sigil-pairs.sh
Expand Up @@ -34,16 +34,6 @@ var strarray = %(

show @strarray

var typedarray = %[
'quoted' 'words' '*.sh' '{a,b}@example.com' 'sq' "dq ${x:-default}"
]

show @typedarray

var typedarray2 = %[1.0 2.3 3.4]

show @typedarray2

var cmd_sub = $(
echo bare words *.sh {a,b}@example.com 'sq' "dq ${x:-default}"
)
Expand Down
3 changes: 2 additions & 1 deletion services/toil-worker.sh
Expand Up @@ -67,7 +67,8 @@ typecheck-other types/run.sh travis -
unit test/unit.sh travis -
oil-spec test/spec.sh oil-all-serial _tmp/spec/oil-language/oil.html
tea-spec test/spec.sh tea-all-serial _tmp/spec/tea-language/tea.html
tea-parsing oil_lang/run.sh parse-all-tea -
oil-large oil_lang/run.sh travis -
tea-large tea/run.sh travis -
osh-minimal test/spec.sh osh-minimal _tmp/spec/survey/osh-minimal.html
EOF

Expand Down
31 changes: 31 additions & 0 deletions tea/README.md
@@ -0,0 +1,31 @@
Tea Language
============

This is an experiment! Background:

- <http://www.oilshell.org/blog/2020/10/big-changes.html#appendix-the-tea-language>
- <https://old.reddit.com/r/ProgrammingLanguages/comments/jb5i5m/help_i_keep_stealing_features_from_elixir_because/g8urxou/>
- <https://lobste.rs/s/4hx42h/assorted_thoughts_on_zig_rust#c_mqpg6e>

tl;dr Tea is a cleanup of Oil's metalanguages, which can be called "statically
typed Python with sum types". (Zephyr ASDL is pretty clean, but mycpp is
messy, and needs cleanup.)

- Tea Grammar: <https://github.com/oilshell/oil/blob/master/oil_lang/grammar.pgen2#L363>
- Tea ASDL Schema: <https://github.com/oilshell/oil/blob/master/frontend/syntax.asdl#L324>

## Testing

This is currently run in the continuous build
(<http://travis-ci.oilshell.org/jobs/>).

tea/run.sh travis

## Demo

$ bin/oil -O parse_tea -n -c 'data Point(x Int, y Int)'

TODO: `bin/tea -c`.



Empty file added tea/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions tea/run.sh
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
#
# Usage:
# ./run.sh <function name>

set -o nounset
set -o pipefail
set -o errexit

tea-files() {
find tea/testdata -name '*.tea'
}

parse-all-tea() {
# Parse with the Oil binary
tea-files | xargs -n 1 -- bin/oil -O parse_tea -n

# Standalone tea parser
tea-files | xargs -n 1 -- bin/tea
}

travis() {
parse-all-tea
}

"$@"
83 changes: 83 additions & 0 deletions tea/tea_main.py
@@ -0,0 +1,83 @@
#!/usr/bin/env python2
"""
tea_main.py
"""
from __future__ import print_function

import sys

from _devbuild.gen.option_asdl import option_i
from _devbuild.gen.syntax_asdl import source
from frontend import parse_lib
from frontend import reader
from core import alloc
from core import error
from core import optview
from core import pyutil
from core.pyutil import stderr_line
from core import state
from core import ui
from mycpp import mylib

import posix_ as posix

from typing import List, Dict, cast, TYPE_CHECKING
if TYPE_CHECKING:
from frontend import args


def Main(arg_r):
# type: (args.Reader) -> int
"""
Usage:
tea myprog.tea # Run the script
tea -c 'func main() { echo "hi" }' # Run this snippet. Not common since
# there's no top level statementes!
# Use bin/oil for that.
tea -n -c 'var x = 1' # Parse it
# Compile to C++. Produce myprog.cc and myprog.h.
tea --cpp-out myprog myprog.tea lib.tea
"""
argv = arg_r.Rest()
arena = alloc.Arena()
try:
script_name = argv[0]
arena.PushSource(source.MainFile(script_name))
except IndexError:
arena.PushSource(source.Stdin())
f = mylib.Stdin() # type: mylib.LineReader
else:
try:
# Hack for type checking. core/shell.py uses fd_state.Open().
f = cast('mylib.LineReader', open(script_name))
except IOError as e:
stderr_line("tea: Couldn't open %r: %s", script_name,
posix.strerror(e.errno))
return 2

# Dummy value; not respecting aliases!
aliases = {} # type: Dict[str, str]

loader = pyutil.GetResourceLoader()
oil_grammar = pyutil.LoadOilGrammar(loader)

# Not used in tea, but OK...
opt0_array = state.InitOpts()
no_stack = None # type: List[bool] # for mycpp
opt_stacks = [no_stack] * option_i.ARRAY_SIZE # type: List[List[bool]]
parse_opts = optview.Parse(opt0_array, opt_stacks)

# parse `` and a[x+1]=bar differently
parse_ctx = parse_lib.ParseContext(arena, parse_opts, aliases, oil_grammar)

line_reader = reader.FileLineReader(f, arena)

try:
parse_ctx.ParseTeaModule(line_reader)
status = 0
except error.Parse as e:
ui.PrettyPrintError(e, arena)
status = 2

return status
File renamed without changes.
1 change: 1 addition & 0 deletions types/more-oil-manifest.txt
Expand Up @@ -7,3 +7,4 @@
./core/shell.py
./osh/builtin_process.py
#./osh/builtin_comp.py
./tea/tea_main.py

0 comments on commit 81a79a5

Please sign in to comment.