Skip to content

Commit

Permalink
Automatically set auto comp args
Browse files Browse the repository at this point in the history
Refs   #768.
  • Loading branch information
evhub committed Jul 16, 2023
1 parent 5eed433 commit 35efe56
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
14 changes: 10 additions & 4 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ which will quietly compile and run `<source>`, passing any additional arguments

To pass additional compilation arguments to `coconut-run` (e.g. `--no-tco`), put them before the `<source>` file.

Additionally, `coconut-run` will always use [automatic compilation](#automatic-compilation), such that Coconut source files can be directly imported from any Coconut files run via `coconut-run`.
`coconut-run` will always use [automatic compilation](#automatic-compilation), such that Coconut source files can be directly imported from any Coconut files run via `coconut-run`. Additionally, compilation parameters (e.g. `--no-tco`) used in `coconut-run` will be passed along and used for any auto compilation.

#### Naming Source Files

Expand Down Expand Up @@ -4390,9 +4390,15 @@ Recommended usage is as a debugging tool, where the code `from coconut import em

### Automatic Compilation

If you don't care about the exact compilation parameters you want to use, automatic compilation lets Coconut take care of everything for you. Automatic compilation can be enabled either by importing [`coconut.api`](#coconut-api) before you import anything else, or by running `coconut --site-install`. Once automatic compilation is enabled, Coconut will check each of your imports to see if you are attempting to import a `.coco` file and, if so, automatically compile it for you. Note that, for Coconut to know what file you are trying to import, it will need to be accessible via `sys.path`, just like a normal import.
Automatic compilation lets you simply import Coconut files directly without having to go through a compilation step first. Automatic compilation can be enabled either by importing [`coconut.api`](#coconut-api) before you import anything else, or by running `coconut --site-install`.

Automatic compilation always compiles modules and packages in-place, and always uses `--target sys --line-numbers --keep-lines --no-wrap-types`. Automatic compilation is always available in the Coconut interpreter, and, if using the Coconut interpreter, a `reload` built-in is provided to easily reload imported modules. Additionally, the interpreter always allows importing from the current working directory, letting you easily compile and play around with a `.coco` file simply by running the Coconut interpreter and importing it.
Once automatic compilation is enabled, Coconut will check each of your imports to see if you are attempting to import a `.coco` file and, if so, automatically compile it for you. Note that, for Coconut to know what file you are trying to import, it will need to be accessible via `sys.path`, just like a normal import.

Automatic compilation always compiles modules and packages in-place, and compiles with `--target sys --line-numbers --keep-lines` by default.

Automatic compilation is always available in the Coconut interpreter or when using [`coconut-run`](#coconut-scripts). When using auto compilation through the Coconut interpreter, any compilation options passed in will also be used for auto compilation. Additionally, the interpreter always allows importing from the current working directory, letting you easily compile and play around with a `.coco` file simply by running the Coconut interpreter and importing it.

If using the Coconut interpreter, a `reload` built-in is always provided to easily reload (and thus recompile) imported modules.

### Coconut Encoding

Expand Down Expand Up @@ -4527,7 +4533,7 @@ Retrieves a string containing information about the Coconut version. The optiona

Turns [automatic compilation](#automatic-compilation) on or off. This function is called automatically when `coconut.api` is imported.

If _args_ is passed, it will set the Coconut command-line arguments to use for automatic compilation.
If _args_ is passed, it will set the Coconut command-line arguments to use for automatic compilation. Arguments will be processed the same way as with [`coconut-run`](#coconut-scripts) such that `--quiet --target sys --keep-lines` will all be set by default.

#### `use_coconut_breakpoint`

Expand Down
4 changes: 2 additions & 2 deletions coconut/command/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,10 @@ def prompt(self, msg):
class Runner(object):
"""Compiled Python executor."""

def __init__(self, comp=None, exit=sys.exit, store=False, path=None, auto_comp_args=None):
def __init__(self, comp=None, exit=sys.exit, store=False, path=None):
"""Create the executor."""
from coconut.api import auto_compilation, use_coconut_breakpoint
auto_compilation(on=interpreter_uses_auto_compilation, args=auto_comp_args)
auto_compilation(on=interpreter_uses_auto_compilation, args=comp.get_cli_args() if comp else None)
use_coconut_breakpoint(on=interpreter_uses_coconut_breakpoint)
self.exit = exit
self.vars = self.build_vars(path, init=True)
Expand Down
19 changes: 18 additions & 1 deletion coconut/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def __init__(self, *args, **kwargs):
"""Creates a new compiler with the given parsing parameters."""
self.setup(*args, **kwargs)

# changes here should be reflected in __reduce__ and in the stub for coconut.api.setup
# changes here should be reflected in __reduce__, get_cli_args, and in the stub for coconut.api.setup
def setup(self, target=None, strict=False, minify=False, line_numbers=False, keep_lines=False, no_tco=False, no_wrap=False):
"""Initializes parsing parameters."""
if target is None:
Expand Down Expand Up @@ -511,6 +511,23 @@ def __reduce__(self):
"""Return pickling information."""
return (self.__class__, (self.target, self.strict, self.minify, self.line_numbers, self.keep_lines, self.no_tco, self.no_wrap))

def get_cli_args(self):
"""Get the Coconut CLI args that can be used to set up an equivalent compiler."""
args = ["--target=" + self.target]
if self.strict:
args.append("--strict")
if self.minify:
args.append("--minify")
if not self.line_numbers:
args.append("--no-line-numbers")
if self.keep_lines:
args.append("--keep-lines")
if self.no_tco:
args.append("--no-tco")
if self.no_wrap:
args.append("--no-wrap-types")
return args

def __copy__(self):
"""Create a new, blank copy of the compiler."""
cls, args = self.__reduce__()
Expand Down
2 changes: 1 addition & 1 deletion coconut/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
VERSION = "3.0.2"
VERSION_NAME = None
# False for release, int >= 1 for develop
DEVELOP = 20
DEVELOP = 21
ALPHA = False # for pre releases rather than post releases

assert DEVELOP is False or DEVELOP >= 1, "DEVELOP must be False or an int >= 1"
Expand Down

0 comments on commit 35efe56

Please sign in to comment.