From 83098e14cb034b59cf96f29eb661e6da8d01e82d Mon Sep 17 00:00:00 2001 From: Yago Iglesias Date: Mon, 19 Feb 2024 19:53:38 +0100 Subject: [PATCH 1/5] feat: Use enum_stringify for imple display of enums --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + src/compiler/code.rs | 45 ++------------------------------------------ 3 files changed, 15 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4bc3de4..ddb880d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,6 +109,7 @@ dependencies = [ "clap", "clap_derive", "criterion", + "enum_stringify", "num-derive", "num-traits", "rustyline", @@ -276,6 +277,17 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "enum_stringify" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c187746a2c96635d1d908a186ce6b698135c58ed1baca73860ad9cd44d80e32" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "errno" version = "0.3.8" diff --git a/Cargo.toml b/Cargo.toml index 52b2bb8..c246231 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ num-traits = "0.2.17" strum = "0.26.1" strum_macros = "0.26.1" rustyline = "13.0.0" +enum_stringify = "0.3.0" [dev-dependencies] criterion = "0.5.1" diff --git a/src/compiler/code.rs b/src/compiler/code.rs index b224cc3..1defa7e 100644 --- a/src/compiler/code.rs +++ b/src/compiler/code.rs @@ -1,4 +1,5 @@ use byteorder::{BigEndian, ByteOrder, WriteBytesExt}; +use enum_stringify::EnumStringify; use num_derive::{FromPrimitive, ToPrimitive}; use num_traits::FromPrimitive; use std::fmt::Display; @@ -62,7 +63,7 @@ impl Instructions { } } -#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive, Clone, Copy)] +#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive, Clone, Copy, EnumStringify)] pub enum Opcode { // Constants Constant, @@ -121,48 +122,6 @@ pub enum Opcode { Pop, } -impl Display for Opcode { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let op = match self { - Opcode::Constant => "OpConstant", - Opcode::Add => "OpAdd", - Opcode::Sub => "OpSub", - Opcode::Mul => "OpMul", - Opcode::Div => "OpDiv", - Opcode::True => "OpTrue", - Opcode::False => "OpFalse", - Opcode::GreaterThan => "OpGreaterThan", - Opcode::GreaterEqualThan => "OpGreaterEqualThan", - Opcode::Equal => "OpEqual", - Opcode::NotEqual => "OpNotEqual", - Opcode::Or => "OpOr", - Opcode::And => "OpAnd", - Opcode::Minus => "OpMinus", - Opcode::Modulo => "OpModulo", - Opcode::Bang => "OpBang", - Opcode::JumpNotTruthy => "OpJumpNotTruthy", - Opcode::Jump => "OpJump", - Opcode::Null => "OpNull", - Opcode::SetGlobal => "OpSetGlobal", - Opcode::GetGlobal => "OpGetGlobal", - Opcode::SetLocal => "OpSetLocal", - Opcode::GetLocal => "OpGetLocal", - Opcode::GetFree => "OpGetFree", - Opcode::CurrentClosure => "OpCurrentClosure", - Opcode::Array => "OpArray", - Opcode::HashMap => "OpHashMap", - Opcode::Index => "OpIndex", - Opcode::Call => "OpCall", - Opcode::ReturnValue => "OpReturnValue", - Opcode::Return => "OpReturn", - Opcode::GetBuiltin => "OpBuiltIn", - Opcode::Closure => "OpClosure", - Opcode::Pop => "OpPop", - }; - write!(f, "{op}") - } -} - impl Opcode { pub fn lookup_widths(&self) -> Vec { match self { From 65ffd77adbae76354ee1e94c47da9d61f345fe12 Mon Sep 17 00:00:00 2001 From: Yago Iglesias Date: Mon, 19 Feb 2024 20:35:13 +0100 Subject: [PATCH 2/5] chore(deps): update dependency enum_stringify to v0.4.0 --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ddb880d..9e20756 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -279,9 +279,9 @@ checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" [[package]] name = "enum_stringify" -version = "0.3.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c187746a2c96635d1d908a186ce6b698135c58ed1baca73860ad9cd44d80e32" +checksum = "ee381173553f10a459501fa2bd5e61b908e672f7343c41008e9268ce73fad133" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index c246231..f90d018 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ num-traits = "0.2.17" strum = "0.26.1" strum_macros = "0.26.1" rustyline = "13.0.0" -enum_stringify = "0.3.0" +enum_stringify = "0.4.0" [dev-dependencies] criterion = "0.5.1" From eb136ff8d80f4a2bd6c7ff077fd29a047ee424ed Mon Sep 17 00:00:00 2001 From: Yago Iglesias Date: Mon, 19 Feb 2024 20:35:32 +0100 Subject: [PATCH 3/5] refactor: Use EnumStringify in builtins.rs --- src/object/builtins.rs | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/src/object/builtins.rs b/src/object/builtins.rs index a5c80fb..004e8da 100644 --- a/src/object/builtins.rs +++ b/src/object/builtins.rs @@ -1,12 +1,14 @@ +use enum_stringify::EnumStringify; use num_derive::{FromPrimitive, ToPrimitive}; use num_traits::FromPrimitive; -use std::{cmp::Ordering, fmt::Display}; +use std::cmp::Ordering; use strum::IntoEnumIterator; use strum_macros::EnumIter; use crate::object::{Object, NULL}; -#[derive(Debug, PartialEq, Clone, FromPrimitive, ToPrimitive, EnumIter)] +#[derive(Debug, PartialEq, Clone, FromPrimitive, ToPrimitive, EnumIter, EnumStringify)] +#[enum_stringify(case = "lower")] pub enum BuiltinFunction { LEN, FIRST, @@ -16,31 +18,10 @@ pub enum BuiltinFunction { PUTS, } -impl Display for BuiltinFunction { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - BuiltinFunction::LEN => write!(f, "len"), - BuiltinFunction::FIRST => write!(f, "first"), - BuiltinFunction::LAST => write!(f, "last"), - BuiltinFunction::REST => write!(f, "rest"), - BuiltinFunction::PUSH => write!(f, "push"), - BuiltinFunction::PUTS => write!(f, "puts"), - } - } -} - #[allow(clippy::needless_pass_by_value)] // false positive impl BuiltinFunction { pub fn get_builtin(name: &str) -> Option { - match name { - "len" => Some(Object::BUILTIN(BuiltinFunction::LEN)), - "first" => Some(Object::BUILTIN(BuiltinFunction::FIRST)), - "last" => Some(Object::BUILTIN(BuiltinFunction::LAST)), - "rest" => Some(Object::BUILTIN(BuiltinFunction::REST)), - "push" => Some(Object::BUILTIN(BuiltinFunction::PUSH)), - "puts" => Some(Object::BUILTIN(BuiltinFunction::PUTS)), - _ => None, - } + Self::try_from(name).ok().map(Object::BUILTIN) } pub fn get_builtin_by_id(id: usize) -> Option { From 83d25427802baf1625d232a492d22faab63ec678 Mon Sep 17 00:00:00 2001 From: Yago Iglesias Date: Mon, 19 Feb 2024 20:36:16 +0100 Subject: [PATCH 4/5] fix: Opcode printing now has a "Op" prefix --- src/compiler/code.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compiler/code.rs b/src/compiler/code.rs index 1defa7e..11245f8 100644 --- a/src/compiler/code.rs +++ b/src/compiler/code.rs @@ -64,6 +64,7 @@ impl Instructions { } #[derive(Debug, PartialEq, FromPrimitive, ToPrimitive, Clone, Copy, EnumStringify)] +#[enum_stringify(prefix = "Op")] pub enum Opcode { // Constants Constant, From 6e4bdb9316fb1e731b7920667cefffa3abb4339e Mon Sep 17 00:00:00 2001 From: Yago Iglesias Date: Mon, 19 Feb 2024 20:38:55 +0100 Subject: [PATCH 5/5] refactor(parser): Derive EnumStringify for loop enum --- src/parser/ast.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/parser/ast.rs b/src/parser/ast.rs index 577e22a..1f0891a 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -1,3 +1,5 @@ +use enum_stringify::EnumStringify; + use crate::{lexer::token::Token, parser::Parser}; use std::fmt::Display; @@ -590,21 +592,13 @@ impl HashMapLiteral { } } -#[derive(PartialEq, Debug, Clone)] +#[derive(PartialEq, Debug, Clone, EnumStringify)] +#[enum_stringify(case = "lower")] pub enum LoopStatement { Break, Continue, } -impl Display for LoopStatement { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - LoopStatement::Break => write!(f, "break"), - LoopStatement::Continue => write!(f, "continue"), - } - } -} - impl LoopStatement { pub fn parse(parser: &mut Parser) -> Result { match parser.current_token {