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
}
fn (v Typ) voidfunc() {}

View file

@ -703,6 +703,7 @@ pub const Parser = struct {
if (self.compareAnyOf(&[_]TokenType{
.ColonEqual, .Equal, .PlusEqual, .MinusEqual, .StarEqual,
.SlashEqual,
})) {
return try self.finishAssignment(expr, mutable);
}
@ -725,15 +726,27 @@ pub const Parser = struct {
.PlusEqual => .Plus,
.MinusEqual => .Minus,
.StarEqual => .Star,
.SlashEqual => .Slash,
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) {
.ColonEqual => ":=",
.Equal => "=",
.PlusEqual => "+",
.MinusEqual => "-",
.StarEqual => "*",
.SlashEqual => "/",
else => unreachable,
};

View file

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

View file

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