Skip to content

Commit

Permalink
Properly handle winape symbol export
Browse files Browse the repository at this point in the history
  • Loading branch information
rgiot committed Aug 17, 2023
1 parent 1c33f6d commit e7e0a1b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 34 deletions.
6 changes: 3 additions & 3 deletions cpclib-asm/src/assembler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use self::function::{Function, FunctionBuilder, HardCodedFunction};
use self::listing_output::*;
use self::processed_token::ProcessedToken;
use self::report::SavedFile;
use self::symbols_output::SymbolOutputGenerator;
use self::symbols_output::{SymbolOutputGenerator, SymbolOutputFormat};
use crate::assembler::processed_token::visit_processed_tokens;
use crate::delayed_command::*;
use crate::page_info::PageInformation;
Expand Down Expand Up @@ -1514,8 +1514,8 @@ impl Env {
#[allow(missing_docs)]
impl Env {
/// Write in w the list of symbols
pub fn generate_symbols_output<W: Write>(&self, w: &mut W) -> std::io::Result<()> {
self.symbols_output.generate(w, self.symbols())
pub fn generate_symbols_output<W: Write>(&self, w: &mut W, fmt: SymbolOutputFormat) -> std::io::Result<()> {
self.symbols_output.generate(w, self.symbols(), fmt)
}

/// Visit all the tokens of the slice of tokens.
Expand Down
108 changes: 82 additions & 26 deletions cpclib-asm/src/assembler/symbols_output.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,88 @@
use std::io::Write;
use std::str::FromStr;

use cpclib_common::itertools::Itertools;
use cpclib_tokens::symbols::{Symbol, SymbolsTableTrait, Value};
use cpclib_tokens::ExprResult;

pub enum SymbolOutputFormat{
Basm,
Winape
}

impl SymbolOutputFormat {
pub fn format(&self, k: &Symbol, v: &Value) -> String {
match self {
SymbolOutputFormat::Basm => {
match v {
Value::Address(a) => {
format!("{} equ #{:04X}", k.value(), a.address())
}
Value::Expr(ExprResult::Value(i)) => {
format!("{} equ #{:04X}", k.value(), i)
}
Value::Expr(ExprResult::Bool(b)) => {
format!("{} equ {}", k.value(), *b)
}
Value::Expr(e @ ExprResult::Float(_f)) => {
format!("{} equ #{:04X}", k.value(), e.int().unwrap())
}
Value::Expr(ExprResult::String(s)) => {
format!("{} equ {}", k.value(), s)
}
Value::Expr(l @ ExprResult::List(_)) => {
format!("{} equ {}", k.value(), l)
}
Value::Expr(m @ ExprResult::Matrix { .. }) => {
format!("{} equ {}", k.value(), m)
}

_ => unimplemented!("{:?}", v)
}
},
SymbolOutputFormat::Winape => {
match v {
Value::Address(a) => {
format!("{} #{:X}", k.value(), a.address())
}
Value::Expr(ExprResult::Value(i)) => {
format!("{} #{:X}", k.value(), i)
}
Value::Expr(ExprResult::Bool(b)) => {
format!("{} {}", k.value(), *b)
}
Value::Expr(e @ ExprResult::Float(_f)) => {
format!("{} #{:X}", k.value(), e.int().unwrap())
}
Value::Expr(ExprResult::String(s)) => {
"".to_owned() // ignored by winape
}
Value::Expr(l @ ExprResult::List(_)) => {
"".to_owned() // ignored by winape
}
Value::Expr(m @ ExprResult::Matrix { .. }) => {
"".to_owned() // ignored by winape
}

_ => unimplemented!("{:?}", v)
}
},
}
}
}

impl FromStr for SymbolOutputFormat {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"basm" => Ok(Self::Basm),
"winape" => Ok(Self::Winape),
_ => Err(format!("Wrong symbol format {s}"))
}
}
}

/// Manage the generation of the symbols output.
/// Could be parametrize by some directives
#[derive(Clone)]
Expand Down Expand Up @@ -32,39 +111,16 @@ impl SymbolOutputGenerator {
pub fn generate<W: Write>(
&self,
w: &mut W,
symbs: &impl SymbolsTableTrait
symbs: &impl SymbolsTableTrait,
format: SymbolOutputFormat
) -> std::io::Result<()> {
for (k, v) in symbs
.expression_symbol()
.iter()
.filter(|(s, _v)| self.keep_symbol(s))
.sorted_by_key(|(s, _v)| s.to_string().to_ascii_lowercase())
{
match v {
Value::Address(a) => {
writeln!(w, "{} equ #{:04X}", k.value(), a.address())
}
Value::Expr(ExprResult::Value(i)) => {
writeln!(w, "{} equ #{:04X}", k.value(), i)
}
Value::Expr(ExprResult::Bool(b)) => {
writeln!(w, "{} equ {}", k.value(), *b)
}
Value::Expr(e @ ExprResult::Float(_f)) => {
writeln!(w, "{} equ #{:04X}", k.value(), e.int().unwrap())
}
Value::Expr(ExprResult::String(s)) => {
writeln!(w, "{} equ {}", k.value(), s)
}
Value::Expr(l @ ExprResult::List(_)) => {
writeln!(w, "{} equ {}", k.value(), l)
}
Value::Expr(m @ ExprResult::Matrix { .. }) => {
writeln!(w, "{} equ {}", k.value(), m)
}

_ => unimplemented!("{:?}", v)
}?;
writeln!(w, "{}", format.format(k, v))?;
}

Ok(())
Expand Down
14 changes: 12 additions & 2 deletions cpclib-basm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use std::fs::{self, File};
use std::io;
use std::io::Write;
use std::path::Path;
use std::str::FromStr;

use cpclib_asm::assembler::file::{get_filename, handle_source_encoding};
use cpclib_asm::preamble::*;
use cpclib_asm::preamble::symbols_output::SymbolOutputFormat;
use cpclib_asm::progress::{normalize, Progress};
use cpclib_common::clap::builder::{PossibleValue, PossibleValuesParser};
use cpclib_common::clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command, ValueHint};
Expand Down Expand Up @@ -293,8 +295,10 @@ pub fn assemble<'arg>(
})?;

