Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed CLI args from filenames to strings/STDIN. #109

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Try a little hello world
run: docker run ${{ steps.meta.outputs.tags }} apply "1 + 2"
run: docker run ${{ steps.meta.outputs.tags }} eval "1 + 2"
deploy:
runs-on: ubuntu-latest
if: |
Expand Down
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ an idea for how the language works.
We support python3.8+.

```bash
# With a file
python3 scrapscript.py eval examples/0_home/factorial.scrap
# With a file via STDIN
python3 scrapscript.py eval < examples/0_home/factorial.scrap

# With a string literal
python3 scrapscript.py apply "1 + 2"
python3 scrapscript.py eval "1 + 2"

# With multiple inputs
echo "4" | python3 scrapscript.py apply "n -> n + 1"

# With a REPL
python3 scrapscript.py repl
Expand All @@ -27,27 +30,32 @@ or with [Cosmopolitan](https://justine.lol/cosmopolitan/index.html):
```bash
./build-com

# With a file
./scrapscript.com eval examples/0_home/factorial.scrap
# With a file via STDIN
./scrapscript.com eval < examples/0_home/factorial.scrap

# With a string literal
./scrapscript.com apply "1 + 2"
./scrapscript.com eval "1 + 2"

# With multiple inputs
echo "4" | ./scrapscript.com apply "n -> n + 1"

# With a REPL
./scrapscript.com repl
```

(if you have an exec format error and use Zsh, either upgrade Zsh or prefix
with `sh`)
(if you have an exec format error and use Zsh, either upgrade Zsh or prefix with `sh`)

or with Docker:

```bash
# With a file (mount your local directory)
docker run --mount type=bind,source="$(pwd)",target=/mnt -i -t ghcr.io/tekknolagi/scrapscript:trunk eval /mnt/examples/0_home/factorial.scrap
# With a file via STDIN (mount your local directory)
docker run --mount type=bind,source="$(pwd)",target=/mnt -i -t ghcr.io/tekknolagi/scrapscript:trunk eval < /mnt/examples/0_home/factorial.scrap

# With a string literal
docker run -i -t ghcr.io/tekknolagi/scrapscript:trunk apply "1 + 2"
docker run -i -t ghcr.io/tekknolagi/scrapscript:trunk eval "1 + 2"

# With multiple inputs
echo "4" | docker run -i -t ghcr.io/tekknolagi/scrapscript:trunk eval "n -> n + 1"

# With a REPL
docker run -i -t ghcr.io/tekknolagi/scrapscript:trunk repl
Expand Down
2 changes: 1 addition & 1 deletion build-com
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ cp style.css repl.html Lib
cp python scrapscript.com
./zip -r scrapscript.com Lib .args
echo "Testing..."
./scrapscript.com apply "1+2"
./scrapscript.com eval "1+2"
cd "$PREV"
cp "$DIR"/scrapscript.com .
40 changes: 29 additions & 11 deletions scrapscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -4514,7 +4514,7 @@ def eval_command(args: argparse.Namespace) -> None:
if args.debug:
logging.basicConfig(level=logging.DEBUG)

program = args.program_file.read()
program = args.scrap if isinstance(args.scrap, str) else args.scrap.read()
tokens = tokenize(program)
logger.debug("Tokens: %s", tokens)
ast = parse(tokens)
Expand All @@ -4527,11 +4527,15 @@ def apply_command(args: argparse.Namespace) -> None:
if args.debug:
logging.basicConfig(level=logging.DEBUG)

tokens = tokenize(args.program)
logger.debug("Tokens: %s", tokens)
ast = parse(tokens)
logger.debug("AST: %s", ast)
result = eval_exp(boot_env(), ast)
tokens_f = tokenize(args.f)
logger.debug("Tokens (f): %s", tokens_f)
tokens_x = tokenize(args.x if isinstance(args.x, str) else args.x.read())
logger.debug("Tokens (x): %s", tokens_x)
ast_f = parse(tokens_f)
logger.debug("AST (f): %s", ast_f)
ast_x = parse(tokens_x)
logger.debug("AST (f): %s", ast_x)
result = eval_exp(boot_env(), Apply(ast_f, ast_x))
print(result)


Expand All @@ -4555,7 +4559,7 @@ def test_command(args: argparse.Namespace) -> None:
unittest.main(argv=[__file__, *args.unittest_args])


def serve_command(args: argparse.Namespace) -> None:
def repl_serve_command(args: argparse.Namespace) -> None:
if args.debug:
logging.basicConfig(level=logging.DEBUG)
server: Union[type[socketserver.TCPServer], type[socketserver.ForkingTCPServer]]
Expand Down Expand Up @@ -4585,20 +4589,34 @@ def main() -> None:

eval_ = subparsers.add_parser("eval")
eval_.set_defaults(func=eval_command)
eval_.add_argument("program_file", type=argparse.FileType("r"))
eval_.add_argument("scrap", nargs="?", default=sys.stdin)
eval_.add_argument("--debug", action="store_true")

apply = subparsers.add_parser("apply")
apply.set_defaults(func=apply_command)
apply.add_argument("program")
apply.add_argument("f")
apply.add_argument("x", nargs="?", default=sys.stdin)
apply.add_argument("--debug", action="store_true")

serve = subparsers.add_parser("serve")
serve.set_defaults(func=serve_command)
serve = subparsers.add_parser("repl-serve")
serve.set_defaults(func=repl_serve_command)
serve.add_argument("--port", type=int, default=8000)
serve.add_argument("--debug", action="store_true")
serve.add_argument("--fork", action="store_true")

# TODO(tay): yard = subparsers.add_parser("yard").add_subparsers(dest="command", required=True)
# TODO(tay): yard_info = yard.add_parser("info")
# TODO(tay): yard_info.set_defaults(func=yard_info_command)
# TODO(tay): yard_init = yard.add_parser("init")
# TODO(tay): yard_init.set_defaults(func=yard_init_command)
# TODO(tay): yard_push = yard.add_parser("push")
# TODO(tay): yard_push.set_defaults(func=yard_push_command)
# TODO(tay): yard_push.add_argument("scrap_name")
# TODO(tay): yard_push.add_argument("scrap", nargs="?", default=sys.stdin)
# TODO(tay): yard_serve = yard.add_parser("serve")
# TODO(tay): yard_serve.set_defaults(func=yard_serve_command)
# TODO(tay): yard_serve.add_argument("port")

args = parser.parse_args()
if not args.command:
args.debug = False
Expand Down
Loading