Add octal, binary and hexadecimal representation
This commit is contained in:
parent
25fb6bf0fd
commit
22160b0802
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
|
|
||||||
fn main() -> u32 {
|
fn main() -> u32 {
|
||||||
let value = 6;
|
let value = 0b110;
|
||||||
let other = 15;
|
let other = 0o17;
|
||||||
|
|
||||||
return value * other + 7 * -value;
|
return value * other + 7 * -value;
|
||||||
}
|
}
|
||||||
|
@ -153,6 +153,24 @@ impl Parse for PrimaryExpression {
|
|||||||
Expression(Kind::VariableName(v.clone()), stream.get_range().unwrap())
|
Expression(Kind::VariableName(v.clone()), stream.get_range().unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Token::BinaryValue(v) => {
|
||||||
|
stream.next(); // Consume octal
|
||||||
|
Expression(
|
||||||
|
Kind::Literal(Literal::Integer(
|
||||||
|
u128::from_str_radix(&v, 2).expect("Binary is not parseable as u128!"),
|
||||||
|
)),
|
||||||
|
stream.get_range().unwrap(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Token::OctalValue(v) => {
|
||||||
|
stream.next(); // Consume octal
|
||||||
|
Expression(
|
||||||
|
Kind::Literal(Literal::Integer(
|
||||||
|
u128::from_str_radix(&v, 8).expect("Octal is not parseable as u128!"),
|
||||||
|
)),
|
||||||
|
stream.get_range().unwrap(),
|
||||||
|
)
|
||||||
|
}
|
||||||
Token::HexadecimalValue(v) => {
|
Token::HexadecimalValue(v) => {
|
||||||
stream.next(); // Consume hexadecimal
|
stream.next(); // Consume hexadecimal
|
||||||
Expression(
|
Expression(
|
||||||
|
@ -19,6 +19,10 @@ pub enum Token {
|
|||||||
DecimalValue(String),
|
DecimalValue(String),
|
||||||
/// Integer number in the hexadecimal base
|
/// Integer number in the hexadecimal base
|
||||||
HexadecimalValue(String),
|
HexadecimalValue(String),
|
||||||
|
/// Integer number in the octal base
|
||||||
|
OctalValue(String),
|
||||||
|
/// Integer number in the binary base
|
||||||
|
BinaryValue(String),
|
||||||
/// Some character literal that was surrounded by 'single-quotes'.
|
/// Some character literal that was surrounded by 'single-quotes'.
|
||||||
CharLit(String),
|
CharLit(String),
|
||||||
/// Some string literal that was surrounded by "double-quotes".
|
/// Some string literal that was surrounded by "double-quotes".
|
||||||
@ -141,7 +145,9 @@ impl ToString for Token {
|
|||||||
match &self {
|
match &self {
|
||||||
Token::Identifier(ident) => ident.clone(),
|
Token::Identifier(ident) => ident.clone(),
|
||||||
Token::DecimalValue(val) => val.to_string(),
|
Token::DecimalValue(val) => val.to_string(),
|
||||||
Token::HexadecimalValue(val) => val.to_string(),
|
Token::HexadecimalValue(val) => format!("0x{}", val),
|
||||||
|
Token::OctalValue(val) => format!("0o{}", val),
|
||||||
|
Token::BinaryValue(val) => format!("0b{}", val),
|
||||||
Token::CharLit(lit) => format!("\'{}\'", lit),
|
Token::CharLit(lit) => format!("\'{}\'", lit),
|
||||||
Token::StringLit(lit) => format!("\"{}\"", lit),
|
Token::StringLit(lit) => format!("\"{}\"", lit),
|
||||||
Token::LetKeyword => String::from("let"),
|
Token::LetKeyword => String::from("let"),
|
||||||
@ -370,6 +376,18 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
|
|||||||
cursor.next();
|
cursor.next();
|
||||||
value = NumberType::Hexadecimal(String::new());
|
value = NumberType::Hexadecimal(String::new());
|
||||||
numerics = HEXADECIMAL_NUMERICS;
|
numerics = HEXADECIMAL_NUMERICS;
|
||||||
|
} else if cursor.first() == Some('o')
|
||||||
|
&& OCTAL_NUMERICS.contains(&second.to_lowercase().next().unwrap_or('.'))
|
||||||
|
{
|
||||||
|
cursor.next();
|
||||||
|
value = NumberType::Octal(String::new());
|
||||||
|
numerics = OCTAL_NUMERICS;
|
||||||
|
} else if cursor.first() == Some('b')
|
||||||
|
&& BINARY_NUMERICS.contains(&second.to_lowercase().next().unwrap_or('.'))
|
||||||
|
{
|
||||||
|
cursor.next();
|
||||||
|
value = NumberType::Binary(String::new());
|
||||||
|
numerics = BINARY_NUMERICS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while let Some(c) = cursor.first() {
|
while let Some(c) = cursor.first() {
|
||||||
@ -380,8 +398,10 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
|
|||||||
cursor.next();
|
cursor.next();
|
||||||
}
|
}
|
||||||
match value {
|
match value {
|
||||||
NumberType::Decimal(value) => Token::DecimalValue(value),
|
NumberType::Decimal(dec) => Token::DecimalValue(dec),
|
||||||
NumberType::Hexadecimal(hex) => Token::HexadecimalValue(hex),
|
NumberType::Hexadecimal(hex) => Token::HexadecimalValue(hex),
|
||||||
|
NumberType::Octal(oct) => Token::OctalValue(oct),
|
||||||
|
NumberType::Binary(bin) => Token::BinaryValue(bin),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
'-' if cursor.first() == Some('>') => {
|
'-' if cursor.first() == Some('>') => {
|
||||||
@ -440,6 +460,8 @@ fn escape_char(c: &char) -> char {
|
|||||||
enum NumberType {
|
enum NumberType {
|
||||||
Decimal(String),
|
Decimal(String),
|
||||||
Hexadecimal(String),
|
Hexadecimal(String),
|
||||||
|
Octal(String),
|
||||||
|
Binary(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AddAssign<char> for NumberType {
|
impl AddAssign<char> for NumberType {
|
||||||
@ -449,6 +471,8 @@ impl AddAssign<char> for NumberType {
|
|||||||
NumberType::Hexadecimal(val) => {
|
NumberType::Hexadecimal(val) => {
|
||||||
NumberType::Hexadecimal(val.to_owned() + &rhs.to_string())
|
NumberType::Hexadecimal(val.to_owned() + &rhs.to_string())
|
||||||
}
|
}
|
||||||
|
NumberType::Octal(val) => NumberType::Octal(val.to_owned() + &rhs.to_string()),
|
||||||
|
NumberType::Binary(val) => NumberType::Binary(val.to_owned() + &rhs.to_string()),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user