compilekv 0.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. compilekv-0.0.0/CompileKvWasm/Package.swift +27 -0
  2. compilekv-0.0.0/CompileKvWasm/Sources/CompileKvWasm/Conversion.swift +27 -0
  3. compilekv-0.0.0/CompileKvWasm/Sources/CompileKvWasm/Exports.swift +89 -0
  4. compilekv-0.0.0/CompileKvWasm/Sources/CompileKvWasm/main.swift +2 -0
  5. compilekv-0.0.0/KvToPyClass/Package.swift +54 -0
  6. compilekv-0.0.0/KvToPyClass/Sources/KvToPyClass/KvToPyClassGenerator.swift +3164 -0
  7. compilekv-0.0.0/KvToPyClass/Sources/KvToPyClass/PythonClassParser.swift +119 -0
  8. compilekv-0.0.0/KvToPyClass/Sources/KvToPyClassCLI/main.swift +65 -0
  9. compilekv-0.0.0/KvToPyClass/Tests/KvToPyClassTests/KivyWidgetRegistryTests.swift +189 -0
  10. compilekv-0.0.0/KvToPyClass/Tests/KvToPyClassTests/KvToPyClassTests.swift +230 -0
  11. compilekv-0.0.0/LICENSE +21 -0
  12. compilekv-0.0.0/LICENSE.txt +202 -0
  13. compilekv-0.0.0/MANIFEST.in +15 -0
  14. compilekv-0.0.0/PKG-INFO +324 -0
  15. compilekv-0.0.0/README.md +312 -0
  16. compilekv-0.0.0/plan.md +79 -0
  17. compilekv-0.0.0/pyproject.toml +36 -0
  18. compilekv-0.0.0/scripts/build_wasm.py +165 -0
  19. compilekv-0.0.0/setup.cfg +4 -0
  20. compilekv-0.0.0/setup.py +50 -0
  21. compilekv-0.0.0/src/compilekv/__init__.py +34 -0
  22. compilekv-0.0.0/src/compilekv/__main__.py +5 -0
  23. compilekv-0.0.0/src/compilekv/cli.py +83 -0
  24. compilekv-0.0.0/src/compilekv/compiler.py +144 -0
  25. compilekv-0.0.0/src/compilekv/runtime.py +122 -0
  26. compilekv-0.0.0/src/compilekv.egg-info/PKG-INFO +324 -0
  27. compilekv-0.0.0/src/compilekv.egg-info/SOURCES.txt +43 -0
  28. compilekv-0.0.0/src/compilekv.egg-info/dependency_links.txt +1 -0
  29. compilekv-0.0.0/src/compilekv.egg-info/requires.txt +1 -0
  30. compilekv-0.0.0/src/compilekv.egg-info/top_level.txt +1 -0
  31. compilekv-0.0.0/tests/conftest.py +100 -0
  32. compilekv-0.0.0/tests/test_canvas.py +116 -0
  33. compilekv-0.0.0/tests/test_class_merge.py +135 -0
  34. compilekv-0.0.0/tests/test_compiler.py +344 -0
  35. compilekv-0.0.0/tests/test_constants.py +257 -0
  36. compilekv-0.0.0/tests/test_directives.py +122 -0
  37. compilekv-0.0.0/tests/test_factory.py +78 -0
  38. compilekv-0.0.0/tests/test_id_references.py +64 -0
  39. compilekv-0.0.0/tests/test_imports.py +55 -0
  40. compilekv-0.0.0/tests/test_module_entry.py +28 -0
  41. compilekv-0.0.0/tests/test_root_bindings.py +100 -0
  42. compilekv-0.0.0/tests/test_runtime.py +179 -0
  43. compilekv-0.0.0/tests/test_self_scope.py +109 -0
  44. compilekv-0.0.0/tests/test_showcase.py +58 -0
  45. compilekv-0.0.0/tests/test_values.py +100 -0
