generated from ElnuDev/rust-project
cargo fmt
This commit is contained in:
parent
4666368b0a
commit
cd01bb6567
9 changed files with 116 additions and 75 deletions
|
@ -1,16 +1,19 @@
|
||||||
use std::{path::PathBuf, sync::{Arc, Mutex}};
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use std::{
|
||||||
|
path::PathBuf,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
|
||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
use renrs::State;
|
use renrs::State;
|
||||||
|
|
||||||
use sfml::{
|
|
||||||
graphics::{Color, RenderTarget, RenderWindow, RectangleShape, Transformable},
|
|
||||||
system::Vector2f,
|
|
||||||
window::{Event, mouse, Key, Style},
|
|
||||||
};
|
|
||||||
use egui_sfml::SfEgui;
|
use egui_sfml::SfEgui;
|
||||||
|
use sfml::{
|
||||||
|
graphics::{Color, RectangleShape, RenderTarget, RenderWindow, Transformable},
|
||||||
|
system::Vector2f,
|
||||||
|
window::{mouse, Event, Key, Style},
|
||||||
|
};
|
||||||
|
|
||||||
const WIDTH: f32 = 800.0;
|
const WIDTH: f32 = 800.0;
|
||||||
const HEIGHT: f32 = 600.0;
|
const HEIGHT: f32 = 600.0;
|
||||||
|
@ -121,8 +124,17 @@ impl App {
|
||||||
while window.is_open() {
|
while window.is_open() {
|
||||||
while let Some(event) = window.poll_event() {
|
while let Some(event) = window.poll_event() {
|
||||||
match event {
|
match event {
|
||||||
Event::Closed | Event::KeyPressed { code: Key::Escape, .. } => return,
|
Event::Closed
|
||||||
Event::MouseButtonPressed { button: mouse::Button::Left, .. } | Event::KeyPressed { code: Key::Space, .. } => self.handle_interaction(),
|
| Event::KeyPressed {
|
||||||
|
code: Key::Escape, ..
|
||||||
|
} => return,
|
||||||
|
Event::MouseButtonPressed {
|
||||||
|
button: mouse::Button::Left,
|
||||||
|
..
|
||||||
|
}
|
||||||
|
| Event::KeyPressed {
|
||||||
|
code: Key::Space, ..
|
||||||
|
} => self.handle_interaction(),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -137,15 +149,18 @@ impl App {
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.show(ctx, |ui| {
|
.show(ctx, |ui| {
|
||||||
if ctx.input(|i|
|
if ctx.input(|i| {
|
||||||
i.key_pressed(Key::Space)
|
i.key_pressed(Key::Space)
|
||||||
|| i.pointer.button_clicked(PointerButton::Primary)
|
|| i.pointer.button_clicked(PointerButton::Primary)
|
||||||
) {
|
}) {
|
||||||
self.handle_interaction();
|
self.handle_interaction();
|
||||||
}
|
}
|
||||||
ui.with_layout(egui::Layout::left_to_right(Align::Max), |ui| ui.heading(&self.text[0..*self.chars.lock().unwrap()]));
|
ui.with_layout(egui::Layout::left_to_right(Align::Max), |ui| {
|
||||||
|
ui.heading(&self.text[0..*self.chars.lock().unwrap()])
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}).unwrap();
|
})
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let Vector2f { x, y } = shape.position();
|
let Vector2f { x, y } = shape.position();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{path::PathBuf, rc::Rc, cell::RefCell};
|
use std::{cell::RefCell, path::PathBuf, rc::Rc};
|
||||||
|
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
use parser::block::CommandBlock;
|
use parser::block::CommandBlock;
|
||||||
|
@ -30,4 +30,4 @@ impl State {
|
||||||
fn next_command(&mut self) -> Option<Command> {
|
fn next_command(&mut self) -> Option<Command> {
|
||||||
self.script.borrow_mut().next()
|
self.script.borrow_mut().next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
use std::{rc::Rc, cell::RefCell, collections::HashMap};
|
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||||
|
|
||||||
use super::{command::{Command, parse_command}, Pair, token::Token, Rule, control::{parse_control, Control}};
|
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
command::{parse_command, Command},
|
||||||
|
control::{parse_control, Control},
|
||||||
|
token::Token,
|
||||||
|
Pair, Rule,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct CommandBlock {
|
pub struct CommandBlock {
|
||||||
|
@ -29,28 +33,22 @@ impl CommandBlock {
|
||||||
result = Some(command.clone());
|
result = Some(command.clone());
|
||||||
next += 1;
|
next += 1;
|
||||||
break;
|
break;
|
||||||
},
|
|
||||||
BlockElement::Block(block) => {
|
|
||||||
match block.borrow_mut().next() {
|
|
||||||
Some(command) => {
|
|
||||||
result = Some(command.clone());
|
|
||||||
break;
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
next += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
BlockElement::Block(block) => match block.borrow_mut().next() {
|
||||||
|
Some(command) => {
|
||||||
|
result = Some(command.clone());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
next += 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
self.next = if count >= next {
|
self.next = if count >= next { Some(next) } else { None };
|
||||||
Some(next)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evaluate_control(&self) -> bool {
|
fn evaluate_control(&self) -> bool {
|
||||||
use Control::*;
|
use Control::*;
|
||||||
self.control.as_ref().map_or(true, |control| match control {
|
self.control.as_ref().map_or(true, |control| match control {
|
||||||
|
@ -69,7 +67,7 @@ impl CommandBlock {
|
||||||
} else {
|
} else {
|
||||||
variables
|
variables
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
None => HashMap::new(),
|
None => HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,12 +91,19 @@ enum BlockElement {
|
||||||
Block(Rc<RefCell<CommandBlock>>),
|
Block(Rc<RefCell<CommandBlock>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_block(pair: Pair, definitions: Option<Rc<RefCell<HashMap<String, Token>>>>) -> Rc<RefCell<CommandBlock>> {
|
pub fn parse_block(
|
||||||
|
pair: Pair,
|
||||||
|
definitions: Option<Rc<RefCell<HashMap<String, Token>>>>,
|
||||||
|
) -> Rc<RefCell<CommandBlock>> {
|
||||||
//let variables: HashMap<String, Token> = HashMap::new();
|
//let variables: HashMap<String, Token> = HashMap::new();
|
||||||
let is_root = definitions.is_none();
|
let is_root = definitions.is_none();
|
||||||
let definitions = definitions.unwrap_or(Rc::new(RefCell::new(HashMap::new())));
|
let definitions = definitions.unwrap_or(Rc::new(RefCell::new(HashMap::new())));
|
||||||
let block_rc = Rc::new(RefCell::new(CommandBlock {
|
let block_rc = Rc::new(RefCell::new(CommandBlock {
|
||||||
variables: if is_root { Some(definitions.clone()) } else { None },
|
variables: if is_root {
|
||||||
|
Some(definitions.clone())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}));
|
}));
|
||||||
{
|
{
|
||||||
|
@ -114,7 +119,7 @@ pub fn parse_block(pair: Pair, definitions: Option<Rc<RefCell<HashMap<String, To
|
||||||
}
|
}
|
||||||
control = Some(parse_control(pair));
|
control = Some(parse_control(pair));
|
||||||
continue;
|
continue;
|
||||||
},
|
}
|
||||||
Rule::Line => BlockElement::Command(match parse_command(pair) {
|
Rule::Line => BlockElement::Command(match parse_command(pair) {
|
||||||
Command::Define { variable, value } => {
|
Command::Define { variable, value } => {
|
||||||
let mut value = value;
|
let mut value = value;
|
||||||
|
@ -137,7 +142,8 @@ pub fn parse_block(pair: Pair, definitions: Option<Rc<RefCell<HashMap<String, To
|
||||||
if let Some(control) = control.as_ref() {
|
if let Some(control) = control.as_ref() {
|
||||||
if control.has_variable_scope() {
|
if control.has_variable_scope() {
|
||||||
// TODO: Sublock-scoped variables
|
// TODO: Sublock-scoped variables
|
||||||
subblock.variables = Some(Rc::new(RefCell::new(HashMap::new())));
|
subblock.variables =
|
||||||
|
Some(Rc::new(RefCell::new(HashMap::new())));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
panic!("block should have control");
|
panic!("block should have control");
|
||||||
|
@ -159,4 +165,4 @@ pub fn parse_block(pair: Pair, definitions: Option<Rc<RefCell<HashMap<String, To
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
block_rc
|
block_rc
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
use std::{rc::Rc, cell::RefCell};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
use super::{token::Token, block::CommandBlock, event::Event, Pair, utils::{describe_line, parse_line}};
|
use super::{
|
||||||
|
block::CommandBlock,
|
||||||
|
event::Event,
|
||||||
|
token::Token,
|
||||||
|
utils::{describe_line, parse_line},
|
||||||
|
Pair,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
@ -13,14 +19,17 @@ pub enum Command {
|
||||||
impl Command {
|
impl Command {
|
||||||
pub fn execute(&self, context: &Rc<RefCell<CommandBlock>>) -> Option<Event> {
|
pub fn execute(&self, context: &Rc<RefCell<CommandBlock>>) -> Option<Event> {
|
||||||
use Command::*;
|
use Command::*;
|
||||||
Some(match self {
|
Some(
|
||||||
Say { name, text } => Event::Say {
|
match self {
|
||||||
name: name.clone(),
|
Say { name, text } => Event::Say {
|
||||||
text: text.clone(),
|
name: name.clone(),
|
||||||
},
|
text: text.clone(),
|
||||||
Define { .. } => panic!("define command should not be executed at runtime!"),
|
},
|
||||||
Eat { .. } => return None,
|
Define { .. } => panic!("define command should not be executed at runtime!"),
|
||||||
}.process(context))
|
Eat { .. } => return None,
|
||||||
|
}
|
||||||
|
.process(context),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,10 +51,14 @@ pub fn parse_command(pair: Pair) -> Command {
|
||||||
name: Some(name.to_owned()),
|
name: Some(name.to_owned()),
|
||||||
text: text.to_owned(),
|
text: text.to_owned(),
|
||||||
},
|
},
|
||||||
[Keyword(keyword), Keyword(variable), Keyword(equals), value] if keyword.eq("define") && equals.eq("=") => Define {
|
[Keyword(keyword), Keyword(variable), Keyword(equals), value]
|
||||||
variable: variable.to_owned(),
|
if keyword.eq("define") && equals.eq("=") =>
|
||||||
value: value.clone(),
|
{
|
||||||
},
|
Define {
|
||||||
|
variable: variable.to_owned(),
|
||||||
|
value: value.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
[Keyword(keyword), Str(food), tail @ ..] if keyword.eq("eat") => Eat {
|
[Keyword(keyword), Str(food), tail @ ..] if keyword.eq("eat") => Eat {
|
||||||
food: food.to_owned(),
|
food: food.to_owned(),
|
||||||
politely: match tail {
|
politely: match tail {
|
||||||
|
@ -55,4 +68,4 @@ pub fn parse_command(pair: Pair) -> Command {
|
||||||
},
|
},
|
||||||
_ => unknown!(),
|
_ => unknown!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
use super::{token::Token, Pair, utils::{describe_line, parse_line}};
|
use super::{
|
||||||
|
token::Token,
|
||||||
|
utils::{describe_line, parse_line},
|
||||||
|
Pair,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
@ -29,4 +33,4 @@ pub fn parse_control(pair: Pair) -> Control {
|
||||||
},
|
},
|
||||||
_ => unknown!(),
|
_ => unknown!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{rc::Rc, cell::RefCell, collections::HashMap};
|
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
@ -23,7 +23,9 @@ impl Event {
|
||||||
Say { name, text } => {
|
Say { name, text } => {
|
||||||
let context = context.borrow();
|
let context = context.borrow();
|
||||||
let variables = context.get_variables();
|
let variables = context.get_variables();
|
||||||
*name = name.as_deref().map(|name| interpolate_string(&name, &variables));
|
*name = name
|
||||||
|
.as_deref()
|
||||||
|
.map(|name| interpolate_string(&name, &variables));
|
||||||
*text = interpolate_string(&text, &variables);
|
*text = interpolate_string(&text, &variables);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,4 +45,4 @@ fn interpolate_string(input: &str, variables: &HashMap<String, Token>) -> String
|
||||||
});
|
});
|
||||||
|
|
||||||
interpolated_string.into_owned()
|
interpolated_string.into_owned()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
pub mod token;
|
pub mod block;
|
||||||
pub mod command;
|
pub mod command;
|
||||||
pub mod control;
|
pub mod control;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod block;
|
pub mod token;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
pub type Pair<'a> = pest::iterators::Pair<'a, Rule>;
|
pub type Pair<'a> = pest::iterators::Pair<'a, Rule>;
|
||||||
|
|
||||||
use std::{path::PathBuf, rc::Rc, cell::RefCell, fs};
|
use std::{cell::RefCell, fs, path::PathBuf, rc::Rc};
|
||||||
|
|
||||||
pub use pest::Parser;
|
pub use pest::Parser;
|
||||||
use pest_derive::Parser;
|
use pest_derive::Parser;
|
||||||
|
|
||||||
use block::{CommandBlock, parse_block};
|
use block::{parse_block, CommandBlock};
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[grammar = "rpy.pest"]
|
#[grammar = "rpy.pest"]
|
||||||
|
@ -30,4 +30,4 @@ pub fn parse(script: &str) -> Rc<RefCell<CommandBlock>> {
|
||||||
.next()
|
.next()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
parse_block(file, None)
|
parse_block(file, None)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use super::{Rule, Pair};
|
use super::{Pair, Rule};
|
||||||
|
|
||||||
// Raw script tokens
|
// Raw script tokens
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -40,7 +40,8 @@ impl ToString for Token {
|
||||||
Boolean(boolean) => match boolean {
|
Boolean(boolean) => match boolean {
|
||||||
true => "True",
|
true => "True",
|
||||||
false => "False",
|
false => "False",
|
||||||
}.to_owned(),
|
}
|
||||||
|
.to_owned(),
|
||||||
Number(number) => number.to_string(),
|
Number(number) => number.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,4 +80,4 @@ pub fn parse_token(pair: Pair) -> Token {
|
||||||
Rule::Keyword => Token::Keyword(pair.as_str().to_owned()),
|
Rule::Keyword => Token::Keyword(pair.as_str().to_owned()),
|
||||||
__ => unreachable!(),
|
__ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use super::{Pair, token::{Token, parse_token}};
|
use super::{
|
||||||
|
token::{parse_token, Token},
|
||||||
|
Pair,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn parse_line(pair: Pair) -> Vec<Token> {
|
pub fn parse_line(pair: Pair) -> Vec<Token> {
|
||||||
pair
|
pair.into_inner().map(|pair| parse_token(pair)).collect()
|
||||||
.into_inner()
|
|
||||||
.map(|pair| parse_token(pair))
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Line description e.g. [String, Keyword, Array]
|
// Line description e.g. [String, Keyword, Array]
|
||||||
|
@ -18,4 +18,4 @@ pub fn describe_line(line: &[Token]) -> String {
|
||||||
}
|
}
|
||||||
description.push_str("]");
|
description.push_str("]");
|
||||||
description
|
description
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue