From f939903df25f9b7f866c7309b71353e36e350add Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sun, 6 Feb 2022 13:18:39 +0700 Subject: [PATCH] refactor: separate Type to its own file --- blspc/src/compiler/compile.rs | 3 +- blspc/src/vm/instr.rs | 147 +--------------------------------- blspc/src/vm/mod.rs | 2 + blspc/src/vm/parser.rs | 2 +- blspc/src/vm/types.rs | 146 +++++++++++++++++++++++++++++++++ blspc/src/vm/vm.rs | 2 +- 6 files changed, 154 insertions(+), 148 deletions(-) create mode 100644 blspc/src/vm/types.rs diff --git a/blspc/src/compiler/compile.rs b/blspc/src/compiler/compile.rs index c4b0cb7..de8e917 100644 --- a/blspc/src/compiler/compile.rs +++ b/blspc/src/compiler/compile.rs @@ -1,4 +1,5 @@ -use crate::{vm::instr::*, compiler::parser::Sexpr::{self, *}}; +use crate::{vm::{instr::*, types::Type}, compiler::parser::Sexpr::{self, *}}; + pub struct Compiler { // Compiled instructions pub instructions: Vec, diff --git a/blspc/src/vm/instr.rs b/blspc/src/vm/instr.rs index 8a53e94..b447a97 100644 --- a/blspc/src/vm/instr.rs +++ b/blspc/src/vm/instr.rs @@ -1,149 +1,6 @@ -use std::{fmt::Display, str::FromStr, ops::{Add, Sub, Mul, Div, Not}}; +use std::{fmt::Display, str::FromStr}; -use crate::vm::vm::Error::{self, InvalidAriphmeticOperation}; - -/// Literal types for the assembler. -#[derive(Clone, Debug, PartialEq)] -pub enum Type { - Null, - Int(i64), - Float(f64), - Boolean(bool), - String(String), -} - -impl Type { - pub fn is_null(&self) -> bool { - match self { - Type::Null => true, - _ => false, - } - } - - pub fn trim(&self) -> Type { - match self { - Type::String(s) => Type::String(s[1..s.len() - 1].to_string()), - _ => self.clone(), - } - } - - pub fn fmt(&self) -> String { - match self { - Type::Null => "null".to_string(), - Type::Int(i) => i.to_string(), - Type::Float(f) => f.to_string(), - Type::Boolean(b) => match b { - true => "true".to_string(), - false => "false".to_string(), - }, - Type::String(s) => s.clone(), - } - } -} - -impl Add for Type { - type Output = Result; - - fn add(self, other: Type) -> Result { - match (self, other) { - (Type::Int(lhs), Type::Int(rhs)) => Ok(Type::Int(lhs + rhs)), - (Type::Int(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs as f64 + rhs)), - (Type::Float(lhs), Type::Int(rhs)) => Ok(Type::Float(lhs + rhs as f64)), - (Type::Float(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs + rhs)), - (Type::String(lhs), Type::String(rhs)) => Ok(Type::String(format!("{}{}", lhs, rhs))), - _ => Err(InvalidAriphmeticOperation), - } - } -} - -impl Sub for Type { - type Output = Result; - - fn sub(self, other: Type) -> Result { - match (self, other) { - (Type::Int(lhs), Type::Int(rhs)) => Ok(Type::Int(lhs - rhs)), - (Type::Int(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs as f64 - rhs)), - (Type::Float(lhs), Type::Int(rhs)) => Ok(Type::Float(lhs - rhs as f64)), - (Type::Float(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs - rhs)), - _ => Err(InvalidAriphmeticOperation), - } - } -} - -impl Mul for Type { - type Output = Result; - - fn mul(self, other: Type) -> Result { - match (self, other) { - (Type::Int(lhs), Type::Int(rhs)) => Ok(Type::Int(lhs * rhs)), - (Type::Int(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs as f64 * rhs)), - (Type::Float(lhs), Type::Int(rhs)) => Ok(Type::Float(lhs * rhs as f64)), - (Type::Float(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs * rhs)), - _ => Err(InvalidAriphmeticOperation), - } - } -} - -impl Div for Type { - type Output = Result; - - fn div(self, other: Type) -> Result { - match (self, other) { - (Type::Int(lhs), Type::Int(rhs)) => Ok(Type::Int(lhs / rhs)), - (Type::Int(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs as f64 / rhs)), - (Type::Float(lhs), Type::Int(rhs)) => Ok(Type::Float(lhs / rhs as f64)), - (Type::Float(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs / rhs)), - _ => Err(InvalidAriphmeticOperation), - } - } -} - -impl Not for Type { - type Output = Result; - - fn not(self) -> Result { - match self { - Type::Boolean(b) => Ok(Type::Boolean(!b)), - _ => Err(InvalidAriphmeticOperation), - } - } -} - -impl Display for Type { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - Type::Int(i) => write!(f, "{}", i), - Type::Float(fl) => write!(f, "{}", fl), - Type::Boolean(b) => write!(f, "{}", b), - Type::String(s) => write!(f, "\"{}\"", s), - _ => unreachable!(), - } - } -} - -impl FromStr for Type { - type Err = String; - - fn from_str(s: &str) -> Result { - match s { - "true" => Ok(Type::Boolean(true)), - "false" => Ok(Type::Boolean(false)), - _ => { - let i = s.parse::(); - if i.is_ok() { - Ok(Type::Int(i.unwrap())) - } else { - let fl = s.parse::(); - if fl.is_ok() { - Ok(Type::Float(fl.unwrap())) - } else { - Ok(Type::String(s.to_string())) - } - } - } - } - } -} +use crate::vm::types::Type; #[derive(Clone, Copy, Debug)] pub struct Register { pub value: usize } diff --git a/blspc/src/vm/mod.rs b/blspc/src/vm/mod.rs index b12f2b9..1fe3b5d 100644 --- a/blspc/src/vm/mod.rs +++ b/blspc/src/vm/mod.rs @@ -1,3 +1,5 @@ +/// Type for instrs. +pub mod types; /// Definition of the VM instructions. pub mod instr; /// Definition of the instruction parser. diff --git a/blspc/src/vm/parser.rs b/blspc/src/vm/parser.rs index 17cd029..3365a01 100644 --- a/blspc/src/vm/parser.rs +++ b/blspc/src/vm/parser.rs @@ -1,6 +1,6 @@ use regex::Regex; -use crate::vm::instr::*; +use crate::vm::{instr::*, types::Type}; const REGEX: &str = r###"[^\s\$";]+|"[^"]*"|;.*"###; diff --git a/blspc/src/vm/types.rs b/blspc/src/vm/types.rs new file mode 100644 index 0000000..4669dc2 --- /dev/null +++ b/blspc/src/vm/types.rs @@ -0,0 +1,146 @@ +use std::{fmt::Display, str::FromStr, ops::{Add, Sub, Mul, Div, Not}}; + +use crate::vm::vm::Error::{self, InvalidAriphmeticOperation}; + +/// Literal types for the assembler. +#[derive(Clone, Debug, PartialEq)] +pub enum Type { + Null, + Int(i64), + Float(f64), + Boolean(bool), + String(String), +} + +impl Type { + pub fn is_null(&self) -> bool { + match self { + Type::Null => true, + _ => false, + } + } + + pub fn trim(&self) -> Type { + match self { + Type::String(s) => Type::String(s[1..s.len() - 1].to_string()), + _ => self.clone(), + } + } + + pub fn fmt(&self) -> String { + match self { + Type::Null => "null".to_string(), + Type::Int(i) => i.to_string(), + Type::Float(f) => f.to_string(), + Type::Boolean(b) => match b { + true => "true".to_string(), + false => "false".to_string(), + }, + Type::String(s) => s.clone(), + } + } +} + +impl Add for Type { + type Output = Result; + + fn add(self, other: Type) -> Result { + match (self, other) { + (Type::Int(lhs), Type::Int(rhs)) => Ok(Type::Int(lhs + rhs)), + (Type::Int(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs as f64 + rhs)), + (Type::Float(lhs), Type::Int(rhs)) => Ok(Type::Float(lhs + rhs as f64)), + (Type::Float(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs + rhs)), + (Type::String(lhs), Type::String(rhs)) => Ok(Type::String(format!("{}{}", lhs, rhs))), + _ => Err(InvalidAriphmeticOperation), + } + } +} + +impl Sub for Type { + type Output = Result; + + fn sub(self, other: Type) -> Result { + match (self, other) { + (Type::Int(lhs), Type::Int(rhs)) => Ok(Type::Int(lhs - rhs)), + (Type::Int(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs as f64 - rhs)), + (Type::Float(lhs), Type::Int(rhs)) => Ok(Type::Float(lhs - rhs as f64)), + (Type::Float(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs - rhs)), + _ => Err(InvalidAriphmeticOperation), + } + } +} + +impl Mul for Type { + type Output = Result; + + fn mul(self, other: Type) -> Result { + match (self, other) { + (Type::Int(lhs), Type::Int(rhs)) => Ok(Type::Int(lhs * rhs)), + (Type::Int(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs as f64 * rhs)), + (Type::Float(lhs), Type::Int(rhs)) => Ok(Type::Float(lhs * rhs as f64)), + (Type::Float(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs * rhs)), + _ => Err(InvalidAriphmeticOperation), + } + } +} + +impl Div for Type { + type Output = Result; + + fn div(self, other: Type) -> Result { + match (self, other) { + (Type::Int(lhs), Type::Int(rhs)) => Ok(Type::Int(lhs / rhs)), + (Type::Int(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs as f64 / rhs)), + (Type::Float(lhs), Type::Int(rhs)) => Ok(Type::Float(lhs / rhs as f64)), + (Type::Float(lhs), Type::Float(rhs)) => Ok(Type::Float(lhs / rhs)), + _ => Err(InvalidAriphmeticOperation), + } + } +} + +impl Not for Type { + type Output = Result; + + fn not(self) -> Result { + match self { + Type::Boolean(b) => Ok(Type::Boolean(!b)), + _ => Err(InvalidAriphmeticOperation), + } + } +} + +impl Display for Type { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Type::Int(i) => write!(f, "{}", i), + Type::Float(fl) => write!(f, "{}", fl), + Type::Boolean(b) => write!(f, "{}", b), + Type::String(s) => write!(f, "\"{}\"", s), + _ => unreachable!(), + } + } +} + +impl FromStr for Type { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "true" => Ok(Type::Boolean(true)), + "false" => Ok(Type::Boolean(false)), + _ => { + let i = s.parse::(); + if i.is_ok() { + Ok(Type::Int(i.unwrap())) + } else { + let fl = s.parse::(); + if fl.is_ok() { + Ok(Type::Float(fl.unwrap())) + } else { + Ok(Type::String(s.to_string())) + } + } + } + } + } +} diff --git a/blspc/src/vm/vm.rs b/blspc/src/vm/vm.rs index e966539..59990f1 100644 --- a/blspc/src/vm/vm.rs +++ b/blspc/src/vm/vm.rs @@ -1,6 +1,6 @@ use std::{io::{self, Read}, fmt::Display, fs::File}; -use crate::vm::instr::{Instr::{self, *}, Type, Register}; +use crate::vm::{instr::{Instr::{self, *}, Register}, types::Type}; pub enum Error { NoMainFunction,