@@ -0,0 +1,27 @@
1
+ // swift-tools-version: 6.2
2
+ import PackageDescription
3
+
4
+ let package = Package(
5
+ name: "CompileKvWasm",
6
+ platforms: [
7
+ .macOS(.v13)
8
+ ],
9
+ products: [
10
+ .executable(
11
+ name: "CompileKvWasm",
12
+ targets: ["CompileKvWasm"]
13
+ )
14
+ ],
15
+ dependencies: [
16
+ .package(path: "../KvToPyClass")
17
+ ],
18
+ targets: [
19
+ .executableTarget(
20
+ name: "CompileKvWasm",
21
+ dependencies: [
22
+ .product(name: "KvToPyClass", package: "KvToPyClass")
23
+ ],
24
+ path: "Sources/CompileKvWasm"
25
+ )
26
+ ]
27
+ )
@@ -0,0 +1,27 @@
1
+ import Foundation
2
+ import KvParser
3
+ import KvToPyClass
4
+
5
+ /// Convert KV source to Python. `pySource` is the existing .py, or empty.
6
+ /// `constantsSource` holds `#:set` directives gathered from other KV files.
7
+ func convert(kvSource: String, pySource: String, constantsSource: String = "") throws -> String {
8
+ let module = try parse(kvSource)
9
+
10
+ let existing = pySource.isEmpty
11
+ ? PythonModuleInfo(body: [], classes: [])
12
+ : PythonClassParser(source: pySource).parseModule()
13
+
14
+ // Directives the file declares itself win over the shared ones.
15
+ let shared = constantsSource.isEmpty ? [] : (try? parse(constantsSource))?.directives ?? []
16
+
17
+ return try KvToPyClassGenerator(
18
+ module: module,
19
+ existing: existing,
20
+ sharedDirectives: shared
21
+ ).generate()
22
+ }
23
+
24
+ private func parse(_ source: String) throws -> KvModule {
25
+ let tokens = try KvTokenizer(source: source).tokenize()
26
+ return try KvParser(tokens: tokens).parse()
27
+ }
@@ -0,0 +1,89 @@
1
+ import Foundation
2
+
3
+ // Wasm can only pass numbers, so strings travel as (pointer, length) into
4
+ // linear memory. The host allocates with kv_alloc, calls kv_convert, then reads
5
+ // the result through kv_result_ptr/kv_result_len and releases it.
6
+
7
+ private nonisolated(unsafe) var resultBuffer: UnsafeMutableRawPointer?
8
+ private nonisolated(unsafe) var resultCount: Int32 = 0
9
+
10
+ private func storeResult(_ string: String) {
11
+ freeResult()
12
+ let utf8 = Array(string.utf8)
13
+ guard !utf8.isEmpty else { return }
14
+ let buffer = UnsafeMutableRawPointer.allocate(byteCount: utf8.count, alignment: 1)
15
+ utf8.withUnsafeBytes { buffer.copyMemory(from: $0.baseAddress!, byteCount: utf8.count) }
16
+ resultBuffer = buffer
17
+ resultCount = Int32(utf8.count)
18
+ }
19
+
20
+ private func freeResult() {
21
+ resultBuffer?.deallocate()
22
+ resultBuffer = nil
23
+ resultCount = 0
24
+ }
25
+
26
+ private func string(from pointer: UnsafeRawPointer?, length: Int32) -> String {
27
+ guard let pointer, length > 0 else { return "" }
28
+ return String(decoding: UnsafeRawBufferPointer(start: pointer, count: Int(length)), as: UTF8.self)
29
+ }
30
+
31
+ @_expose(wasm, "kv_alloc")
32
+ @_cdecl("kv_alloc")
33
+ public func kv_alloc(_ size: Int32) -> UnsafeMutableRawPointer? {
34
+ guard size > 0 else { return nil }
35
+ return UnsafeMutableRawPointer.allocate(byteCount: Int(size), alignment: 1)
36
+ }
37
+
38
+ @_expose(wasm, "kv_dealloc")
39
+ @_cdecl("kv_dealloc")
40
+ public func kv_dealloc(_ pointer: UnsafeMutableRawPointer?, _ size: Int32) {
41
+ pointer?.deallocate()
42
+ }
43
+
44
+ /// Returns 0 on success, 1 on failure. Either way the result buffer holds the
45
+ /// generated code or the error description.
46
+ ///
47
+ /// `constants` carries `#:set` directives from other KV files. They are passed
48
+ /// separately rather than pasted onto the front of the source so parser error
49
+ /// line numbers still match the file the caller is looking at.
50
+ @_expose(wasm, "kv_convert")
51
+ @_cdecl("kv_convert")
52
+ public func kv_convert(
53
+ _ kvPointer: UnsafeRawPointer?,
54
+ _ kvLength: Int32,
55
+ _ pyPointer: UnsafeRawPointer?,
56
+ _ pyLength: Int32,
57
+ _ constantsPointer: UnsafeRawPointer?,
58
+ _ constantsLength: Int32
59
+ ) -> Int32 {
60
+ do {
61
+ storeResult(try convert(
62
+ kvSource: string(from: kvPointer, length: kvLength),
63
+ pySource: string(from: pyPointer, length: pyLength),
64
+ constantsSource: string(from: constantsPointer, length: constantsLength)
65
+ ))
66
+ return 0
67
+ } catch {
68
+ storeResult("\(error)")
69
+ return 1
70
+ }
71
+ }
72
+
73
+ @_expose(wasm, "kv_result_ptr")
74
+ @_cdecl("kv_result_ptr")
75
+ public func kv_result_ptr() -> UnsafeMutableRawPointer? {
76
+ resultBuffer
77
+ }
78
+
79
+ @_expose(wasm, "kv_result_len")
80
+ @_cdecl("kv_result_len")
81
+ public func kv_result_len() -> Int32 {
82
+ resultCount
83
+ }
84
+
85
+ @_expose(wasm, "kv_result_free")
86
+ @_cdecl("kv_result_free")
87
+ public func kv_result_free() {
88
+ freeResult()
89
+ }
@@ -0,0 +1,2 @@
1
+ // Built as a reactor: the host calls `_initialize`, then the exports in
2
+ // Exports.swift. SwiftPM still wants an entry point, so this stays empty.
@@ -0,0 +1,54 @@
1
+ // swift-tools-version: 6.2
2
+ import PackageDescription
3
+
4
+ let package = Package(
5
+ name: "KvToPyClass",
6
+ platforms: [
7
+ .macOS(.v13)
8
+ ],
9
+ products: [
10
+ .library(
11
+ name: "KvToPyClass",
12
+ targets: ["KvToPyClass"]
13
+ ),
14
+ .executable(
15
+ name: "KvToPyClassCLI",
16
+ targets: ["KvToPyClassCLI"]
17
+ )
18
+ ],
19
+ dependencies: [
20
+ // Local dependency on SwiftyKvLang parser
21
+ .package(url: "https://github.com/kivy-school/SwiftyKvLang.git", exact: "0.0.0"),
22
+ // PySwiftAST for generating Python code
23
+ .package(url: "https://github.com/Py-Swift/PySwiftAST.git", exact: "0.0.1")
24
+ ],
25
+ targets: [
26
+ .target(
27
+ name: "KvToPyClass",
28
+ dependencies: [
29
+ .product(name: "KivyWidgetRegistry", package: "SwiftyKvLang"),
30
+ .product(name: "KvParser", package: "SwiftyKvLang"),
31
+ .product(name: "PySwiftAST", package: "PySwiftAST"),
32
+ .product(name: "PySwiftCodeGen", package: "PySwiftAST"),
33
+ .product(name: "PyFormatters", package: "PySwiftAST")
34
+ ],
35
+ path: "Sources/KvToPyClass"
36
+ ),
37
+ .executableTarget(
38
+ name: "KvToPyClassCLI",
39
+ dependencies: [
40
+ "KvToPyClass",
41
+ .product(name: "KivyWidgetRegistry", package: "SwiftyKvLang")
42
+ ],
43
+ path: "Sources/KvToPyClassCLI"
44
+ ),
45
+ .testTarget(
46
+ name: "KvToPyClassTests",
47
+ dependencies: [
48
+ "KvToPyClass",
49
+ .product(name: "KivyWidgetRegistry", package: "SwiftyKvLang")
50
+ ],
51
+ path: "Tests/KvToPyClassTests"
52
+ )
53
+ ]
54
+ )