Skip to content

Commit 9364902

Browse files
committed
Add tilde unary operator in syntax.
1 parent 17fc5a2 commit 9364902

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

parser/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ pub enum BooleanOperator {
286286

287287
#[derive(Debug, PartialEq)]
288288
pub enum UnaryOperator {
289+
Pos,
289290
Neg,
290291
Not,
292+
Inv,
291293
}
292294

293295
#[derive(Debug, PartialEq)]

parser/src/parser.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,9 @@ pub fn read_file(filename: &Path) -> Result<String, String> {
3333

3434
pub fn parse(filename: &Path) -> Result<ast::Program, String> {
3535
info!("Parsing: {}", filename.display());
36-
match read_file(filename) {
37-
Ok(txt) => {
38-
debug!("Read contents of file: {}", txt);
39-
parse_program(&txt)
40-
}
41-
Err(msg) => Err(msg),
42-
}
36+
let txt = read_file(filename)?;
37+
debug!("Read contents of file: {}", txt);
38+
parse_program(&txt)
4339
}
4440

4541
macro_rules! do_lalr_parsing {

parser/src/python.lalrpop

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,9 @@ MulOp: ast::Operator = {
748748
};
749749

750750
Factor: ast::Expression = {
751-
"+" <e:Factor> => e,
751+
"+" <e:Factor> => ast::Expression::Unop { a: Box::new(e), op: ast::UnaryOperator::Pos },
752752
"-" <e:Factor> => ast::Expression::Unop { a: Box::new(e), op: ast::UnaryOperator::Neg },
753+
"~" <e:Factor> => ast::Expression::Unop { a: Box::new(e), op: ast::UnaryOperator::Inv },
753754
<e:Power> => e,
754755
};
755756

@@ -1013,6 +1014,7 @@ extern {
10131014
StartExpression => lexer::Tok::StartExpression,
10141015
"+" => lexer::Tok::Plus,
10151016
"-" => lexer::Tok::Minus,
1017+
"~" => lexer::Tok::Tilde,
10161018
":" => lexer::Tok::Colon,
10171019
"." => lexer::Tok::Dot,
10181020
"," => lexer::Tok::Comma,

vm/src/compile.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,10 @@ impl Compiler {
847847

848848
// Perform operation:
849849
let i = match op {
850+
ast::UnaryOperator::Pos => bytecode::UnaryOperator::Plus,
850851
ast::UnaryOperator::Neg => bytecode::UnaryOperator::Minus,
851852
ast::UnaryOperator::Not => bytecode::UnaryOperator::Not,
853+
ast::UnaryOperator::Inv => bytecode::UnaryOperator::Invert,
852854
};
853855
let i = Instruction::UnaryOperation { op: i };
854856
self.emit(i);

vm/src/stdlib/ast.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
/*
2-
* Ast standard module
3-
*
4-
* This module makes use of the parser logic, and translates all ast nodes
5-
* into python ast.AST objects.
6-
*/
1+
//! `ast` standard module for abstract syntax trees.
2+
//!
3+
//! This module makes use of the parser logic, and translates all ast nodes
4+
//! into python ast.AST objects.
75
86
extern crate rustpython_parser;
97

@@ -302,7 +300,9 @@ fn expression_to_ast(ctx: &PyContext, expression: &ast::Expression) -> PyObjectR
302300

303301
let str_op = match op {
304302
ast::UnaryOperator::Not => "Not",
303+
ast::UnaryOperator::Inv => "Invert",
305304
ast::UnaryOperator::Neg => "USub",
305+
ast::UnaryOperator::Pos => "UAdd",
306306
};
307307
let py_op = ctx.new_str(str_op.to_string());
308308
node.set_attr("op", py_op);

0 commit comments

Comments
 (0)