From b2ea4fd2f3d51690ea06b9a46bc769b180a382aa Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Sun, 12 Feb 2023 09:04:00 -0500 Subject: [PATCH] uci parse basics --- .gitignore | 1 + .idea/.gitignore | 8 ++++++ .idea/bamboo.iml | 11 +++++++++ .idea/misc.xml | 6 +++++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 +++++ Cargo.lock | 41 +++++++++++++++++++++++++++++++ Cargo.toml | 9 +++++++ log.txt | 36 +++++++++++++++++++++++++++ src/main.rs | 38 +++++++++++++++++++++++++++++ src/uci.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 226 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/bamboo.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 log.txt create mode 100644 src/main.rs create mode 100644 src/uci.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/bamboo.iml b/.idea/bamboo.iml new file mode 100644 index 0000000..c254557 --- /dev/null +++ b/.idea/bamboo.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3ce3588 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..49f2e36 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c2fea0b --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,41 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bamboo" +version = "0.1.0" +dependencies = [ + "spin", +] + +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "spin" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dccf47db1b41fa1573ed27ccf5e08e3ca771cb994f776668c5ebda893b248fc" +dependencies = [ + "lock_api", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..01f81eb --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "bamboo" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +spin = "0.9.5" \ No newline at end of file diff --git a/log.txt b/log.txt new file mode 100644 index 0000000..2c387b9 --- /dev/null +++ b/log.txt @@ -0,0 +1,36 @@ + +uci + +isready + +ucinewgame + +quit + +uci + +isready + +ucinewgame + +quit + +< uci + +> id name Bamboo 1.0 +id author c0repwn3r +uciok + +< isready + +> readyok + +< ucinewgame + +> +< position startpos moves e2e4 + +> Unrecognized command `position startpos moves e2e4`, consult the documentation for more information +< go + +> Unrecognized command `go`, consult the documentation for more information \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..3699e57 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,38 @@ +use std::{fs, io}; +use std::path::Path; +use std::sync::{Arc}; +use crate::uci::{uci_handle_command, UCIState}; +use spin::Mutex; + +pub mod uci; + +pub const ENGINE_NAME: &str = "Bamboo"; +pub const ENGINE_VERSION: &str = "1.0"; +pub const ENGINE_AUTHOR: &str = "c0repwn3r"; + +fn main() { + println!("{} {} by {}", ENGINE_NAME, ENGINE_VERSION, ENGINE_AUTHOR); + + let mut uci_state = UCIState::new(); + let mut uci_lock = Arc::new(Mutex::new(uci_state)); + + loop { + let mut cmdbuf = String::new(); + match io::stdin().read_line(&mut cmdbuf) { + Ok(_) => (), + Err(e) => { + println!("fatal error reading standard input: {}", e); + return; + } + } + + let resp = match uci_handle_command(uci_lock.clone(), cmdbuf.clone()) { + Ok(response) => response, + Err(e) => { + format!("error handling uci command: {}", e) + } + }; + + print!("{}", resp); + } +} diff --git a/src/uci.rs b/src/uci.rs new file mode 100644 index 0000000..4dcec17 --- /dev/null +++ b/src/uci.rs @@ -0,0 +1,62 @@ +use std::error::Error; +use crate::{ENGINE_AUTHOR, ENGINE_NAME, ENGINE_VERSION}; +use std::fmt::Write; +use std::fs; +use std::path::Path; +use std::sync::{Arc}; +use spin::Mutex; + +#[derive(Default)] +pub struct UCIState { + debug: bool, + is_blocking: bool +} + +impl UCIState { + pub fn new() -> Self { + Self::default() + } +} + +pub fn uci_handle_command(state: Arc>, cmd: String) -> Result> { + let cmd_split = cmd.trim().split(' ').collect::>(); + + match cmd_split[0] { + "uci" => Ok(uci_id()), + "isready" => Ok(uci_isready(state)), + "debug" => Ok(uci_debug(state, &cmd_split)), + "ucinewgame" => Ok("".to_string()), + _ => Ok(format!("Unrecognized command `{}`, consult the documentation for more information", cmd.trim())) + } +} + +pub fn uci_id() -> String { + let mut result = String::new(); + + writeln!(result, "id name {} {}", ENGINE_NAME, ENGINE_VERSION).unwrap(); + writeln!(result, "id author {}", ENGINE_AUTHOR).unwrap(); + writeln!(result, "uciok").unwrap(); + + result +} + +pub fn uci_debug(state: Arc>, cmd: &Vec<&str>) -> String { + if cmd.len() != 2 { + return format!("Unrecognized command `{}`, consult the documentation for more information", cmd.join(" ")) + } + + match cmd[1] { + "on" => state.lock().debug = true, + "off" => state.lock().debug = false, + _ => return format!("Unrecognized command `{}`, consult the documentation for more information", cmd.join(" ")) + }; + + "".to_string() +} + +pub fn uci_isready(state: Arc>) -> String { + loop { + if !state.lock().is_blocking { break; } + } + "readyok\n".to_string() +} \ No newline at end of file