add increment/decrement operators

development
Thomas Lindner 2023-01-07 16:07:24 +01:00
parent 80643b910c
commit 1c4022cebc
5 changed files with 37 additions and 4 deletions

View File

@ -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<std::string> args;

View File

@ -230,6 +230,21 @@ std::any TypeCheckVisitor::visitVariable(xlangParser::VariableContext *ctx) {
return type;
}
}
if (auto expr = ctx->expr()) {
auto righttype = std::any_cast<Type>(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);

View File

@ -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 : ';';

View File

@ -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;
}

View File

@ -9,11 +9,11 @@ main() : int {
composite = true;
break;
}
j = j + 2;
j =+ 2;
}
if not composite {
print i;
}
i = i + 2;
i =+ 2;
}
}