diff --git a/bootstrap/emit.cc b/bootstrap/emit.cc index d4d37cb..5cfdbfb 100644 --- a/bootstrap/emit.cc +++ b/bootstrap/emit.cc @@ -190,6 +190,20 @@ std::any EmitVisitor::visitVariable(xlangParser::VariableContext *ctx) { last = "%_" + name; return {}; } + if (ctx->Increment()) { + visitExpr(ctx->expr()); + output << " %_" << name << " = w add %_" << name << ", " << last + << std::endl; + last = "%_" + name; + return {}; + } + if (ctx->Decrement()) { + visitExpr(ctx->expr()); + output << " %_" << name << " = w sub %_" << name << ", " << last + << std::endl; + last = "%_" + name; + return {}; + } if (ctx->LeftParen()) { std::vector args; diff --git a/bootstrap/typecheck.cc b/bootstrap/typecheck.cc index 8afb90a..9e75038 100644 --- a/bootstrap/typecheck.cc +++ b/bootstrap/typecheck.cc @@ -230,6 +230,21 @@ std::any TypeCheckVisitor::visitVariable(xlangParser::VariableContext *ctx) { return type; } } + if (auto expr = ctx->expr()) { + auto righttype = std::any_cast(visitExpr(expr)); + + if (auto lefttype = scope.get(name)) { + if (*lefttype != Type::Integer) { + errorlistener.typeMismatch(token, Type::Integer, *lefttype); + } + } else { + errorlistener.unknownVariable(token, name); + } + if (righttype != Type::Integer) { + errorlistener.typeMismatch(token, Type::Integer, righttype); + } + return Type::Integer; + } if (ctx->LeftParen()) { auto it = signatures.find(name); diff --git a/bootstrap/xlang.g4 b/bootstrap/xlang.g4 index d1fa872..3b32a1b 100644 --- a/bootstrap/xlang.g4 +++ b/bootstrap/xlang.g4 @@ -43,6 +43,8 @@ factor : Minus factor variable : Identifier | Identifier Define value | Identifier Assign value + | Identifier Increment expr + | Identifier Decrement expr | Identifier LeftParen argumentList? RightParen ; argumentList : value (Comma value)*; @@ -87,6 +89,8 @@ BitNot : '~'; Mul : '*'; Div : '/'; Rem : '%'; +Increment : '=+'; +Decrement : '=-'; Comma : ','; Semicolon : ';'; diff --git a/test/fib.x b/test/fib.x index e097db3..c785953 100644 --- a/test/fib.x +++ b/test/fib.x @@ -3,7 +3,7 @@ main() : int { while i < 10 { print fib_rec(i); print fib_iter(i); - i = i + 1; + i =+ 1; } } @@ -22,7 +22,7 @@ fib_iter(n : int) : int { t := x0 + x1; x0 = x1; x1 = t; - i = i + 1; + i =+ 1; } return x0; } diff --git a/test/sieve.x b/test/sieve.x index 751198b..f97ec62 100644 --- a/test/sieve.x +++ b/test/sieve.x @@ -9,11 +9,11 @@ main() : int { composite = true; break; } - j = j + 2; + j =+ 2; } if not composite { print i; } - i = i + 2; + i =+ 2; } }