From 7d20869dc0602cecf4fc91d528d6cffc0ec7601f Mon Sep 17 00:00:00 2001 From: Windel Bouwman Date: Sun, 8 Jul 2018 21:03:25 +0200 Subject: [PATCH] Fix return value from function --- Cargo.lock | 109 -------------------------------- Cargo.toml | 1 - parser/src/python.lalrpop | 8 +++ py_code_object/compile_code.py | 2 +- py_code_object/test_snippets.py | 8 ++- src/compile_py_code_object.rs | 3 + src/main.rs | 6 +- vm/src/vm.rs | 17 ++--- 8 files changed, 26 insertions(+), 128 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2169e1c587..1cda8f9d9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -131,11 +131,6 @@ dependencies = [ "strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "dtoa" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "either" version = "1.5.0" @@ -215,11 +210,6 @@ dependencies = [ "either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "itoa" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "kernel32-sys" version = "0.2.2" @@ -330,19 +320,6 @@ dependencies = [ "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "num-traits" -version = "0.1.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-traits" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "ordermap" version = "0.3.5" @@ -395,28 +372,11 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "py_code_object" -version = "0.1.0" -dependencies = [ - "env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", - "rustpython_vm 0.1.0", - "serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "quick-error" version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "quote" -version = "0.3.15" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "quote" version = "0.5.2" @@ -525,7 +485,6 @@ dependencies = [ "clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "py_code_object 0.1.0", "rustpython_parser 0.0.1", "rustpython_vm 0.1.0", ] @@ -550,42 +509,11 @@ dependencies = [ "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "serde" -version = "0.8.23" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "serde" version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "serde_codegen" -version = "0.8.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_codegen_internals 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.10.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "serde_codegen_internals" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "syn 0.10.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "serde_derive" -version = "0.8.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "serde_codegen 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "serde_derive" version = "1.0.66" @@ -596,17 +524,6 @@ dependencies = [ "syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "serde_json" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "dtoa 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "itoa 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "sha2" version = "0.7.1" @@ -664,15 +581,6 @@ name = "strsim" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "syn" -version = "0.10.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "syn" version = "0.14.2" @@ -759,11 +667,6 @@ name = "unicode-width" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "unicode-xid" -version = "0.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "unicode-xid" version = "0.1.0" @@ -852,7 +755,6 @@ dependencies = [ "checksum diff 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "3c2b69f912779fbb121ceb775d74d51e915af17aaebc38d28a592843a2dd0a3a" "checksum digest 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "00a49051fef47a72c9623101b19bd71924a45cca838826caae3eaa4d00772603" "checksum docopt 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d8acd393692c503b168471874953a2531df0e9ab77d0b6bbc582395743300a4a" -"checksum dtoa 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0dd841b58510c9618291ffa448da2e4e0f699d984d436122372f446dae62263d" "checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0" "checksum ena 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cabe5a5078ac8c506d3e4430763b1ba9b609b1286913e7d08e581d1c2de9b7e5" "checksum env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "15abd780e45b3ea4f76b4e9a26ff4843258dd8a3eed2775a0e7368c2e7936c2f" @@ -864,7 +766,6 @@ dependencies = [ "checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" "checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e" "checksum itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)" = "f58856976b776fedd95533137617a02fb25719f40e7d9b01c7043cd65474f450" -"checksum itoa 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ae3088ea4baeceb0284ee9eea42f591226e6beaecf65373e41b38d95a1b8e7a1" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lalrpop 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ba451f7bd819b7afc99d4cf4bdcd5a4861e64955ba9680ac70df3a50625ad6cf" "checksum lalrpop-snap 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)" = "60013fd6be14317d43f47658b1440956a9ca48a9ed0257e0e0a59aac13e43a1f" @@ -876,8 +777,6 @@ dependencies = [ "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" "checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d" "checksum new_debug_unreachable 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0cdc457076c78ab54d5e0d6fa7c47981757f1e34dc39ff92787f217dede586c4" -"checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" -"checksum num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "775393e285254d2f5004596d69bb8bc1149754570dcc08cf30cabeba67955e28" "checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063" "checksum petgraph 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "8b30dc85588cd02b9b76f5e386535db546d21dc68506cff2abebee0b6445e8e4" "checksum phf_generator 0.7.22 (registry+https://github.com/rust-lang/crates.io-index)" = "05a079dd052e7b674d21cb31cbb6c05efd56a2cd2827db7692e2f1a507ebd998" @@ -886,7 +785,6 @@ dependencies = [ "checksum proc-macro2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1b06e2f335f48d24442b35a19df506a835fb3547bc3c06ef27340da9acf5cae7" "checksum proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "effdb53b25cdad54f8f48843d67398f7ef2e14f12c1b4cb4effc549a6462a4d6" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" -"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9949cfe66888ffe1d53e6ec9d9f3b70714083854be20fd5e271b232a017401e8" "checksum quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e44651a0dc4cdd99f71c83b561e221f714912d11af1a4dff0631f923d53af035" "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" @@ -899,13 +797,8 @@ dependencies = [ "checksum regex-syntax 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8e931c58b93d86f080c734bfd2bce7dd0079ae2331235818133c8be7f422e20e" "checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" "checksum regex-syntax 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8f1ac0f60d675cc6cf13a20ec076568254472551051ad5dd050364d70671bf6b" -"checksum serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9dad3f759919b92c3068c696c15c3d17238234498bbdcc80f2c469606f948ac8" "checksum serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "e9a2d9a9ac5120e0f768801ca2b58ad6eec929dc9d1d616c162f208869c2ce95" -"checksum serde_codegen 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "a4c5d8a33087d8984f9535daa62a6498a08f6476050b00ab9339dd847e4c25cc" -"checksum serde_codegen_internals 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "afad7924a009f859f380e4a2e3a509a845c2ac66435fcead74a4d983b21ae806" -"checksum serde_derive 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "ce44e5f4264b39e9d29c875357b7cc3ebdfb967bb9e22bfb5e44ffa400af5306" "checksum serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "0a90213fa7e0f5eac3f7afe2d5ff6b088af515052cc7303bd68c7e3b91a3fb79" -"checksum serde_json 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)" = "67f7d2e9edc3523a9c8ec8cd6ec481b3a27810aafee3e625d311febd3e656b4c" "checksum sha2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9eb6be24e4c23a84d7184280d2722f7f2731fcdd4a9d886efbfe4413e4847ea0" "checksum siphasher 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0df90a788073e8d0235a67e50441d47db7c8ad9debd91cbf43736a2a92d36537" "checksum string_cache 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "25d70109977172b127fe834e5449e5ab1740b9ba49fa18a2020f509174f25423" @@ -913,7 +806,6 @@ dependencies = [ "checksum string_cache_shared 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b1884d1bc09741d466d9b14e6d37ac89d6909cbcac41dd9ae982d4d063bbedfc" "checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" -"checksum syn 0.10.8 (registry+https://github.com/rust-lang/crates.io-index)" = "58fd09df59565db3399efbba34ba8a2fec1307511ebd245d0061ff9d42691673" "checksum syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c67da57e61ebc7b7b6fff56bb34440ca3a83db037320b0507af4c10368deda7d" "checksum term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "fa63644f74ce96fbeb9b794f66aff2a52d601cbd5e80f4b97123e3899f4570f1" "checksum termcolor 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "adc4587ead41bf016f11af03e55a624c06568b5a19db4e90fde573d805074f83" @@ -925,7 +817,6 @@ dependencies = [ "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" "checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" -"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" "checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" diff --git a/Cargo.toml b/Cargo.toml index bd49a911fc..47f25f107e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,3 @@ env_logger="0.5.10" clap = "2.31.2" rustpython_parser = {path = "parser"} rustpython_vm = {path = "vm"} -py_code_object = { path = "py_code_object" } diff --git a/parser/src/python.lalrpop b/parser/src/python.lalrpop index 782ffc7be2..4eede7296c 100644 --- a/parser/src/python.lalrpop +++ b/parser/src/python.lalrpop @@ -129,6 +129,10 @@ Comparison: ast::Expression = { CompOp: ast::Comparison = { "==" => ast::Comparison::Equal, "!=" => ast::Comparison::NotEqual, + "<" => ast::Comparison::Less, + "<=" => ast::Comparison::LessOrEqual, + ">" => ast::Comparison::Greater, + ">=" => ast::Comparison::GreaterOrEqual, }; Expression: ast::Expression = { @@ -248,6 +252,10 @@ extern { "-=" => lexer::Tok::MinusEqual, "==" => lexer::Tok::EqEqual, "!=" => lexer::Tok::NotEqual, + "<" => lexer::Tok::Less, + "<=" => lexer::Tok::LessEqual, + ">" => lexer::Tok::Greater, + ">=" => lexer::Tok::GreaterEqual, "assert" => lexer::Tok::Assert, "import" => lexer::Tok::Import, "break" => lexer::Tok::Break, diff --git a/py_code_object/compile_code.py b/py_code_object/compile_code.py index 684bf47cde..055b099ab0 100644 --- a/py_code_object/compile_code.py +++ b/py_code_object/compile_code.py @@ -48,7 +48,7 @@ def compile_to_bytecode(filename, out_file=None): code = compile(code, filename, "exec") - print(CodeEncoder().encode(code), file=out_file) + print(CodeEncoder(indent=4).encode(code), file=out_file) def main(): diff --git a/py_code_object/test_snippets.py b/py_code_object/test_snippets.py index 6b389db21c..382d765a26 100644 --- a/py_code_object/test_snippets.py +++ b/py_code_object/test_snippets.py @@ -85,9 +85,13 @@ def populate(cls): def get_test_files(): """ Retrieve test files """ - for filename in sorted(glob.iglob(os.path.join( + for filepath in sorted(glob.iglob(os.path.join( TEST_DIR, '*.py'))): - yield os.path.abspath(filename) + filename = os.path.split(filepath)[1] + if filename.startswith('xfail_'): + continue + + yield os.path.abspath(filepath) @populate diff --git a/src/compile_py_code_object.rs b/src/compile_py_code_object.rs index c93dc9b88a..8b3de24a1c 100644 --- a/src/compile_py_code_object.rs +++ b/src/compile_py_code_object.rs @@ -1,5 +1,8 @@ /* * Take an AST and transform it into py_code_object compatiable bytecode + * TODO: this file is obsoleted. It might be better to translate internal + * bytecode into cpython compatible bytecode. + * */ extern crate rustpython_parser; extern crate py_code_object; diff --git a/src/main.rs b/src/main.rs index 91ef5746ba..76949bb3dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,6 @@ extern crate log; extern crate rustpython_parser; extern crate rustpython_vm; -// mod compile_py_code_object; mod compile; use clap::{Arg, App}; use std::path::Path; @@ -43,7 +42,10 @@ fn main() { debug!("Code object: {:?}", bytecode); evaluate(bytecode); }, - Err(msg) => error!("Parsing went horribly wrong: {}", msg), + Err(msg) => { + error!("Parsing went horribly wrong: {}", msg); + std::process::exit(1); + }, } } diff --git a/vm/src/vm.rs b/vm/src/vm.rs index a045a4d7f0..3f4f20067a 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -132,14 +132,11 @@ impl VirtualMachine { fn run_frame(&mut self, mut frame: Frame) -> PyObjectRef { self.frames.push(frame); - //let mut why = None; - // Change this to a loop for jump - loop { - //while curr_frame.lasti < curr_frame.code.co_code.len() { + let value = loop { let result = self.execute_instruction(); match result { None => {}, - Some(ExeResult::Value { value }) => { break; }, + Some(ExeResult::Value { value }) => { break value; }, Some(ExeResult::Exception) => { panic!("TODO"); } } /*if curr_frame.blocks.len() > 0 { @@ -149,16 +146,10 @@ impl VirtualMachine { //if let Some(_) = why { // break; //} - } - - let return_value = { - //let curr_frame = self.frames.last_mut().unwrap(); - // self.curr_frame().return_value.clone() - // TODO - PyObject::Integer { value: 1 } }; + self.pop_frame(); - return_value.into_ref().clone() + value } fn subscript(&mut self, a: &PyObject, b: &PyObject) -> PyObjectRef {