Move commands into separate module
This commit is contained in:
parent
c7c7704b5d
commit
756d572d21
40
src/basic.rs
40
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 nom::{bytes::complete::tag, multi::separated_list0, IResult};
|
||||||
|
|
||||||
use crate::parsers::commands;
|
use crate::{parsers, commands::{Line, Primitive, PrintOutput, Command}};
|
||||||
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
pub enum Node {
|
pub enum Node {
|
||||||
@ -124,7 +90,7 @@ impl Program {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn read(i: &str) -> IResult<&str, Self> {
|
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;
|
let mut node = Node::None;
|
||||||
|
|
||||||
for line in lines.iter() {
|
for line in lines.iter() {
|
||||||
|
35
src/commands.rs
Normal file
35
src/commands.rs
Normal 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),
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
mod basic;
|
mod basic;
|
||||||
|
mod commands;
|
||||||
mod parsers;
|
mod parsers;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -3,7 +3,7 @@ use nom::{
|
|||||||
sequence::terminated, IResult,
|
sequence::terminated, IResult,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::basic::{Command, Line, PrintOutput};
|
use crate::commands::{Command, Line, PrintOutput};
|
||||||
|
|
||||||
use super::{generic, variables};
|
use super::{generic, variables};
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ use nom::{
|
|||||||
IResult,
|
IResult,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::basic::Primitive;
|
use crate::commands::Primitive;
|
||||||
|
|
||||||
use super::generic::{consume_line, read_string};
|
use super::generic::{consume_line, read_string};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user