From 756d572d2156e26e245e07b08c27f741c5a06a68 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Fri, 27 Jan 2023 08:27:44 +0000 Subject: [PATCH] Move commands into separate module --- src/basic.rs | 40 +++------------------------------------- src/commands.rs | 35 +++++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/parsers/commands.rs | 2 +- src/parsers/variables.rs | 2 +- 5 files changed, 41 insertions(+), 39 deletions(-) create mode 100644 src/commands.rs diff --git a/src/basic.rs b/src/basic.rs index 20da862..7aa74a3 100644 --- a/src/basic.rs +++ b/src/basic.rs @@ -1,42 +1,8 @@ -use std::{collections::HashMap, fmt::Display}; +use std::{collections::HashMap}; use nom::{bytes::complete::tag, multi::separated_list0, IResult}; -use crate::parsers::commands; - -pub type Line = (usize, Command); - -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum Command { - Print(PrintOutput), - GoTo(usize), - Var((String, Primitive)), - Comment, - None, -} - -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum Primitive { - Int(i64), - String(String), - Assignment(String), -} - -impl Display for Primitive { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Primitive::Int(i) => write!(f, "{}", i), - Primitive::String(s) => write!(f, "{}", s), - Primitive::Assignment(a) => write!(f, "Assigment from {}", a), - } - } -} - -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum PrintOutput { - Value(String), - Variable(String), -} +use crate::{parsers, commands::{Line, Primitive, PrintOutput, Command}}; #[derive(Debug, PartialEq, Eq, Clone)] pub enum Node { @@ -124,7 +90,7 @@ impl Program { } fn read(i: &str) -> IResult<&str, Self> { - let (i, lines) = separated_list0(tag("\n"), commands::parse_line)(i)?; + let (i, lines) = separated_list0(tag("\n"), parsers::commands::parse_line)(i)?; let mut node = Node::None; for line in lines.iter() { diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..2dd275d --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,35 @@ +use std::fmt::Display; + +pub type Line = (usize, Command); + +#[derive(Debug, PartialEq, Eq, Clone)] +pub enum Command { + Print(PrintOutput), + GoTo(usize), + Var((String, Primitive)), + Comment, + None, +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub enum Primitive { + Int(i64), + String(String), + Assignment(String), +} + +impl Display for Primitive { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Primitive::Int(i) => write!(f, "{}", i), + Primitive::String(s) => write!(f, "{}", s), + Primitive::Assignment(a) => write!(f, "Assigment from {}", a), + } + } +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub enum PrintOutput { + Value(String), + Variable(String), +} diff --git a/src/main.rs b/src/main.rs index 80736f8..6b7b9df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::fs; mod basic; +mod commands; mod parsers; fn main() { diff --git a/src/parsers/commands.rs b/src/parsers/commands.rs index edc9941..bb643d7 100644 --- a/src/parsers/commands.rs +++ b/src/parsers/commands.rs @@ -3,7 +3,7 @@ use nom::{ sequence::terminated, IResult, }; -use crate::basic::{Command, Line, PrintOutput}; +use crate::commands::{Command, Line, PrintOutput}; use super::{generic, variables}; diff --git a/src/parsers/variables.rs b/src/parsers/variables.rs index 71cee18..bddd30b 100644 --- a/src/parsers/variables.rs +++ b/src/parsers/variables.rs @@ -7,7 +7,7 @@ use nom::{ IResult, }; -use crate::basic::Primitive; +use crate::commands::Primitive; use super::generic::{consume_line, read_string};