Skip to content
Merged
Show file tree
Hide file tree
Changes from 151 commits
Commits
Show all changes
164 commits
Select commit Hold shift + click to select a range
e7ec344
Initial Python POC
dbrattli Jan 4, 2021
db879d6
`return None` instead not nothing
dbrattli Jan 5, 2021
f46b933
This is not a reserved word in python
dbrattli Jan 5, 2021
3fd17eb
Generate .fs.py files instead
dbrattli Jan 5, 2021
cee5615
Fable is now a separate module in Expression
dbrattli Jan 6, 2021
cfb7ed0
Fixes for undefined, null, and ofArray (of_seq)
dbrattli Jan 9, 2021
d052a92
Python AST (wip)
dbrattli Jan 10, 2021
f893abc
Update Python AST
dbrattli Jan 10, 2021
69c1177
Transform from Babel AST to Python AST
dbrattli Jan 11, 2021
900dc24
Revert
dbrattli Jan 11, 2021
e68eeda
Revert
dbrattli Jan 11, 2021
bd36059
Revert
dbrattli Jan 11, 2021
680a9f8
Fix import statement
dbrattli Jan 11, 2021
591a93a
Fix expression statements
dbrattli Jan 11, 2021
a6344f2
Add language handling
dbrattli Jan 12, 2021
0701377
Fix Babel expression to Python statements
dbrattli Jan 12, 2021
c0268eb
Fixes for class definitions
dbrattli Jan 13, 2021
86a9fa6
Fix fable library imports
dbrattli Jan 13, 2021
2b1e138
Revert newline changes
dbrattli Jan 13, 2021
fb50321
Fix more newlines
dbrattli Jan 13, 2021
e987802
More newlines
dbrattli Jan 13, 2021
6d14a28
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli Jan 13, 2021
9dda9e6
Fix comparison operators
dbrattli Jan 13, 2021
08328c4
Fix subscript operator
dbrattli Jan 14, 2021
aa7013e
Fix single line lambda
dbrattli Jan 14, 2021
e1595d7
Fixes for while and for loops
dbrattli Jan 14, 2021
3324b81
Lift arrow and function expressions to function definitions
dbrattli Jan 14, 2021
5b58e79
Print bases for classes
dbrattli Jan 15, 2021
2cecd0c
Better name mangling of extracted functions
dbrattli Jan 15, 2021
c83eb2c
Fixes for python dicts
dbrattli Jan 15, 2021
b30cea9
Move isProductive transformation out of the Python AST
dbrattli Jan 15, 2021
5db9b89
Translate `void 0` to None
dbrattli Jan 15, 2021
53d0312
Fix local imports
dbrattli Jan 16, 2021
3863ca8
Add pyNative and Fable.Core.PyInterop
dbrattli Jan 18, 2021
8540447
Try out active patterns
dbrattli Jan 20, 2021
875a469
Rewrite Python AST to DU / Records
dbrattli Jan 20, 2021
6c86af2
Fix assingments
dbrattli Jan 20, 2021
183dd84
Fix build script.
dbrattli Jan 21, 2021
7e7913a
Initail support for switch statements
dbrattli Jan 23, 2021
a68cbd3
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli Jan 23, 2021
52dc5f6
Merge
dbrattli Jan 23, 2021
12c4816
Fixes for sequence expressions
dbrattli Jan 23, 2021
ac1fb28
Fix exception message handling
dbrattli Jan 23, 2021
1756237
Fix argument annotations
dbrattli Jan 23, 2021
b2a8ab6
Fix lifting out of object properties
dbrattli Jan 23, 2021
4215d95
Handle varargs
dbrattli Jan 25, 2021
02309fb
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli Jan 26, 2021
627289f
Revert changes
dbrattli Jan 31, 2021
3ab4ed5
Merge remote-tracking branch 'origin/babel-unions' into python
dbrattli Jan 31, 2021
b37affe
Refactor to babel unions
dbrattli Jan 31, 2021
dc1f822
Simplify pattern matching
dbrattli Jan 31, 2021
c676183
Pattern matching fixes
dbrattli Jan 31, 2021
5c5d948
Add simple for-loops
dbrattli Jan 31, 2021
880596f
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli Jan 31, 2021
c63973e
Cleanup
dbrattli Jan 31, 2021
ebd9868
Fix for length / str
dbrattli Jan 31, 2021
0f87c3c
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli Feb 8, 2021
559d79e
Refactor for latest Fable
dbrattli Feb 9, 2021
ce1741e
Refactor Python printer
dbrattli Feb 11, 2021
fec543d
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli Feb 11, 2021
d8189e8
Refactored Python AST extensions
dbrattli Feb 11, 2021
15af659
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli Feb 12, 2021
4209a4b
Better handling of babel member expressions
dbrattli Feb 14, 2021
be027a6
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli Feb 16, 2021
102eb8b
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli Feb 18, 2021
e9caa73
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli Feb 27, 2021
0d8fb62
Merge branch 'nagareyama' into python
dbrattli Mar 27, 2021
cbb90dd
Import fixes
dbrattli Mar 27, 2021
fded5c6
Fix import statement
dbrattli Mar 27, 2021
3af2ade
Rename
dbrattli Mar 27, 2021
06edc58
First test running
dbrattli Mar 28, 2021
29f6129
Avoid importing Xunit for every test file
dbrattli Mar 28, 2021
405a938
Add more tests
dbrattli Mar 28, 2021
f909256
Add more tests
dbrattli Mar 28, 2021
cf17446
Test list
dbrattli Mar 28, 2021
634f181
Fix for member expressions
dbrattli Mar 28, 2021
0515e5b
String interpolation
dbrattli Mar 29, 2021
26b81e0
Merge branch 'nagareyama' into python
dbrattli Mar 29, 2021
6518188
Better string testing
dbrattli Mar 30, 2021
45fcad1
String tests
dbrattli Mar 30, 2021
b082f4f
Fix string test
dbrattli Mar 30, 2021
8dba1d2
Fix sequence expression to lifted function
dbrattli Mar 30, 2021
d9f7c42
Fix nonlocals and print of iterables
dbrattli Apr 2, 2021
34bef83
Fix nonlocal ordering issue
dbrattli Apr 2, 2021
9317fbf
Fix tests
dbrattli Apr 2, 2021
677b4bd
Set tests (wip)
dbrattli Apr 2, 2021
30f9ba6
Add language selector arg `--lang` since ts, js and py are exclusive
dbrattli Apr 3, 2021
044c11a
Be a little forgiving about language selection
dbrattli Apr 3, 2021
dc399fc
Better assignment handling. Handle len, str and iter for classes.
dbrattli Apr 3, 2021
2d2fc03
Fix empty methods
dbrattli Apr 3, 2021
6e27f39
Fix language handling
dbrattli Apr 3, 2021
8c82c80
Fixes
dbrattli Apr 4, 2021
f0c66b3
Move Python fable-library to Fable
dbrattli Apr 4, 2021
17e703b
Add ts option compatibility
dbrattli Apr 4, 2021
db90eea
Add option tests
dbrattli Apr 4, 2021
a156a62
Fix option test
dbrattli Apr 4, 2021
7cf8821
Update fable-library for Python
dbrattli Apr 5, 2021
0e1d2e1
Fix tostring, __str__ and str
dbrattli Apr 6, 2021
6dd686a
Fix substring and join for string module
dbrattli Apr 6, 2021
df9e0bd
Add stringbuilder test
dbrattli Apr 6, 2021
5f9f9eb
Add stringbuilder tests and fix member expressions
dbrattli Apr 7, 2021
021ee83
Add stringbuilder tests
dbrattli Apr 7, 2021
a859e33
Add more string tests
dbrattli Apr 8, 2021
4f0956a
fix install on windows
tengelskar Apr 15, 2021
31685a0
Merge branch 'nagareyama' into python
dbrattli Apr 16, 2021
cb2ba24
Fix pyinterop
dbrattli May 13, 2021
7c9d349
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli May 14, 2021
fa78716
Adding initial async support (wip)
dbrattli May 15, 2021
eba41be
Async fixes
dbrattli May 16, 2021
0abd586
Fix async catch and ignore
dbrattli May 16, 2021
6b15331
Nonlocal bugfix
dbrattli May 17, 2021
18447c3
Merge pull request #2442 from fable-compiler/update-fcs
alfonsogarciacaro May 25, 2021
7dd75e4
Merge branch 'nagareyama' of https://github.com/fable-compiler/Fable …
dbrattli May 25, 2021
48af9b8
Fix merge conflict
dbrattli May 25, 2021
6de0250
Initial version of Fable2Python.fs (wip)
dbrattli May 27, 2021
ac25680
WIP
dbrattli May 28, 2021
0968fc0
Fix printing of pos only args
dbrattli May 29, 2021
4dd84a7
Fix classes (wip)
dbrattli May 30, 2021
97ec1a3
Fix class setters
dbrattli May 30, 2021
a70f8ea
Fable2Python (wip)
dbrattli Jun 1, 2021
63826ba
Initial non-local handling (wip)
dbrattli Jun 5, 2021
53f89ef
Merge branch 'next' of https://github.com/fable-compiler/Fable into p…
dbrattli Jun 5, 2021
9d597ca
Import fixes
dbrattli Jun 5, 2021
a818b3e
Fixes for sequence expressions, for-loops
dbrattli Jun 12, 2021
0812d22
Clean identifiers
dbrattli Jun 19, 2021
586f538
Fix errors with empty body and array create
dbrattli Jun 20, 2021
5eadd5f
Fix import identifier cleaning
dbrattli Jun 20, 2021
956968f
Transform x.toString() into str(x)
dbrattli Jun 20, 2021
484608a
Sort nonlocals
dbrattli Jun 20, 2021
e73fa6d
Fix emit expressions
dbrattli Jun 20, 2021
0115f6d
Propagate nonlocals out of if and else blocks
dbrattli Jun 20, 2021
705d6e3
Enable Fable2Python instead of Babel2Python
dbrattli Jun 20, 2021
2ef3a00
Better cleaning of identifiers
dbrattli Jun 21, 2021
146359b
Fixes for push vs append, indexOf vs find, length vs len, ...
dbrattli Jul 3, 2021
9a60689
Fix tc arg names
dbrattli Jul 4, 2021
fe5481b
Merge branch 'next' of https://github.com/fable-compiler/Fable into p…
dbrattli Jul 8, 2021
eee5696
Fixes for sequence expressions + get dict
dbrattli Jul 8, 2021
a7294ed
Fixed var args for union type
dbrattli Jul 10, 2021
516d9eb
Fix get attribute and exception raising
dbrattli Jul 11, 2021
ba679e5
Fix for dict getters
dbrattli Jul 12, 2021
fcd1d6a
Rewrite `.message` to `str`
dbrattli Jul 12, 2021
5cc053f
Fix seq tests
dbrattli Jul 14, 2021
8ee6a53
Add set tests
dbrattli Jul 14, 2021
4e33158
Fixes for object expressions
dbrattli Jul 14, 2021
827f0d9
Use subscript access for anonymous records
dbrattli Jul 15, 2021
5106a41
Fixes for object expressions when value
dbrattli Jul 15, 2021
9ca3054
Added reflection tests and fixes
dbrattli Jul 15, 2021
d3b0420
Remove unused files
dbrattli Jul 15, 2021
db9e196
Remove Babel2Python
dbrattli Jul 15, 2021
c1c8dab
Remove obsolete files
dbrattli Jul 15, 2021
a7988cf
Revert quicktest
dbrattli Jul 15, 2021
3f1c236
Revert changes to build.fsx
dbrattli Jul 15, 2021
c42c106
Remove unused file
dbrattli Jul 15, 2021
5ff46d3
Remove unused files
dbrattli Jul 15, 2021
1dc225e
Use global .gitignore
dbrattli Jul 15, 2021
dfbe385
Remove vscode settings
dbrattli Jul 15, 2021
a1095de
Fix warnings
dbrattli Jul 15, 2021
b33d270
Fix reflection tests
dbrattli Jul 15, 2021
9f40850
Fix union field index
dbrattli Jul 15, 2021
c3f8b5e
Rename JS to Python
dbrattli Jul 15, 2021
b81c02b
Cleanup Fable.Core.PY.fs
dbrattli Jul 15, 2021
38161ac
Remove `callable`
dbrattli Jul 15, 2021
2eb5a52
Remove callable from Native.fs
dbrattli Jul 15, 2021
72c972b
Enable disabled tests
dbrattli Jul 15, 2021
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
56 changes: 54 additions & 2 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,32 @@ let buildLibraryTs() =
runInDir buildDirTs "npm run tsc -- --init --target es2020 --module es2020 --allowJs"
runInDir buildDirTs ("npm run tsc -- --outDir ../../" + buildDirJs)

