tangled
alpha
login
or
join now
cass.cityboundforest.com
/
triangular-viking
0
fork
atom
A Quadrilateral Cowboy clone intended to help me learn Game Dev
0
fork
atom
overview
issues
pulls
pipelines
Added lexing terminal input into tokens
cass.cityboundforest.com
2 weeks ago
25cbc1b2
52dc888c
+101
-3
3 changed files
expand all
collapse all
unified
split
src
level.rs
main.rs
terminal.rs
+6
-3
src/level.rs
···
1
1
use std::mem;
2
2
3
3
use bevy::{asset::RenderAssetUsages, camera::RenderTarget, input::keyboard::{Key, KeyboardInput}, math::Affine2, prelude::*, render::render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages}};
4
4
-
use crate::game::{GameAssets, GameState};
4
4
+
use crate::{game::{GameAssets, GameState}, terminal};
5
5
6
6
pub fn plugin(app: &mut App) {
7
7
app.add_systems(OnEnter(GameState::Loading), check_levels)
···
179
179
Camera3d::default(),
180
180
Transform::from_xyz(0.5, -1.0, 3.5).looking_at(Vec3::new(0.0, -1.0, 0.0), Vec3::Y)
181
181
));
182
182
+
183
183
+
commands.spawn(terminal::TerminalSystem);
182
184
}
183
185
184
184
-
fn check_input(mut keyboard_input_reader: MessageReader<KeyboardInput>, mut query: Query<(&mut CurrentInput, &Terminal, &mut ConsoleHistory)>, mut terminal_query: Query<(&mut Text, &TerminalText)>) {
186
186
+
fn check_input(mut keyboard_input_reader: MessageReader<KeyboardInput>, mut query: Query<(&mut CurrentInput, &Terminal, &mut ConsoleHistory)>, mut terminal_query: Query<(&mut Text, &TerminalText)>, mut terminal_system: Query<&mut terminal::TerminalSystem>) {
185
187
let mut current_input = query.single_mut().unwrap();
186
188
let mut current_terminal_text = terminal_query.single_mut().unwrap();
189
189
+
let mut terminal = terminal_system.single_mut().unwrap();
187
190
188
191
for keyboard_input in keyboard_input_reader.read() {
189
192
if !keyboard_input.state.is_pressed() {
···
199
202
}
200
203
201
204
current_input.2.0.push(ConsoleEntry::Command(current_input.0.0.to_owned()));
205
205
+
terminal.parse(current_input.0.0.clone());
202
206
current_input.0.0.clear();
203
203
-
// TODO: Process input
204
207
},
205
208
(Key::Backspace, _) => {
206
209
current_input.0.0.pop();
+1
src/main.rs
···
3
3
4
4
mod game;
5
5
mod level;
6
6
+
mod terminal;
6
7
7
8
fn main() {
8
9
App::new()
+94
src/terminal.rs
···
1
1
+
use bevy::prelude::*;
2
2
+
3
3
+
#[derive(Component)]
4
4
+
pub struct TerminalSystem;
5
5
+
6
6
+
enum Token {
7
7
+
Identifier(String),
8
8
+
Integer(i32),
9
9
+
Period,
10
10
+
Semicolon,
11
11
+
LParen,
12
12
+
RParen
13
13
+
}
14
14
+
15
15
+
impl TerminalSystem {
16
16
+
pub fn parse(&mut self, input: String) {
17
17
+
let tokens = self.lex(input);
18
18
+
self.parse_tokens(tokens);
19
19
+
}
20
20
+
21
21
+
fn lex(&mut self, input: String) -> Vec<Token> {
22
22
+
let mut tokens = Vec::new();
23
23
+
let mut buffer = String::new();
24
24
+
let mut in_id = false;
25
25
+
let mut in_int = false;
26
26
+
27
27
+
let mut iter = 0;
28
28
+
29
29
+
while iter < input.len() {
30
30
+
let chr = input.chars().skip(iter).take(1).collect::<String>();
31
31
+
32
32
+
if in_id {
33
33
+
if chr.chars().any(|c| matches!(c, 'a'..='z')) || "0123456789".contains(&chr) {
34
34
+
buffer += &chr;
35
35
+
iter += 1;
36
36
+
continue;
37
37
+
} else {
38
38
+
tokens.push(Token::Identifier(buffer.clone()));
39
39
+
in_id = false;
40
40
+
continue;
41
41
+
}
42
42
+
} else if in_int {
43
43
+
if "0123456789".contains(&chr) {
44
44
+
buffer += &chr;
45
45
+
iter += 1;
46
46
+
continue;
47
47
+
} else {
48
48
+
tokens.push(Token::Integer(buffer.parse::<i32>().unwrap()));
49
49
+
in_int = false;
50
50
+
continue;
51
51
+
}
52
52
+
} else if chr == ";" {
53
53
+
tokens.push(Token::Semicolon);
54
54
+
iter += 1;
55
55
+
continue;
56
56
+
} else if chr == "." {
57
57
+
tokens.push(Token::Period);
58
58
+
iter += 1;
59
59
+
continue;
60
60
+
} else if chr == "(" {
61
61
+
tokens.push(Token::LParen);
62
62
+
iter += 1;
63
63
+
continue;
64
64
+
} else if chr == ")" {
65
65
+
tokens.push(Token::RParen);
66
66
+
iter += 1;
67
67
+
continue;
68
68
+
} else if chr == " " {
69
69
+
iter += 1;
70
70
+
continue;
71
71
+
} else if chr.is_ascii() {
72
72
+
in_id = true;
73
73
+
buffer = chr.clone();
74
74
+
iter += 1;
75
75
+
continue;
76
76
+
} else if "0123456789".contains(&chr) {
77
77
+
if !in_int {
78
78
+
in_int = true;
79
79
+
buffer = chr.clone();
80
80
+
iter += 1;
81
81
+
continue;
82
82
+
}
83
83
+
} else {
84
84
+
panic!("Need to figure out how to throw errors");
85
85
+
}
86
86
+
}
87
87
+
88
88
+
tokens
89
89
+
}
90
90
+
91
91
+
fn parse_tokens(&mut self, tokens: Vec<Token>) {
92
92
+
93
93
+
}
94
94
+
}