Skip to content

Commit 22d79e0

Browse files
committed
remove impl_opcode macro since repeation in metavariable expr is unstable
rust_issue: rust-lang/rust#83527
1 parent 41cf5a4 commit 22d79e0

File tree

9 files changed

+105
-18
lines changed

9 files changed

+105
-18
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[workspace]
2-
members = [ "asm","jnd", "vm"]
2+
members = [ "asm","jnd", "utils/jop", "vm"]
33
resolver = "2"

jnd/src/errors/asme.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use core::fmt;
2+
3+
use super::Erroring;
4+
5+
#[derive(Debug)]
6+
pub enum AsmErr {
7+
ReadFile,
8+
OpenFile,
9+
Parse,
10+
Write,
11+
}
12+
13+
impl Erroring for AsmErr {
14+
fn err(&self) -> &str {
15+
match self {
16+
Self::ReadFile => "failed to read file to buffer",
17+
Self::OpenFile => "failed to open file",
18+
Self::Parse => "failed to parse token",
19+
Self::Write => "failed to write binary to stdout",
20+
}
21+
}
22+
}
23+
24+
impl fmt::Display for AsmErr {
25+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26+
write!(f, "{}", self.err())
27+
}
28+
}
29+
30+
#[macro_export]
31+
macro_rules! asme {
32+
($err: expr, $kind: ident, $($args:tt)*) => {{
33+
use $crate::errors::Erroring;
34+
$err.vme(format!("{}: {}", $crate::errors::vme::AsmErr::$kind.err(), format_args!($($args)*)).as_str())
35+
}};
36+
37+
($kind:ident, $($args:tt)*) => {{
38+
use $crate::errors::Erroring;
39+
$crate::errors::Jerror::Vme(format!("{}: {}", $crate::errors::vme::AsmErr::$kind.err(), format_args!($($args)*)))
40+
}};
41+
}

jnd/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod disassembler;
22
pub mod errors;
33
pub mod interrupts;
4+
pub mod macros;
45
pub mod mem;
56
pub mod op;
67
pub mod reg;

jnd/src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

jnd/src/op.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use crate::{reg::Register, vme, Res};
22

33
/// return-eq
44
macro_rules! retq {
5-
($name: ident, $x: expr) => {
6-
$x == $crate::op::Op::$name.as_u8()
7-
};
5+
($name: ident, $x: expr) => {
6+
$x == $crate::op::Op::$name.as_u8()
7+
};
88

9-
($name:ident($($args:tt)*), $x: expr) => {
10-
$x == $crate::op::Op::$name($($args)*).as_u8()
11-
};
12-
}
9+
($name:ident($($args:tt)*), $x: expr) => {
10+
$x == $crate::op::Op::$name($($args)*).as_u8()
11+
};
12+
}
1313

1414
#[derive(Debug, PartialEq, PartialOrd)]
1515
#[repr(u8)]
@@ -66,3 +66,15 @@ impl Op {
6666
})
6767
}
6868
}
69+
70+
// impl FromStr for Op {
71+
// type Err = errors::Jerror;
72+
73+
// fn from_str(s: &str) -> Result<Self, Self::Err> {
74+
// let byte = (match s.to_lowercase().as_str() {
75+
// "nop" => Op::Nop,
76+
// ""
77+
// _ => return Err(vme!(UnknownInstruction, "instruction: {s}")),
78+
// })
79+
// }
80+
// }

jnd/src/reg.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{errors, vme};
1+
use crate::{errors, op::Op, vme};
22

33
#[derive(Debug, PartialEq, PartialOrd)]
44
pub enum Register {
@@ -17,14 +17,14 @@ impl TryFrom<u8> for Register {
1717

1818
fn try_from(value: u8) -> Result<Self, Self::Error> {
1919
Ok(match value {
20-
x if x == Register::A as u8 => Register::A,
21-
x if x == Register::B as u8 => Register::B,
22-
x if x == Register::C as u8 => Register::C,
23-
x if x == Register::M as u8 => Register::M,
24-
x if x == Register::SP as u8 => Register::SP,
25-
x if x == Register::PC as u8 => Register::PC,
26-
x if x == Register::BP as u8 => Register::BP,
27-
x if x == Register::Flags as u8 => Register::Flags,
20+
0 => Register::A,
21+
1 => Register::B,
22+
2 => Register::C,
23+
3 => Register::M,
24+
4 => Register::SP,
25+
5 => Register::PC,
26+
6 => Register::BP,
27+
7 => Register::Flags,
2828
_ => return Err(vme!(InvalidRegister, "{}", value)),
2929
})
3030
}
@@ -34,6 +34,18 @@ impl TryFrom<u16> for Register {
3434
type Error = errors::Jerror;
3535

3636
fn try_from(value: u16) -> Result<Self, Self::Error> {
37-
(value as u8).try_into()
37+
((value & 0xff) as u8).try_into()
3838
}
3939
}
40+
41+
#[macro_export]
42+
macro_rules! reg {
43+
($kind: ident) => {{
44+
$crate::reg::Register::$kind as u8
45+
}};
46+
}
47+
48+
pub trait Parser {
49+
fn encode(&self) -> u16;
50+
fn decode(&self) -> Op;
51+
}

utils/jop/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "jop"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]

utils/jop/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extern crate proc_macro;
2+
use proc_macro::TokenStream;
3+
4+
#[proc_macro_derive(AnswerFn)]
5+
pub fn derive_answer_fn(_item: TokenStream) -> TokenStream {
6+
"fn answer() -> u32 { 42 }".parse().unwrap()
7+
}

0 commit comments

Comments
 (0)