add SlashEqual operator

This commit is contained in:
Luna 2019-08-27 11:58:32 -03:00
parent eb7a485206
commit e0a60ddc2c
4 changed files with 18 additions and 0 deletions

View file

@ -60,6 +60,7 @@ fn main(a int) int {
str.len += 1 str.len += 1
str.len -= 1 str.len -= 1
str.len *= 1 str.len *= 1
str.len /= 1
} }
fn (v Typ) voidfunc() {} fn (v Typ) voidfunc() {}

View file

@ -703,6 +703,7 @@ pub const Parser = struct {
if (self.compareAnyOf(&[_]TokenType{ if (self.compareAnyOf(&[_]TokenType{
.ColonEqual, .Equal, .PlusEqual, .MinusEqual, .StarEqual, .ColonEqual, .Equal, .PlusEqual, .MinusEqual, .StarEqual,
.SlashEqual,
})) { })) {
return try self.finishAssignment(expr, mutable); return try self.finishAssignment(expr, mutable);
} }
@ -725,15 +726,27 @@ pub const Parser = struct {
.PlusEqual => .Plus, .PlusEqual => .Plus,
.MinusEqual => .Minus, .MinusEqual => .Minus,
.StarEqual => .Star, .StarEqual => .Star,
.SlashEqual => .Slash,
else => unreachable, else => unreachable,
}; };
// we create new_lexeme so that
// the AST printer properly prints
// x += 1
// as
// (set x (+ x 1))
// and not
// (set x (+= x 1))
const new_lexeme: []const u8 = switch (op.ttype) { const new_lexeme: []const u8 = switch (op.ttype) {
.ColonEqual => ":=", .ColonEqual => ":=",
.Equal => "=", .Equal => "=",
.PlusEqual => "+", .PlusEqual => "+",
.MinusEqual => "-", .MinusEqual => "-",
.StarEqual => "*", .StarEqual => "*",
.SlashEqual => "/",
else => unreachable, else => unreachable,
}; };

View file

@ -315,7 +315,10 @@ pub const Scanner = struct {
'/' => blk: { '/' => blk: {
var next = self.peekNext(); var next = self.peekNext();
switch (next) { switch (next) {
'=' => return self.makeToken(.SlashEqual),
'/' => blk2: { '/' => blk2: {
while (self.peek() != '\n' and !self.isAtEnd()) { while (self.peek() != '\n' and !self.isAtEnd()) {
_ = self.advance(); _ = self.advance();

View file

@ -31,6 +31,7 @@ pub const TokenType = enum {
MinusEqual, MinusEqual,
ColonEqual, ColonEqual,
StarEqual, StarEqual,
SlashEqual,
// comparison ones // comparison ones
EqualEqual, EqualEqual,