read program from file; fmt, cleanup

pull/21/head
Ondřej Hruška 4 years ago
parent 01068ea001
commit c37408c7a2
  1. 340
      Cargo.lock
  2. 10
      crsn.json5
  3. 2
      crsn/src/asm/parse/parse_data.rs
  4. 3
      crsn/src/builtin/exec.rs
  5. 3
      crsn/src/runtime/fault.rs
  6. 5
      crsn/src/runtime/run_thread.rs
  7. 3
      crsn/src/utils/mod.rs
  8. 4
      crsn_arith/src/lib.rs
  9. 2
      crsn_arith/src/parse.rs
  10. 2
      crsn_screen/src/defs.rs
  11. 25
      crsn_screen/src/exec.rs
  12. 6
      crsn_screen/src/lib.rs
  13. 2
      crsn_stacks/src/exec.rs
  14. 12
      crsn_stacks/src/lib.rs
  15. 5
      crsn_stacks/src/parse.rs
  16. 15
      examples/factorial.csn
  17. 28
      examples/screen_bounce.csn
  18. 3
      launcher/Cargo.toml
  19. 186
      launcher/src/main.rs
  20. 14
      launcher/src/read_file.rs

340
Cargo.lock generated

@ -1,5 +1,23 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86"
dependencies = [
"memchr",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.32" version = "1.0.32"
@ -35,6 +53,39 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "block-buffer"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b"
dependencies = [
"block-padding",
"byte-tools",
"byteorder",
"generic-array",
]
[[package]]
name = "block-padding"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5"
dependencies = [
"byte-tools",
]
[[package]]
name = "byte-tools"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
[[package]]
name = "byteorder"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.60" version = "1.0.60"
@ -58,6 +109,36 @@ dependencies = [
"time", "time",
] ]
[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "clappconfig"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee3bdf54005ba6e218706edda1659879b0bc2c28d01e8f15a45b33ff52c89848"
dependencies = [
"anyhow",
"clap",
"env_logger",
"json5",
"log",
"serde",
"serde_json",
]
[[package]] [[package]]
name = "cloudabi" name = "cloudabi"
version = "0.0.3" version = "0.0.3"
@ -124,6 +205,15 @@ dependencies = [
"crsn", "crsn",
] ]
[[package]]
name = "digest"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5"
dependencies = [
"generic-array",
]
[[package]] [[package]]
name = "downcast-rs" name = "downcast-rs"
version = "1.2.0" version = "1.2.0"
@ -157,12 +247,40 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c53dc3a653e0f64081026e4bf048d48fec9fce90c66e8326ca7292df0ff2d82" checksum = "4c53dc3a653e0f64081026e4bf048d48fec9fce90c66e8326ca7292df0ff2d82"
[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "fake-simd"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
[[package]] [[package]]
name = "fuchsia-cprng" name = "fuchsia-cprng"
version = "0.1.1" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "generic-array"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec"
dependencies = [
"typenum",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.1.15" version = "0.1.15"
@ -183,6 +301,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error",
]
[[package]] [[package]]
name = "instant" name = "instant"
version = "0.1.7" version = "0.1.7"
@ -192,16 +319,35 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "itoa"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]]
name = "json5"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eb2522ff59fbfefb955e9bd44d04d5e5c2d0e8865bfc2c3d1ab3916183ef5ee"
dependencies = [
"pest",
"pest_derive",
"serde",
]
[[package]] [[package]]
name = "launcher" name = "launcher"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clappconfig",
"crsn", "crsn",
"crsn_arith", "crsn_arith",
"crsn_screen", "crsn_screen",
"crsn_stacks", "crsn_stacks",
"log", "log",
"serde",
"simple_logger", "simple_logger",
"thiserror", "thiserror",
] ]
@ -236,6 +382,12 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "maplit"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
[[package]] [[package]]
name = "maybe-uninit" name = "maybe-uninit"
version = "2.0.0" version = "2.0.0"
@ -337,6 +489,12 @@ version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad"
[[package]]
name = "opaque-debug"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
[[package]] [[package]]
name = "orbclient" name = "orbclient"
version = "0.3.27" version = "0.3.27"
@ -373,6 +531,49 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "pest"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53"
dependencies = [
"ucd-trie",
]
[[package]]
name = "pest_derive"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "833d1ae558dc601e9a60366421196a8d94bc0ac980476d0b67e1d0988d72b2d0"
dependencies = [
"pest",
"pest_generator",
]
[[package]]
name = "pest_generator"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99b8db626e31e5b81787b9783425769681b347011cc59471e33ea46d2ea0cf55"
dependencies = [
"pest",
"pest_meta",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "pest_meta"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54be6e404f5317079812fc8f9f5279de376d8856929e21c184ecf6bbd692a11d"
dependencies = [
"maplit",
"pest",
"sha-1",
]
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.18" version = "0.3.18"
@ -394,6 +595,12 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.7" version = "1.0.7"
@ -574,6 +781,24 @@ version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "regex"
version = "1.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"thread_local",
]
[[package]]
name = "regex-syntax"
version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
[[package]] [[package]]
name = "remove_dir_all" name = "remove_dir_all"
version = "0.5.3" version = "0.5.3"
@ -583,6 +808,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.1.0" version = "1.1.0"
@ -613,12 +844,55 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "serde"
version = "1.0.116"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96fe57af81d28386a513cbc6858332abc6117cfdb5999647c6444b8f43a370a5"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.116"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f630a6370fd8e457873b4bd2ffdae75408bc291ba72be773772a4c2a065d9ae8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "sexp" name = "sexp"
version = "1.1.4" version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8fa7ac9df84000b0238cf497cb2d3056bac2ff2a7d8cf179d2803b4b58571f" checksum = "9c8fa7ac9df84000b0238cf497cb2d3056bac2ff2a7d8cf179d2803b4b58571f"
[[package]]
name = "sha-1"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df"
dependencies = [
"block-buffer",
"digest",
"fake-simd",
"opaque-debug",
]
[[package]] [[package]]
name = "simple_logger" name = "simple_logger"
version = "1.9.0" version = "1.9.0"
@ -638,6 +912,12 @@ version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252"
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.41" version = "1.0.41"
@ -663,6 +943,24 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "termcolor"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.20" version = "1.0.20"
@ -683,6 +981,15 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "thread_local"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
dependencies = [
"lazy_static",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.1.44" version = "0.1.44"
@ -694,12 +1001,36 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "typenum"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
[[package]]
name = "ucd-trie"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c"
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.1" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.2" version = "0.9.2"
@ -810,6 +1141,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"

@ -0,0 +1,10 @@
/* Croissant runtime config */
{
"log": {
/* Log level: "off", "error", "warn", "info", "debug", "trace" */
"level": "trace",
/* Selective debug for modules:
* Mapping (Rust module path) -> (log level) */
"modules": {}
}
}

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::convert::TryFrom; use std::convert::TryFrom;
use sexp::{Atom, Sexp}; use sexp::{Atom, Sexp};
@ -6,7 +7,6 @@ use crate::asm::data::{DataDisp, Rd, RdData, reg, Wr, WrData};
use crate::asm::data::literal::Label; use crate::asm::data::literal::Label;
use crate::asm::error::CrsnError; use crate::asm::error::CrsnError;
use crate::asm::parse::sexp_expect::expect_string_atom; use crate::asm::parse::sexp_expect::expect_string_atom;
use std::borrow::Cow;
/// Parse a label /// Parse a label
pub fn parse_label(name: Option<Sexp>) -> Result<Label, CrsnError> { pub fn parse_label(name: Option<Sexp>) -> Result<Label, CrsnError> {

@ -1,3 +1,5 @@
use std::time::Duration;
use crate::asm::data::{Rd, RdData}; use crate::asm::data::{Rd, RdData};
use crate::asm::data::literal::Addr; use crate::asm::data::literal::Addr;
use crate::asm::instr::Cond; use crate::asm::instr::Cond;
@ -6,7 +8,6 @@ use crate::module::{EvalRes, OpTrait};
use crate::runtime::fault::Fault; use crate::runtime::fault::Fault;
use crate::runtime::frame::StackFrame; use crate::runtime::frame::StackFrame;
use crate::runtime::run_thread::{state::RunState, ThreadInfo}; use crate::runtime::run_thread::{state::RunState, ThreadInfo};
use std::time::Duration;
impl OpTrait for BuiltinOp { impl OpTrait for BuiltinOp {
fn execute(&self, info: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault> { fn execute(&self, info: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault> {

@ -1,8 +1,9 @@
use std::borrow::Cow;
use thiserror::Error; use thiserror::Error;
use crate::asm::data::literal::{DebugMsg, Label, RoutineName, Value}; use crate::asm::data::literal::{DebugMsg, Label, RoutineName, Value};
use crate::asm::data::Register; use crate::asm::data::Register;
use std::borrow::Cow;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum Fault { pub enum Fault {

@ -11,7 +11,6 @@ use crate::module::EvalRes;
use crate::runtime::fault::Fault; use crate::runtime::fault::Fault;
use crate::runtime::frame::StackFrame; use crate::runtime::frame::StackFrame;
use crate::runtime::program::Program; use crate::runtime::program::Program;
use std::pin::Pin;
#[derive(Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd)] #[derive(Clone, Copy, Eq, PartialEq, Debug, Ord, PartialOrd)]
pub struct ThreadToken(pub u32); pub struct ThreadToken(pub u32);
@ -67,7 +66,7 @@ impl RunThread {
} }
} }
Err(Fault::Halt) => { Err(Fault::Halt) => {
info!("Program ended."); // TODO implement coordinated shutdown when more threads are running!
break 'run; break 'run;
} }
Err(e) => { Err(e) => {
@ -76,5 +75,7 @@ impl RunThread {
} }
} }
} }
debug!("Thread ended.");
} }
} }

@ -1,3 +1,4 @@
pub use option_ext::UncheckedOptionExt;
mod option_ext; mod option_ext;
pub use option_ext::UncheckedOptionExt;

@ -1,7 +1,7 @@
use crsn::module::{CrsnExtension, ParseOpRes};
use crsn::asm::parse::arg_parser::ArgParser;
use crsn::asm::error::CrsnError; use crsn::asm::error::CrsnError;
use crsn::asm::instr::Op; use crsn::asm::instr::Op;
use crsn::asm::parse::arg_parser::ArgParser;
use crsn::module::{CrsnExtension, ParseOpRes};
mod defs; mod defs;
mod parse; mod parse;

@ -2,7 +2,7 @@ use crsn::asm::data::{Rd, Wr};
use crsn::asm::error::CrsnError; use crsn::asm::error::CrsnError;
use crsn::asm::instr::Op; use crsn::asm::instr::Op;
use crsn::asm::parse::arg_parser::ArgParser; use crsn::asm::parse::arg_parser::ArgParser;
use crsn::module::{ParseOpRes}; use crsn::module::ParseOpRes;
use crate::defs::ArithOp; use crate::defs::ArithOp;

@ -1,4 +1,4 @@
use crsn::asm::data::{Rd, RdObj, Wr}; use crsn::asm::data::Rd;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum ScreenOp { pub enum ScreenOp {

@ -1,18 +1,15 @@
use std::collections::{HashMap, VecDeque}; use std::ops::Sub;
use std::time::{Duration, Instant};
use minifb::{ScaleMode, Window, WindowOptions};
use crsn::asm::data::literal::Value; use crsn::asm::data::literal::Value;
use crsn::asm::instr::Cond; use crsn::asm::instr::Cond;
use crsn::module::{CrsnExtension, EvalRes, OpTrait}; use crsn::module::{EvalRes, OpTrait};
use crsn::runtime::fault::Fault; use crsn::runtime::fault::Fault;
use crsn::runtime::run_thread::{state::RunState, ThreadInfo}; use crsn::runtime::run_thread::{state::RunState, ThreadInfo};
use crate::defs::ScreenOp; use crate::defs::ScreenOp;
use minifb::{Window, WindowOptions, ScaleMode};
use parking_lot::Mutex;
use std::sync::Arc;
use std::time::{Instant, Duration};
use std::ops::Sub;
use crsn::utils::UncheckedOptionExt;
#[derive(Debug)] #[derive(Debug)]
struct Opts { struct Opts {
@ -27,7 +24,7 @@ struct Backend {
buffer: Vec<u32>, buffer: Vec<u32>,
window: Option<Window>, window: Option<Window>,
last_render: Instant, last_render: Instant,
opts: Opts opts: Opts,
} }
impl Default for Backend { impl Default for Backend {
@ -41,7 +38,7 @@ impl Default for Backend {
opts: Opts { opts: Opts {
auto_blit: true, auto_blit: true,
frame_rate: Duration::from_micros(16600), frame_rate: Duration::from_micros(16600),
} },
} }
} }
} }
@ -53,7 +50,7 @@ const OPT_FRAME_RATE : u64 = 2;
unsafe impl std::marker::Send for Backend {} unsafe impl std::marker::Send for Backend {}
impl OpTrait for ScreenOp { impl OpTrait for ScreenOp {
fn execute(&self, info: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault> { fn execute(&self, _info: &ThreadInfo, state: &mut RunState) -> Result<EvalRes, Fault> {
let eres = EvalRes::default(); let eres = EvalRes::default();
match self { match self {
ScreenOp::ScreenInit { width, height } => { ScreenOp::ScreenInit { width, height } => {
@ -68,7 +65,7 @@ impl OpTrait for ScreenOp {
match opt { match opt {
OPT_AUTO_BLIT => { OPT_AUTO_BLIT => {
backend.opts.auto_blit = (val != 0); backend.opts.auto_blit = val != 0;
debug!("Set auto blit to {:?}", backend.opts.auto_blit); debug!("Set auto blit to {:?}", backend.opts.auto_blit);
} }
OPT_FRAME_RATE => { OPT_FRAME_RATE => {
@ -104,7 +101,7 @@ impl OpTrait for ScreenOp {
} }
match &mut backend.window { match &mut backend.window {
Some(w) => { Some(_w) => {
let index = y * backend.width as u64 + x; let index = y * backend.width as u64 + x;
if index as usize > backend.buffer.len() { if index as usize > backend.buffer.len() {
warn!("Screen set pixel out of bounds"); warn!("Screen set pixel out of bounds");
@ -130,7 +127,7 @@ impl OpTrait for ScreenOp {
} }
fn init(state: &mut RunState, width: Value, height: Value) -> Result<(), Fault> { fn init(state: &mut RunState, width: Value, height: Value) -> Result<(), Fault> {
let mut window = Window::new( let window = Window::new(
"Croissant", "Croissant",
width as usize, width as usize,
height as usize, height as usize,

@ -1,10 +1,10 @@
#[macro_use] #[macro_use]
extern crate log; extern crate log;
use crsn::module::{CrsnExtension, ParseOpRes};
use crsn::asm::parse::arg_parser::ArgParser;
use crsn::asm::instr::Op;
use crsn::asm::error::CrsnError; use crsn::asm::error::CrsnError;
use crsn::asm::instr::Op;
use crsn::asm::parse::arg_parser::ArgParser;
use crsn::module::{CrsnExtension, ParseOpRes};
mod defs; mod defs;
mod parse; mod parse;

@ -2,7 +2,7 @@ use std::collections::{HashMap, VecDeque};
use crsn::asm::data::literal::Value; use crsn::asm::data::literal::Value;
use crsn::asm::instr::Cond; use crsn::asm::instr::Cond;
use crsn::module::{CrsnExtension, EvalRes, OpTrait}; use crsn::module::{EvalRes, OpTrait};
use crsn::runtime::fault::Fault; use crsn::runtime::fault::Fault;
use crsn::runtime::run_thread::{state::RunState, ThreadInfo}; use crsn::runtime::run_thread::{state::RunState, ThreadInfo};

@ -1,10 +1,10 @@
use crsn::module::{CrsnExtension, ParseOpRes};
use crsn::asm::parse::arg_parser::ArgParser;
use crsn::asm::instr::Op;
use crsn::asm::error::CrsnError;
use crsn::runtime::run_thread::{ThreadInfo, RunState};
use crsn::asm::data::literal::Value; use crsn::asm::data::literal::Value;
use crsn::asm::error::CrsnError;
use crsn::asm::instr::Op;
use crsn::asm::parse::arg_parser::ArgParser;
use crsn::module::{CrsnExtension, ParseOpRes};
use crsn::runtime::fault::Fault; use crsn::runtime::fault::Fault;
use crsn::runtime::run_thread::{RunState, ThreadInfo};
mod defs; mod defs;
mod parse; mod parse;
@ -28,7 +28,7 @@ impl CrsnExtension for StackOps {
parse::parse(keyword, args) parse::parse(keyword, args)
} }
fn drop_obj(&self, ti: &ThreadInfo, state: &mut RunState, handle: Value) -> Result<Option<()>, Fault> { fn drop_obj(&self, _ti: &ThreadInfo, state: &mut RunState, handle: Value) -> Result<Option<()>, Fault> {
exec::drop_obj(state, handle) exec::drop_obj(state, handle)
} }
} }

@ -1,10 +1,7 @@
use crsn::asm::error::CrsnError; use crsn::asm::error::CrsnError;
use crsn::asm::instr::Op; use crsn::asm::instr::Op;
use crsn::asm::parse::arg_parser::ArgParser; use crsn::asm::parse::arg_parser::ArgParser;
use crsn::module::{ParseOpRes}; use crsn::module::ParseOpRes;
use crate::defs::StackOp; use crate::defs::StackOp;

@ -0,0 +1,15 @@
(
(main
(call fac 5)
(ld r0 res0)
(halt)
)
(fac
(cmp arg0 2
(eq? (ret 2)))
(sub r0 arg0 1)
(call fac r0)
(mul r0 arg0 res0)
(ret r0)
)
)

@ -0,0 +1,28 @@
(
(main
(sc-init 800 600)
(sc-opt 1 0) ; auto blit
(sc-opt 2 60) ; frame rate
(ld r0 5) ; x
(ld r1 0) ; y
(ld r2 1) ; dx
(ld r3 1) ; dy
(ld r5 0x3300ff)
(:loop)
(add r5 0x000001)
(and r5 0xffffff)
(sc-px r0 r1 r5)
(add r0 r2)
(add r1 r3)
(cmp r0 799 (eq? (ld r2 -1)))
(cmp r0 0 (eq? (ld r2 1)))
(cmp r1 599 (eq? (ld r3 -1)))
(cmp r1 0 (eq? (ld r3 1)))
(sc-blit 0)
(j :loop)
)
)

@ -16,3 +16,6 @@ simple_logger = "1.9.0"
log = "0.4.11" log = "0.4.11"
thiserror = "1.0.20" thiserror = "1.0.20"
anyhow = "1.0.32" anyhow = "1.0.32"
clappconfig = "0.4.0"
serde = { version = "1.0.116", features = ["derive"] }

@ -1,109 +1,82 @@
#[macro_use] #[macro_use]
extern crate log; extern crate log;
use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use simple_logger::SimpleLogger; use clappconfig::{AppConfig, clap};
use clappconfig::clap::ArgMatches;
use serde::{Deserialize, Serialize};
use crsn::asm::data::literal::Addr; use crsn::asm::data::literal::Addr;
use crsn::runtime::run_thread::{RunThread, ThreadToken}; use crsn::runtime::run_thread::{RunThread, ThreadToken};
use crsn::runtime::run_thread;
use crsn_arith::ArithOps; use crsn_arith::ArithOps;
use crsn_stacks::StackOps;
use crsn_screen::ScreenOps; use crsn_screen::ScreenOps;
use std::time::Duration; use crsn_stacks::StackOps;
use log::LevelFilter;
mod read_file;
fn main() {
SimpleLogger::new() #[derive(Debug, Clone, Serialize, Deserialize)]
.with_level(LevelFilter::Warn) struct LogConfig {
.init().unwrap(); level: String,
modules: HashMap<String, String>,
// ;(dec r0 (z? (ret))) }
/*let program = "
( #[derive(Debug, Clone, Serialize, Deserialize)]
(main struct Config {
(ld r0 2) log: LogConfig,
(:again) #[serde(skip)]
(dec r0) program_file: String,
(jif nz :again) }
(fault \"that's it\")
) impl Default for Config {
) fn default() -> Self {
";*/ Self {
/* log: LogConfig {
let program = " level: "info".to_string(),
( modules: Default::default(),
(main },
(ld r0 2) program_file: "".to_string(),
(call add2x r0 15) }
(ld r0 res0) }
(fault \"that's it\") }
)
(add2x impl AppConfig for Config {
(ld r0 arg0) type Init = Self;
(ld r1 arg1)
(add r0 r1) fn logging(&self) -> &str {
(add r0 r1) &self.log.level
(ret r0) }
)
) fn logging_mod_levels(&self) -> Option<&HashMap<String, String>> {
";*/ Some(&self.log.modules)
}
/* let program = "
( /// Add args to later use in the `configure` method.
(main fn add_args<'a: 'b, 'b>(clap: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
(j :lbl) // Default impl
(ld _ arg0) clap.arg(
(fault) clap::Arg::with_name("input")
(:lbl) .value_name("FILE")
(call fac 5) .help("Program to run")
(ld r0 res0) .required_unless("default-config")
) .takes_value(true),
(fac
(cmp arg0 2
(eq? (ret 2)))
(ld r0 arg0)
(dec r0)
(call fac r0)
(ld r0 arg0)
(mul r0 res0)
(ret r0)
)
)
";*/
let program = "
(
(main
(sc-init 800 600)
(sc-opt 1 0) ; auto blit
(sc-opt 2 60) ; frame rate
(ld r0 5) ; x
(ld r1 0) ; y
(ld r2 1) ; dx
(ld r3 1) ; dy
(ld r5 0x3300ff)
(:loop)
(add r5 0x000001)
(and r5 0xffffff)
(sc-px r0 r1 r5)
(add r0 r2)
(add r1 r3)
(cmp r0 799 (eq? (ld r2 -1)))
(cmp r0 0 (eq? (ld r2 1)))
(cmp r1 599 (eq? (ld r3 -1)))
(cmp r1 0 (eq? (ld r3 1)))
(sc-blit 0)
(j :loop)
)
) )
"; }
fn configure(mut self, clap: &ArgMatches) -> anyhow::Result<Self> {
self.program_file = clap.value_of("input").unwrap().to_string();
Ok(self)
}
}
fn main() -> anyhow::Result<()> {
let config = Config::init("crsn", "crsn.json5", env!("CARGO_PKG_VERSION"))?;
info!("Loading {}", config.program_file);
let source = read_file::read_file(&config.program_file)?;
let parsers = Arc::new(vec![ let parsers = Arc::new(vec![
ArithOps::new(), ArithOps::new(),
@ -111,18 +84,23 @@ fn main() {
ScreenOps::new(), ScreenOps::new(),
]); ]);
let parsed = crsn::asm::assemble(program, parsers).unwrap(); let parsed = crsn::asm::assemble(&source, parsers)?;
let uniq = run_thread::new_uniq(); info!("Start runtime");
let mut thread1 = RunThread::new(ThreadToken(0), Some(uniq.clone()), parsed.clone(), Addr(0), &[]); let thread = RunThread::new(
//thread1.set_speed(Duration::from_millis(250)); ThreadToken(0),
None,
parsed.clone(),
Addr(0), // TODO find "main"?
&[], // TODO from CLI?
);
let _thread2 = RunThread::new(ThreadToken(1), Some(uniq), parsed.clone(), Addr(0), &[]); let a = thread.start();
// ...
let _ = a.join();
let a = thread1.start(); info!("Runtime shut down.");
//let b = thread2.start();
a.join().unwrap(); Ok(())
//b.join().unwrap();
} }

@ -0,0 +1,14 @@
use std::fs::File;
use std::io;
use std::io::Read;
use std::path::Path;
/// Read a file to string
pub fn read_file<P: AsRef<Path>>(path: P) -> io::Result<String> {
let path = path.as_ref();
let mut file = File::open(path)?;
let mut buf = String::new();
file.read_to_string(&mut buf)?;
Ok(buf)
}
Loading…
Cancel
Save