let buildLibraryPy() =
let libraryDir = "src/fable-library-py"
let projectDir = libraryDir + "/fable"
let buildDirPy = "build/fable-library-py"

cleanDirs [buildDirPy]

runFableWithArgs projectDir [
"--outDir " + buildDirPy </> "fable"
"--fableLib " + buildDirPy </> "fable"
"--lang Python"
"--exclude Fable.Core"
]
// Copy *.py from projectDir to buildDir
copyDirRecursive libraryDir buildDirPy
copyDirNonRecursive (buildDirPy </> "fable/fable-library") (buildDirPy </> "fable")
//copyFile (buildDirPy </> "fable/fable-library/*.py") (buildDirPy </> "fable")
copyFile (buildDirPy </> "fable/system.text.py") (buildDirPy </> "fable/system_text.py")
copyFile (buildDirPy </> "fable/fsharp.core.py") (buildDirPy </> "fable/fsharp_core.py")
copyFile (buildDirPy </> "fable/fsharp.collections.py") (buildDirPy </> "fable/fsharp_collections.py")
//copyFile (buildDirPy </> "fable/async.py") (buildDirPy </> "fable/async_.py")
removeFile (buildDirPy </> "fable/system.text.py")

runInDir buildDirPy ("python3 --version")
runInDir buildDirPy ("python3 ./setup.py develop")

