forked from py2many/py2many
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move transpiler settings to respective packages
Finish making main() arg parsing dynamic.
- Loading branch information
1 parent
c5cc536
commit efdfc61
Showing
13 changed files
with
375 additions
and
425 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import os | ||
import pathlib | ||
|
||
from unittest.mock import Mock | ||
|
||
from .language import LanguageSettings | ||
|
||
from .python_transformer import PythonTranspiler, RestoreMainRewriter | ||
|
||
from .rewriters import InferredAnnAssignRewriter | ||
|
||
from pycpp import settings as cpp_settings | ||
from pydart import settings as dart_settings | ||
from pygo import settings as go_settings | ||
from pyjl import settings as julia_settings | ||
from pykt import settings as kotlin_settings | ||
from pynim import settings as nim_settings | ||
from pyrs import settings as rust_settings | ||
from pysmt import settings as smt_settings | ||
from pyv import settings as vlang_settings | ||
|
||
|
||
PY2MANY_DIR = pathlib.Path(__file__).parent | ||
ROOT_DIR = PY2MANY_DIR.parent | ||
FAKE_ARGS = Mock(indent=4) | ||
|
||
|
||
def python_settings(args, env=os.environ): | ||
return LanguageSettings( | ||
PythonTranspiler(), | ||
".py", | ||
"Python", | ||
formatter=["black"], | ||
rewriters=[RestoreMainRewriter()], | ||
post_rewriters=[InferredAnnAssignRewriter()], | ||
) | ||
|
||
|
||
ALL_SETTINGS = { | ||
"python": python_settings, | ||
"cpp": cpp_settings, | ||
"rust": rust_settings, | ||
"julia": julia_settings, | ||
"kotlin": kotlin_settings, | ||
"nim": nim_settings, | ||
"dart": dart_settings, | ||
"go": go_settings, | ||
"vlang": vlang_settings, | ||
"smt": smt_settings, | ||
} | ||
|
||
|
||
def _get_all_settings(args, env=os.environ): | ||
return dict((key, func(args, env=env)) for key, func in ALL_SETTINGS.items()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import os | ||
import os.path | ||
import sys | ||
|
||
from distutils import spawn | ||
|
||
from py2many.language import LanguageSettings | ||
|
||
from .transpiler import CppListComparisonRewriter, CppTranspiler | ||
|
||
USER_HOME = os.path.expanduser("~/") | ||
|
||
|
||
def _conan_include_dirs(): | ||
CONAN_CATCH2 = "catch2/3.0.1/_/_/package/a25d6c83542b56b72fdaa05a85db5d46f5f0f71c" | ||
CONAN_CPPITERTOOLS = ( | ||
"cppitertools/2.1/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9" | ||
) | ||
return [ | ||
"-I", | ||
f"{USER_HOME}/.conan/data/{CONAN_CATCH2}/include", | ||
"-I", | ||
f"{USER_HOME}/.conan/data/{CONAN_CPPITERTOOLS}/include", | ||
] | ||
|
||
|
||
def settings(args, env=os.environ): | ||
clang_format_style = env.get("CLANG_FORMAT_STYLE") | ||
cxx = env.get("CXX") | ||
default_cxx = ["clang++", "g++-11", "g++"] | ||
if cxx: | ||
if not spawn.find_executable(cxx): | ||
print(f"Warning: CXX({cxx}) not found") | ||
cxx = None | ||
if not cxx: | ||
for exe in default_cxx: | ||
if spawn.find_executable(exe): | ||
cxx = exe | ||
break | ||
else: | ||
cxx = default_cxx[0] | ||
cxx_flags = env.get("CXXFLAGS") | ||
if cxx_flags: | ||
cxx_flags = cxx_flags.split() | ||
else: | ||
cxx_flags = ["-std=c++17", "-Wall", "-Werror"] | ||
cxx_flags = _conan_include_dirs() + cxx_flags | ||
if cxx.startswith("clang++") and not sys.platform == "win32": | ||
cxx_flags += ["-stdlib=libc++"] | ||
|
||
if clang_format_style: | ||
clang_format_cmd = ["clang-format", f"-style={clang_format_style}", "-i"] | ||
else: | ||
clang_format_cmd = ["clang-format", "-i"] | ||
|
||
return LanguageSettings( | ||
CppTranspiler(args.extension, args.no_prologue), | ||
".cpp", | ||
"C++", | ||
clang_format_cmd, | ||
None, | ||
[CppListComparisonRewriter()], | ||
linter=[cxx, *cxx_flags], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import os | ||
|
||
from py2many.language import LanguageSettings | ||
|
||
from .transpiler import DartIntegerDivRewriter, DartTranspiler | ||
|
||
|
||
def settings(args, env=os.environ): | ||
return LanguageSettings( | ||
DartTranspiler(), | ||
".dart", | ||
"Dart", | ||
["dart", "format"], | ||
post_rewriters=[DartIntegerDivRewriter()], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
|
||
from pathlib import Path | ||
|
||
from py2many.language import LanguageSettings | ||
|
||
import py2many | ||
|
||
from .inference import infer_go_types | ||
from .transpiler import ( | ||
GoIfExpRewriter, | ||
GoMethodCallRewriter, | ||
GoNoneCompareRewriter, | ||
GoPropagateTypeAnnotation, | ||
GoTranspiler, | ||
GoVisibilityRewriter, | ||
) | ||
|
||
PY2MANY_DIR = Path(py2many.__file__).parent | ||
ROOT_DIR = PY2MANY_DIR.parent | ||
|
||
|
||
def settings(args, env=os.environ): | ||
config_filename = "revive.toml" | ||
CWD = Path.cwd() | ||
if os.path.exists(CWD / config_filename): | ||
revive_config = CWD / config_filename | ||
elif os.path.exists(ROOT_DIR / config_filename): | ||
revive_config = ROOT_DIR / config_filename | ||
else: | ||
revive_config = None | ||
return LanguageSettings( | ||
GoTranspiler(), | ||
".go", | ||
"Go", | ||
["gofmt", "-w"], | ||
None, | ||
[GoNoneCompareRewriter(), GoVisibilityRewriter(), GoIfExpRewriter()], | ||
[infer_go_types], | ||
[GoMethodCallRewriter(), GoPropagateTypeAnnotation()], | ||
linter=( | ||
["revive", "--config", str(revive_config)] if revive_config else ["revive"] | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
|
||
from distutils import spawn | ||
from functools import lru_cache | ||
from pathlib import Path | ||
from subprocess import run | ||
|
||
from py2many.language import LanguageSettings | ||
|
||
from .inference import infer_julia_types | ||
from .rewriters import JuliaIndexingRewriter | ||
from .transpiler import JuliaMethodCallRewriter, JuliaTranspiler | ||
|
||
|
||
@lru_cache() | ||
def _julia_formatter_path(): | ||
proc = run( | ||
["julia", "-e", "import JuliaFormatter;print(pathof(JuliaFormatter))"], | ||
capture_output=True, | ||
) | ||
if not proc.returncode and proc.stdout: | ||
return str(Path(proc.stdout.decode("utf8")).parent.parent / "bin" / "format.jl") | ||
|
||
|
||
def settings(args, env=os.environ): | ||
format_jl = spawn.find_executable("format.jl") | ||
if not format_jl: | ||
julia = spawn.find_executable("julia") | ||
if julia: | ||
format_jl = _julia_formatter_path() | ||
|
||
if format_jl: | ||
format_jl = ["julia", "-O0", "--compile=min", "--startup=no", format_jl, "-v"] | ||
else: | ||
format_jl = ["format.jl", "-v"] | ||
return LanguageSettings( | ||
JuliaTranspiler(), | ||
".jl", | ||
"Julia", | ||
format_jl, | ||
None, | ||
rewriters=[], | ||
transformers=[infer_julia_types], | ||
post_rewriters=[JuliaIndexingRewriter(), JuliaMethodCallRewriter()], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import os | ||
|
||
from py2many.language import LanguageSettings | ||
|
||
from .inference import infer_kotlin_types | ||
from .transpiler import KotlinBitOpRewriter, KotlinPrintRewriter, KotlinTranspiler | ||
|
||
|
||
def settings(args, env=os.environ): | ||
return LanguageSettings( | ||
KotlinTranspiler(), | ||
".kt", | ||
"Kotlin", | ||
["ktlint", "-F"], | ||
rewriters=[KotlinBitOpRewriter()], | ||
transformers=[infer_kotlin_types], | ||
post_rewriters=[KotlinPrintRewriter()], | ||
linter=["ktlint"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
|
||
from py2many.language import LanguageSettings | ||
|
||
from .inference import infer_nim_types | ||
from .transpiler import NimNoneCompareRewriter, NimTranspiler | ||
|
||
|
||
def settings(args, env=os.environ): | ||
nim_args = {} | ||
nimpretty_args = [] | ||
if args.indent is not None: | ||
nim_args["indent"] = args.indent | ||
nimpretty_args.append(f"--indent:{args.indent}") | ||
return LanguageSettings( | ||
NimTranspiler(**nim_args), | ||
".nim", | ||
"Nim", | ||
["nimpretty", *nimpretty_args], | ||
None, | ||
[NimNoneCompareRewriter()], | ||
[infer_nim_types], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os | ||
|
||
from functools import partial | ||
|
||
from py2many.language import LanguageSettings | ||
|
||
from .inference import infer_rust_types | ||
from .transpiler import ( | ||
RustLoopIndexRewriter, | ||
RustNoneCompareRewriter, | ||
RustStringJoinRewriter, | ||
RustTranspiler, | ||
) | ||
|
||
|
||
def settings(args, env=os.environ): | ||
return LanguageSettings( | ||
RustTranspiler(args.extension, args.no_prologue), | ||
".rs", | ||
"Rust", | ||
["rustfmt", "--edition=2018"], | ||
None, | ||
[RustNoneCompareRewriter()], | ||
[partial(infer_rust_types, extension=args.extension)], | ||
[RustLoopIndexRewriter(), RustStringJoinRewriter()], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import os | ||
|
||
from py2many.language import LanguageSettings | ||
|
||
from .inference import infer_smt_types | ||
from .transpiler import SmtTranspiler | ||
|
||
|
||
def settings(args, env=os.environ): | ||
cljstyle_args = ["fix"] | ||
return LanguageSettings( | ||
SmtTranspiler(), | ||
".smt", | ||
"SMT", | ||
["cljstyle", *cljstyle_args], | ||
None, | ||
[], | ||
[infer_smt_types], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import os | ||
|
||
from py2many.language import LanguageSettings | ||
|
||
from .inference import infer_v_types | ||
from .transpiler import ( | ||
VComprehensionRewriter, | ||
VDictRewriter, | ||
VNoneCompareRewriter, | ||
VTranspiler, | ||
) | ||
|
||
|
||
def settings(args, env=os.environ): | ||
v_args = {} | ||
vfmt_args = ["fmt", "-w"] | ||
if args.indent is not None: | ||
v_args["indent"] = args.indent | ||
vfmt_args.append(f"--indent:{args.indent}") | ||
return LanguageSettings( | ||
VTranspiler(**v_args), | ||
".v", | ||
"V", | ||
["v", *vfmt_args], | ||
None, | ||
[VNoneCompareRewriter(), VDictRewriter(), VComprehensionRewriter()], | ||
[infer_v_types], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters