Parse binary literal
This commit is contained in:
parent
a6b336d84c
commit
bd6bf2f4bb
@ -448,10 +448,11 @@ impl Parser {
|
|||||||
|
|
||||||
fn int_literal(&mut self) -> ParseResult<Expression> {
|
fn int_literal(&mut self) -> ParseResult<Expression> {
|
||||||
use self::Expression::*;
|
use self::Expression::*;
|
||||||
let digits = self.digits()?;
|
|
||||||
match self.next() {
|
match self.next() {
|
||||||
BinNumberSigil => {
|
BinNumberSigil => {
|
||||||
unimplemented!()
|
let digits = self.digits()?;
|
||||||
|
let n = parse_binary(digits)?;
|
||||||
|
Ok(IntLiteral(n))
|
||||||
},
|
},
|
||||||
HexNumberSigil => {
|
HexNumberSigil => {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
@ -493,6 +494,20 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_binary(digits: String) -> ParseResult<u64> {
|
||||||
|
let mut result: u64 = 0;
|
||||||
|
let mut multiplier = 1;
|
||||||
|
for d in digits.chars().rev() {
|
||||||
|
match d {
|
||||||
|
'1' => result += multiplier,
|
||||||
|
'0' => (),
|
||||||
|
_ => return ParseError::new("Encountered a character not '1' or '0 while parsing a binary literal"),
|
||||||
|
}
|
||||||
|
multiplier *= 2;
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse(input: Vec<Token>) -> Result<AST, ParseError> {
|
pub fn parse(input: Vec<Token>) -> Result<AST, ParseError> {
|
||||||
let mut parser = Parser::new(input);
|
let mut parser = Parser::new(input);
|
||||||
parser.program()
|
parser.program()
|
||||||
|
Loading…
Reference in New Issue
Block a user