Skip to content

Commit

Permalink
refactor: move some stuff around
Browse files Browse the repository at this point in the history
  • Loading branch information
voximity committed Mar 10, 2023
1 parent 934b4bc commit a8f911b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/assembler/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::BTreeMap, ops::Range};

use super::IndexedChars;
use crate::util::IndexedChars;

#[derive(Default, Debug, Clone)]
pub struct Lexeme {
Expand Down Expand Up @@ -138,7 +138,7 @@ impl<'a> Lexer<'a> {
if self.peek_is(|c| c == ':') {
// a label marker
self.chars.next();
slice.end = self.chars.offset;
slice.end = self.chars.offset();

lexemes.push(Lexeme {
slice,
Expand Down
49 changes: 0 additions & 49 deletions src/assembler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,2 @@
use std::str::CharIndices;

pub mod inst;
pub mod lexer;

pub struct IndexedChars<'a> {
slice: &'a str,
iter: CharIndices<'a>,
peeked: Option<Option<(usize, char)>>,
offset: usize,
}

impl<'a> Iterator for IndexedChars<'a> {
type Item = (usize, char);

fn next(&mut self) -> Option<Self::Item> {
match self.peeked.take().unwrap_or_else(|| self.iter.next()) {
Some((offset, c)) => {
self.offset = self.peek_boundary();
Some((offset, c))
}
None => {
self.offset = self.slice.len();
None
}
}
}
}

impl<'a> IndexedChars<'a> {
pub fn new(slice: &'a str) -> Self {
Self {
slice,
iter: slice.char_indices(),
peeked: None,
offset: 0,
}
}

pub fn offset(&self) -> usize {
self.offset
}

pub fn peek(&mut self) -> Option<&(usize, char)> {
self.peeked.get_or_insert_with(|| self.iter.next()).as_ref()
}

fn peek_boundary(&mut self) -> usize {
self.peek().map(|(idx, _)| *idx).unwrap_or(self.slice.len())
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ mod assembler;
mod editor;
mod highlighting;
mod output;
mod par_buf;
mod simulator;
mod util;

fn main() {
eframe::run_native(
Expand Down
2 changes: 1 addition & 1 deletion src/output.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::par_buf::ParBuf;
use crate::util::ParBuf;

#[derive(Debug, Default)]
pub enum OutputTab {
Expand Down
48 changes: 48 additions & 0 deletions src/util/indexed_chars.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::str::CharIndices;

pub struct IndexedChars<'a> {
slice: &'a str,
iter: CharIndices<'a>,
peeked: Option<Option<(usize, char)>>,
offset: usize,
}

impl<'a> Iterator for IndexedChars<'a> {
type Item = (usize, char);

fn next(&mut self) -> Option<Self::Item> {
match self.peeked.take().unwrap_or_else(|| self.iter.next()) {
Some((offset, c)) => {
self.offset = self.peek_boundary();
Some((offset, c))
}
None => {
self.offset = self.slice.len();
None
}
}
}
}

impl<'a> IndexedChars<'a> {
pub fn new(slice: &'a str) -> Self {
Self {
slice,
iter: slice.char_indices(),
peeked: None,
offset: 0,
}
}

pub fn offset(&self) -> usize {
self.offset
}

pub fn peek(&mut self) -> Option<&(usize, char)> {
self.peeked.get_or_insert_with(|| self.iter.next()).as_ref()
}

pub fn peek_boundary(&mut self) -> usize {
self.peek().map(|(idx, _)| *idx).unwrap_or(self.slice.len())
}
}
5 changes: 5 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod indexed_chars;
mod par_buf;

pub use indexed_chars::*;
pub use par_buf::*;
File renamed without changes.

0 comments on commit a8f911b

Please sign in to comment.