mirror of
https://github.com/Noratrieb/crapderive.git
synced 2026-01-14 16:45:08 +01:00
initial commit
This commit is contained in:
commit
ca0e376e79
6 changed files with 590 additions and 0 deletions
9
src/main.rs
Normal file
9
src/main.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use std::io;
|
||||
|
||||
mod parser;
|
||||
|
||||
fn main() -> Result<(), io::Error> {
|
||||
let file = std::fs::read_to_string("test.asm")?;
|
||||
parser::run(&file);
|
||||
Ok(())
|
||||
}
|
||||
48
src/parser.rs
Normal file
48
src/parser.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
use dbg_pls::DebugPls;
|
||||
use logos::{Lexer, Logos};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Logos, DebugPls)]
|
||||
pub enum Token<'a> {
|
||||
#[token("mov")]
|
||||
Mov,
|
||||
#[token("jmp")]
|
||||
Jmp,
|
||||
#[token("je")]
|
||||
Je,
|
||||
#[token("cmp")]
|
||||
Cmp,
|
||||
#[token("add")]
|
||||
Add,
|
||||
#[token("sub")]
|
||||
Sub,
|
||||
#[token("mul")]
|
||||
Mul,
|
||||
#[token("div")]
|
||||
Div,
|
||||
#[token("[")]
|
||||
BracketOpen,
|
||||
#[token("]")]
|
||||
BracketClose,
|
||||
#[token(",")]
|
||||
Comma,
|
||||
#[regex(r"\w+:")]
|
||||
Label(&'a str),
|
||||
#[regex(r"[0-9]+", |lex| lex.slice().parse::<u64>())]
|
||||
Number(u64),
|
||||
#[regex(r"[a-zA-Z]\w+")]
|
||||
Word(&'a str),
|
||||
|
||||
#[regex(r"[ \t\n\f]+", logos::skip)]
|
||||
#[regex("//[^\n]*", logos::skip)]
|
||||
#[error]
|
||||
Error,
|
||||
}
|
||||
|
||||
pub fn lex(src: &str) -> Lexer<'_, Token<'_>> {
|
||||
<Token as Logos>::lexer(src)
|
||||
}
|
||||
|
||||
pub fn run(src: &str) {
|
||||
let tokens = lex(src).collect::<Vec<_>>();
|
||||
dbg_pls::color!(tokens);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue