From b04e314a345038cb855b814ecfba2f04c308f520 Mon Sep 17 00:00:00 2001 From: Dylan Beattie Date: Mon, 16 Dec 2024 16:24:10 +0000 Subject: [PATCH] Fixes can't rock with pronouns Fixes #359 * Evaluate all elements (e.g. pronouns) first, BEFORE allocating/assigning the array - because allocation/assignment updates the pronoun subject so `it` points to the array after it's done, and if one of the elements is a pronoun, this will create an infinite loop. Eval()ing all the elements first fixes this. --- Starship/Rockstar.Engine/RockstarEnvironment.cs | 14 ++++++-------- "Starship/Rockstar.Engine/Values/Str\303\257ng.cs" | 5 +++++ .../fixtures/arrays/various-array-tests.rock | 8 +++++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Starship/Rockstar.Engine/RockstarEnvironment.cs b/Starship/Rockstar.Engine/RockstarEnvironment.cs index 1ec0363b..d56f3ac5 100644 --- a/Starship/Rockstar.Engine/RockstarEnvironment.cs +++ b/Starship/Rockstar.Engine/RockstarEnvironment.cs @@ -247,16 +247,14 @@ private Result Dequeue(Dequeue dequeue) { private Result Enlist(Enlist e) { var scope = e.Expressions.Any() ? Scope.Global : Scope.Local; - var value = Lookup(e.Variable, scope); - if (value is Strïng s) { - foreach (var expr in e.Expressions) s.Append(Eval(expr)); - return new(s); - } - if (value is not Arräy array) { - array = value == Mysterious.Instance ? new Arräy() : new(value); + var target = Lookup(e.Variable, scope); + var values = e.Expressions.Select(Eval).ToArray(); + if (target is Strïng s) return new(s.Append(values)); + if (target is not Arräy array) { + array = target == Mysterious.Instance ? new Arräy() : new(target); SetVariable(e.Variable, array, Scope.Local); } - foreach (var expr in e.Expressions) array.Push(Eval(expr)); + foreach (var value in values) array.Push(value); return new(array); } diff --git "a/Starship/Rockstar.Engine/Values/Str\303\257ng.cs" "b/Starship/Rockstar.Engine/Values/Str\303\257ng.cs" index 62d581c0..561bdffd 100644 --- "a/Starship/Rockstar.Engine/Values/Str\303\257ng.cs" +++ "b/Starship/Rockstar.Engine/Values/Str\303\257ng.cs" @@ -128,6 +128,11 @@ public Value ToCharCodes() { }; } + public Value Append(params Value[] values) { + foreach (var value in values) this.Append(value); + return this; + } + public Value Append(Value v) { if (v is Numbër number) { this.Value += (char) number.IntegerValue; diff --git a/Starship/Rockstar.Test/programs/fixtures/arrays/various-array-tests.rock b/Starship/Rockstar.Test/programs/fixtures/arrays/various-array-tests.rock index e4f70977..defb5fc4 100644 --- a/Starship/Rockstar.Test/programs/fixtures/arrays/various-array-tests.rock +++ b/Starship/Rockstar.Test/programs/fixtures/arrays/various-array-tests.rock @@ -2,4 +2,10 @@ for x in 3 n=0 r at i=0 -print r (prints: [ 0 ]) \ No newline at end of file +print r (prints: [ 0 ]) + + +foo is 1 +shout it (prints: 1) +rock bar with it +shout bar (prints: [ 1 ]) \ No newline at end of file