Mercurial > python-compiler.rs
comparison src/ast_type.rs @ 5:ddf372373a77
Add ast.For, ast.UnaryOp, and Sub and Div to ast.BinOp.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 31 May 2016 04:15:00 +0100 |
parents | f27a4aee9dfa |
children | 6f2bf13f4cb5 |
comparison
equal
deleted
inserted
replaced
4:f27a4aee9dfa | 5:ddf372373a77 |
---|---|
82 for statement in orelse { | 82 for statement in orelse { |
83 self.visit_statement(statement); | 83 self.visit_statement(statement); |
84 } | 84 } |
85 Type::Bottom | 85 Type::Bottom |
86 }, | 86 }, |
87 Statement::For(target, iter, body, orelse) => { | |
88 self.visit_expr(target); | |
89 self.visit_expr(iter); | |
90 for statement in body { | |
91 self.visit_statement(statement); | |
92 } | |
93 for statement in orelse { | |
94 self.visit_statement(statement); | |
95 } | |
96 Type::Bottom | |
97 }, | |
87 Statement::Assign(targets, value) => { | 98 Statement::Assign(targets, value) => { |
88 let type_ = self.visit_expr(value); | 99 let type_ = self.visit_expr(value); |
89 if targets.len() != 1 { | 100 if targets.len() != 1 { |
90 panic!(); | 101 panic!(); |
91 } | 102 } |
123 } | 134 } |
124 | 135 |
125 fn visit_expr(&mut self, expr: Expr) -> Type { | 136 fn visit_expr(&mut self, expr: Expr) -> Type { |
126 let expr_str = format!("{:?}", expr); | 137 let expr_str = format!("{:?}", expr); |
127 let type_ = match expr { | 138 let type_ = match expr { |
139 Expr::UnaryOp(op, operand) => { | |
140 let type_operand = self.visit_expr(*operand); | |
141 type_operand | |
142 }, | |
128 Expr::BinOp(left, op, right) => { | 143 Expr::BinOp(left, op, right) => { |
129 let type_left = self.visit_expr(*left); | 144 let type_left = self.visit_expr(*left); |
130 let type_right = self.visit_expr(*right); | 145 let type_right = self.visit_expr(*right); |
131 | 146 |
132 if op == BinOp::BinMult && type_left == Type::Str && type_right == Type::Int { | 147 if op == BinOp::BinMult && type_left == Type::Str && type_right == Type::Int { |