76 lines
2.1 KiB
Rust
76 lines
2.1 KiB
Rust
#[unsafe(no_mangle)]
|
|
extern "C" fn panic() -> ! {
|
|
panic!("Called panic from external code.");
|
|
}
|
|
|
|
struct Lexeme(u8);
|
|
|
|
impl Lexeme {
|
|
fn lex(&self) -> &'static str {
|
|
// SAFETY: lens contains the correct length for each lexeme, and lexemes
|
|
// contains pointers to valid 'static UTF-8 data.
|
|
unsafe {
|
|
core::str::from_utf8_unchecked(
|
|
core::slice::from_raw_parts(
|
|
(&raw const LEXEMES).read().add((self.0) as usize).read(),
|
|
(&raw const LEXEME_LENS).read().add((self.0) as usize).read(),
|
|
)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
trait AsLexeme {
|
|
fn as_lexeme(self) -> Option<Lexeme>;
|
|
}
|
|
|
|
impl AsLexeme for u8 {
|
|
fn as_lexeme(self) -> Option<Lexeme> {
|
|
match self {
|
|
1..=10 => Some(Lexeme(self)),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
unsafe extern "C" {
|
|
unsafe fn tokeniser_init(path: *const i8) -> ();
|
|
unsafe fn tokeniser_print() -> ();
|
|
unsafe fn is_ident(len: usize) -> bool;
|
|
unsafe fn is_number(len: usize) -> bool;
|
|
unsafe fn skip_whitespace() -> ();
|
|
unsafe fn find_lexeme() -> u8;
|
|
|
|
static mut LEXEMES: *mut *const u8;
|
|
static mut LEXEME_LENS: *mut usize;
|
|
static mut NUM_LEXEMES: usize;
|
|
static mut TOKENS: *mut u32;
|
|
|
|
static mut input_file: u32;
|
|
static mut buffer: *mut u8;
|
|
static mut cursor: usize;
|
|
static mut buffer_len: usize;
|
|
|
|
unsafe fn exit(code: i32) -> !;
|
|
}
|
|
|
|
fn main() {
|
|
let path = c"tests/tokens.l";
|
|
unsafe {
|
|
assert_eq!((&raw const input_file).read(), 0);
|
|
assert_eq!((&raw const buffer_len).read(), 0);
|
|
assert_eq!((&raw const cursor).read(), 0);
|
|
assert_eq!((&raw const buffer).read(), core::ptr::null_mut());
|
|
eprint!("Initializing tokeniser.. ");
|
|
tokeniser_init(path.as_ptr());
|
|
eprintln!("ok.");
|
|
eprintln!("{}: {:?}[{}..{}]", (&raw const input_file).read(), (&raw const buffer).read(), (&raw const cursor).read(), (&raw const buffer_len).read());
|
|
tokeniser_print();
|
|
|
|
find_lexeme().as_lexeme().map(|lexeme| {
|
|
eprintln!("Found lexeme: {}", lexeme.lex());
|
|
});
|
|
}
|
|
}
|