Navigation Menu

Skip to content

Commit

Permalink
add support for decimal literals
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Apr 7, 2012
1 parent 053e569 commit 054e553
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions asm.rs
Expand Up @@ -4,13 +4,22 @@ import result::result;
import result::err;
import result::extensions;

fn is_num(p: str) -> bool {
import iter::*;
let digits = iter::to_vec(uint::range(0u, 9u, _));
let digits = digits.map {|d| #fmt("%u", d) };
digits.any {|d| p.trim().starts_with(d) }
}

fn parse_num(p:str) -> result<u16, str> {
if str::find_str(p, "0x").is_none() {
ret err("expecting 0x");
}
let mut buf : [u8] = [];
for str::replace(p, "0x", "").each {|t| vec::push(buf, t as u8); };
let num = uint::parse_buf(buf, 16u);
let num = if str::find_str(p, "0x").is_some() {
let mut buf : [u8] = [];
for str::replace(p, "0x", "").each {|t| vec::push(buf, t as u8); };
uint::parse_buf(buf, 16u)
} else {
let buf = str::bytes(p);
uint::parse_buf(buf, 10u)
};
if num.is_none() {
ret err("invalid number");
}
Expand Down Expand Up @@ -47,7 +56,7 @@ fn make_val(part:str) -> result<[u16], str> {

if !str::find_char(part, '+').is_none() {
let v = str::replace(str::replace(part, "[", ""), "]", "").split_char('+');
let (reg, word) = if str::find_str(v[0], "0x").is_none() {
let (reg, word) = if !is_num(v[0]) {
if (str::len(v[0]) != 1u) {
ret err("expected register");
}
Expand Down

0 comments on commit 054e553

Please sign in to comment.