strings in variables can now be received by get_string_token
and inputs can be directly assigned to a variable
This commit is contained in:
@@ -60,7 +60,7 @@ pub fn token_parse(
|
|||||||
// Handle a character
|
// Handle a character
|
||||||
Some(tokenise::Token::Character(character_name)) =>
|
Some(tokenise::Token::Character(character_name)) =>
|
||||||
{
|
{
|
||||||
index = match character_parse::character_parse(index+1,tokens,character_name.clone(),characters,happening_queue)
|
index = match character_parse::character_parse(index+1,tokens,character_name.clone(),characters,happening_queue,&variables)
|
||||||
{
|
{
|
||||||
Ok(increment) => increment,
|
Ok(increment) => increment,
|
||||||
Err((err,increment)) =>
|
Err((err,increment)) =>
|
||||||
@@ -83,9 +83,9 @@ pub fn token_parse(
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Some(_) =>
|
Some(tok) =>
|
||||||
{
|
{
|
||||||
warn!("Unexpected token");
|
warn!("Unexpected token, at index {index}, expected character, keyword or identifier, got {tok:?}");
|
||||||
index += 1;
|
index += 1;
|
||||||
},
|
},
|
||||||
None => return Err("File unexpectedly reached termination point".to_string()),
|
None => return Err("File unexpectedly reached termination point".to_string()),
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ pub fn character_parse
|
|||||||
character_name: String,
|
character_name: String,
|
||||||
characters: &Arc<Mutex<HashMap::<String, character::Character>>>,
|
characters: &Arc<Mutex<HashMap::<String, character::Character>>>,
|
||||||
happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,
|
happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,
|
||||||
|
variables: &HashMap<String, tokenise::Value>,
|
||||||
) -> Result<usize,(String,usize)>
|
) -> Result<usize,(String,usize)>
|
||||||
{
|
{
|
||||||
let mut sum_index: usize = index;
|
let mut sum_index: usize = index;
|
||||||
@@ -45,7 +46,7 @@ pub fn character_parse
|
|||||||
{
|
{
|
||||||
info!("SAYS command with character {character_name}");
|
info!("SAYS command with character {character_name}");
|
||||||
sum_index += 1;
|
sum_index += 1;
|
||||||
let output = tokenise::get_string_token(tokens, sum_index)
|
let output = tokenise::get_string_token(tokens, sum_index, variables)
|
||||||
.map_err(|err| (err, sum_index))?;
|
.map_err(|err| (err, sum_index))?;
|
||||||
debug!("Saying {output}");
|
debug!("Saying {output}");
|
||||||
api::modify_data(happening_queue, "output".to_string(), output, character_name, vec![]);
|
api::modify_data(happening_queue, "output".to_string(), output, character_name, vec![]);
|
||||||
@@ -58,7 +59,7 @@ pub fn character_parse
|
|||||||
let feature = tokenise::get_keyword_token(tokens, sum_index)
|
let feature = tokenise::get_keyword_token(tokens, sum_index)
|
||||||
.map_err(|err| (err, sum_index))?;
|
.map_err(|err| (err, sum_index))?;
|
||||||
sum_index += 1;
|
sum_index += 1;
|
||||||
let string = tokenise::get_string_token(tokens, sum_index)
|
let string = tokenise::get_string_token(tokens, sum_index,variables)
|
||||||
.map_err(|err| (err, sum_index))?;
|
.map_err(|err| (err, sum_index))?;
|
||||||
info!("CHANGE command with character {character_name} feature {feature}");
|
info!("CHANGE command with character {character_name} feature {feature}");
|
||||||
let mut characters = characters.lock().unwrap_or_exit("Character Mutex was poisoned",3);
|
let mut characters = characters.lock().unwrap_or_exit("Character Mutex was poisoned",3);
|
||||||
|
|||||||
@@ -44,6 +44,10 @@ pub fn identifier_parse
|
|||||||
// Another thing like a choice or an input
|
// Another thing like a choice or an input
|
||||||
Err(_) =>
|
Err(_) =>
|
||||||
{
|
{
|
||||||
|
if operator != tokenise::Operator::Assignment // Only assignment is valid here
|
||||||
|
{
|
||||||
|
return Err((format!("Unexpected operator: {operator:?} at index {}",sum_index-1),sum_index + 1))
|
||||||
|
};
|
||||||
let keyword = tokenise::get_keyword_token(tokens,sum_index)
|
let keyword = tokenise::get_keyword_token(tokens,sum_index)
|
||||||
.map_err(|err| (err,sum_index))?;
|
.map_err(|err| (err,sum_index))?;
|
||||||
match keyword.to_lowercase().as_str()
|
match keyword.to_lowercase().as_str()
|
||||||
@@ -51,10 +55,24 @@ pub fn identifier_parse
|
|||||||
"choice" =>
|
"choice" =>
|
||||||
{
|
{
|
||||||
let choice: String;
|
let choice: String;
|
||||||
(sum_index, choice) = keyword_parse::choice_parse(tokens, index+1, happening_queue,rx)
|
(sum_index, choice) = keyword_parse::choice_parse(tokens, index+1, happening_queue,rx,variables)
|
||||||
.map_err(|err| (err,sum_index+1))?;
|
.map_err(|err| (err,sum_index+1))?;
|
||||||
variables.insert(identifier.to_owned(), tokenise::Value::String(choice));
|
variables.insert(identifier.to_owned(), tokenise::Value::String(choice));
|
||||||
}
|
},
|
||||||
|
"input" =>
|
||||||
|
{
|
||||||
|
api::modify_data(happening_queue, "input".to_string(), String::new(), String::new(), Vec::new());
|
||||||
|
let input = match rx.recv()
|
||||||
|
{
|
||||||
|
Ok((_,input)) => input,
|
||||||
|
Err(err) =>
|
||||||
|
{
|
||||||
|
warn!("Error receiving input from client, defaulting to choice \"\" {err}");
|
||||||
|
"".to_string()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
variables.insert(identifier.to_owned(), tokenise::Value::String(input));
|
||||||
|
},
|
||||||
_ =>
|
_ =>
|
||||||
{
|
{
|
||||||
warn!("Unexpected keyword {keyword}");
|
warn!("Unexpected keyword {keyword}");
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ pub fn keyword_parse(
|
|||||||
{
|
{
|
||||||
"choice" =>
|
"choice" =>
|
||||||
{
|
{
|
||||||
(index,_) = choice_parse(tokens, index, happening_queue, rx)?;
|
(index,_) = choice_parse(tokens, index, happening_queue, rx,variables)?;
|
||||||
},
|
},
|
||||||
"if" =>
|
"if" =>
|
||||||
{
|
{
|
||||||
@@ -114,6 +114,7 @@ pub fn choice_parse
|
|||||||
mut index: usize,
|
mut index: usize,
|
||||||
happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,
|
happening_queue: &Arc<Mutex<VecDeque<api::DataToSend>>>,
|
||||||
rx: &Receiver<(usize,String)>,
|
rx: &Receiver<(usize,String)>,
|
||||||
|
variables: &HashMap<String,tokenise::Value>,
|
||||||
)
|
)
|
||||||
-> Result<(usize,String), String>
|
-> Result<(usize,String), String>
|
||||||
{
|
{
|
||||||
@@ -125,7 +126,7 @@ pub fn choice_parse
|
|||||||
index += 1;
|
index += 1;
|
||||||
choices.push
|
choices.push
|
||||||
(
|
(
|
||||||
tokenise::get_string_token(tokens, index)?
|
tokenise::get_string_token(tokens, index,variables)?
|
||||||
);
|
);
|
||||||
index += 1;
|
index += 1;
|
||||||
choice_indeces.push(index+1);
|
choice_indeces.push(index+1);
|
||||||
|
|||||||
+30
-18
@@ -28,7 +28,7 @@ pub enum Bracket
|
|||||||
Closing,
|
Closing,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone,PartialEq)]
|
||||||
pub enum Operator
|
pub enum Operator
|
||||||
{
|
{
|
||||||
// Changing a value
|
// Changing a value
|
||||||
@@ -39,7 +39,7 @@ pub enum Operator
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Comparing a value
|
// Comparing a value
|
||||||
#[derive(Debug,Clone)]
|
#[derive(Debug,Clone,PartialEq)]
|
||||||
pub enum Comparison
|
pub enum Comparison
|
||||||
{
|
{
|
||||||
Equate,
|
Equate,
|
||||||
@@ -60,22 +60,6 @@ pub fn get_operator_token(tokens: &[Token], index: usize)
|
|||||||
None => Err("File unexpectedly reached termination point".to_string()),
|
None => Err("File unexpectedly reached termination point".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_string_token(tokens: &[Token], index: usize)
|
|
||||||
-> Result<String, String>
|
|
||||||
{
|
|
||||||
match tokens.get(index) {
|
|
||||||
Some(Token::Value(val)) =>
|
|
||||||
{
|
|
||||||
match val
|
|
||||||
{
|
|
||||||
Value::String(s) => Ok(s.clone()),
|
|
||||||
_ => Err("Unexpected value type".to_string()),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Some(tok) => Err(format!("Unexpected token at index {index}, expected string value, got {tok:?}")),
|
|
||||||
None => Err("File unexpectedly reached termination point".to_string()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn get_keyword_token(tokens: &[Token], index: usize)
|
pub fn get_keyword_token(tokens: &[Token], index: usize)
|
||||||
-> Result<String, String>
|
-> Result<String, String>
|
||||||
{
|
{
|
||||||
@@ -112,6 +96,34 @@ pub fn get_identifier_token(tokens: &[Token], index: usize)
|
|||||||
None => Err("File unexpectedly reached termination point".to_string()),
|
None => Err("File unexpectedly reached termination point".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn get_string_token(tokens: &[Token], index: usize, variables: &HashMap<String, Value>)
|
||||||
|
-> Result<String, String>
|
||||||
|
{
|
||||||
|
match tokens.get(index) {
|
||||||
|
// Either get string directly
|
||||||
|
Some(Token::Value(val)) =>
|
||||||
|
{
|
||||||
|
match val
|
||||||
|
{
|
||||||
|
Value::String(s) => Ok(s.clone()),
|
||||||
|
_ => Err("Unexpected value type".to_string()),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// Or get the string that the identifier references
|
||||||
|
Some(Token::Identifier(iden)) =>
|
||||||
|
{
|
||||||
|
let val = variables.get(iden)
|
||||||
|
.ok_or(format!("Variable {iden} not initialised"))?;
|
||||||
|
match val
|
||||||
|
{
|
||||||
|
Value::String(s) => Ok(s.clone()),
|
||||||
|
_ => Err("Unexpected value type".to_string()),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Some(tok) => Err(format!("Unexpected token at index {index}, expected string value, got {tok:?}")),
|
||||||
|
None => Err("File unexpectedly reached termination point".to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Tokenise the story to Vec<Token>
|
// Tokenise the story to Vec<Token>
|
||||||
// It can contain sub-objects (using recursive calls)
|
// It can contain sub-objects (using recursive calls)
|
||||||
|
|||||||
+3
-1
@@ -1,4 +1,6 @@
|
|||||||
@tim says "hello world, it's a good day"
|
$name = input
|
||||||
|
@tim says "hello"
|
||||||
|
@tim says $name
|
||||||
$x = 3
|
$x = 3
|
||||||
$x + 1
|
$x + 1
|
||||||
if $x == 4 {
|
if $x == 4 {
|
||||||
|
|||||||
@@ -44,6 +44,12 @@ def choice(choices):
|
|||||||
print("Invalid choice, defaulting to 0")
|
print("Invalid choice, defaulting to 0")
|
||||||
requests.post(api_url, json=choice);
|
requests.post(api_url, json=choice);
|
||||||
|
|
||||||
|
def get_input():
|
||||||
|
api_url = "http://localhost:20264/input"
|
||||||
|
input_string = input()
|
||||||
|
requests.post(api_url, json=input_string);
|
||||||
|
|
||||||
|
|
||||||
# Character outputs text to the user
|
# Character outputs text to the user
|
||||||
def output(character, text):
|
def output(character, text):
|
||||||
print(character["name"], "says")
|
print(character["name"], "says")
|
||||||
|
|||||||
Reference in New Issue
Block a user