if let Some(dest) = matches.get_one::<String>("SYMBOLS_OUTPUT") {
let kind = matches.get_one::<String>("SYMBOLS_KIND").unwrap();
let kind = SymbolOutputFormat::from_str(kind).unwrap();
if dest == "-" {
env.generate_symbols_output(&mut std::io::stdout())
env.generate_symbols_output(&mut std::io::stdout(), kind)
}
else {
let mut f = File::create(dest).map_err(|e| {
Expand All @@ -303,7 +307,7 @@ pub fn assemble<'arg>(
ctx: format!("creating {}", dest)
}
})?;
env.generate_symbols_output(&mut f)
env.generate_symbols_output(&mut f, kind)
}
.map_err(|err| {
BasmError::ListingGeneration {
Expand Down Expand Up @@ -578,6 +582,12 @@ pub fn build_args_parser() -> clap::Command {
.long("sym")
.value_hint(ValueHint::FilePath)
)
.arg(Arg::new("SYMBOLS_KIND")
.help("Format of the output symbols file")
.long("sym_kind")
.value_parser(["winape", "basm"])
.default_value("basm")
)
.group(
ArgGroup::new("ANY_OUTPUT")
.args(&["DB_LIST", "OUTPUT"])
Expand Down
6 changes: 3 additions & 3 deletions cpclib-bndbuild/tests/dummy/bndbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
dep: dummy.sna
help: Ask to build the snapshot file without explicitly giving its name

- tgt: dummy.sna dummy.lst
- tgt: dummy.sna dummy.lst dummy.sym
dep: dummy_code.asm dummy_logo.o dummy_logo_palette.bin
cmd: basm dummy_code.asm --snapshot -o dummy.sna --lst dummy.lst
cmd: basm dummy_code.asm --snapshot -o dummy.sna --lst dummy.lst --sym dummy.sym --sym_kind winape
help: Generate the snapshot file using basm

- tgt: dummy_logo.o dummy_logo_palette.bin
Expand Down Expand Up @@ -40,5 +40,5 @@

- tgt: winape
dep: build
cmd: extern .\tools\winape\WinApe.exe /SN:../../dummy.sna
cmd: extern .\tools\winape\WinApe.exe /SYM:dummy.sym /SN:../../dummy.sna
help: Launch Winape as an external process (all other commands are embedded in bndbuild). Hopefully it works !!

0 comments on commit e7e0a1b

Please sign in to comment.