Repository navigation
Expand file tree
/
Copy pathPythonCompiler.fs
More file actions
149 lines (114 loc) · 5.3 KB
/
Copy pathPythonCompiler.fs
File metadata and controls
149 lines (114 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
module Fable.Transforms.Python.Compiler
open System.Collections.Generic
open Fable
open Fable.AST
open Fable.Transforms
open Fable.Transforms.Python.AST
open Fable.Transforms.Python.Types
open Fable.Transforms.Python.Util
open Fable.Transforms.Python.Transforms
type PythonCompiler(com: Compiler) =
let onlyOnceWarnings = HashSet<string>()
let imports = Dictionary<string, Import>()
let exports: HashSet<string> = HashSet()
let typeVars: HashSet<string> = HashSet()
interface IPythonCompiler with
member _.WarnOnlyOnce(msg, ?range) =
if onlyOnceWarnings.Add(msg) then
addWarning com [] range msg
member _.GetImportExpr(ctx, moduleName, ?name, ?r) =
// printfn "GetImportExpr: %A" (moduleName, name)
let name =
match name with
| None
| Some null -> ""
| Some name -> name.Trim()
let isQualifiedPythonImport =
match name with
| "default"
| "*" -> false
| name when name.Length = 0 -> false // CA1820
| _ -> true
let cachedName =
moduleName
+ "::"
+ if isQualifiedPythonImport then
name
else
""
match imports.TryGetValue(cachedName) with
| true, i -> i.LocalIdent |> Expression.identifier
| false, _ ->
let local_id =
if isQualifiedPythonImport then
Some name
else
None
|> getIdentForImport ctx moduleName
let importName =
match name with
| _ when not isQualifiedPythonImport -> None
| Naming.placeholder ->
"`importMember` must be assigned to a variable" |> addError com [] r
Some name
| _ -> Some name
let i =
{
Name = importName
Module = moduleName
LocalIdent = local_id
}
imports.Add(cachedName, i)
// If the import member is empty we understand this is done for side-effects only
if name.Length = 0 then // CA1820
Expression.none
else
Expression.identifier local_id
member _.GetAllImports() =
// printfn "GetAllImports: %A" imports
imports.Values :> Import seq |> List.ofSeq
member _.GetAllTypeVars() = typeVars
member _.GetAllExports() = exports
member _.AddTypeVar(ctx, name: string) =
// For Python 3.12, use clean type parameter names (no underscores)
let name = name.ToUpperInvariant() |> Helpers.clean
// Don't add to global typeVars collection since Python 3.12 uses scoped type parameters
ctx.UsedNames.DeclarationScopes.Add(name) |> ignore
Expression.name name
member _.AddExport(name: string) =
exports.Add name |> ignore
Expression.name name
member bcom.TransformAsExpr(ctx, e) = transformAsExpr bcom ctx e
member bcom.TransformAsStatements(ctx, ret, e) = transformAsStatements bcom ctx ret e
member bcom.TransformFunction(ctx, name, args, body, generics) =
transformFunction bcom ctx name args body generics
member bcom.TransformImport(ctx, selector, path) =
transformImport bcom ctx None selector path
member bcom.GetIdentifier(ctx, name) = Util.getIdentifier bcom ctx name
member bcom.GetIdentifierAsExpr(ctx, name) =
Util.getIdentifier bcom ctx name |> Expression.name
interface Compiler with
member _.Options = com.Options
member _.Plugins = com.Plugins
member _.LibraryDir = com.LibraryDir
member _.CurrentFile = com.CurrentFile
member _.OutputDir = com.OutputDir
member _.OutputType = com.OutputType
member _.ProjectFile = com.ProjectFile
member _.ProjectOptions = com.ProjectOptions
member _.SourceFiles = com.SourceFiles
member _.IncrementCounter() = com.IncrementCounter()
member _.IsPrecompilingInlineFunction = com.IsPrecompilingInlineFunction
member _.WillPrecompileInlineFunction(file) = com.WillPrecompileInlineFunction(file)
member _.GetImplementationFile(fileName) = com.GetImplementationFile(fileName)
member _.GetRootModule(fileName) = com.GetRootModule(fileName)
member _.TryGetEntity(entRef) = com.TryGetEntity(entRef)
member _.GetInlineExpr(fullName) = com.GetInlineExpr(fullName)
member _.AddWatchDependency(fileName) = com.AddWatchDependency(fileName)
member _.AddSignatureDependency(fileName) = com.AddSignatureDependency(fileName)
member _.AddLog(msg, severity, ?range, ?fileName: string, ?tag: string) =
com.AddLog(msg, severity, ?range = range, ?fileName = fileName, ?tag = tag)
let makeCompiler com = PythonCompiler(com)
let transformFile (com: Compiler) (file: Fable.File) =
let com = makeCompiler com :> IPythonCompiler
transformFile com file