// Like testJs() but doesn't create bundles/packages for fable-standalone & friends
// Mainly intended for CI
let testJsFast() =
Expand Down Expand Up @@ -368,6 +394,24 @@ let test() =
if envVarOrNone "APPVEYOR" |> Option.isSome then
testJsFast()

let testPython() =
buildLibraryIfNotExists() // NOTE: fable-library-py needs to be built seperatly.

let projectDir = "tests/Python"
let buildDir = "build/tests/Python"

cleanDirs [buildDir]
runInDir projectDir "dotnet test"
runFableWithArgs projectDir [
"--outDir " + buildDir
"--exclude Fable.Core"
"--lang Python"
]

runInDir buildDir "touch __init__.py" // So relative imports works.
runInDir buildDir "pytest"


let buildLocalPackageWith pkgDir pkgCommand fsproj action =
let version = "3.0.0-local-build-" + DateTime.Now.ToString("yyyyMMdd-HHmm")
action version
Expand Down Expand Up @@ -525,9 +569,16 @@ match argsLower with
| "test-react"::_ -> testReact()
| "test-compiler"::_ -> testCompiler()
| "test-integration"::_ -> testIntegration()
| "test-py"::_ -> testPython()
| "quicktest"::_ ->
buildLibraryIfNotExists()
run "dotnet watch -p src/Fable.Cli run -- watch --cwd ../quicktest --exclude Fable.Core --noCache --runScript"
run "dotnet watch -p src/Fable.Cli run -- watch --cwd ../quicktest --exclude Fable.Core --noCache"
| "quicktest-py"::_ ->
buildLibraryIfNotExists()
run "dotnet watch -p src/Fable.Cli run -- watch --cwd ../quicktest --lang Python --exclude Fable.Core --noCache"
| "jupyter" :: _ ->
buildLibraryIfNotExists ()
run "dotnet watch -p src/Fable.Cli run -- watch --cwd /Users/dbrattli/Developer/GitHub/Fable.Jupyter/src --lang Python --exclude Fable.Core --noCache 2>> /Users/dbrattli/Developer/GitHub/Fable.Jupyter/src/fable.out"

