Skip to content

Commit

Permalink
[basm] Raise a warning when using . in equ label name
Browse files Browse the repository at this point in the history
  • Loading branch information
Krusty/Benediction committed Jun 10, 2024
1 parent 3a47195 commit 9ba31d8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 14 deletions.
40 changes: 39 additions & 1 deletion cpclib-asm/src/assembler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::fmt;
use std::fmt::{Debug, Display};
use std::io::{stdout, Write};
use std::ops::Neg;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use std::time::Instant;

Expand Down Expand Up @@ -351,6 +351,10 @@ impl CharsetEncoding {
/// Environment of the assembly
#[allow(missing_docs)]
pub struct Env {
/// Lookup directory when searching for a file. Must be pushed at each import directive and pop after
lookup_directory_stack: Vec<PathBuf>,


/// Current pass
pass: AssemblingPass,
options: EnvOptions,
Expand Down Expand Up @@ -451,6 +455,7 @@ pub struct Env {
impl Clone for Env {
fn clone(&self) -> Self {
Self {
lookup_directory_stack: self.lookup_directory_stack.clone(),
options: self.options.clone(),
can_skip_next_passes: (*self.can_skip_next_passes.read().unwrap().deref()).into(),
request_additional_pass: (*self.request_additional_pass.read().unwrap().deref()).into(),
Expand Down Expand Up @@ -528,6 +533,7 @@ impl fmt::Debug for Env {
impl Default for Env {
fn default() -> Self {
Self {
lookup_directory_stack: Vec::with_capacity(3),
pass: AssemblingPass::Uninitialized,
options: EnvOptions::default(),
stable_counters: StableTickerCounters::default(),
Expand Down Expand Up @@ -770,6 +776,33 @@ impl Env {
}
}


/// Handle the file search relatively to the current file
impl Env {
fn set_current_working_directory<P: Into<PathBuf>>(&mut self, p: P) {
self.lookup_directory_stack.push(p.into())
}

pub fn enter_current_working_file<P: AsRef<Path>>(&mut self, f: P) {
let f = f.as_ref();
debug_assert!(f.is_file() || f.starts_with("inner://"));
self.set_current_working_directory(f.parent().unwrap());
}

pub fn leave_current_working_file(&mut self) -> Option<PathBuf> {
self.lookup_directory_stack.pop()
}

pub fn get_current_working_directory(&self) -> Option<&Path> {
self.lookup_directory_stack.last()
.map(|p| p.as_path())
}

pub fn has_current_working_directory(&self) -> bool {
!self.lookup_directory_stack.is_empty()
}
}

/// Error handling
impl Env {
/// If the error has not been raised at the previous pass, store it and do not propagate it. Otherwise, propagate it
Expand Down Expand Up @@ -4086,6 +4119,11 @@ impl Env {
else {
let label = self.handle_global_and_local_labels(label.as_str())?;

if label.starts_with(".") {
let warning = AssemblerError::AssemblingError { msg: format!("{} is not a local label. A better name without the dot would be better", &label) };
self.warnings.push(warning);
}

// XXX Disabled behavior the 12/01/2024
// if !label.starts_with('.') {
// self.symbols_mut().set_current_label(label)?;
Expand Down
11 changes: 9 additions & 2 deletions cpclib-asm/src/assembler/processed_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ struct SwitchState<'token, T: Visited + ListingElement + Debug + Sync> {
struct IncludeState(BTreeMap<PathBuf, IncludeStateInner>);

impl IncludeState {
/// By constructon fname exists and is correct
///
fn retreive_listing(
&mut self,
env: &mut Env,
Expand Down Expand Up @@ -170,6 +172,8 @@ impl IncludeState {
self.0.get_mut(fname).unwrap()
};



// handle the listing
env.mark_included(fname.clone());

Expand Down Expand Up @@ -198,10 +202,13 @@ impl IncludeState {
}

// Visit the included listing
state.with_processed_tokens_mut(|tokens| {
env.enter_current_working_file(fname);
let res = state.with_processed_tokens_mut(|tokens| {
let tokens: &mut [ProcessedToken<'_, LocatedToken>] = &mut tokens[..];
visit_processed_tokens::<'_, LocatedToken>(tokens, env)
})?;
});
env.leave_current_working_file();
res?;

// Remove module if necessary
if namespace.is_some() {
Expand Down
26 changes: 18 additions & 8 deletions cpclib-asm/src/parser/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,26 @@ impl ParserOptions {

let fname = std::path::Path::new(fname);

// We expect the file to exists if no search_path is provided
if self.search_path.is_empty() {
if fname.is_file() {
return Ok(fname.into());
}
else {
does_not_exists.push(fname.to_str().unwrap().to_owned());
// check if file exists
if fname.is_file() {
return Ok(fname.into());
}
does_not_exists.push(fname.to_str().unwrap().to_owned());

// otherwhise, try with the current directory of the environment
if let Some(env) = env.as_ref() {
if let Some(search) = env.get_current_working_directory() {
let current_path = search.join(fname);
if current_path.is_file() {
return Ok(current_path);
} else {
does_not_exists.push(current_path.to_str().unwrap().to_owned());
}
}
}
else {

// otherwhise try with the folder set up at the beginning
{
// loop over all possibilities
for search in &self.search_path {
assert!(std::path::Path::new(&search).is_dir());
Expand Down
4 changes: 2 additions & 2 deletions cpclib-asm/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2605,7 +2605,7 @@ pub fn parse_conditional(input: &mut InnerZ80Span) -> PResult<LocatedToken, Z80P
// Here we have read the latest block
// dbg!("Everythng has been read", &input);

let _ = dbg!((
let _ = (
opt(alt((
delimited(my_space0, ':', my_space0).value(()),
delimited(my_space0, parse_comment, line_ending).value(())
Expand All @@ -2617,7 +2617,7 @@ pub fn parse_conditional(input: &mut InnerZ80Span) -> PResult<LocatedToken, Z80P
.recognize()
)
.parse_next(input)
.map_err(|e| e.add_context(&if_clone, &if_start, "End directive not found")))?;
.map_err(|e| e.add_context(&if_clone, &if_start, "End directive not found"))?;

// dbg!(unsafe{std::str::from_utf8_unchecked(input.as_bytes())}); // endif must have been eaten

Expand Down
27 changes: 26 additions & 1 deletion cpclib-basm/tests/asm/good_equ.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,29 @@
label equ 3
.label equ 2

assert label == 3
assert label == 3
assert .label == 2


other: equ 5
.label2 equ 4

assert other == 5
assert .label2 == 4


ifdef label.label
fail "label.label does not exist"
endif

ifdef other.label
fail"label.label does not exist"
endif

ifndef .label
fail ".label must exist."
endif

ifndef .label2
fail ".label2 must exist"
endif

0 comments on commit 9ba31d8

Please sign in to comment.