Browse Source

fix some errors, first test

ag-yep
Taylor Bockman 7 years ago
parent
commit
4f8a5ef694
  1. 6
      README.md
  2. 1
      src/lib.rs
  3. 7
      src/parser.rs
  4. 32
      tests/parser.rs

6
README.md

@ -98,7 +98,11 @@ EXAMPLE HERE WITH LOOPS IN THREADS AND BLOCKING AND WHATEVER
Notice how in our STDIN thread we are calling `Engine::parse` and the handling the output using a match statement Notice how in our STDIN thread we are calling `Engine::parse` and the handling the output using a match statement
depending on what kind of token it was. You are responsible for obeying the commands from this thread, the UCI library depending on what kind of token it was. You are responsible for obeying the commands from this thread, the UCI library
just makes it convenient to work with. just makes it convenient to work with. Here is an example of handling a parsed command:
```
EXAMPLE OF HANDLING A PARSED COMMAND AND SENDING BACK SOMETHING
```
At this point the engine will send a series of `setoption` commands to you in your STDIN thread. We can use the At this point the engine will send a series of `setoption` commands to you in your STDIN thread. We can use the
`Engine::parse` function to get these: `Engine::parse` function to get these:

1
src/lib.rs

@ -9,6 +9,7 @@
pub mod commands; pub mod commands;
pub mod options; pub mod options;
pub mod parser;
use std::io::{BufRead, Write}; use std::io::{BufRead, Write};
use std::vec; use std::vec;

7
src/parser.rs

@ -4,6 +4,7 @@ use std::collections::HashMap;
use commands; use commands;
/// Token represents a parsable token in the string sent via STDIN from the GUI /// Token represents a parsable token in the string sent via STDIN from the GUI
#[derive(Debug)]
pub enum Token { pub enum Token {
UCI, UCI,
DEBUG, DEBUG,
@ -18,6 +19,7 @@ pub enum Token {
QUIT, QUIT,
} }
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub enum CommandValue { pub enum CommandValue {
Int(i32), Int(i32),
Float(f64), Float(f64),
@ -39,6 +41,7 @@ pub struct Parser {}
/// ///
/// The user is still responsible for knowing what to look for in the hashmap but this structure makes it far /// The user is still responsible for knowing what to look for in the hashmap but this structure makes it far
/// easier to work with commands. Some commands don't have arguments, in that case the args will be None. /// easier to work with commands. Some commands don't have arguments, in that case the args will be None.
#[derive(Debug)]
pub struct TokenResult { pub struct TokenResult {
token: Token, token: Token,
args: Option<HashMap<&'static str, CommandValue>>, args: Option<HashMap<&'static str, CommandValue>>,
@ -61,6 +64,10 @@ impl Parser {
// TODO: Parse things based on the token, probably need a special function for each token based on a switch // TODO: Parse things based on the token, probably need a special function for each token based on a switch
// of tokens[0]. // of tokens[0].
// remove this when it's working
Err("lol")
} }
} }

32
tests/parser.rs

@ -4,3 +4,35 @@
// a cakewalk // a cakewalk
// //
// // Also remember to fix the constant thing noted in the lib.rs for uci.... lol // // Also remember to fix the constant thing noted in the lib.rs for uci.... lol
//
extern crate uci;
use uci::parser;
#[test]
fn parse_setoption() {
let s = "setoption name Hash value 32\n";
let expected = parser::TokenResult {
token: parser::Token::SETOPTION,
args: Some(
[
("name", parser::CommandValue::Text(String::from("Hash"))),
("value", parser::CommandValue::Int(32)),
].iter().cloned().collect()),
};
let p = parser::Parser::new();
match p.parse(s) {
Ok(r) => {
assert_eq!(r, expected);
}
Err(_) => panic!("failed to parse setoption as expected")
}
}
#[test]
fn bad_parse() {
assert_eq!(true, false);
}

Loading…
Cancel
Save