Skip to content

Commit 09ac444

Browse files
committed
refactor: move helper functions to spec
1 parent 73946a6 commit 09ac444

File tree

2 files changed

+43
-38
lines changed

2 files changed

+43
-38
lines changed

src/parser.rs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,9 @@
11
use crate::lexer::spec::arch_v1::*;
22
use crate::lexer::{Token::*, *};
3-
use colored::Colorize;
43
use miette::{miette, Error, LabeledSpan, Severity};
54
use std::collections::HashMap;
65
use std::ops::Range;
76

8-
/* This should be placed in spec ======================================== */
9-
trait BitStream {
10-
fn bit_stream(&self) -> String;
11-
}
12-
13-
enum OpOrCond {
14-
Operation(Op),
15-
Condition(Cond),
16-
}
17-
18-
impl BitStream for OpOrCond {
19-
fn bit_stream(&self) -> String {
20-
match self {
21-
OpOrCond::Operation(op) => op.bit_stream(),
22-
OpOrCond::Condition(cond) => cond.bit_stream(),
23-
}
24-
}
25-
}
26-
27-
/// takes a 15 bits value and format it in a recognizable word for the cpu
28-
fn data_mode_format(val: u16) -> String {
29-
format!("{}{}", "1".green(), format!("{:015b}", val).red())
30-
}
31-
32-
/// takes operands operation and destination register and format it in a recognizable word for the cpu
33-
fn inst_mode_format(op_or_cond: OpOrCond, rega: Reg, regb: Reg, regc: Reg) -> String {
34-
format!(
35-
"{}{}000{}{}{}",
36-
"0".green().bold(),
37-
op_or_cond.bit_stream().blue(),
38-
rega.bit_stream().yellow(),
39-
regb.bit_stream().purple(),
40-
regc.bit_stream().cyan()
41-
)
42-
}
43-
/* ============================================================= */
44-
457
#[derive(PartialEq, Debug, Clone)]
468
pub struct ColType {
479
val: u16,

src/spec.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod arch_v1 {
22
use crate::lexer::HandleToken;
33
use logos::Lexer;
4+
use colored::Colorize;
45

56
#[allow(dead_code)]
67

@@ -121,4 +122,46 @@ pub mod arch_v1 {
121122
.to_string()
122123
}
123124
}
125+
126+
127+
// wrapper around Op and Cond
128+
#[allow(dead_code)]
129+
pub enum OpOrCond {
130+
Operation(Op),
131+
Condition(Cond),
132+
}
133+
134+
impl HandleToken for OpOrCond {
135+
fn new(_lex: &mut Lexer<crate::lexer::Token>) -> Option<Self>
136+
where
137+
Self: Sized {
138+
None
139+
}
140+
141+
fn bit_stream(&self) -> String {
142+
match self {
143+
OpOrCond::Operation(op) => op.bit_stream(),
144+
OpOrCond::Condition(cond) => cond.bit_stream(),
145+
}
146+
}
147+
}
148+
149+
/// takes a 15 bits value and format it in a recognizable word for the cpu
150+
#[allow(dead_code)]
151+
pub fn data_mode_format(val: u16) -> String {
152+
format!("{}{}", "1".green(), format!("{:015b}", val).red())
153+
}
154+
155+
/// takes operands operation and destination register and format it in a recognizable word for the cpu
156+
#[allow(dead_code)]
157+
pub fn inst_mode_format(op_or_cond: OpOrCond, rega: Reg, regb: Reg, regc: Reg) -> String {
158+
format!(
159+
"{}{}000{}{}{}",
160+
"0".green().bold(),
161+
op_or_cond.bit_stream().blue(),
162+
rega.bit_stream().yellow(),
163+
regb.bit_stream().purple(),
164+
regc.bit_stream().cyan()
165+
)
166+
}
124167
}

0 commit comments

Comments
 (0)