Cargo fmt

This commit is contained in:
Lewis Dale 2023-01-13 15:57:17 +00:00
parent 007634c980
commit 98e2380981
2 changed files with 28 additions and 11 deletions

View File

@ -1,2 +1,3 @@
10 PRINT "Hello World" 10 PRINT "Hello World"
20 GO TO 10 20 LET apple=10
30 LET b$="Hello"

View File

@ -1,8 +1,15 @@
use std::collections::HashMap;
use nom::{ use nom::{
branch::alt, branch::alt,
bytes::complete::{escaped_transform, tag, take_until}, bytes::complete::{escaped_transform, tag, take_until},
character::{complete::{u64 as ccu64, anychar}, streaming::none_of, complete::char as ccchar, complete::{i64 as cci64, alphanumeric1, digit1, alpha1}}, character::{
combinator::{map, value, not, verify}, complete::char as ccchar,
complete::{alpha1, alphanumeric1, digit1, i64 as cci64},
complete::{anychar, u64 as ccu64},
streaming::none_of,
},
combinator::{map, not, value, verify},
multi::separated_list0, multi::separated_list0,
sequence::{delimited, terminated, tuple}, sequence::{delimited, terminated, tuple},
IResult, IResult,
@ -14,7 +21,7 @@ pub type Line = (usize, Command);
pub enum Command { pub enum Command {
Print(String), Print(String),
GoTo(usize), GoTo(usize),
Var((String, Primitive)), // Vlack Sheep Var((String, Primitive)),
None, None,
} }
@ -64,11 +71,13 @@ impl Node {
pub struct Program { pub struct Program {
nodes: Node, nodes: Node,
current: Node, current: Node,
vars: HashMap<String, Primitive>,
} }
impl Program { impl Program {
pub fn new(node: Node) -> Self { pub fn new(node: Node) -> Self {
Program { Program {
vars: HashMap::new(),
nodes: node.clone(), nodes: node.clone(),
current: node, current: node,
} }
@ -90,10 +99,14 @@ impl Program {
match item.1 { match item.1 {
Command::Print(line) => println!("{}", line), Command::Print(line) => println!("{}", line),
Command::GoTo(line) => iter.jump_to_line(line), Command::GoTo(line) => iter.jump_to_line(line),
Command::Var((id, var)) => {
self.vars.insert(id, var);
}
_ => panic!("Unrecognised command"), _ => panic!("Unrecognised command"),
} }
}; };
} }
println!("{:?}", self.vars);
} }
} }
@ -154,10 +167,7 @@ fn parse_str(i: &str) -> IResult<&str, (String, Primitive)> {
} }
fn parse_var(i: &str) -> IResult<&str, (String, Primitive)> { fn parse_var(i: &str) -> IResult<&str, (String, Primitive)> {
alt(( alt((parse_int, parse_str))(i)
parse_int,
parse_str
))(i)
} }
fn parse_command(i: &str) -> IResult<&str, Command> { fn parse_command(i: &str) -> IResult<&str, Command> {
@ -193,7 +203,7 @@ pub fn read_program(i: &str) -> IResult<&str, Program> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{parse_line, read_program, read_string, Command, Node, Primitive, Line}; use super::{parse_line, read_program, read_string, Command, Line, Node, Primitive};
#[test] #[test]
fn it_parses_a_print_command() { fn it_parses_a_print_command() {
@ -308,7 +318,13 @@ mod tests {
#[test] #[test]
fn it_parses_a_string_variable() { fn it_parses_a_string_variable() {
let line = r#"10 LET a$="Hello world""#; let line = r#"10 LET a$="Hello world""#;
let expected: Line = (10, Command::Var((String::from("a$"), Primitive::String(String::from("Hello world"))))); let expected: Line = (
10,
Command::Var((
String::from("a$"),
Primitive::String(String::from("Hello world")),
)),
);
let (_, result) = parse_line(line).unwrap(); let (_, result) = parse_line(line).unwrap();
assert_eq!(expected, result); assert_eq!(expected, result);
} }