parser: simplify finishAssignment, add StarEqual support
This commit is contained in:
		
							parent
							
								
									a0808b54aa
								
							
						
					
					
						commit
						eb7a485206
					
				
					 4 changed files with 29 additions and 24 deletions
				
			
		|  | @ -58,6 +58,8 @@ fn main(a int) int { | ||||||
| 	// those should yield the same ast | 	// those should yield the same ast | ||||||
| 	str.len = str.len + 1 | 	str.len = str.len + 1 | ||||||
| 	str.len += 1 | 	str.len += 1 | ||||||
|  | 	str.len -= 1 | ||||||
|  | 	str.len *= 1 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn (v Typ) voidfunc() {} | fn (v Typ) voidfunc() {} | ||||||
|  |  | ||||||
|  | @ -702,7 +702,7 @@ pub const Parser = struct { | ||||||
|         var expr = try self.parseOr(); |         var expr = try self.parseOr(); | ||||||
| 
 | 
 | ||||||
|         if (self.compareAnyOf(&[_]TokenType{ |         if (self.compareAnyOf(&[_]TokenType{ | ||||||
|             .ColonEqual, .Equal, .PlusEqual, .MinusEqual, |             .ColonEqual, .Equal, .PlusEqual, .MinusEqual, .StarEqual, | ||||||
|         })) { |         })) { | ||||||
|             return try self.finishAssignment(expr, mutable); |             return try self.finishAssignment(expr, mutable); | ||||||
|         } |         } | ||||||
|  | @ -718,22 +718,33 @@ pub const Parser = struct { | ||||||
|         // TODO convert binary's op field from Token to |         // TODO convert binary's op field from Token to | ||||||
|         // something else, maybe enum'd |         // something else, maybe enum'd | ||||||
| 
 | 
 | ||||||
|  |         const new_op_ttype: TokenType = switch (op.ttype) { | ||||||
|  |             .ColonEqual => TokenType.ColonEqual, | ||||||
|  |             .Equal => .Equal, | ||||||
|  | 
 | ||||||
|  |             .PlusEqual => .Plus, | ||||||
|  |             .MinusEqual => .Minus, | ||||||
|  |             .StarEqual => .Star, | ||||||
|  |             else => unreachable, | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         const new_lexeme: []const u8 = switch (op.ttype) { | ||||||
|  |             .ColonEqual => ":=", | ||||||
|  |             .Equal => "=", | ||||||
|  |             .PlusEqual => "+", | ||||||
|  |             .MinusEqual => "-", | ||||||
|  |             .StarEqual => "*", | ||||||
|  |             else => unreachable, | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|         switch (expr.*) { |         switch (expr.*) { | ||||||
|             .Variable => { |             .Variable => { | ||||||
|                 switch (op.ttype) { |                 switch (op.ttype) { | ||||||
|                     .ColonEqual => return try self.mkVarDecl(expr.Variable, value, mutable), |                     .ColonEqual => return try self.mkVarDecl(expr.Variable, value, mutable), | ||||||
|                     .Equal => return try self.mkAssign(expr.Variable, value), |                     .Equal => return try self.mkAssign(expr.Variable, value), | ||||||
| 
 | 
 | ||||||
|                     .PlusEqual => { |                     .PlusEqual, .MinusEqual, .StarEqual => { | ||||||
|                         var plus_op = try self.mkToken(.Plus, "+", op.line); |                         var new_op = try self.mkToken(new_op_ttype, new_lexeme, op.line); | ||||||
|                         return try self.mkAssign( |  | ||||||
|                             expr.Variable, |  | ||||||
|                             try self.mkBinary(expr, plus_op, value), |  | ||||||
|                         ); |  | ||||||
|                     }, |  | ||||||
| 
 |  | ||||||
|                     .MinusEqual => { |  | ||||||
|                         var new_op = try self.mkToken(.Minus, "-", op.line); |  | ||||||
|                         return try self.mkAssign( |                         return try self.mkAssign( | ||||||
|                             expr.Variable, |                             expr.Variable, | ||||||
|                             try self.mkBinary(expr, new_op, value), |                             try self.mkBinary(expr, new_op, value), | ||||||
|  | @ -751,17 +762,8 @@ pub const Parser = struct { | ||||||
|                         return Result.CompileError; |                         return Result.CompileError; | ||||||
|                     }, |                     }, | ||||||
| 
 | 
 | ||||||
|                     .PlusEqual => { |                     .PlusEqual, .MinusEqual, .StarEqual => { | ||||||
|                         var new_op = try self.mkToken(.Plus, "+", op.line); |                         var new_op = try self.mkToken(new_op_ttype, new_lexeme, op.line); | ||||||
|                         return try self.mkSet( |  | ||||||
|                             get.struc, |  | ||||||
|                             get.name, |  | ||||||
|                             try self.mkBinary(expr, new_op, value), |  | ||||||
|                         ); |  | ||||||
|                     }, |  | ||||||
| 
 |  | ||||||
|                     .MinusEqual => { |  | ||||||
|                         var new_op = try self.mkToken(.Minus, "-", op.line); |  | ||||||
|                         return try self.mkSet( |                         return try self.mkSet( | ||||||
|                             get.struc, |                             get.struc, | ||||||
|                             get.name, |                             get.name, | ||||||
|  |  | ||||||
|  | @ -295,11 +295,11 @@ pub const Scanner = struct { | ||||||
|             '?' => self.makeToken(.QuestionMark), |             '?' => self.makeToken(.QuestionMark), | ||||||
|             '$' => self.makeToken(.DollarSign), |             '$' => self.makeToken(.DollarSign), | ||||||
| 
 | 
 | ||||||
|             '-' => self.makeToken(.Minus), |  | ||||||
|             '*' => self.makeToken(.Star), |  | ||||||
|             '%' => self.makeToken(.Modulo), |             '%' => self.makeToken(.Modulo), | ||||||
| 
 | 
 | ||||||
|             ':' => self.makeMatchToken('=', .ColonEqual, .Colon), |             ':' => self.makeMatchToken('=', .ColonEqual, .Colon), | ||||||
|  |             '*' => self.makeMatchToken('=', .StarEqual, .Star), | ||||||
|  |             '-' => self.makeMatchToken('=', .MinusEqual, .Minus), | ||||||
| 
 | 
 | ||||||
|             // we use the existing .And and .Or tokens |             // we use the existing .And and .Or tokens | ||||||
|             // representing the and and or keywords to |             // representing the and and or keywords to | ||||||
|  |  | ||||||
|  | @ -30,6 +30,7 @@ pub const TokenType = enum { | ||||||
|     PlusEqual, |     PlusEqual, | ||||||
|     MinusEqual, |     MinusEqual, | ||||||
|     ColonEqual, |     ColonEqual, | ||||||
|  |     StarEqual, | ||||||
| 
 | 
 | ||||||
|     // comparison ones |     // comparison ones | ||||||
|     EqualEqual, |     EqualEqual, | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue