Skip to content

Commit

Permalink
Use fast grammar methods
Browse files Browse the repository at this point in the history
  • Loading branch information
evhub committed Nov 10, 2023
1 parent dd0ba2a commit 1cfbe06
Show file tree
Hide file tree
Showing 8 changed files with 1,928 additions and 1,831 deletions.
9 changes: 8 additions & 1 deletion coconut/command/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
get_clock_time,
first_import_time,
ensure_dir,
assert_remove_prefix,
)
from coconut.command.util import (
writefile,
Expand Down Expand Up @@ -324,7 +325,13 @@ def execute_args(self, args, interact=True, original_args=None):
# process mypy args and print timing info (must come after compiler setup)
if args.mypy is not None:
self.set_mypy_args(args.mypy)
logger.log("Grammar init time: " + str(self.comp.grammar_init_time) + " secs / Total init time: " + str(get_clock_time() - first_import_time) + " secs")
if logger.verbose:
logger.log("Grammar init time: " + str(self.comp.grammar_init_time) + " secs / Total init time: " + str(get_clock_time() - first_import_time) + " secs")
for stat_name, (no_copy, yes_copy) in logger.recorded_stats.items():
if not stat_name.startswith("maybe_copy_"):
continue
name = assert_remove_prefix(stat_name, "maybe_copy_")
logger.printlog("\tGrammar copying stats (" + name + "):", no_copy, "not copied;", yes_copy, "copied")

# do compilation, keeping track of compiled filepaths
filepaths = []
Expand Down
34 changes: 25 additions & 9 deletions coconut/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2936,39 +2936,55 @@ def item_handle(self, original, loc, tokens):
out = "_coconut_iter_getitem(" + out + ", " + trailer[1] + ")"
elif trailer[0] == "$(?":
pos_args, star_args, base_kwd_args, dubstar_args = self.split_function_call(trailer[1], loc)

has_question_mark = False
needs_complex_partial = False

argdict_pairs = []
last_pos_i = -1
for i, arg in enumerate(pos_args):
if arg == "?":
has_question_mark = True
else:
if last_pos_i != i - 1:
needs_complex_partial = True
argdict_pairs.append(str(i) + ": " + arg)

pos_kwargs = []
kwd_args = []
for i, arg in enumerate(base_kwd_args):
if arg.endswith("=?"):
has_question_mark = True
needs_complex_partial = True
pos_kwargs.append(arg[:-2])
else:
kwd_args.append(arg)

extra_args_str = join_args(star_args, kwd_args, dubstar_args)
if not has_question_mark:
raise CoconutInternalException("no question mark in question mark partial", trailer[1])
elif argdict_pairs or pos_kwargs or extra_args_str:

if needs_complex_partial:
extra_args_str = join_args(star_args, kwd_args, dubstar_args)
if argdict_pairs or pos_kwargs or extra_args_str:
out = (
"_coconut_complex_partial("
+ out
+ ", {" + ", ".join(argdict_pairs) + "}"
+ ", " + str(len(pos_args))
+ ", " + tuple_str_of(pos_kwargs, add_quotes=True)
+ (", " if extra_args_str else "") + extra_args_str
+ ")"
)
else:
raise CoconutDeferredSyntaxError("a non-? partial application argument is required", loc)
else:
out = (
"_coconut_complex_partial("
"_coconut_partial("
+ out
+ ", {" + ", ".join(argdict_pairs) + "}"
+ ", " + str(len(pos_args))
+ ", " + tuple_str_of(pos_kwargs, add_quotes=True)
+ (", " if extra_args_str else "") + extra_args_str
+ ", "
+ join_args([arg for arg in pos_args if arg != "?"], star_args, kwd_args, dubstar_args)
+ ")"
)
else:
raise CoconutDeferredSyntaxError("a non-? partial application argument is required", loc)

else:
raise CoconutInternalException("invalid special trailer", trailer[0])
Expand Down
Loading

0 comments on commit 1cfbe06

Please sign in to comment.