fix(muscript): handle "this" correctly

This commit is contained in:
Johannes Frohnmeyer 2024-04-07 17:18:23 +02:00
parent c8b5a186ba
commit ab6c3b5b2d
Signed by: Johannes
GPG Key ID: E76429612C2929F4
1 changed files with 3 additions and 2 deletions

View File

@ -326,17 +326,18 @@ public class Parser extends VersionedComponent {
private Expr primary() {
if (match(Token.Null)) return Expr.literalNull(previous.location());
if (match(Token.String)) return "this".equals(previous.lexeme()) ? new This(previous.location()) : Expr.literal(previous.location(), previous.lexeme());
if (match(Token.String)) return Expr.literal(previous.location(), previous.lexeme());
if (match(Token.True, Token.False)) return Expr.literal(previous.location(), previous.lexeme().equals("true"));
if (match(Token.Number)) return Expr.literal(previous.location(), Double.parseDouble(previous.lexeme()));
if (match(Token.Identifier)) {
CodeLocation location = previous.location();
String name = previous.lexeme();
if (match(Token.Assign)) {
if (name.equals("this")) throw error("Cannot assign to 'this' keyword");
Expr expr = expression();
return new DynamicAssign(location, name, ExprUtils.asDynamic(expr));
}
else return new Variable(location, name);
else return name.equals("this") ? new This(location) : new Variable(location, name);
}
if (match(Token.LeftParen)) {