From 9fe5a1fff928ea3f5360160172346f6f8dbca32e Mon Sep 17 00:00:00 2001 From: lofcz Date: Sun, 9 Oct 2022 04:46:42 +0200 Subject: [PATCH 1/2] Relax class fields declr rhs --- .../Statements/ClassDefinitionStatement.cs | 15 ++++++++++++--- .../Classes/30-relaxed-field-rhs.lua | 18 ++++++++++++++++++ .../Classes/30-relaxed-field-rhs.txt | 3 +++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/WattleScript.Tests/EndToEnd/CLike/SyntaxCLike/Classes/30-relaxed-field-rhs.lua create mode 100644 src/WattleScript.Tests/EndToEnd/CLike/SyntaxCLike/Classes/30-relaxed-field-rhs.txt diff --git a/src/WattleScript.Interpreter/Tree/Statements/ClassDefinitionStatement.cs b/src/WattleScript.Interpreter/Tree/Statements/ClassDefinitionStatement.cs index 99a60010..ec40e759 100644 --- a/src/WattleScript.Interpreter/Tree/Statements/ClassDefinitionStatement.cs +++ b/src/WattleScript.Interpreter/Tree/Statements/ClassDefinitionStatement.cs @@ -153,6 +153,7 @@ public ClassDefinitionStatement(ScriptLoadingContext lcontext) : base(lcontext) { var T = lcontext.Lexer.Current; lcontext.Lexer.Next(); + bool resetModifiers = true; switch (lcontext.Lexer.Current.Type) { case TokenType.Brk_Open_Round: @@ -181,14 +182,22 @@ public ClassDefinitionStatement(ScriptLoadingContext lcontext) : base(lcontext) var exp = Expression.Expr(lcontext, true); fields.Add(T, exp, modifierFlags, false); break; - case TokenType.Comma: //no-op + case TokenType.Comma: + fields.Add(T, new LiteralExpression(lcontext, DynValue.Nil), modifierFlags, false); + resetModifiers = false; + break; case TokenType.SemiColon: break; default: - CheckTokenType(lcontext, TokenType.SemiColon); //throws error + fields.Add(T, new LiteralExpression(lcontext, DynValue.Nil), modifierFlags, false); break; } - modifierFlags = MemberModifierFlags.None; + + if (resetModifiers) + { + modifierFlags = MemberModifierFlags.None; + } + break; } default: diff --git a/src/WattleScript.Tests/EndToEnd/CLike/SyntaxCLike/Classes/30-relaxed-field-rhs.lua b/src/WattleScript.Tests/EndToEnd/CLike/SyntaxCLike/Classes/30-relaxed-field-rhs.lua new file mode 100644 index 00000000..775a7b6a --- /dev/null +++ b/src/WattleScript.Tests/EndToEnd/CLike/SyntaxCLike/Classes/30-relaxed-field-rhs.lua @@ -0,0 +1,18 @@ +class C { + private a, b, c + p2 + + C() { + this.a = 2 + this.p2 = 20 + } + + f2() { + print(this.a) + } +} + +c = new C() +print(c.a) +c.f2() +print(c.p2) \ No newline at end of file diff --git a/src/WattleScript.Tests/EndToEnd/CLike/SyntaxCLike/Classes/30-relaxed-field-rhs.txt b/src/WattleScript.Tests/EndToEnd/CLike/SyntaxCLike/Classes/30-relaxed-field-rhs.txt new file mode 100644 index 00000000..3ad7ee60 --- /dev/null +++ b/src/WattleScript.Tests/EndToEnd/CLike/SyntaxCLike/Classes/30-relaxed-field-rhs.txt @@ -0,0 +1,3 @@ +nil +2 +20 \ No newline at end of file From 9a992ade2f2ed15391f469bd04640e79b1295408 Mon Sep 17 00:00:00 2001 From: lofcz Date: Sun, 9 Oct 2022 17:29:24 +0200 Subject: [PATCH 2/2] Optimize class fields declarations without rhs --- src/WattleScript.Interpreter/Tree/GeneratedClosure.cs | 2 +- .../Tree/Statements/ClassDefinitionStatement.cs | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/WattleScript.Interpreter/Tree/GeneratedClosure.cs b/src/WattleScript.Interpreter/Tree/GeneratedClosure.cs index 23eabaa1..996484f1 100644 --- a/src/WattleScript.Interpreter/Tree/GeneratedClosure.cs +++ b/src/WattleScript.Interpreter/Tree/GeneratedClosure.cs @@ -61,7 +61,7 @@ public void ResolveScope(ScriptLoadingContext lcontext, Action !x.Flags.HasFlag(MemberModifierFlags.Static))) { + if (field.Expr == null) + { + continue; + } + field.Expr.CompilePossibleLiteral(bc); sym["table"].Compile(bc); bc.Emit_IndexSet(0, 0, field.Name, false, false, true);