make hexadecimal generic over input

This commit is contained in:
notafile 2022-01-04 19:09:33 +01:00
parent e33dfdc340
commit 0699278eab
4 changed files with 26 additions and 18 deletions

View File

@ -1,25 +1,37 @@
use std::ops::RangeFrom;
use std::ops::{RangeFrom, RangeTo};
use nom::{
bytes::complete::tag,
character::complete::{char, one_of},
combinator::{map_res, recognize},
multi::{many0, many1},
sequence::{preceded, terminated},
AsChar, FindToken, InputIter, InputLength, Offset, Slice
};
use crate::parser::IResult;
pub fn hexadecimal(input: &str) -> IResult<&str, u64> {
pub fn hexadecimal<'a, I>(input: I) -> IResult<I, u64>
where
// TODO: ask if there is any way to avoid this hellish constraint
I: InputIter
+ nom::Slice<RangeFrom<usize>>
+ InputLength
+ Clone
+ Offset
+ Into<&'a str>
+ Slice<RangeTo<usize>>,
<I as InputIter>::Item: AsChar + Copy,
&'a str: FindToken<<I as InputIter>::Item>
{
map_res(
preceded(
tag("h"),
char('h'),
recognize(many1(terminated(
one_of("0123456789abcdefABCDEF"),
many0(char('_')),
))),
),
|out: &str| u64::from_str_radix(&str::replace(&out, "_", ""), 16),
|out: I| u64::from_str_radix(&str::replace(&out.into(), "_", ""), 16),
)(input)
}
@ -27,12 +39,6 @@ pub fn hexadecimal(input: &str) -> IResult<&str, u64> {
mod test {
use super::*;
#[test]
fn test_dec() {
assert_eq!(decimal("123").unwrap().1, 123);
assert_eq!(decimal("0123").unwrap().1, 123);
}
#[test]
fn test_hex() {
assert_eq!(hexadecimal("hfF").unwrap().1, 0xff);

View File

@ -3,9 +3,9 @@ mod parser;
mod rtlil;
use nom::error::convert_error;
use std::path::PathBuf;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
@ -20,7 +20,9 @@ fn main() {
let opt = Opt::from_args();
let mut infile = File::open(opt.input).expect("could not open file");
let mut input = String::new();
infile.read_to_string(&mut input).expect("error reading file");
infile
.read_to_string(&mut input)
.expect("error reading file");
let input: &str = input.as_str();
let parsed = parser::parse(&input);
match parsed {

View File

@ -16,7 +16,7 @@ type Span<'a> = LocatedSpan<&'a str>;
// custom IResult type for VerboseError
pub type IResult<I, O, E = VerboseError<I>> = nom::IResult<I, O, E>;
use crate::literals::{hexadecimal};
use crate::literals::hexadecimal;
fn ws0<'a, F: 'a, O, E: ParseError<&'a str>>(
inner: F,

View File

@ -208,23 +208,23 @@ fn lower_expression(module: &mut Module, expr: &parser::Expression) -> String {
let arg_b = args_resolved.next().unwrap();
let cell_id = module.make_genid("and");
builtin_binop_cell("$and", &cell_id, &arg_a, &arg_b, &output_gen_id)
},
}
"xor" => {
let arg_a = args_resolved.next().unwrap();
let arg_b = args_resolved.next().unwrap();
let cell_id = module.make_genid("xor");
builtin_binop_cell("$xor", &cell_id, &arg_a, &arg_b, &output_gen_id)
},
}
"not" => {
let arg_a = args_resolved.next().unwrap();
let cell_id = module.make_genid("not");
builtin_unop_cell("$not", &cell_id, &arg_a, &output_gen_id)
},
}
"reduce_or" => {
let arg_a = args_resolved.next().unwrap();
let cell_id = module.make_genid("reduce_or");
builtin_unop_cell("$reduce_or", &cell_id, &arg_a, &output_gen_id)
},
}
_ => todo!("unknown function"),
};
module.cells.push(cell);