diff --git a/Cargo.lock b/Cargo.lock index d96474c..e62854d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -476,6 +476,12 @@ dependencies = [ "gl_generator 0.14.0", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "image" version = "0.22.5" @@ -493,6 +499,12 @@ dependencies = [ "tiff", ] +[[package]] +name = "indoc" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" + [[package]] name = "inflate" version = "0.4.5" @@ -622,6 +634,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg 1.4.0", +] + [[package]] name = "miniz_oxide" version = "0.8.0" @@ -933,6 +954,12 @@ dependencies = [ "inflate", ] +[[package]] +name = "portable-atomic" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6" + [[package]] name = "proc-macro2" version = "0.4.30" @@ -951,6 +978,69 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "pyo3" +version = "0.23.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e484fd2c8b4cb67ab05a318f1fd6fa8f199fcc30819f08f07d200809dba26c15" +dependencies = [ + "cfg-if 1.0.0", + "indoc", + "libc", + "memoffset", + "once_cell", + "portable-atomic", + "pyo3-build-config", + "pyo3-ffi", + "pyo3-macros", + "unindent", +] + +[[package]] +name = "pyo3-build-config" +version = "0.23.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc0e0469a84f208e20044b98965e1561028180219e35352a2afaf2b942beff3b" +dependencies = [ + "once_cell", + "target-lexicon", +] + +[[package]] +name = "pyo3-ffi" +version = "0.23.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb1547a7f9966f6f1a0f0227564a9945fe36b90da5a93b3933fc3dc03fae372d" +dependencies = [ + "libc", + "pyo3-build-config", +] + +[[package]] +name = "pyo3-macros" +version = "0.23.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdb6da8ec6fa5cedd1626c886fc8749bdcbb09424a86461eb8cdf096b7c33257" +dependencies = [ + "proc-macro2 1.0.92", + "pyo3-macros-backend", + "quote 1.0.37", + "syn 2.0.89", +] + +[[package]] +name = "pyo3-macros-backend" +version = "0.23.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38a385202ff5a92791168b1136afae5059d3ac118457bb7bc304c197c2d33e7d" +dependencies = [ + "heck", + "proc-macro2 1.0.92", + "pyo3-build-config", + "quote 1.0.37", + "syn 2.0.89", +] + [[package]] name = "quote" version = "0.6.13" @@ -1332,6 +1422,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "target-lexicon" +version = "0.12.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" + [[package]] name = "tiff" version = "0.3.1" @@ -1363,6 +1459,7 @@ dependencies = [ name = "ulohy" version = "0.1.0" dependencies = [ + "pyo3", "turtle", ] @@ -1378,6 +1475,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +[[package]] +name = "unindent" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce" + [[package]] name = "vecmath" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index 7c2aae9..59732c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2024" opt-level = 3 [dependencies] +pyo3 = { version = "0.23.3", features = ["auto-initialize"] } turtle = "1.0.0-rc.3" diff --git a/src/u2.py b/src/u2.py new file mode 100644 index 0000000..59e25ad --- /dev/null +++ b/src/u2.py @@ -0,0 +1,65 @@ +import turtle + +radius = 100 +t = turtle.Turtle() +t.speed(100) +t.penup() + +def a(): + t.setheading(180) + t.forward(radius*3) + circle2(False) + t.setheading(0) + t.penup() + t.forward(radius*2) + circle2(True) + t.setheading(0) + t.penup() + t.forward(radius*2) + circle2(False) + t.setheading(0) + t.penup() + t.forward(radius*2) + circle2(True) + +def b(): + circle2(False) + t.setheading(90) + t.forward(radius*2) + circle2(True) + t.setheading(180) + t.forward(radius*2) + circle2(False) + t.setheading(270) + t.forward(radius*2) + circle2(True) + + +def c(): + circle2(False) + t.setheading(90) + t.forward(radius*2) + circle2(False) + t.setheading(180) + t.forward(radius*2) + circle2(True) + t.setheading(270) + t.forward(radius*2) + circle2(True) + +def circle2(reverse): + if reverse: + t.dot(radius*2, "black") + t.dot(radius, "gray") + else: + t.dot(radius*2, "gray") + t.dot(radius, "black") + +def main(): + func = input("a b abo c\n") + globals().get(func)() + + input() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/u2.rs b/src/u2.rs index da0f5d9..9f7bc22 100644 --- a/src/u2.rs +++ b/src/u2.rs @@ -1 +1,13 @@ -pub fn main() {} +use std::ffi::{CStr, CString}; +use pyo3::ffi::c_str; +use pyo3::prelude::*; + +static code: &'static str = include_str!("u2.py"); + +pub fn main() { + Python::with_gil(|py| { + let c = CString::new(code.as_bytes()).unwrap(); + let activator = PyModule::from_code(py, c.as_c_str(), c_str!("main.py"), c_str!("main")).unwrap(); + activator.call_method("main", (), None).unwrap(); + }) +}