Skip to content

Commit 860bb23

Browse files
committed
Some fixes
1 parent 1d5ad00 commit 860bb23

File tree

6 files changed

+101
-20
lines changed

6 files changed

+101
-20
lines changed

Project/Properties/launchSettings.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

Project/src/Extensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using System.Text;
4+
using System.Transactions;
45

56
namespace Golfscript
67
{
@@ -15,6 +16,33 @@ public static IEnumerable<T> Difference<T>(this IEnumerable<T> left, IEnumerable
1516
}
1617
}
1718

19+
public static IEnumerable<int> FindAll<T>(this List<T> left, List<T> right, int offset = 0)
20+
{
21+
int match = 0;
22+
for (int i = 0; i < left.Count; i++)
23+
{
24+
if (match == right.Count)
25+
{
26+
yield return i - right.Count;
27+
match = 0;
28+
}
29+
30+
match = left[i].Equals(right[match]) ? match + 1 : 0;
31+
}
32+
}
33+
34+
35+
public static IEnumerable<IEnumerable<T>> Split<T>(this List<T> left, List<T> right)
36+
{
37+
int start = 0;
38+
foreach (var index in left.FindAll(right))
39+
{
40+
yield return left.GetRange(start, index - start);
41+
start = index + right.Count;
42+
}
43+
yield return left.GetRange(start, left.Count - start);
44+
}
45+
1846
public static IEnumerable<T> SymmetricDifference<T>(this IEnumerable<T> left, IEnumerable<T> right)
1947
{
2048
var union = left.Union(right);

Project/src/Golfscript.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void ResetVariables()
9595
SetVariable("if", Operators.If);
9696
SetVariable("print", Operators.Print);
9797
SetVariable("until", Operators.Until);
98-
SetVariable("rand", Operators.Until);
98+
SetVariable("rand", Operators.Rand);
9999
SetVariable("while", Operators.While);
100100

101101
SetVariable("n", new StringItem("\n"));

Project/src/Operators.cs

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -390,26 +390,66 @@ internal static void Division(Stack context)
390390
var value = second.GetInt() / first.GetInt();
391391
result = new IntegerItem(value);
392392
}
393+
#region Split Matches
393394
else if (tuple == (ItemType.String, ItemType.String))
394395
{
395-
var separator = second.GetString();
396-
var split = first.GetString().Split(separator);
397-
var value = split.Where(str => str.Length > 0).Select(str => new StringItem(str));
396+
var separator = first.GetString();
397+
var split = second.GetString().Split(separator);
398+
var value = split.Select(str => new StringItem(str));
398399
result = new ArrayItem(value);
399400
}
400-
// TODO: Array implementation
401-
//else if (tuple == (ItemType.Array, ItemType.Array))
402-
//{
403-
// var separator = second.GetArray();
404-
// var split = first.GetArray().Split(separator);
405-
// var value = split.Where(str => str.Length > 0).Select(str => new StringItem(str));
406-
// result = new ArrayItem(value);
407-
//}
401+
else if (tuple == (ItemType.Array, ItemType.Array))
402+
{
403+
var separator = first.GetArray();
404+
var split = second.GetArray().Split(separator);
405+
var value = split.Select(str => new ArrayItem(str));
406+
result = new ArrayItem(value);
407+
}
408+
#endregion
409+
#region Split Size
410+
else if (tuple == (ItemType.String, ItemType.Integer))
411+
{
412+
var size = (int)second.GetInt();
413+
var split = first.GetString().Chunk(size);
414+
var value = split.Select(str => new StringItem(str));
415+
result = new ArrayItem(value);
416+
}
417+
else if (tuple == (ItemType.Array, ItemType.Integer))
418+
{
419+
var size = (int)second.GetInt();
420+
var split = first.GetArray().Chunk(size);
421+
var value = split.Select(arr => new ArrayItem(arr));
422+
result = new ArrayItem(value);
423+
}
424+
#endregion
408425
else if (tuple == (ItemType.Block, ItemType.Array))
409426
{
410427
Each(context, first, second);
411428
return;
412429
}
430+
else if (tuple == (ItemType.Block, ItemType.Block))
431+
{
432+
var condition = second.GetString();
433+
var code = first.GetString();
434+
435+
var list = new List<Item>();
436+
437+
while (true)
438+
{
439+
Duplicate(context);
440+
context.Golfscript.Run(condition);
441+
442+
if (!context.Pop().Truthy)
443+
break;
444+
445+
list.Add(context.Peek());
446+
context.Golfscript.Run(code);
447+
}
448+
449+
context.Pop();
450+
context.Push(new ArrayItem(list));
451+
return;
452+
}
413453
else
414454
{
415455
return;
@@ -722,6 +762,24 @@ internal static void Pow(Stack context)
722762
var index = str.IndexOf(value);
723763
context.Push(new IntegerItem(index));
724764
}
765+
else if (tuple == (ItemType.Block, ItemType.Array))
766+
{
767+
var array = second.GetArray();
768+
var condition = first.GetString();
769+
770+
foreach (var item in array)
771+
{
772+
context.Push(item);
773+
context.Golfscript.Run(condition);
774+
775+
if (context.Pop().Truthy)
776+
{
777+
context.Push(item);
778+
break;
779+
}
780+
}
781+
return;
782+
}
725783
else if (first.Type == ItemType.Array)
726784
{
727785
var array = first.GetArray();

Project/src/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34

45
namespace Golfscript

Project/src/Stack.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ public Item Peek(int index = 0)
6868

6969
StackFrame? FindFrame(ref int index)
7070
{
71-
foreach (var frame in stackFrames)
71+
for (int i = stackFrames.Count - 1; i >= 0; i--)
7272
{
73+
var frame = stackFrames[i];
7374
if (frame.Size > index)
7475
return frame;
7576

0 commit comments

Comments
 (0)