| "run"::_ ->
buildLibraryIfNotExists()
Expand All @@ -546,6 +597,7 @@ match argsLower with
| ("watch-library")::_ -> watchLibrary()
| ("fable-library"|"library")::_ -> buildLibrary()
| ("fable-library-ts"|"library-ts")::_ -> buildLibraryTs()
| ("fable-library-py"|"library-py")::_ -> buildLibraryPy()
| ("fable-compiler-js"|"compiler-js")::_ -> buildCompilerJs(minify)
| ("fable-standalone"|"standalone")::_ -> buildStandalone {|minify=minify; watch=false|}
| "watch-standalone"::_ -> buildStandalone {|minify=false; watch=true|}
Expand All @@ -572,4 +624,4 @@ match argsLower with
dotnet fsi build.fsx quicktest
"""

printfn "Build finished successfully"
printfn "Build finished successfully"
1 change: 1 addition & 0 deletions src/Fable.AST/Plugins.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Verbosity =
type Language =
| JavaScript
| TypeScript
| Python
| Php

type CompilerOptions =
Expand Down
6 changes: 4 additions & 2 deletions src/Fable.Cli/Entry.fs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ Arguments:
--sourceMapsRoot Set the value of the `sourceRoot` property in generated source maps

--optimize Compile with optimized F# AST (experimental)
--lang|--language Compile to JavaScript (default), "TypeScript" or "Php".
Support for TypeScript and Php is experimental.
--lang|--language Compile to JavaScript (default), TypeScript, Php or Python.
Support for TypeScript, Php and Python is experimental.

Environment variables:
DOTNET_USE_POLLING_FILE_WATCHER
Expand All @@ -92,6 +92,7 @@ let defaultFileExt language args =
| None -> CompilerOptionsHelper.DefaultExtension
match language with
| TypeScript -> Path.replaceExtension ".ts" fileExt
| Python -> Path.replaceExtension ".py" fileExt
| Php -> ".php"
| _ -> fileExt

Expand All @@ -102,6 +103,7 @@ let argLanguage args =
|> Option.defaultValue "JavaScript"
|> (function
| "ts" | "typescript" | "TypeScript" -> TypeScript
| "py" | "python" | "Python" -> Python
| "php" | "Php" | "PHP" -> Php
| _ -> JavaScript)

Expand Down
31 changes: 30 additions & 1 deletion src/Fable.Cli/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,29 @@ module private Util =
member _.SourceMap =
mapGenerator.Force().toJSON()

type PythonFileWriter(sourcePath: string, targetPath: string, cliArgs: CliArgs, dedupTargetDir) =
let fileExt = ".py"
let targetDir = Path.GetDirectoryName(targetPath)
// PEP8: Modules should have short, all-lowercase names
let fileName = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(targetPath))
let fileName = Naming.applyCaseRule Core.CaseRules.SnakeCase fileName
// Note that Python modules cannot contain dots or it will be impossible to import them
let targetPath = Path.Combine(targetDir, fileName + fileExt)

let stream = new IO.StreamWriter(targetPath)

interface PythonPrinter.Writer with
member _.Write(str) =
stream.WriteAsync(str) |> Async.AwaitTask
member _.MakeImportPath(path) =
let projDir = IO.Path.GetDirectoryName(cliArgs.ProjectFile)
let path = Imports.getImportPath dedupTargetDir sourcePath targetPath projDir cliArgs.OutDir path
if path.EndsWith(".fs") then
let isInFableHiddenDir = Path.Combine(targetDir, path) |> Naming.isInFableHiddenDir
changeFsExtension isInFableHiddenDir path "" // Remove file extension
else path
member _.Dispose() = stream.Dispose()

let compileFile isRecompile (cliArgs: CliArgs) dedupTargetDir (com: CompilerImpl) = async {
try
let fable =
Expand Down Expand Up @@ -194,12 +217,18 @@ module private Util =

| Php ->
let php = fable |> Fable2Php.transformFile com

use w = new IO.StreamWriter(outPath)
let ctx = PhpPrinter.Output.Writer.create w
PhpPrinter.Output.writeFile ctx php
w.Flush()

| Python ->
let python = fable |> Fable2Python.Compiler.transformFile com
let map = { new PythonPrinter.SourceMapGenerator with
member _.AddMapping(_,_,_,_,_) = () }
let writer = new PythonFileWriter(com.CurrentFile, outPath, cliArgs, dedupTargetDir)
do! PythonPrinter.run writer map python

"Compiled " + File.getRelativePathFromCwd com.CurrentFile
|> Log.verboseOrIf isRecompile
Expand Down
4 changes: 4 additions & 0 deletions src/Fable.Core/Fable.Core.JsInterop.fs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ let jsTypeof (x: obj): string = jsNative
[<Emit("$0 instanceof $1")>]
let jsInstanceof (x: obj) (cons: obj): bool = jsNative

/// Check if object is callable, i.e a function.
[<Emit("typeof $0 == \"function\"")>]
let inline callable(x: obj) = jsNative

[<Emit("this")>]
let jsThis<'T> : 'T = jsNative

Expand Down
Loading