Skip to content

Commit

Permalink
Use enum stringify (#48)
Browse files Browse the repository at this point in the history
# Pull Request

<!-- Provide a general summary of your changes in the Title above -->
<!-- Optional fields can be removed if not applicable -->

## Description

Simplify enum printing with `EnumStringify`

## Checklist

- [x] I have self-reviewed my code
- [x] Code follows project's style guidelines
- [x] Tests added and passing
- [x] Documentation updated (if needed)
  • Loading branch information
Yag000 authored Feb 19, 2024
2 parents 83d652c + 6e4bdb9 commit 88f4d87
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 77 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.4.0"

[dev-dependencies]
criterion = "0.5.1"
Expand Down
46 changes: 3 additions & 43 deletions src/compiler/code.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -62,7 +63,8 @@ impl Instructions {
}
}

#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive, Clone, Copy)]
#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive, Clone, Copy, EnumStringify)]
#[enum_stringify(prefix = "Op")]
pub enum Opcode {
// Constants
Constant,
Expand Down Expand Up @@ -121,48 +123,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<u32> {
match self {
Expand Down
29 changes: 5 additions & 24 deletions src/object/builtins.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<Object> {
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<Object> {
Expand Down
14 changes: 4 additions & 10 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use enum_stringify::EnumStringify;

use crate::{lexer::token::Token, parser::Parser};
use std::fmt::Display;

Expand Down Expand Up @@ -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<Self, String> {
match parser.current_token {
Expand Down

0 comments on commit 88f4d87

Please sign in to comment.