Move commands into separate module

This commit is contained in:
Lewis Dale 2023-01-27 08:27:44 +00:00
parent c7c7704b5d
commit 756d572d21
5 changed files with 41 additions and 39 deletions

View File

@ -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() {

35
src/commands.rs Normal file
View File

@ -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),
}

View File

@ -1,6 +1,7 @@
use std::fs;
mod basic;
mod commands;
mod parsers;
fn main() {

View File

@ -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};

View File

@ -7,7 +7,7 @@ use nom::{
IResult,
};
use crate::basic::Primitive;
use crate::commands::Primitive;
use super::generic::{consume_line, read_string};