uci parse basics

This commit is contained in:
c0repwn3r 2023-02-12 09:04:00 -05:00
commit b2ea4fd2f3
Signed by: core
GPG Key ID: FDBF740DADDCEECF
11 changed files with 226 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

8
.idea/.gitignore vendored Normal file
View File

@ -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

11
.idea/bamboo.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/bamboo.iml" filepath="$PROJECT_DIR$/.idea/bamboo.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

41
Cargo.lock generated Normal file
View File

@ -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",
]

9
Cargo.toml Normal file
View File

@ -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"

36
log.txt Normal file
View File

@ -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

38
src/main.rs Normal file
View File

@ -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);
}
}

62
src/uci.rs Normal file
View File

@ -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<Mutex<UCIState>>, cmd: String) -> Result<String, Box<dyn Error>> {
let cmd_split = cmd.trim().split(' ').collect::<Vec<&str>>();
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<Mutex<UCIState>>, 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<Mutex<UCIState>>) -> String {
loop {
if !state.lock().is_blocking { break; }
}
"readyok\n".to_string()
}