diff --git a/.gitattributes b/.gitattributes index d663b4830a..aa993ad110 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,8 +1,6 @@ Lib/** linguist-vendored Cargo.lock linguist-generated -merge *.snap linguist-generated -merge -ast/src/ast_gen.rs linguist-generated -merge vm/src/stdlib/ast/gen.rs linguist-generated -merge -compiler/parser/python.lalrpop text eol=LF Lib/*.py text working-tree-encoding=UTF-8 eol=LF **/*.rs text working-tree-encoding=UTF-8 eol=LF diff --git a/Cargo.lock b/Cargo.lock index c781d7b7d9..623a723ff1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,15 +85,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" -[[package]] -name = "ascii-canvas" -version = "3.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8824ecca2e851cec16968d54a01dd372ef8f95b244fb84b84e70128be347c3c6" -dependencies = [ - "term", -] - [[package]] name = "atomic" version = "0.5.1" @@ -126,21 +117,6 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" -[[package]] -name = "bit-set" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1" -dependencies = [ - "bit-vec", -] - -[[package]] -name = "bit-vec" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" - [[package]] name = "bitflags" version = "1.3.2" @@ -638,12 +614,6 @@ dependencies = [ "syn", ] -[[package]] -name = "diff" -version = "0.1.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" - [[package]] name = "digest" version = "0.10.6" @@ -700,15 +670,6 @@ version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" -[[package]] -name = "ena" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7402b94a93c24e742487327a7cd839dc9d36fec9de9fb25b09f2dae459f36c3" -dependencies = [ - "log", -] - [[package]] name = "encode_unicode" version = "0.3.6" @@ -780,12 +741,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "fixedbitset" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" - [[package]] name = "flame" version = "0.2.2" @@ -885,9 +840,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.9" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ "cfg-if", "js-sys", @@ -1036,18 +991,6 @@ dependencies = [ "syn", ] -[[package]] -name = "is-terminal" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" -dependencies = [ - "hermit-abi 0.3.1", - "io-lifetimes", - "rustix", - "windows-sys 0.48.0", -] - [[package]] name = "itertools" version = "0.10.5" @@ -1081,59 +1024,6 @@ dependencies = [ "cpufeatures", ] -[[package]] -name = "lalrpop" -version = "0.19.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a1cbf952127589f2851ab2046af368fd20645491bb4b376f04b7f94d7a9837b" -dependencies = [ - "ascii-canvas", - "bit-set", - "diff", - "ena", - "is-terminal", - "itertools", - "lalrpop-util 0.19.12", - "petgraph", - "regex", - "regex-syntax 0.6.28", - "string_cache", - "term", - "tiny-keccak", - "unicode-xid", -] - -[[package]] -name = "lalrpop" -version = "0.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da4081d44f4611b66c6dd725e6de3169f9f63905421e8626fcb86b6a898998b8" -dependencies = [ - "ascii-canvas", - "bit-set", - "diff", - "ena", - "is-terminal", - "itertools", - "lalrpop-util 0.20.0", - "petgraph", - "regex", - "regex-syntax 0.7.1", - "string_cache", - "term", - "tiny-keccak", - "unicode-xid", -] - -[[package]] -name = "lalrpop-util" -version = "0.19.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3c48237b9604c5a4702de6b824e02006c3214327564636aef27c1028a8fa0ed" -dependencies = [ - "regex", -] - [[package]] name = "lalrpop-util" version = "0.20.0" @@ -1370,12 +1260,6 @@ dependencies = [ "rand_core", ] -[[package]] -name = "new_debug_unreachable" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" - [[package]] name = "nibble_vec" version = "0.1.0" @@ -1615,23 +1499,13 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d01a5bd0424d00070b0098dd17ebca6f961a959dead1dbcbbbc1d1cd8d3deeba" -[[package]] -name = "petgraph" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dd7d28ee937e54fe3080c91faa1c3a46c06de6252988a7f4592ba2310ef22a4" -dependencies = [ - "fixedbitset", - "indexmap", -] - [[package]] name = "phf" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "928c6535de93548188ef63bb7c4036bd415cd8f36ad25af44b9789b2ee72a48c" dependencies = [ - "phf_shared 0.11.1", + "phf_shared", ] [[package]] @@ -1641,7 +1515,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a56ac890c5e3ca598bbdeaa99964edb5b0258a583a9eb6ef4e89fc85d9224770" dependencies = [ "phf_generator", - "phf_shared 0.11.1", + "phf_shared", ] [[package]] @@ -1650,19 +1524,10 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1181c94580fa345f50f19d738aaa39c0ed30a600d95cb2d3e23f94266f14fbf" dependencies = [ - "phf_shared 0.11.1", + "phf_shared", "rand", ] -[[package]] -name = "phf_shared" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096" -dependencies = [ - "siphasher", -] - [[package]] name = "phf_shared" version = "0.11.1" @@ -1729,12 +1594,6 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" -[[package]] -name = "precomputed-hash" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" - [[package]] name = "proc-macro-crate" version = "1.3.0" @@ -1893,7 +1752,7 @@ checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.6.28", + "regex-syntax", ] [[package]] @@ -1908,12 +1767,6 @@ version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" -[[package]] -name = "regex-syntax" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c" - [[package]] name = "region" version = "2.2.0" @@ -1949,48 +1802,19 @@ dependencies = [ ] [[package]] -name = "ruff_python_ast" +name = "ruff_source_location" version = "0.0.0" -source = "git+https://github.com/youknowone/ruff.git?rev=583df5c1fa43b2732896219f8ab425116c140c80#583df5c1fa43b2732896219f8ab425116c140c80" +source = "git+https://github.com/youknowone/RustPython-parser.git?rev=6fa3d0f90a5115680ded1ed1606918b4b26251de#6fa3d0f90a5115680ded1ed1606918b4b26251de" dependencies = [ - "anyhow", - "bitflags 2.2.1", - "is-macro", - "itertools", - "log", "memchr", - "num-bigint", - "num-traits", "once_cell", - "regex", - "ruff_rustpython", - "ruff_text_size 0.0.0 (git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a)", - "rustc-hash", - "rustpython-common 0.2.0 (git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a)", - "rustpython-parser 0.2.0 (git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a)", - "smallvec", -] - -[[package]] -name = "ruff_rustpython" -version = "0.0.0" -source = "git+https://github.com/youknowone/ruff.git?rev=583df5c1fa43b2732896219f8ab425116c140c80#583df5c1fa43b2732896219f8ab425116c140c80" -dependencies = [ - "anyhow", - "once_cell", - "rustpython-common 0.2.0 (git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a)", - "rustpython-parser 0.2.0 (git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a)", + "ruff_text_size", ] [[package]] name = "ruff_text_size" version = "0.0.0" -source = "git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d#09a6afdd04587ccc8841a88e34890c9409d8649d" - -[[package]] -name = "ruff_text_size" -version = "0.0.0" -source = "git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a#c3147d2c1524ebd0e90cf1c2938d770314fd5a5a" +source = "git+https://github.com/youknowone/RustPython-parser.git?rev=6fa3d0f90a5115680ded1ed1606918b4b26251de#6fa3d0f90a5115680ded1ed1606918b4b26251de" dependencies = [ "serde", ] @@ -2041,7 +1865,7 @@ dependencies = [ "log", "python3-sys", "rustpython-compiler", - "rustpython-parser 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", + "rustpython-parser", "rustpython-pylib", "rustpython-stdlib", "rustpython-vm", @@ -2051,20 +1875,11 @@ dependencies = [ [[package]] name = "rustpython-ast" version = "0.2.0" -source = "git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d#09a6afdd04587ccc8841a88e34890c9409d8649d" +source = "git+https://github.com/youknowone/RustPython-parser.git?rev=6fa3d0f90a5115680ded1ed1606918b4b26251de#6fa3d0f90a5115680ded1ed1606918b4b26251de" dependencies = [ "num-bigint", - "rustpython-compiler-core 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", "rustpython-literal", -] - -[[package]] -name = "rustpython-ast" -version = "0.2.0" -source = "git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a#c3147d2c1524ebd0e90cf1c2938d770314fd5a5a" -dependencies = [ - "num-bigint", - "ruff_text_size 0.0.0 (git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a)", + "rustpython-parser-core", ] [[package]] @@ -2072,16 +1887,17 @@ name = "rustpython-codegen" version = "0.2.0" dependencies = [ "ahash", - "bitflags 1.3.2", + "bitflags 2.2.1", "indexmap", "insta", "itertools", "log", "num-complex", "num-traits", - "rustpython-ast 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", - "rustpython-compiler-core 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", - "rustpython-parser 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", + "rustpython-ast", + "rustpython-compiler-core", + "rustpython-parser", + "rustpython-parser-core", ] [[package]] @@ -2089,7 +1905,7 @@ name = "rustpython-common" version = "0.2.0" dependencies = [ "ascii", - "bitflags 1.3.2", + "bitflags 2.2.1", "bstr", "cfg-if", "itertools", @@ -2108,66 +1924,26 @@ dependencies = [ "widestring", ] -[[package]] -name = "rustpython-common" -version = "0.2.0" -source = "git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a#c3147d2c1524ebd0e90cf1c2938d770314fd5a5a" -dependencies = [ - "ascii", - "bitflags 1.3.2", - "bstr", - "cfg-if", - "getrandom", - "hexf-parse", - "itertools", - "lexical-parse-float", - "libc", - "lock_api", - "num-bigint", - "num-traits", - "once_cell", - "radium", - "rand", - "siphasher", - "unic-ucd-category", - "volatile", - "widestring", -] - [[package]] name = "rustpython-compiler" version = "0.2.0" dependencies = [ "rustpython-codegen", - "rustpython-compiler-core 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", - "rustpython-parser 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", + "rustpython-compiler-core", + "rustpython-parser", ] [[package]] name = "rustpython-compiler-core" version = "0.2.0" -source = "git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d#09a6afdd04587ccc8841a88e34890c9409d8649d" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.2.1", "itertools", "lz4_flex", "num-bigint", "num-complex", - "ruff_python_ast", - "ruff_text_size 0.0.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", -] - -[[package]] -name = "rustpython-compiler-core" -version = "0.2.0" -source = "git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a#c3147d2c1524ebd0e90cf1c2938d770314fd5a5a" -dependencies = [ - "bitflags 1.3.2", - "itertools", - "lz4_flex", - "num-bigint", - "num-complex", - "ruff_text_size 0.0.0 (git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a)", + "rustpython-parser-core", + "serde", ] [[package]] @@ -2189,8 +1965,9 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "rustpython-compiler-core 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", + "rustpython-compiler-core", "rustpython-doc", + "rustpython-parser-core", "syn", "syn-ext", "textwrap 0.15.2", @@ -2214,7 +1991,7 @@ dependencies = [ "cranelift-module", "libffi", "num-traits", - "rustpython-compiler-core 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", + "rustpython-compiler-core", "rustpython-derive", "thiserror", ] @@ -2222,7 +1999,7 @@ dependencies = [ [[package]] name = "rustpython-literal" version = "0.2.0" -source = "git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d#09a6afdd04587ccc8841a88e34890c9409d8649d" +source = "git+https://github.com/youknowone/RustPython-parser.git?rev=6fa3d0f90a5115680ded1ed1606918b4b26251de#6fa3d0f90a5115680ded1ed1606918b4b26251de" dependencies = [ "hexf-parse", "lexical-parse-float", @@ -2233,21 +2010,19 @@ dependencies = [ [[package]] name = "rustpython-parser" version = "0.2.0" -source = "git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d#09a6afdd04587ccc8841a88e34890c9409d8649d" +source = "git+https://github.com/youknowone/RustPython-parser.git?rev=6fa3d0f90a5115680ded1ed1606918b4b26251de#6fa3d0f90a5115680ded1ed1606918b4b26251de" dependencies = [ - "ahash", "anyhow", "itertools", - "lalrpop 0.20.0", - "lalrpop-util 0.20.0", + "lalrpop-util", "log", "num-bigint", "num-traits", "phf", "phf_codegen", "rustc-hash", - "rustpython-ast 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", - "rustpython-compiler-core 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", + "rustpython-ast", + "rustpython-parser-core", "tiny-keccak", "unic-emoji-char", "unic-ucd-ident", @@ -2255,27 +2030,16 @@ dependencies = [ ] [[package]] -name = "rustpython-parser" +name = "rustpython-parser-core" version = "0.2.0" -source = "git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a#c3147d2c1524ebd0e90cf1c2938d770314fd5a5a" +source = "git+https://github.com/youknowone/RustPython-parser.git?rev=6fa3d0f90a5115680ded1ed1606918b4b26251de#6fa3d0f90a5115680ded1ed1606918b4b26251de" dependencies = [ - "anyhow", "itertools", - "lalrpop 0.19.12", - "lalrpop-util 0.19.12", - "log", + "lz4_flex", "num-bigint", - "num-traits", - "phf", - "phf_codegen", - "ruff_text_size 0.0.0 (git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a)", - "rustc-hash", - "rustpython-ast 0.2.0 (git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a)", - "rustpython-compiler-core 0.2.0 (git+https://github.com/charliermarsh/RustPython.git?rev=c3147d2c1524ebd0e90cf1c2938d770314fd5a5a)", - "tiny-keccak", - "unic-emoji-char", - "unic-ucd-ident", - "unicode_names2", + "num-complex", + "ruff_source_location", + "ruff_text_size", ] [[package]] @@ -2283,7 +2047,7 @@ name = "rustpython-pylib" version = "0.2.0" dependencies = [ "glob", - "rustpython-compiler-core 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", + "rustpython-compiler-core", "rustpython-derive", ] @@ -2335,7 +2099,7 @@ dependencies = [ "puruspe", "rand", "rand_core", - "rustpython-common 0.2.0", + "rustpython-common", "rustpython-derive", "rustpython-vm", "schannel", @@ -2367,7 +2131,7 @@ dependencies = [ "ahash", "ascii", "atty", - "bitflags 1.3.2", + "bitflags 2.2.1", "bstr", "caseless", "cfg-if", @@ -2402,15 +2166,16 @@ dependencies = [ "rand", "result-like", "rustc_version", - "rustpython-ast 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", + "rustpython-ast", "rustpython-codegen", - "rustpython-common 0.2.0", + "rustpython-common", "rustpython-compiler", - "rustpython-compiler-core 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", + "rustpython-compiler-core", "rustpython-derive", "rustpython-jit", "rustpython-literal", - "rustpython-parser 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", + "rustpython-parser", + "rustpython-parser-core", "rustyline", "schannel", "serde", @@ -2441,8 +2206,8 @@ version = "0.2.0" dependencies = [ "console_error_panic_hook", "js-sys", - "rustpython-common 0.2.0", - "rustpython-parser 0.2.0 (git+https://github.com/youknowone/RustPython-parser.git?rev=09a6afdd04587ccc8841a88e34890c9409d8649d)", + "rustpython-common", + "rustpython-parser", "rustpython-pylib", "rustpython-stdlib", "rustpython-vm", @@ -2666,19 +2431,6 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e08d8363704e6c71fc928674353e6b7c23dcea9d82d7012c8faf2a3a025f8d0" -[[package]] -name = "string_cache" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "213494b7a2b503146286049378ce02b482200519accc31872ee8be91fa820a08" -dependencies = [ - "new_debug_unreachable", - "once_cell", - "parking_lot", - "phf_shared 0.10.0", - "precomputed-hash", -] - [[package]] name = "strsim" version = "0.8.0" @@ -2757,17 +2509,6 @@ version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ae9980cab1db3fceee2f6c6f643d5d8de2997c58ee8d25fb0cc8a9e9e7348e5" -[[package]] -name = "term" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" -dependencies = [ - "dirs-next", - "rustversion", - "winapi", -] - [[package]] name = "termcolor" version = "1.2.0" @@ -3090,12 +2831,6 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" -[[package]] -name = "unicode-xid" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" - [[package]] name = "unicode_names2" version = "0.6.0" diff --git a/Cargo.toml b/Cargo.toml index f4900ce28f..492b8e368e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,17 +12,29 @@ include = ["LICENSE", "Cargo.toml", "src/**/*.rs"] [workspace] resolver = "2" members = [ - "compiler", "compiler/codegen", + "compiler", "compiler/core", "compiler/codegen", ".", "common", "derive", "jit", "vm", "pylib", "stdlib", "wasm/lib", "derive-impl", ] [workspace.dependencies] -rustpython-literal = { git = "https://github.com/youknowone/RustPython-parser.git", rev = "09a6afdd04587ccc8841a88e34890c9409d8649d" } -rustpython-compiler-core = { git = "https://github.com/youknowone/RustPython-parser.git", rev = "09a6afdd04587ccc8841a88e34890c9409d8649d" } -rustpython-parser = { git = "https://github.com/youknowone/RustPython-parser.git", rev = "09a6afdd04587ccc8841a88e34890c9409d8649d" } -rustpython-ast = { git = "https://github.com/youknowone/RustPython-parser.git", rev = "09a6afdd04587ccc8841a88e34890c9409d8649d" } +rustpython-compiler-core = { path = "compiler/core" } +rustpython-compiler = { path = "compiler" } +rustpython-codegen = { path = "compiler/codegen" } +rustpython-common = { path = "common" } +rustpython-derive = { path = "derive" } +rustpython-derive-impl = { path = "derive-impl" } +rustpython-jit = { path = "jit" } +rustpython-vm = { path = "vm" } +rustpython-pylib = { path = "pylib" } +rustpython-stdlib = { path = "stdlib" } +rustpython-doc = { git = "https://github.com/RustPython/__doc__", branch = "main" } + +rustpython-literal = { git = "https://github.com/youknowone/RustPython-parser.git", rev = "6fa3d0f90a5115680ded1ed1606918b4b26251de" } +rustpython-parser-core = { git = "https://github.com/youknowone/RustPython-parser.git", rev = "6fa3d0f90a5115680ded1ed1606918b4b26251de" } +rustpython-parser = { git = "https://github.com/youknowone/RustPython-parser.git", rev = "6fa3d0f90a5115680ded1ed1606918b4b26251de" } +rustpython-ast = { git = "https://github.com/youknowone/RustPython-parser.git", rev = "6fa3d0f90a5115680ded1ed1606918b4b26251de" } # rustpython-literal = { path = "../RustPython-parser/literal" } -# rustpython-compiler-core = { path = "../RustPython-parser/core" } +# rustpython-parser-core = { path = "../RustPython-parser/core" } # rustpython-parser = { path = "../RustPython-parser/parser" } # rustpython-ast = { path = "../RustPython-parser/ast" } @@ -30,7 +42,7 @@ ahash = "0.7.6" anyhow = "1.0.45" ascii = "1.0" atty = "0.2.14" -bitflags = "1.3.2" +bitflags = "2.2.1" bstr = "0.2.17" cfg-if = "1.0" chrono = "0.4.19" @@ -65,7 +77,7 @@ unicode_names2 = { version = "0.6.0", git = "https://github.com/youknowone/unico widestring = "0.5.1" [features] -default = ["threading", "stdlib", "zlib", "importlib", "encodings", "rustpython-parser/lalrpop"] +default = ["threading", "stdlib", "zlib", "importlib", "encodings"] importlib = ["rustpython-vm/importlib"] encodings = ["rustpython-vm/encodings"] stdlib = ["rustpython-stdlib", "rustpython-pylib"] diff --git a/Lib/test/test_opcodes.py b/Lib/test/test_opcodes.py index 8cab050096..e880c3f1ac 100644 --- a/Lib/test/test_opcodes.py +++ b/Lib/test/test_opcodes.py @@ -21,8 +21,6 @@ class OpcodeTest(unittest.TestCase): if n != 90: self.fail('try inside for') - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_setup_annotations_line(self): # check that SETUP_ANNOTATIONS does not create spurious line numbers try: diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index cdb4cbeab7..9726d3cc1e 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -2092,8 +2092,6 @@ def func2(): """ self._check_error(code, "expected ':'") - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_invalid_line_continuation_error_position(self): self._check_error(r"a = 3 \ 4", "unexpected character after line continuation character", diff --git a/common/src/cformat.rs b/common/src/cformat.rs index aa31e47b7f..77f1e1e7d8 100644 --- a/common/src/cformat.rs +++ b/common/src/cformat.rs @@ -83,6 +83,7 @@ impl From for CFormatPrecision { } bitflags! { + #[derive(Copy, Clone, Debug, PartialEq)] pub struct CConversionFlags: u32 { const ALTERNATE_FORM = 0b0000_0001; const ZERO_PAD = 0b0000_0010; diff --git a/compiler/Cargo.toml b/compiler/Cargo.toml index 22c182fafb..8d5d81e0bd 100644 --- a/compiler/Cargo.toml +++ b/compiler/Cargo.toml @@ -6,6 +6,6 @@ authors = ["RustPython Team"] edition = "2021" [dependencies] -rustpython-codegen = { path = "codegen" } +rustpython-codegen = { workspace = true } rustpython-compiler-core = { workspace = true } rustpython-parser = { workspace = true } diff --git a/compiler/codegen/Cargo.toml b/compiler/codegen/Cargo.toml index 861c9fba1a..6d33f48831 100644 --- a/compiler/codegen/Cargo.toml +++ b/compiler/codegen/Cargo.toml @@ -8,7 +8,8 @@ license = "MIT" edition = "2021" [dependencies] -rustpython-ast = { workspace = true, features=["unparse"] } +rustpython-ast = { workspace = true, features=["unparse", "constant-optimization"] } +rustpython-parser-core = { workspace = true } rustpython-compiler-core = { workspace = true } ahash = { workspace = true } diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index 3cf5dc007c..e640547086 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -18,13 +18,15 @@ use num_complex::Complex64; use num_traits::ToPrimitive; use rustpython_ast as ast; use rustpython_compiler_core::{ - self as bytecode, source_code::SourceLocation, Arg as OpArgMarker, CodeObject, ConstantData, - Instruction, LineNumber, OpArg, OpArgType, + bytecode::{self, Arg as OpArgMarker, CodeObject, ConstantData, Instruction, OpArg, OpArgType}, + Mode, +}; +use rustpython_parser_core::{ + source_code::{LineNumber, SourceLocation}, + ConversionFlag, }; use std::borrow::Cow; -pub use rustpython_compiler_core::Mode; - type CompileResult = Result; #[derive(PartialEq, Eq, Clone, Copy)] @@ -225,7 +227,7 @@ impl Compiler { code_stack: vec![module_code], symbol_table_stack: Vec::new(), source_path, - current_source_location: SourceLocation::MIN, + current_source_location: SourceLocation::default(), qualified_path: Vec::new(), done_with_future_stmts: false, future_annotations: false, @@ -2228,7 +2230,7 @@ impl Compiler { emit!( self, Instruction::FormatValue { - conversion: bytecode::ConversionFlag::try_from(*conversion) + conversion: ConversionFlag::from_op_arg(*conversion) .expect("invalid conversion flag"), }, ); @@ -2898,15 +2900,18 @@ impl ToU32 for usize { mod tests { use super::*; use rustpython_parser as parser; + use rustpython_parser_core::source_code::SourceLocator; fn compile_exec(source: &str) -> CodeObject { + let mut locator = SourceLocator::new(source); + use rustpython_parser::ast::fold::Fold; let mut compiler: Compiler = Compiler::new( CompileOpts::default(), "source_path".to_owned(), "".to_owned(), - source, ); let ast = parser::parse_program(source, "").unwrap(); + let ast = locator.fold(ast).unwrap(); let symbol_scope = SymbolTable::scan_program(&ast).unwrap(); compiler.compile_program(&ast, symbol_scope).unwrap(); compiler.pop_code_object() diff --git a/compiler/codegen/src/error.rs b/compiler/codegen/src/error.rs index d62afeacac..017f735105 100644 --- a/compiler/codegen/src/error.rs +++ b/compiler/codegen/src/error.rs @@ -1,6 +1,6 @@ use std::fmt; -pub type CodegenError = rustpython_compiler_core::LocatedError; +pub type CodegenError = rustpython_parser_core::source_code::LocatedError; #[derive(Debug)] #[non_exhaustive] diff --git a/compiler/codegen/src/ir.rs b/compiler/codegen/src/ir.rs index d88d7d37c8..de7055e0d7 100644 --- a/compiler/codegen/src/ir.rs +++ b/compiler/codegen/src/ir.rs @@ -1,10 +1,10 @@ use std::ops; use crate::IndexSet; -use rustpython_compiler_core::{ - source_code::SourceLocation, CodeFlags, CodeObject, CodeUnit, ConstantData, - InstrDisplayContext, Instruction, Label, LineNumber, OpArg, +use rustpython_compiler_core::bytecode::{ + CodeFlags, CodeObject, CodeUnit, ConstantData, InstrDisplayContext, Instruction, Label, OpArg, }; +use rustpython_parser_core::source_code::{LineNumber, SourceLocation}; #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct BlockIdx(pub u32); @@ -158,7 +158,7 @@ impl CodeInfo { arg_count, kwonlyarg_count, source_path, - first_line_number, + first_line_number: Some(first_line_number), obj_name, max_stackdepth, diff --git a/compiler/codegen/src/symboltable.rs b/compiler/codegen/src/symboltable.rs index 8cd06ea99e..bf63cc5df6 100644 --- a/compiler/codegen/src/symboltable.rs +++ b/compiler/codegen/src/symboltable.rs @@ -13,10 +13,7 @@ use crate::{ }; use bitflags::bitflags; use rustpython_ast as ast; -use rustpython_compiler_core::{ - source_code::{OneIndexed, SourceLocation}, - LineNumber, -}; +use rustpython_parser_core::source_code::{LineNumber, SourceLocation}; use std::{borrow::Cow, fmt}; /// Captures all symbols in the current scope, and has a list of sub-scopes in this scope. @@ -29,7 +26,7 @@ pub struct SymbolTable { pub typ: SymbolTableType, /// The line number in the source code where this symboltable begins. - pub line_number: LineNumber, + pub line_number: u32, // Return True if the block is a nested class or function pub is_nested: bool, @@ -43,7 +40,7 @@ pub struct SymbolTable { } impl SymbolTable { - fn new(name: String, typ: SymbolTableType, line_number: LineNumber, is_nested: bool) -> Self { + fn new(name: String, typ: SymbolTableType, line_number: u32, is_nested: bool) -> Self { SymbolTable { name, typ, @@ -99,6 +96,7 @@ pub enum SymbolScope { } bitflags! { + #[derive(Copy, Clone, Debug, PartialEq)] pub struct SymbolFlags: u16 { const REFERENCED = 0x001; const ASSIGNED = 0x002; @@ -121,7 +119,7 @@ bitflags! { /// return x // is_free_class /// ``` const FREE_CLASS = 0x100; - const BOUND = Self::ASSIGNED.bits | Self::PARAMETER.bits | Self::IMPORTED.bits | Self::ITER.bits; + const BOUND = Self::ASSIGNED.bits() | Self::PARAMETER.bits() | Self::IMPORTED.bits() | Self::ITER.bits(); } } @@ -172,7 +170,7 @@ impl SymbolTableError { error: CodegenErrorType::SyntaxError(self.error), location: self.location.map(|l| SourceLocation { row: l.row, - column: OneIndexed::MIN, + column: l.column, }), source_path, } @@ -565,7 +563,7 @@ impl SymbolTableBuilder { tables: vec![], future_annotations: false, }; - this.enter_scope("top", SymbolTableType::Module, LineNumber::MIN); + this.enter_scope("top", SymbolTableType::Module, 0); this } } @@ -578,7 +576,7 @@ impl SymbolTableBuilder { Ok(symbol_table) } - fn enter_scope(&mut self, name: &str, typ: SymbolTableType, line_number: LineNumber) { + fn enter_scope(&mut self, name: &str, typ: SymbolTableType, line_number: u32) { let is_nested = self .tables .last() @@ -697,7 +695,7 @@ impl SymbolTableBuilder { keywords, decorator_list, }) => { - self.enter_scope(name, SymbolTableType::Class, location.row); + self.enter_scope(name, SymbolTableType::Class, location.row.get()); let prev_class = std::mem::replace(&mut self.class_name, Some(name.to_owned())); self.register_name("__module__", SymbolUsage::Assigned, location)?; self.register_name("__qualname__", SymbolUsage::Assigned, location)?; @@ -1080,7 +1078,11 @@ impl SymbolTableBuilder { location: SourceLocation, ) -> SymbolTableResult { // Comprehensions are compiled as functions, so create a scope for them: - self.enter_scope(scope_name, SymbolTableType::Comprehension, location.row); + self.enter_scope( + scope_name, + SymbolTableType::Comprehension, + location.row.get(), + ); // Register the passed argument to the generator function as the name ".0" self.register_name(".0", SymbolUsage::Parameter, location)?; @@ -1136,7 +1138,7 @@ impl SymbolTableBuilder { self.scan_parameter_annotation(name)?; } - self.enter_scope(name, SymbolTableType::Function, line_number); + self.enter_scope(name, SymbolTableType::Function, line_number.get()); // Fill scope with parameter names: self.scan_parameters(&args.posonlyargs)?; diff --git a/compiler/core/Cargo.toml b/compiler/core/Cargo.toml new file mode 100644 index 0000000000..30d74d62df --- /dev/null +++ b/compiler/core/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "rustpython-compiler-core" +description = "RustPython specific bytecode." +version = "0.2.0" +authors = ["RustPython Team"] +edition = "2021" +repository = "https://github.com/RustPython/RustPython" +license = "MIT" + +[dependencies] +rustpython-parser-core = { workspace = true } + +bitflags = { workspace = true } +itertools = { workspace = true } +num-bigint = { workspace = true } +num-complex = { workspace = true } +serde = { version = "1.0.133", optional = true, default-features = false, features = ["derive"] } + +lz4_flex = "0.9.2" + diff --git a/compiler/core/src/bytecode.rs b/compiler/core/src/bytecode.rs new file mode 100644 index 0000000000..7241f97ff7 --- /dev/null +++ b/compiler/core/src/bytecode.rs @@ -0,0 +1,1477 @@ +//! Implement python as a virtual machine with bytecode. This module +//! implements bytecode structure. + +use bitflags::bitflags; +use itertools::Itertools; +use num_bigint::BigInt; +use num_complex::Complex64; +use rustpython_parser_core::source_code::{OneIndexed, SourceLocation}; +use std::marker::PhantomData; +use std::{collections::BTreeSet, fmt, hash, mem}; + +pub use rustpython_parser_core::ConversionFlag; + +pub trait Constant: Sized { + type Name: AsRef; + + /// Transforms the given Constant to a BorrowedConstant + fn borrow_constant(&self) -> BorrowedConstant; +} + +impl Constant for ConstantData { + type Name = String; + fn borrow_constant(&self) -> BorrowedConstant { + use BorrowedConstant::*; + match self { + ConstantData::Integer { value } => Integer { value }, + ConstantData::Float { value } => Float { value: *value }, + ConstantData::Complex { value } => Complex { value: *value }, + ConstantData::Boolean { value } => Boolean { value: *value }, + ConstantData::Str { value } => Str { value }, + ConstantData::Bytes { value } => Bytes { value }, + ConstantData::Code { code } => Code { code }, + ConstantData::Tuple { elements } => Tuple { elements }, + ConstantData::None => None, + ConstantData::Ellipsis => Ellipsis, + } + } +} + +/// A Constant Bag +pub trait ConstantBag: Sized + Copy { + type Constant: Constant; + fn make_constant(&self, constant: BorrowedConstant) -> Self::Constant; + fn make_int(&self, value: BigInt) -> Self::Constant; + fn make_tuple(&self, elements: impl Iterator) -> Self::Constant; + fn make_code(&self, code: CodeObject) -> Self::Constant; + fn make_name(&self, name: &str) -> ::Name; +} + +pub trait AsBag { + type Bag: ConstantBag; + #[allow(clippy::wrong_self_convention)] + fn as_bag(self) -> Self::Bag; +} + +impl AsBag for Bag { + type Bag = Self; + fn as_bag(self) -> Self { + self + } +} + +#[derive(Clone, Copy)] +pub struct BasicBag; + +impl ConstantBag for BasicBag { + type Constant = ConstantData; + fn make_constant(&self, constant: BorrowedConstant) -> Self::Constant { + constant.to_owned() + } + fn make_int(&self, value: BigInt) -> Self::Constant { + ConstantData::Integer { value } + } + fn make_tuple(&self, elements: impl Iterator) -> Self::Constant { + ConstantData::Tuple { + elements: elements.collect(), + } + } + fn make_code(&self, code: CodeObject) -> Self::Constant { + ConstantData::Code { + code: Box::new(code), + } + } + fn make_name(&self, name: &str) -> ::Name { + name.to_owned() + } +} + +/// Primary container of a single code object. Each python function has +/// a code object. Also a module has a code object. +#[derive(Clone)] +pub struct CodeObject { + pub instructions: Box<[CodeUnit]>, + pub locations: Box<[SourceLocation]>, + pub flags: CodeFlags, + pub posonlyarg_count: u32, + // Number of positional-only arguments + pub arg_count: u32, + pub kwonlyarg_count: u32, + pub source_path: C::Name, + pub first_line_number: Option, + pub max_stackdepth: u32, + pub obj_name: C::Name, + // Name of the object that created this code object + pub cell2arg: Option>, + pub constants: Box<[C]>, + pub names: Box<[C::Name]>, + pub varnames: Box<[C::Name]>, + pub cellvars: Box<[C::Name]>, + pub freevars: Box<[C::Name]>, +} + +bitflags! { + #[derive(Copy, Clone, Debug, PartialEq)] + pub struct CodeFlags: u16 { + const NEW_LOCALS = 0x01; + const IS_GENERATOR = 0x02; + const IS_COROUTINE = 0x04; + const HAS_VARARGS = 0x08; + const HAS_VARKEYWORDS = 0x10; + const IS_OPTIMIZED = 0x20; + } +} + +impl CodeFlags { + pub const NAME_MAPPING: &'static [(&'static str, CodeFlags)] = &[ + ("GENERATOR", CodeFlags::IS_GENERATOR), + ("COROUTINE", CodeFlags::IS_COROUTINE), + ( + "ASYNC_GENERATOR", + Self::from_bits_truncate(Self::IS_GENERATOR.bits() | Self::IS_COROUTINE.bits()), + ), + ("VARARGS", CodeFlags::HAS_VARARGS), + ("VARKEYWORDS", CodeFlags::HAS_VARKEYWORDS), + ]; +} + +/// an opcode argument that may be extended by a prior ExtendedArg +#[derive(Copy, Clone, PartialEq, Eq)] +#[repr(transparent)] +pub struct OpArgByte(pub u8); +impl OpArgByte { + pub const fn null() -> Self { + OpArgByte(0) + } +} +impl fmt::Debug for OpArgByte { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +/// a full 32-bit op_arg, including any possible ExtendedArg extension +#[derive(Copy, Clone, Debug)] +#[repr(transparent)] +pub struct OpArg(pub u32); +impl OpArg { + pub const fn null() -> Self { + OpArg(0) + } + + /// Returns how many CodeUnits a instruction with this op_arg will be encoded as + #[inline] + pub fn instr_size(self) -> usize { + (self.0 > 0xff) as usize + (self.0 > 0xff_ff) as usize + (self.0 > 0xff_ff_ff) as usize + 1 + } + + /// returns the arg split into any necessary ExtendedArg components (in big-endian order) and + /// the arg for the real opcode itself + #[inline(always)] + pub fn split(self) -> (impl ExactSizeIterator, OpArgByte) { + let mut it = self + .0 + .to_le_bytes() + .map(OpArgByte) + .into_iter() + .take(self.instr_size()); + let lo = it.next().unwrap(); + (it.rev(), lo) + } +} + +#[derive(Default, Copy, Clone)] +#[repr(transparent)] +pub struct OpArgState { + state: u32, +} + +impl OpArgState { + #[inline(always)] + pub fn get(&mut self, ins: CodeUnit) -> (Instruction, OpArg) { + let arg = self.extend(ins.arg); + if ins.op != Instruction::ExtendedArg { + self.reset(); + } + (ins.op, arg) + } + #[inline(always)] + pub fn extend(&mut self, arg: OpArgByte) -> OpArg { + self.state = self.state << 8 | u32::from(arg.0); + OpArg(self.state) + } + #[inline(always)] + pub fn reset(&mut self) { + self.state = 0 + } +} + +pub trait OpArgType: Copy { + fn from_op_arg(x: u32) -> Option; + fn to_op_arg(self) -> u32; +} + +impl OpArgType for u32 { + #[inline(always)] + fn from_op_arg(x: u32) -> Option { + Some(x) + } + #[inline(always)] + fn to_op_arg(self) -> u32 { + self + } +} + +impl OpArgType for bool { + #[inline(always)] + fn from_op_arg(x: u32) -> Option { + Some(x != 0) + } + #[inline(always)] + fn to_op_arg(self) -> u32 { + self as u32 + } +} + +macro_rules! op_arg_enum_impl { + (enum $name:ident { $($(#[$var_attr:meta])* $var:ident = $value:literal,)* }) => { + impl OpArgType for $name { + fn to_op_arg(self) -> u32 { + self as u32 + } + fn from_op_arg(x: u32) -> Option { + Some(match u8::try_from(x).ok()? { + $($value => Self::$var,)* + _ => return None, + }) + } + } + }; +} + +macro_rules! op_arg_enum { + ($(#[$attr:meta])* $vis:vis enum $name:ident { $($(#[$var_attr:meta])* $var:ident = $value:literal,)* }) => { + $(#[$attr])* + $vis enum $name { + $($(#[$var_attr])* $var = $value,)* + } + + op_arg_enum_impl!(enum $name { + $($(#[$var_attr])* $var = $value,)* + }); + }; +} + +#[derive(Copy, Clone)] +pub struct Arg(PhantomData); + +impl Arg { + #[inline] + pub fn marker() -> Self { + Arg(PhantomData) + } + #[inline] + pub fn new(arg: T) -> (Self, OpArg) { + (Self(PhantomData), OpArg(arg.to_op_arg())) + } + #[inline] + pub fn new_single(arg: T) -> (Self, OpArgByte) + where + T: Into, + { + (Self(PhantomData), OpArgByte(arg.into())) + } + #[inline(always)] + pub fn get(self, arg: OpArg) -> T { + self.try_get(arg).unwrap() + } + #[inline(always)] + pub fn try_get(self, arg: OpArg) -> Option { + T::from_op_arg(arg.0) + } + #[inline(always)] + /// # Safety + /// T::from_op_arg(self) must succeed + pub unsafe fn get_unchecked(self, arg: OpArg) -> T { + match T::from_op_arg(arg.0) { + Some(t) => t, + None => std::hint::unreachable_unchecked(), + } + } +} + +impl PartialEq for Arg { + fn eq(&self, _: &Self) -> bool { + true + } +} +impl Eq for Arg {} + +impl fmt::Debug for Arg { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Arg<{}>", std::any::type_name::()) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] +#[repr(transparent)] +// XXX: if you add a new instruction that stores a Label, make sure to add it in +// Instruction::label_arg +pub struct Label(pub u32); + +impl OpArgType for Label { + #[inline(always)] + fn from_op_arg(x: u32) -> Option { + Some(Label(x)) + } + #[inline(always)] + fn to_op_arg(self) -> u32 { + self.0 + } +} + +impl fmt::Display for Label { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.fmt(f) + } +} + +op_arg_enum_impl!( + enum ConversionFlag { + /// No conversion + None = 0, // CPython uses -1 but not pleasure for us + /// Converts by calling `str()`. + Str = b's', + /// Converts by calling `ascii()`. + Ascii = b'a', + /// Converts by calling `repr()`. + Repr = b'r', + } +); + +op_arg_enum!( + /// The kind of Raise that occurred. + #[derive(Copy, Clone, Debug, PartialEq, Eq)] + #[repr(u8)] + pub enum RaiseKind { + Reraise = 0, + Raise = 1, + RaiseCause = 2, + } +); + +pub type NameIdx = u32; + +/// A Single bytecode instruction. +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[repr(u8)] +pub enum Instruction { + /// Importing by name + ImportName { + idx: Arg, + }, + /// Importing without name + ImportNameless, + /// Import * + ImportStar, + /// from ... import ... + ImportFrom { + idx: Arg, + }, + LoadFast(Arg), + LoadNameAny(Arg), + LoadGlobal(Arg), + LoadDeref(Arg), + LoadClassDeref(Arg), + StoreFast(Arg), + StoreLocal(Arg), + StoreGlobal(Arg), + StoreDeref(Arg), + DeleteFast(Arg), + DeleteLocal(Arg), + DeleteGlobal(Arg), + DeleteDeref(Arg), + LoadClosure(Arg), + Subscript, + StoreSubscript, + DeleteSubscript, + StoreAttr { + idx: Arg, + }, + DeleteAttr { + idx: Arg, + }, + LoadConst { + /// index into constants vec + idx: Arg, + }, + UnaryOperation { + op: Arg, + }, + BinaryOperation { + op: Arg, + }, + BinaryOperationInplace { + op: Arg, + }, + LoadAttr { + idx: Arg, + }, + TestOperation { + op: Arg, + }, + CompareOperation { + op: Arg, + }, + Pop, + Rotate2, + Rotate3, + Duplicate, + Duplicate2, + GetIter, + Continue { + target: Arg