Initial commit

* Parser, compiler, objects, and VM base implementations
* Stuff will print out, functions called, etc
* There are probably plenty of bugs but this is a good starting point to
  start committing changes into git

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2023-04-01 22:34:35 -07:00
commit bfe7ca33bd
26 changed files with 13853 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

765
Cargo.lock generated Normal file
View File

@@ -0,0 +1,765 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]]
name = "ascii-canvas"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8824ecca2e851cec16968d54a01dd372ef8f95b244fb84b84e70128be347c3c6"
dependencies = [
"term",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi 0.1.19",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bit-set"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e11e16035ea35e4e5997b393eacbf6f63983188f7a2ad25bfb13465f5ad59de"
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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cc"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "diff"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499"
[[package]]
name = "dirs-next"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1"
dependencies = [
"cfg-if",
"dirs-sys-next",
]
[[package]]
name = "dirs-sys-next"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "ena"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7402b94a93c24e742487327a7cd839dc9d36fec9de9fb25b09f2dae459f36c3"
dependencies = [
"log",
]
[[package]]
name = "errno"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d6a0976c999d473fe89ad888d5a284e55366d9dc9038b1ba2aa15128c4afa0"
dependencies = [
"errno-dragonfly",
"libc",
"windows-sys",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "fixedbitset"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "gc"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3edaac0f5832202ebc99520cb77c932248010c4645d20be1dc62d6579f5b3752"
dependencies = [
"gc_derive",
]
[[package]]
name = "gc_derive"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60df8444f094ff7885631d80e78eb7d88c3c2361a98daaabb06256e4500db941"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "getrandom"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "hermit-abi"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]]
name = "indexmap"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
dependencies = [
"cfg-if",
]
[[package]]
name = "io-lifetimes"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09270fd4fa1111bc614ed2246c7ef56239a3063d5be0d1ec3b589c505d400aeb"
dependencies = [
"hermit-abi 0.3.1",
"libc",
"windows-sys",
]
[[package]]
name = "is-terminal"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "256017f749ab3117e93acb91063009e1f1bb56d03965b14c2c8df4eb02c524d8"
dependencies = [
"hermit-abi 0.3.1",
"io-lifetimes",
"rustix",
"windows-sys",
]
[[package]]
name = "itertools"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"
dependencies = [
"either",
]
[[package]]
name = "lalrpop"
version = "0.19.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f34313ec00c2eb5c3c87ca6732ea02dcf3af99c3ff7a8fb622ffb99c9d860a87"
dependencies = [
"ascii-canvas",
"bit-set",
"diff",
"ena",
"is-terminal",
"itertools",
"lalrpop-util",
"petgraph",
"pico-args",
"regex",
"regex-syntax",
"string_cache",
"term",
"tiny-keccak",
"unicode-xid",
]
[[package]]
name = "lalrpop-util"
version = "0.19.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5c1f7869c94d214466c5fd432dfed12c379fd87786768d36455892d46b18edd"
dependencies = [
"regex",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
[[package]]
name = "linux-raw-sys"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f"
[[package]]
name = "lock_api"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b"
dependencies = [
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [
"cfg-if",
]
[[package]]
name = "memchr"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "new_debug_unreachable"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
[[package]]
name = "not-python"
version = "0.1.0"
dependencies = [
"gc",
"lalrpop",
"lalrpop-util",
"regex",
"structopt",
]
[[package]]
name = "parking_lot"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99"
dependencies = [
"instant",
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216"
dependencies = [
"cfg-if",
"instant",
"libc",
"redox_syscall",
"smallvec",
"winapi",
]
[[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_shared"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096"
dependencies = [
"siphasher",
]
[[package]]
name = "pico-args"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db8bcd96cb740d03149cbad5518db9fd87126a10ab519c011893b1754134c468"
[[package]]
name = "precomputed-hash"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"
dependencies = [
"bitflags",
]
[[package]]
name = "redox_users"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
dependencies = [
"getrandom",
"redox_syscall",
]
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "rustix"
version = "0.37.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e78cc525325c06b4a7ff02db283472f3c042b7ff0c391f96c6d5ac6f4f91b75"
dependencies = [
"bitflags",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "rustversion"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2cc38e8fa666e2de3c4aba7edeb5ffc5246c1c2ed0e3d17e560aeeba736b23f"
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "siphasher"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a86232ab60fa71287d7f2ddae4a7073f6b7aac33631c3015abb556f08c6d0a3e"
[[package]]
name = "smallvec"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
[[package]]
name = "string_cache"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33994d0838dc2d152d17a62adf608a869b5e846b65b389af7f3dbc1de45c5b26"
dependencies = [
"lazy_static",
"new_debug_unreachable",
"parking_lot",
"phf_shared",
"precomputed-hash",
]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "synstructure"
version = "0.12.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f"
dependencies = [
"proc-macro2",
"quote",
"syn",
"unicode-xid",
]
[[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 = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]]
name = "unicode-segmentation"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99"
[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"

14
Cargo.toml Normal file
View File

@@ -0,0 +1,14 @@
[package]
name = "not-python"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
lalrpop = "0.19.9"
[dependencies]
lalrpop-util = { version = "0.19.9", features = ["lexer"] }
gc = { version = "0.4", features = ["derive", "nightly"] }
regex = "1"
structopt = "0.3.26"

6
build.rs Normal file
View File

@@ -0,0 +1,6 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
lalrpop::Configuration::new()
.generate_in_source_tree()
.process()?;
Ok(())
}

3
examples/hello.np Normal file
View File

@@ -0,0 +1,3 @@
println(123);
println(None);
println("hell world");

138
src/compile/compiler.rs Normal file
View File

@@ -0,0 +1,138 @@
use crate::compile::{scope::GlobalScope, thunk::Thunk};
use crate::obj::prelude::*;
use crate::obj::ObjPtr;
use crate::syn::ast::*;
use crate::vm::inst::Inst;
pub struct Compiler {
scope: GlobalScope,
}
impl Compiler {
pub fn new() -> Self {
Self {
scope: Default::default(),
}
}
/// Gets the global scope.
pub fn scope(&self) -> &GlobalScope {
&self.scope
}
/// Gets the global scope, mutably.
pub fn scope_mut(&mut self) -> &mut GlobalScope {
&mut self.scope
}
/// Compile the given list of AST statements, returning the body.
pub fn compile_stmts(&mut self, stmts: &Vec<SpStmt>) -> Vec<Inst> {
self.gather_names(stmts);
let mut thunks = Vec::new();
for stmt in stmts {
thunks.push(self.emit_stmt(stmt));
}
Thunk::List(thunks).flatten()
}
fn emit_stmt(&mut self, stmt: &SpStmt) -> Thunk {
match stmt.inner() {
Stmt::Assign(_lhs_expr, _expr) => todo!(),
Stmt::Expr(expr) => {
let mut thunk = self.emit_expr(expr);
thunk.push(Inst::Pop);
thunk
}
Stmt::If { .. } => todo!(),
Stmt::Def { .. } => todo!(),
Stmt::Import { .. } => todo!(),
}
}
fn emit_expr(&mut self, expr: &SpExpr) -> Thunk {
match expr.inner() {
Expr::Call(expr, args) => {
let mut thunk = Vec::with_capacity(args.len() + 1);
thunk.push(self.emit_expr(expr));
thunk.extend(args.iter().map(|arg| self.emit_expr(arg)));
let mut thunk = Thunk::List(thunk);
thunk.push(Inst::Call(args.len()));
thunk
}
Expr::Get(expr, name) => {
let mut thunk = self.emit_expr(expr);
thunk.push(Inst::GetAttr(name.clone()));
thunk
}
Expr::Atom(atom) => self.emit_atom(atom),
}
}
fn emit_atom(&mut self, atom: &SpAtom) -> Thunk {
match atom.inner() {
Atom::Int(i) => Thunk::Block(vec![Inst::Push(ObjPtr::new(Int::new(*i)))]),
Atom::Float(_) => todo!(), // Thunk::Block(vec![Inst::Push(Gc::new(Float::new(*f)))]),
Atom::Str(s) => Thunk::Block(vec![Inst::Push(ObjPtr::new(Str::new(s.clone())))]),
Atom::Sym(_) => todo!(),
Atom::Name(n) => {
// Look up the symbol first. It may already exist.
// If it doesn't exist, create it. It may be created
// dynamically.
let name = self
.scope()
.lookup_scoped(n)
.unwrap_or_else(|| self.scope_mut().insert_local(n));
Thunk::Block(vec![Inst::Load(name)])
}
}
}
/// Gather all names from the list of statements and add them to the scope.
fn gather_names(&mut self, stmts: &Vec<SpStmt>) {
for stmt in stmts {
match stmt.inner() {
Stmt::Assign(lhs, _) => match lhs {
AssignLhs::Name(name) => {
self.scope_mut().insert_local(name);
}
AssignLhs::Complex(expr, _) => self.gather_expr_names(expr.inner()),
},
Stmt::Def { name, .. } => {
self.scope_mut().insert_local(name);
}
Stmt::Expr(expr) => {
// Get the names of the variables used in this expr
self.gather_expr_names(expr.inner());
}
Stmt::If { .. } => {
todo!()
}
Stmt::Import { .. } => {
todo!()
}
}
}
}
/// Gather the names used inside of an expression, recursively.
fn gather_expr_names(&mut self, expr: &Expr) {
match expr {
Expr::Call(fun, args) => {
self.gather_expr_names(fun.inner());
for arg in args {
self.gather_expr_names(arg.inner());
}
}
Expr::Get(head, _) => {
self.gather_expr_names(head.inner());
}
Expr::Atom(atom) => match atom.inner() {
Atom::Name(name) => {
self.scope_mut().insert_local(name);
}
// TODO symbols
_ => { /* no-op on non-names */ }
},
}
}
}

View File

@@ -0,0 +1,25 @@
use std::any::{Any, TypeId};
use std::collections::BTreeMap;
type ConstId = usize;
#[derive(Debug, Default)]
pub struct ConstTable {
tables: BTreeMap<TypeId, BTreeMap<ConstId, Box<dyn Any + 'static>>>,
}
impl ConstTable {
/// Registers a type of constant. This should be done before anything of a
/// given type is added.
pub fn register_type<T: Any + 'static>(&mut self) {
let type_id = TypeId::of::<T>();
assert!(
!self.tables.contains_key(&type_id),
"type {} already registered in const table",
std::any::type_name::<T>()
);
self.tables.insert(type_id, Default::default());
}
pub fn insert<T: Any + 'static>(&mut self) {}
}

4
src/compile/mod.rs Normal file
View File

@@ -0,0 +1,4 @@
pub mod compiler;
//pub mod const_table;
pub mod scope;
pub mod thunk;

79
src/compile/scope.rs Normal file
View File

@@ -0,0 +1,79 @@
use crate::vm::name::Name;
use std::collections::BTreeMap;
pub type LocalScope = BTreeMap<Name, String>;
#[derive(Debug, Default)]
pub struct GlobalScope {
names: Vec<String>,
scopes: Vec<LocalScope>,
}
impl GlobalScope {
pub fn push(&mut self, local: LocalScope) {
self.scopes.push(local);
}
pub fn pop(&mut self) -> Option<LocalScope> {
self.scopes.pop()
}
pub fn lookup_scoped(&self, name: &str) -> Option<Name> {
self.scopes
.iter()
.filter_map(|names| {
names
.iter()
.find(|(_, s)| s.as_str() == name)
.map(|(n, _)| *n)
})
.next()
}
pub fn lookup_local(&self, name: &str) -> Option<Name> {
self.scopes.last().and_then(|names| {
names
.iter()
.find(|(_, s)| s.as_str() == name)
.map(|(n, _)| *n)
})
}
pub fn insert_local(&mut self, name: impl ToString) -> Name {
let name = name.to_string();
if let Some(local) = self.lookup_local(&name) {
local
} else {
let next = Name(self.names.len());
self.names.push(name.clone());
let top = self.scopes.last_mut().expect("no scope");
top.insert(next, name);
next
}
}
}
#[test]
fn test_global_scope_insert() {
let mut scope = GlobalScope::default();
scope.push(Default::default());
assert_eq!(scope.insert_local("zero"), Name(0));
assert_eq!(scope.insert_local("zero"), Name(0));
assert_eq!(scope.insert_local("one"), Name(1));
assert_eq!(scope.insert_local("zero"), Name(0));
assert_eq!(scope.insert_local("one"), Name(1));
scope.push(Default::default());
assert_eq!(scope.insert_local("zero"), Name(2));
assert_eq!(scope.insert_local("one"), Name(3));
assert_eq!(scope.insert_local("zero"), Name(2));
assert_eq!(scope.insert_local("one"), Name(3));
scope.push(Default::default());
assert_eq!(scope.insert_local("zero"), Name(4));
assert_eq!(scope.insert_local("one"), Name(5));
scope.pop().unwrap();
assert_eq!(scope.insert_local("zero"), Name(2));
assert_eq!(scope.insert_local("one"), Name(3));
scope.pop().unwrap();
assert_eq!(scope.insert_local("zero"), Name(0));
assert_eq!(scope.insert_local("one"), Name(1));
}

52
src/compile/thunk.rs Normal file
View File

@@ -0,0 +1,52 @@
use crate::vm::inst::Inst;
pub enum Thunk {
Block(Vec<Inst>),
List(Vec<Thunk>),
/*
IfElse {
cond: Box<Thunk>,
if_true: Box<Thunk>,
if_false: Box<Thunk>,
},
*/
}
impl Thunk {
pub fn flatten(self) -> Vec<Inst> {
use Thunk::*;
match self {
Block(block) => block,
List(list) => list.into_iter().flat_map(|thunk| thunk.flatten()).collect(),
/*
IfElse { .. } => {
todo!("flatten if/else")
}
*/
}
}
pub fn push(&mut self, inst: Inst) {
use Thunk::*;
match self {
Block(block) => {
block.push(inst);
}
List(list) => {
if let Some(Block(last)) = list.last_mut() {
// if this is a block, then push the inst to the end of the
// block instead of creating a new element
last.push(inst);
} else {
// otherwise, push a new list block
list.push(Block(vec![inst]));
}
} /*
IfElse { .. } => {
// replace this with a List type, with *self as the first element
todo!()
}
*/
}
}
}

93
src/main.rs Normal file
View File

@@ -0,0 +1,93 @@
#![feature(lazy_cell)]
mod compile;
mod obj;
mod syn;
mod vm;
use std::io::Read;
use std::path::PathBuf;
use structopt::StructOpt;
use vm::inst::Inst;
use crate::obj::prelude::*;
use crate::vm::frame::{Frame, UserFrame};
#[derive(Debug, StructOpt)]
struct Opt {
#[structopt(name = "PATH", parse(from_os_str))]
path: Option<PathBuf>,
}
type Result<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
fn main() -> Result {
let opt = Opt::from_args();
let (text, _path) = if let Some(path) = opt.path.as_ref() {
(std::fs::read_to_string(path)?, path.display().to_string())
} else {
let mut input = String::new();
std::io::stdin().read_to_string(&mut input)?;
(input, "<stdin>".to_string())
};
let stmts = syn::parser::FileParser::new().parse(&text).unwrap();
//println!("{:#?}", stmts);
let builtin_names: &[(&str, ObjPtr)] = &[("None", ObjPtr::clone(&NONE_INST))];
let (mut exec_body, scope) = {
let mut compiler = compile::compiler::Compiler::new();
compiler.scope_mut().push(Default::default());
// add builtin function names
for (name, _ptr) in BUILTIN_FUNS {
compiler.scope_mut().insert_local(name);
}
for (name, _ptr) in builtin_names {
compiler.scope_mut().insert_local(name);
}
let exec_body = compiler.compile_stmts(&stmts);
let scope = compiler.scope_mut().pop().unwrap();
(exec_body, scope)
};
// XXX
// Push the return statement to the end of the body since this doesn't get
// added otherwise.
exec_body.push(Inst::Push(ObjPtr::new(Int::new(0))));
exec_body.push(Inst::Return);
//println!("{:#?}", exec_body);
let locals: Vec<_> = scope.keys().copied().collect();
let main_fun: ObjPtr<_> = UserFun::new(None, vec![], locals, exec_body).into();
// Add the __main__ value
let mut main_frame = UserFrame::new(main_fun.clone(), vec![], 0);
// Add builtin functions
for (name, ptr) in BUILTIN_FUNS {
let (opaque_name, _) = scope.iter().find(|(_, v)| v == name).unwrap();
main_frame.locals_mut().insert(
*opaque_name,
Some(ObjPtr::new(BuiltinFun::new(name.to_string(), *ptr))),
);
}
// Add builtin values
for (name, ptr) in builtin_names {
let (opaque_name, _) = scope.iter().find(|(_, v)| v == name).unwrap();
main_frame
.locals_mut()
.insert(*opaque_name, Some(ObjPtr::clone(ptr)));
}
//main_frame.locals_mut().insert(main_name, main_fun);
let mut vm = crate::vm::Vm::new();
vm.frames_mut().push(Frame::User(main_frame));
// Start 'er up
vm.resume();
Ok(())
}

137
src/obj/builtin_fun.rs Normal file
View File

@@ -0,0 +1,137 @@
use std::fmt;
use crate::vm::frame::Frame;
use crate::{obj::prelude::*, vm::Vm};
pub type BuiltinFunPtr = fn(BuiltinFunVm, Vec<ObjPtr>, BuiltinFunState) -> BuiltinFunResult;
#[macro_export]
macro_rules! builtin_fun {
($name:expr, $ptr:expr) => {
$crate::obj::builtin_fun::BuiltinFun::new($name.into(), $ptr)
};
}
#[macro_export]
macro_rules! builtin_fun_ptr {
($($tt:tt)+) => { $crate::obj::ObjPtr::new(builtin_fun!($($tt)+)) };
}
pub use builtin_fun;
pub use builtin_fun_ptr;
/// A result from a builtin function.
#[derive(Debug)]
pub enum BuiltinFunResult {
/// Return the given optional object.
Return(Option<ObjPtr>),
/// Yield function execution to the given stack frame.
Yield(Frame),
}
/// A builtin function may be resumed after it's done calling a user-defined
/// function. This enum determines what the function execution context is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BuiltinFunState {
/// This function is being called for the first time.
Begin,
/// This function is being resumed after yielding control to some other function.
Resume(usize),
}
impl BuiltinFunState {
pub fn next(&self) -> Self {
match self {
BuiltinFunState::Begin => BuiltinFunState::Resume(0),
BuiltinFunState::Resume(n) => BuiltinFunState::Resume(n + 1),
}
}
}
/// This is a wrapper structure around the VM that provides an API to a
/// `BuiltinFun` function call.
///
/// It has some extra checks that prevent the VM being used in a weird/unexpected way.
pub struct BuiltinFunVm<'vm> {
vm: &'vm mut Vm,
last_sp: usize,
}
impl<'vm> BuiltinFunVm<'vm> {
pub fn new(vm: &'vm mut Vm, last_sp: usize) -> Self {
Self { vm, last_sp }
}
pub fn stack(&self) -> &Vec<ObjPtr> {
self.vm.stack()
}
pub fn make_frame(&self, fun: ObjPtr, args: Vec<ObjPtr>) -> Option<Frame> {
self.vm.make_frame(fun, args)
}
pub fn push_stack(&mut self, value: ObjPtr) {
self.vm.push_stack(value);
}
pub fn pop_stack(&mut self) -> Option<ObjPtr> {
if self.stack().len() <= self.last_sp {
panic!("Tried to underflow stack in BuiltinFun call ");
}
self.pop_stack_unchecked()
}
pub fn pop_stack_unchecked(&mut self) -> Option<ObjPtr> {
self.vm.pop_stack()
}
}
pub struct BuiltinFun {
name: String,
fun: BuiltinFunPtr,
attrs: AttrsPtr,
}
impl fmt::Debug for BuiltinFun {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("BuiltinFun")
.field("name", &self.name)
.field(
"fun",
&format!(
"<builtin function at {:#016x}>",
(&self.fun as *const _) as usize
),
)
.field("attrs", &self.attrs)
.finish()
}
}
impl BuiltinFun {
pub fn new(name: String, fun: BuiltinFunPtr) -> Self {
Self {
name,
fun,
attrs: Default::default(),
}
}
pub fn fun(&self) -> BuiltinFunPtr {
self.fun
}
pub fn name(&self) -> &String {
&self.name
}
}
impl Obj for BuiltinFun {
fn attrs(&self) -> AttrsPtr {
AttrsPtr::clone(&self.attrs)
}
fn as_any(&self) -> &(dyn std::any::Any + 'static) {
self
}
}

55
src/obj/builtins.rs Normal file
View File

@@ -0,0 +1,55 @@
//! Builtin functions and objects.
use crate::obj::prelude::*;
pub const BUILTIN_FUNS: &[(&str, BuiltinFunPtr)] = &[
//
("println", println),
("print", print),
];
fn println(vm: BuiltinFunVm, args: Vec<ObjPtr>, state: BuiltinFunState) -> BuiltinFunResult {
do_print(vm, args, state, "\n")
}
fn print(vm: BuiltinFunVm, args: Vec<ObjPtr>, state: BuiltinFunState) -> BuiltinFunResult {
do_print(vm, args, state, "")
}
/// Shared logic of `print` and `println`.
/// It can append a string to the end of whatever is being printed.
fn do_print(
mut vm: BuiltinFunVm,
args: Vec<ObjPtr>,
state: BuiltinFunState,
end: &str,
) -> BuiltinFunResult {
const POST_CALL_STR: BuiltinFunState = BuiltinFunState::Resume(0);
if state == BuiltinFunState::Begin {
if args.len() == 0 {
println!();
return BuiltinFunResult::Return(None);
} else if args.len() != 1 {
todo!("Throw exception: arity error");
}
let arg = ObjPtr::clone(&args[0]);
let str_fun = arg.get("__str__").expect("No __str__ function?");
let frame = vm
.make_frame(str_fun, vec![arg])
.expect("__str__ attr was not a function?");
BuiltinFunResult::Yield(frame)
} else if state == POST_CALL_STR {
// Get return value, cast it as an `Any`, and then downcast to a string.
let result = vm.pop_stack().expect("No return value from __str__?");
let result_any = result.as_any();
if let Some(value) = result_any.downcast_ref::<Str>() {
print!("{}{}", value.value(), end);
} else {
todo!("Throw exception: __str__ function didn't return a Str?");
}
BuiltinFunResult::Return(None)
} else {
panic!("unexpected state: {state:?}");
}
}

53
src/obj/int.rs Normal file
View File

@@ -0,0 +1,53 @@
use crate::obj::prelude::*;
use std::{any::Any, sync::LazyLock};
pub type IntValue = i64;
static INT_ATTRS: LazyLock<AttrsPtr> = LazyLock::new(|| {
attrs_ptr! {
//
"__str__" => builtin_fun_ptr!("__str__", |_vm, args, _state| {
if args.len() != 1 {
todo!("Throw exception: arity error");
}
let arg_any = &args[0].as_any();
let obj = arg_any.downcast_ref::<Int>().expect("Int.__str__ did not receive Int?");
BuiltinFunResult::Return(Some(ObjPtr::new(Str::new(obj.value().to_string()))))
}),
}
});
#[derive(Debug, Clone)]
pub struct Int {
attrs: AttrsPtr,
value: IntValue,
}
impl Int {
pub fn new(value: IntValue) -> Self {
let attrs = AttrsPtr::clone(&INT_ATTRS);
// TODO intern
Int { attrs, value }
}
pub fn value(&self) -> IntValue {
self.value
}
}
impl Obj for Int {
fn attrs(&self) -> AttrsPtr {
AttrsPtr::clone(&self.attrs)
}
fn as_any(&self) -> &(dyn Any + 'static) {
self
}
}
impl ToString for Int {
fn to_string(&self) -> String {
format!("{}", self.value())
}
}

73
src/obj/mod.rs Normal file
View File

@@ -0,0 +1,73 @@
pub mod builtin_fun;
pub mod builtins;
pub mod int;
pub mod none;
pub mod str;
pub mod user_fun;
pub mod prelude {
// Module types
pub use super::{builtin_fun::*, int::*, none::*, str::*, user_fun::*};
// Local types
pub use super::{Attrs, AttrsPtr, Obj, ObjPtr};
// Macros
pub use super::{attrs, attrs_ptr};
// Builtins
pub use super::builtins::*;
}
use std::any::Any;
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::{Arc, RwLock};
// Macro definitions
#[macro_export]
macro_rules! attrs {
($($key:expr => $value:expr),* $(,)?) => {{
let mut __attrs = $crate::obj::Attrs::new();
$(
__attrs.insert($key.into(), $value);
)*
__attrs
}};
}
#[macro_export]
macro_rules! attrs_ptr {
($($tt:tt)*) => {{
$crate::obj::AttrsPtr::new(
std::sync::RwLock::new(
attrs! {
$($tt)*
}
)
)
}};
}
pub use attrs;
pub use attrs_ptr;
pub type ObjPtr<T = dyn Obj + 'static> = Arc<T>;
pub type Attrs = HashMap<String, ObjPtr>;
pub type AttrsPtr = Arc<RwLock<Attrs>>;
pub trait Obj: Debug + Sync + Send {
fn attrs(&self) -> AttrsPtr;
fn as_any(&self) -> &(dyn Any + 'static);
fn get(&self, key: &str) -> Option<ObjPtr> {
let attrs_ptr = self.attrs();
let attrs = attrs_ptr.read().unwrap();
attrs.get(key).cloned()
}
fn set(&mut self, key: String, value: ObjPtr) -> Option<ObjPtr> {
let attrs_ptr = self.attrs();
let mut attrs = attrs_ptr.write().unwrap();
attrs.insert(key, value)
}
}

39
src/obj/none.rs Normal file
View File

@@ -0,0 +1,39 @@
use crate::obj::prelude::*;
use std::{any::Any, sync::LazyLock};
pub static NONE_INST: LazyLock<ObjPtr> = LazyLock::new(|| ObjPtr::new(None::new()));
static NONE_STR: LazyLock<ObjPtr> = LazyLock::new(|| ObjPtr::new(Str::new("None")));
static NONE_ATTRS: LazyLock<AttrsPtr> = LazyLock::new(|| {
attrs_ptr! {
//
"__str__" => builtin_fun_ptr!("__str__", |_vm, args, _state| {
if args.len() != 1 {
todo!("Throw exception: arity error");
}
BuiltinFunResult::Return(Some(ObjPtr::clone(&NONE_STR)))
}),
}
});
#[derive(Debug)]
pub struct None {
attrs: AttrsPtr,
}
impl None {
fn new() -> Self {
let attrs = AttrsPtr::clone(&NONE_ATTRS);
Self { attrs }
}
}
impl Obj for None {
fn attrs(&self) -> AttrsPtr {
AttrsPtr::clone(&self.attrs)
}
fn as_any(&self) -> &(dyn Any + 'static) {
self
}
}

53
src/obj/str.rs Normal file
View File

@@ -0,0 +1,53 @@
use crate::obj::prelude::*;
use std::{any::Any, sync::LazyLock};
pub type StrValue = String;
static STR_ATTRS: LazyLock<AttrsPtr> = LazyLock::new(|| {
attrs_ptr! {
//
"__str__" => builtin_fun_ptr!("__str__", |_vm, mut args, _state| {
if args.len() != 1 {
todo!("Throw exception: arity error");
}
BuiltinFunResult::Return(Some(args.pop().unwrap()))
}),
}
});
#[derive(Debug, Clone)]
pub struct Str {
attrs: AttrsPtr,
value: StrValue,
}
impl Str {
pub fn new(value: impl Into<StrValue>) -> Self {
let attrs = AttrsPtr::clone(&STR_ATTRS);
// TODO intern
Str {
attrs,
value: value.into(),
}
}
pub fn value(&self) -> &StrValue {
&self.value
}
}
impl Obj for Str {
fn attrs(&self) -> AttrsPtr {
AttrsPtr::clone(&self.attrs)
}
fn as_any(&self) -> &(dyn Any + 'static) {
self
}
}
impl ToString for Str {
fn to_string(&self) -> String {
self.value().clone()
}
}

50
src/obj/user_fun.rs Normal file
View File

@@ -0,0 +1,50 @@
use crate::obj::prelude::*;
use crate::vm::{inst::Inst, name::Name};
#[derive(Debug)]
pub struct UserFun {
name: Option<Name>,
params: Vec<Name>,
locals: Vec<Name>,
body: Vec<Inst>,
attrs: AttrsPtr,
}
impl UserFun {
pub fn new(name: Option<Name>, params: Vec<Name>, locals: Vec<Name>, body: Vec<Inst>) -> Self {
Self {
name,
params,
locals,
body,
attrs: Default::default(),
}
}
/// Returns the name of this function, `None` if it is anonymous.
pub fn name(&self) -> Option<Name> {
self.name
}
pub fn params(&self) -> &Vec<Name> {
&self.params
}
pub fn locals(&self) -> &Vec<Name> {
&self.locals
}
pub fn body(&self) -> &Vec<Inst> {
&self.body
}
}
impl Obj for UserFun {
fn attrs(&self) -> AttrsPtr {
AttrsPtr::clone(&self.attrs)
}
fn as_any(&self) -> &(dyn std::any::Any + 'static) {
self
}
}

72
src/syn/ast.rs Normal file
View File

@@ -0,0 +1,72 @@
use crate::syn::Spanned;
#[derive(Debug, Clone)]
pub enum Stmt {
Assign(AssignLhs, Expr),
Expr(SpExpr),
If {
if_true: SpCondExpr,
elseif: Vec<SpCondExpr>,
else_expr: Option<SpExpr>,
},
Def {
name: String,
params: Vec<SpParam>,
body: Vec<SpStmt>,
},
Import {
target: String,
from: Option<String>,
},
}
#[derive(Debug, Clone)]
pub enum AssignLhs {
/// A simple assignment.
///
/// `x = foo`
Name(String),
/// A more complex assignment.
///
/// `x().bar = foo`
Complex(SpExpr, String),
}
pub type SpStmt = Spanned<Stmt>;
#[derive(Debug, Clone)]
pub struct CondExpr {
pub cond: SpExpr,
pub expr: SpExpr,
}
pub type SpCondExpr = Spanned<CondExpr>;
#[derive(Debug, Clone)]
pub struct Param {
pub name: String,
pub ty: Option<SpExpr>,
}
pub type SpParam = Spanned<Param>;
#[derive(Debug, Clone)]
pub enum Expr {
Call(Box<SpExpr>, Vec<SpExpr>),
Get(Box<SpExpr>, String),
Atom(SpAtom),
}
pub type SpExpr = Spanned<Expr>;
#[derive(Debug, Clone)]
pub enum Atom {
Int(i64),
Float(f64),
Str(String),
Sym(String),
Name(String),
}
pub type SpAtom = Spanned<Atom>;

69
src/syn/mod.rs Normal file
View File

@@ -0,0 +1,69 @@
// TODO - remove this when we start working on the syntax again
#![allow(unused)]
pub mod ast;
pub mod parser;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
start: usize,
end: usize,
}
impl Span {
pub fn new(start: usize, end: usize) -> Self {
Self { start, end }
}
pub fn start(&self) -> usize {
self.start
}
pub fn end(&self) -> usize {
self.end
}
}
#[derive(Debug, Clone)]
pub struct Spanned<T> {
span: Span,
inner: T,
}
impl<T> Spanned<T> {
pub fn new(span: Span, inner: T) -> Self {
Self { span, inner }
}
pub fn span(&self) -> &Span {
&self.span
}
pub fn inner(&self) -> &T {
&self.inner
}
pub fn into_inner(self) -> T {
self.inner
}
pub fn split(self) -> (Span, T) {
(self.span, self.inner)
}
}
pub(self) fn unescape(s: impl ToString) -> String {
const REPLACE: &[(&str, &str)] = &[
(r"\\", r"\"),
("\\n", "\n"),
("\\t", "\t"),
("\\r", "\r"),
("\\\"", "\""),
("\\\'", "\'"),
];
let mut s = s.to_string();
for (old, new) in REPLACE {
s = s.replace(old, new);
}
s
}

62
src/syn/parser.lalrpop Normal file
View File

@@ -0,0 +1,62 @@
use std::str::FromStr;
use crate::syn::{Span, Spanned, unescape};
use crate::syn::ast::*;
grammar;
Spanned<T>: Spanned<T> = {
<start:@L> <inner:T> <end:@R> => Spanned::new(Span::new(start, end), inner),
};
pub File: Vec<SpStmt> = {
<mut head:(<SpStmt> ";")*> <tail:SpStmt?> => {
if let Some(tail) = tail {
head.push(tail);
}
head
},
}
pub SpStmt = Spanned<Stmt>;
pub Stmt: Stmt = {
<SpExpr> => Stmt::Expr(<>),
}
pub SpExpr = Spanned<Expr>;
pub Expr: Expr = {
<fun:SpExpr> "(" <mut argv:(<SpExpr> ",")*> <last:SpExpr?> ")" => {
let argv = match last {
Some(expr) => {
argv.push(expr);
argv
}
None => argv,
};
Expr::Call(Box::new(fun), argv)
},
<atom:SpAtom> => Expr::Atom(atom),
"(" <expr:Expr> ")" => expr,
};
pub SpAtom = Spanned<Atom>;
pub Atom: Atom = {
<s:Str> => Atom::Str(s),
<n:Int> => Atom::Int(n),
<name:Name> => Atom::Name(name),
};
Int: i64 = <s:r"[0-9]+"> => i64::from_str(s).unwrap();
Str: String = {
// NOTE - doing 1 .. s.len() - 1 is safe because these are ascii characters
// and will always be one byte
<s:r#""([^"\\]|\\[ntr"'\\])*""#> => unescape(&s[1..s.len() - 1]),
<s:r#"'([^"\\]|\\[ntr"'\\])*'"#> => unescape(&s[1..s.len() - 1]),
}
Name: String = {
<s:r"[a-zA-Z_][a-zA-Z0-9_]*"> => s.to_string(),
}

11540
src/syn/parser.rs Normal file

File diff suppressed because it is too large Load Diff

190
src/vm/frame.rs Normal file
View File

@@ -0,0 +1,190 @@
use std::collections::BTreeMap;
use crate::{
obj::{
builtin_fun::{BuiltinFun, BuiltinFunState},
user_fun::UserFun,
ObjPtr,
},
vm::name::Name,
};
#[derive(Debug)]
pub enum Frame {
Builtin(BuiltinFrame),
User(UserFrame),
}
impl Frame {
pub fn last_sp(&self) -> usize {
match self {
Frame::Builtin(frame) => frame.last_sp(),
Frame::User(frame) => frame.last_sp(),
}
}
}
impl From<UserFrame> for Frame {
fn from(other: UserFrame) -> Self {
Self::User(other)
}
}
impl From<BuiltinFrame> for Frame {
fn from(other: BuiltinFrame) -> Self {
Self::Builtin(other)
}
}
#[derive(Debug)]
pub struct BuiltinFrame {
builtin_fun: ObjPtr,
args: Vec<ObjPtr>,
last_sp: usize,
state: BuiltinFunState,
}
impl BuiltinFrame {
pub fn new(builtin_fun: ObjPtr, args: Vec<ObjPtr>, last_sp: usize) -> Self {
assert!(
builtin_fun.as_any().is::<BuiltinFun>(),
"BuiltinFrame::new got a new object pointer that isn't a BuiltinFun, instead got {builtin_fun:?}"
);
BuiltinFrame {
builtin_fun,
args,
last_sp,
state: BuiltinFunState::Begin,
}
}
pub fn builtin_fun(&self) -> &BuiltinFun {
// TODO
// when downcast_ref_unchecked
if let Some(builtin_fun) = self.builtin_fun.as_any().downcast_ref() {
builtin_fun
} else {
// using panic! here so we get formatting
panic!(
"BuiltinFrame::new got a new object that isn't a BuiltinFun, instead got {:?}",
self.builtin_fun,
);
}
}
pub fn args(&self) -> &Vec<ObjPtr> {
&self.args
}
pub fn state(&self) -> BuiltinFunState {
self.state
}
pub fn set_state(&mut self, state: BuiltinFunState) {
self.state = state;
}
pub fn last_sp(&self) -> usize {
self.last_sp
}
}
pub type FrameLocals = BTreeMap<Name, Option<ObjPtr>>;
#[derive(Debug)]
pub struct UserFrame {
user_fun: ObjPtr,
last_sp: usize,
pc: usize,
locals: FrameLocals,
}
impl UserFrame {
pub fn new(user_fun: ObjPtr, args: Vec<ObjPtr>, last_sp: usize) -> Self {
assert!(
user_fun.as_any().is::<UserFun>(),
"UserFrame::new got a new object pointer that isn't a UserFun, instead got {user_fun:?}"
);
let user_fun_obj: &UserFun = if let Some(user_fun) = user_fun.as_any().downcast_ref() {
user_fun
} else {
// using panic! here so we get formatting
panic!(
"UserFrame::new got a new object that isn't a UserFun, instead got {:?}",
user_fun,
)
};
// Check arity
if user_fun_obj.params().len() != args.len() {
todo!(
"Throw exception: arity error ({} args passed, {} expected)",
args.len(),
user_fun_obj.params().len()
);
}
// Create the locals
let mut locals = FrameLocals::new();
for local in user_fun_obj.locals().iter().copied() {
locals.insert(local, None);
}
// Set the args
for (param, arg) in user_fun_obj.params().iter().copied().zip(args) {
assert!(
locals.contains_key(&param),
"param {:?} not found in locals for function with name {:?}",
param,
user_fun_obj.name()
);
locals.insert(param, Some(arg));
}
Self {
user_fun,
last_sp,
pc: 0,
locals,
}
}
pub fn user_fun(&self) -> &UserFun {
// TODO
// when downcast_ref_unchecked
if let Some(user_fun) = self.user_fun.as_any().downcast_ref() {
user_fun
} else {
// using panic! here so we get formatting
panic!(
"UserFrame::new got a new object that isn't a UserFun, instead got {:?}",
self.user_fun,
);
}
}
pub fn last_sp(&self) -> usize {
self.last_sp
}
pub fn locals(&self) -> &FrameLocals {
&self.locals
}
pub fn locals_mut(&mut self) -> &mut FrameLocals {
&mut self.locals
}
pub fn get_inst(&self) -> &crate::vm::inst::Inst {
&self.user_fun().body()[self.pc]
}
pub fn pc(&self) -> usize {
self.pc
}
pub fn set_pc(&mut self, pc: usize) {
self.pc = pc;
}
}

32
src/vm/inst.rs Normal file
View File

@@ -0,0 +1,32 @@
use crate::obj::ObjPtr;
use crate::vm::name::Name;
#[derive(Debug, Clone)]
pub enum Inst {
/// Pushes an object to the stack.
Push(ObjPtr),
/// Loads a local value in a name and pushes it to the stack.
Load(Name),
/// Pops the top item off of the stack and stores it in a name.
Store(Name),
/// Get an attribute from the top item of the stack.
GetAttr(String),
/// Pops the top item off of the stack, discarding it.
Pop,
/// Pops the top N function arguments off the stack, and attempt to call the
/// next stack item.
Call(usize),
/// Exits the current function stack frame and returns control to the
/// calling function.
///
/// This will take the last value on top of the stack (the return value),
/// resize the stack to its last size (pre-call), and push the return value
/// back to the top of the stack.
Return,
}

246
src/vm/mod.rs Normal file
View File

@@ -0,0 +1,246 @@
pub mod frame;
pub mod inst;
pub mod name;
use crate::obj::prelude::*;
use crate::vm::frame::{BuiltinFrame, UserFrame};
use crate::vm::{frame::Frame, inst::Inst, name::Name};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum State {
Running,
Stopped,
}
/// The virtual machine structure.
#[derive(Debug)]
pub struct Vm {
frames: Vec<Frame>,
state: State,
stack: Vec<ObjPtr>,
}
impl Vm {
pub fn new() -> Self {
Self {
frames: Default::default(),
state: State::Stopped,
stack: Default::default(),
}
}
pub fn state(&self) -> State {
self.state
}
/// Gets all stack frames.
pub fn frames(&self) -> &Vec<Frame> {
&self.frames
}
/// Gets all stack frames, mutably.
pub fn frames_mut(&mut self) -> &mut Vec<Frame> {
&mut self.frames
}
/// Gets the currently active stack frame.
pub fn active_frame(&self) -> Option<&Frame> {
self.frames().last()
}
/// Gets the currently active stack frame.
pub fn active_frame_mut(&mut self) -> Option<&mut Frame> {
self.frames_mut().last_mut()
}
/// Get the stack.
pub fn stack(&self) -> &Vec<ObjPtr> {
&self.stack
}
/// Get the stack, mutably.
pub fn stack_mut(&mut self) -> &mut Vec<ObjPtr> {
&mut self.stack
}
/// Push a value to the stack.
pub fn push_stack(&mut self, value: ObjPtr) {
self.stack_mut().push(value);
}
/// Pop a value from the stack.
pub fn pop_stack(&mut self) -> Option<ObjPtr> {
self.stack_mut().pop()
}
/// Loads a given name in the currently executing context.
pub fn load(&self, name: Name) -> Option<ObjPtr> {
// Search up the stack frame for local variable with the given name
for frame in self.frames() {
match frame {
Frame::User(frame) => {
// Figure out if this frame should hold this local
if let Some(local) = frame.locals().get(&name) {
// Return the found value (regardless of whether it's set or not).
return local.clone();
}
}
Frame::Builtin(_frame) => continue,
}
}
None
}
pub fn make_frame(&self, fun: ObjPtr, args: Vec<ObjPtr>) -> Option<Frame> {
let fun_any = fun.as_any();
if fun_any.is::<BuiltinFun>() {
Some(Frame::Builtin(BuiltinFrame::new(
fun,
args,
self.stack().len(),
)))
} else if fun_any.is::<UserFun>() {
Some(Frame::User(UserFrame::new(fun, args, self.stack().len())))
} else {
todo!("Throw exception: Tried to call un-callable object: {fun:?}");
}
}
/// Execute an instruction, using the given program counter.
///
/// Returns the address of the next instruction to load.
fn exec_inst(&mut self) {
// don't like this, but this is the consequence of not using a pipeline I think
let frame = match self.active_frame_mut() {
Some(Frame::User(frame)) => frame,
other => panic!("Expected user frame, got {other:?} instead"),
};
let inst = frame.get_inst();
let pc = frame.pc() + 1;
let mut next_frame: Option<Frame> = None;
let mut pop_frame = false;
match &inst {
Inst::Push(value) => {
let value = value.clone();
self.push_stack(value)
}
Inst::Load(name) => {
let name = *name;
if let Some(value) = self.load(name) {
self.push_stack(value);
} else {
todo!("Throw exception: Could not load name {name:?} (is it set?)");
}
}
Inst::Store(_name) => todo!(),
Inst::GetAttr(_attr) => todo!(),
Inst::Pop => {
self
.pop_stack()
.expect("Inst::Pop executed, but there was no value on top of the stack. This is most likely a bug.");
}
Inst::Call(argc) => {
let argc = *argc;
let stack = self.stack_mut();
let split_at = stack.len() - argc;
let args = stack.split_off(split_at);
assert_eq!(
args.len(),
argc,
"Not enough arguments popped in Inst::Call"
);
let fun = stack.pop().expect("No function pointer in Inst::Call");
next_frame = self.make_frame(fun, args);
}
Inst::Return => {
let last_sp = frame.last_sp();
let return_value = self
.pop_stack()
.expect("Inst::Return expected a value on top of the stack, but it was empty.");
let _discard = self.stack_mut().split_at(last_sp);
self.push_stack(return_value);
pop_frame = true;
}
}
// Same as the top - this time update the program counter
let frame = match self.active_frame_mut() {
Some(Frame::User(frame)) => frame,
other => panic!("Expected user frame, got {other:?} instead"),
};
frame.set_pc(pc);
// This *shouldn't* happen. But it'd be bad if it did.
assert!(!(next_frame.is_some() && pop_frame));
// Set up the next stack frame if that's required
if let Some(frame) = next_frame {
self.frames_mut().push(frame);
}
// Pop the stack frame
if pop_frame {
self.frames_mut().pop();
}
}
/// Resumes execution of the currently running function.
pub fn resume(&mut self) {
self.state = State::Running;
while self.state() == State::Running {
// Get the currently running function, figure out what to do next.
match self.active_frame() {
Some(Frame::User(_)) => {
self.exec_inst();
}
Some(Frame::Builtin(frame)) => {
let state = frame.state();
let builtin_fun = frame.builtin_fun();
let fun_ptr = builtin_fun.fun();
let fun_name = builtin_fun.name().clone();
let args = frame.args().clone();
let last_sp = frame.last_sp();
// frame is implicitly dropped here
let result = fun_ptr(BuiltinFunVm::new(self, last_sp), args, state);
match result {
BuiltinFunResult::Return(maybe_value) => {
let frame = self
.frames_mut()
.pop()
.expect("Active frame modified by BuiltinFun");
// Reset the stack size
let _remain = self.stack_mut().split_at(frame.last_sp());
// Push the value to the stack
if let Some(value) = maybe_value {
self.stack_mut().push(value);
} else {
self.stack_mut().push(ObjPtr::clone(&NONE_INST));
}
}
BuiltinFunResult::Yield(yield_to) => {
let frame = if let Some(Frame::Builtin(frame)) = self.active_frame_mut()
{
frame
} else {
panic!("Active frame modified by BuiltinFun {fun_name:?}");
};
// update builtin function's state for when we resume execution
let state = frame.state();
frame.set_state(state.next());
// yield control to the next function
self.frames_mut().push(yield_to);
}
}
}
None => {
self.state = State::Stopped;
}
}
}
}
}

2
src/vm/name.rs Normal file
View File

@@ -0,0 +1,2 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Name(pub usize);