Add parsing for the rest of binary and unary operators
This commit is contained in:
parent
260963f7cd
commit
ed5c9d8b8f
70
src/ast.rs
70
src/ast.rs
@ -558,8 +558,10 @@ pub struct PrimaryExpression(Node<Expression>);
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum UnaryOperator {
|
pub enum UnaryOperator {
|
||||||
|
Not,
|
||||||
Negation,
|
Negation,
|
||||||
Length,
|
Length,
|
||||||
|
BitNot,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for UnaryOperator {
|
impl Parse for UnaryOperator {
|
||||||
@ -568,6 +570,8 @@ impl Parse for UnaryOperator {
|
|||||||
match token {
|
match token {
|
||||||
Token::Symbol('-') => Ok(UnaryOperator::Negation),
|
Token::Symbol('-') => Ok(UnaryOperator::Negation),
|
||||||
Token::Symbol('#') => Ok(UnaryOperator::Length),
|
Token::Symbol('#') => Ok(UnaryOperator::Length),
|
||||||
|
Token::Keyword(Keyword::Not) => Ok(UnaryOperator::Not),
|
||||||
|
Token::Symbol('~') => Ok(UnaryOperator::BitNot),
|
||||||
_ => Err(stream.expected_err("unop")),
|
_ => Err(stream.expected_err("unop")),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -580,13 +584,28 @@ impl Parse for UnaryOperator {
|
|||||||
pub enum BinaryOperator {
|
pub enum BinaryOperator {
|
||||||
And,
|
And,
|
||||||
Or,
|
Or,
|
||||||
|
Concat,
|
||||||
|
|
||||||
LessThan,
|
LessThan,
|
||||||
LessThanOrEqual,
|
LessThanOrEqual,
|
||||||
GreaterThan,
|
GreaterThan,
|
||||||
GreaterThanOrEqual,
|
GreaterThanOrEqual,
|
||||||
Equal,
|
Equal,
|
||||||
|
NotEqual,
|
||||||
|
|
||||||
Add,
|
Add,
|
||||||
Sub,
|
Sub,
|
||||||
|
Mult,
|
||||||
|
Div,
|
||||||
|
IDiv,
|
||||||
|
Mod,
|
||||||
|
Exp,
|
||||||
|
|
||||||
|
BitAnd,
|
||||||
|
BitOr,
|
||||||
|
BitXOr,
|
||||||
|
BitSRight,
|
||||||
|
BitSLeft,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BinaryOperator {
|
impl BinaryOperator {
|
||||||
@ -594,13 +613,32 @@ impl BinaryOperator {
|
|||||||
match self {
|
match self {
|
||||||
BinaryOperator::Or => 0,
|
BinaryOperator::Or => 0,
|
||||||
BinaryOperator::And => 1,
|
BinaryOperator::And => 1,
|
||||||
|
|
||||||
BinaryOperator::LessThan => 10,
|
BinaryOperator::LessThan => 10,
|
||||||
BinaryOperator::LessThanOrEqual => 10,
|
BinaryOperator::LessThanOrEqual => 10,
|
||||||
BinaryOperator::GreaterThan => 10,
|
BinaryOperator::GreaterThan => 10,
|
||||||
BinaryOperator::GreaterThanOrEqual => 10,
|
BinaryOperator::GreaterThanOrEqual => 10,
|
||||||
BinaryOperator::Equal => 10,
|
BinaryOperator::Equal => 10,
|
||||||
BinaryOperator::Add => 20,
|
BinaryOperator::NotEqual => 10,
|
||||||
BinaryOperator::Sub => 20,
|
|
||||||
|
BinaryOperator::BitOr => 20,
|
||||||
|
BinaryOperator::BitXOr => 21,
|
||||||
|
BinaryOperator::BitAnd => 22,
|
||||||
|
|
||||||
|
BinaryOperator::BitSLeft => 25,
|
||||||
|
BinaryOperator::BitSRight => 25,
|
||||||
|
|
||||||
|
BinaryOperator::Concat => 30,
|
||||||
|
|
||||||
|
BinaryOperator::Add => 40,
|
||||||
|
BinaryOperator::Sub => 40,
|
||||||
|
|
||||||
|
BinaryOperator::Mult => 50,
|
||||||
|
BinaryOperator::Div => 50,
|
||||||
|
BinaryOperator::IDiv => 50,
|
||||||
|
BinaryOperator::Mod => 50,
|
||||||
|
|
||||||
|
BinaryOperator::Exp => 60,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -626,10 +664,38 @@ impl Parse for BinaryOperator {
|
|||||||
stream.next();
|
stream.next();
|
||||||
Ok(BinaryOperator::Equal)
|
Ok(BinaryOperator::Equal)
|
||||||
}
|
}
|
||||||
|
(Token::Symbol('~'), Some(Token::Symbol('='))) => {
|
||||||
|
stream.next();
|
||||||
|
Ok(BinaryOperator::NotEqual)
|
||||||
|
}
|
||||||
|
|
||||||
|
(Token::Symbol('>'), Some(Token::Symbol('>'))) => {
|
||||||
|
stream.next();
|
||||||
|
Ok(BinaryOperator::BitSRight)
|
||||||
|
}
|
||||||
|
(Token::Symbol('<'), Some(Token::Symbol('<'))) => {
|
||||||
|
stream.next();
|
||||||
|
Ok(BinaryOperator::BitSLeft)
|
||||||
|
}
|
||||||
|
|
||||||
|
(Token::Symbol('/'), Some(Token::Symbol('/'))) => {
|
||||||
|
stream.next();
|
||||||
|
Ok(BinaryOperator::IDiv)
|
||||||
|
}
|
||||||
|
|
||||||
(Token::Symbol('<'), _) => Ok(BinaryOperator::LessThan),
|
(Token::Symbol('<'), _) => Ok(BinaryOperator::LessThan),
|
||||||
(Token::Symbol('>'), _) => Ok(BinaryOperator::GreaterThan),
|
(Token::Symbol('>'), _) => Ok(BinaryOperator::GreaterThan),
|
||||||
|
|
||||||
(Token::Symbol('+'), _) => Ok(BinaryOperator::Add),
|
(Token::Symbol('+'), _) => Ok(BinaryOperator::Add),
|
||||||
(Token::Symbol('-'), _) => Ok(BinaryOperator::Sub),
|
(Token::Symbol('-'), _) => Ok(BinaryOperator::Sub),
|
||||||
|
(Token::Symbol('*'), _) => Ok(BinaryOperator::Mult),
|
||||||
|
(Token::Symbol('/'), _) => Ok(BinaryOperator::Div),
|
||||||
|
(Token::Symbol('%'), _) => Ok(BinaryOperator::Mod),
|
||||||
|
(Token::Symbol('^'), _) => Ok(BinaryOperator::Exp),
|
||||||
|
|
||||||
|
(Token::Symbol('&'), _) => Ok(BinaryOperator::BitAnd),
|
||||||
|
(Token::Symbol('|'), _) => Ok(BinaryOperator::BitOr),
|
||||||
|
(Token::Symbol('~'), _) => Ok(BinaryOperator::BitXOr),
|
||||||
_ => Err(stream.expected_err("binop")),
|
_ => Err(stream.expected_err("binop")),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -858,6 +858,18 @@ impl Expression {
|
|||||||
*rhs.get(0).unwrap(),
|
*rhs.get(0).unwrap(),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
BinaryOperator::Concat => todo!(),
|
||||||
|
BinaryOperator::NotEqual => todo!(),
|
||||||
|
BinaryOperator::Mult => todo!(),
|
||||||
|
BinaryOperator::Div => todo!(),
|
||||||
|
BinaryOperator::IDiv => todo!(),
|
||||||
|
BinaryOperator::Mod => todo!(),
|
||||||
|
BinaryOperator::Exp => todo!(),
|
||||||
|
BinaryOperator::BitAnd => todo!(),
|
||||||
|
BinaryOperator::BitOr => todo!(),
|
||||||
|
BinaryOperator::BitXOr => todo!(),
|
||||||
|
BinaryOperator::BitSRight => todo!(),
|
||||||
|
BinaryOperator::BitSLeft => todo!(),
|
||||||
};
|
};
|
||||||
(instructions, vec![reg])
|
(instructions, vec![reg])
|
||||||
}
|
}
|
||||||
@ -873,6 +885,8 @@ impl Expression {
|
|||||||
UnaryOperator::Length => {
|
UnaryOperator::Length => {
|
||||||
instructions.push(PreInstr::Instr(Instruction::Len(*reg, *reg)))
|
instructions.push(PreInstr::Instr(Instruction::Len(*reg, *reg)))
|
||||||
}
|
}
|
||||||
|
UnaryOperator::Not => todo!(),
|
||||||
|
UnaryOperator::BitNot => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(instructions, registers)
|
(instructions, registers)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user