improved exer

This commit is contained in:
mlokr 2024-06-25 19:12:35 +02:00
parent 6a69042cb7
commit e07265c88b
No known key found for this signature in database
GPG key ID: DEA147DDEE644993
2 changed files with 31 additions and 19 deletions

View file

@ -97,14 +97,15 @@ pub enum TokenKind {
Sub = b'-', Sub = b'-',
Dot = b'.', Dot = b'.',
Div = b'/', Div = b'/',
Shl = b'0', // Unused = 2-6
Shr = b'1', Shr = b'<' - 5,
// Unused = 2-9 // Unused = 8
Shl = b'>' - 5,
Colon = b':', Colon = b':',
Semi = b';', Semi = b';',
Gt = b'>',
Assign = b'=',
Lt = b'<', Lt = b'<',
Assign = b'=',
Gt = b'>',
Que = b'?', Que = b'?',
Directive = b'@', Directive = b'@',
@ -248,7 +249,11 @@ impl<'a> Lexer<'a> {
} }
fn peek(&self) -> Option<u8> { fn peek(&self) -> Option<u8> {
self.bytes.get(self.pos as usize).copied() if std::intrinsics::unlikely(self.pos >= self.bytes.len() as u32) {
None
} else {
Some(unsafe { *self.bytes.get_unchecked(self.pos as usize) })
}
} }
fn advance(&mut self) -> Option<u8> { fn advance(&mut self) -> Option<u8> {
@ -276,8 +281,15 @@ impl<'a> Lexer<'a> {
} }
}; };
let mut kind = match c { let identity = |s: u8| unsafe { std::mem::transmute::<u8, T>(s) };
let kind = match c {
..=b' ' => continue, ..=b' ' => continue,
b'@' | b'$' => {
start += 1;
advance_ident(self);
identity(c)
}
b'0'..=b'9' => { b'0'..=b'9' => {
while let Some(b'0'..=b'9') = self.peek() { while let Some(b'0'..=b'9') = self.peek() {
self.advance(); self.advance();
@ -301,21 +313,19 @@ impl<'a> Lexer<'a> {
} }
b'.' if self.advance_if(b'{') => T::Ctor, b'.' if self.advance_if(b'{') => T::Ctor,
b'.' if self.advance_if(b'(') => T::Tupl, b'.' if self.advance_if(b'(') => T::Tupl,
b'<' if self.advance_if(b'<') => T::Shl, b'<' | b'>' if self.advance_if(c) => {
b'>' if self.advance_if(b'>') => T::Shr, identity(c - 5 + 128 * self.advance_if(b'=') as u8)
}
b'&' if self.advance_if(b'&') => T::And, b'&' if self.advance_if(b'&') => T::And,
b'|' if self.advance_if(b'|') => T::Or, b'|' if self.advance_if(b'|') => T::Or,
b => unsafe { std::mem::transmute::<u8, T>(b) }, b':' | b'=' | b'!' | b'<' | b'>' | b'|' | b'+' | b'-' | b'*' | b'/' | b'%'
}; | b'^' | b'&'
if self.advance_if(b'=') =>
if matches!(kind, T::Directive | T::CtIdent) {
start += 1;
advance_ident(self);
} else if ascii_mask(b":=!<>|+-*/%^&01") & (1u128 << kind as u8) != 0
&& self.advance_if(b'=')
{ {
kind = unsafe { std::mem::transmute::<u8, T>(kind as u8 + 128) }; identity(c + 128)
} }
_ => identity(c),
};
return Token { return Token {
kind, kind,

View file

@ -1,4 +1,5 @@
#![feature(vec_pop_if)] #![feature(vec_pop_if)]
#![feature(core_intrinsics)]
#![feature(new_uninit)] #![feature(new_uninit)]
#![feature(anonymous_lifetime_in_impl_trait)] #![feature(anonymous_lifetime_in_impl_trait)]
#![feature(inline_const_pat)] #![feature(inline_const_pat)]
@ -15,6 +16,7 @@
#![feature(ptr_metadata)] #![feature(ptr_metadata)]
#![feature(const_mut_refs)] #![feature(const_mut_refs)]
#![feature(slice_ptr_get)] #![feature(slice_ptr_get)]
#![allow(internal_features)]
#![allow(clippy::format_collect)] #![allow(clippy::format_collect)]
use std::{ use std::{