Skip to content

Commit 8c51acd

Browse files
committed
Some fixes
1 parent 22d8a67 commit 8c51acd

File tree

10 files changed

+178
-114
lines changed

10 files changed

+178
-114
lines changed

Project/Golfscript.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
<PublishReadyToRun>true</PublishReadyToRun>
1212
<PublishSingleFile>true</PublishSingleFile>
1313

14-
<AssemblyVersion>0.9.0</AssemblyVersion>
15-
<FileVersion>0.9.0</FileVersion>
16-
<Version>0.9.0</Version>
14+
<AssemblyVersion>0.9.1</AssemblyVersion>
15+
<FileVersion>0.9.1</FileVersion>
16+
<Version>0.9.1</Version>
1717
<NeutralLanguage>en</NeutralLanguage>
1818
</PropertyGroup>
1919

Project/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

Project/src/Extensions.cs renamed to Project/src/Helpers/EnumerableHelpers.cs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using System.Collections.Generic;
1+
using Golfscript;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Text;
45
using System.Transactions;
56

6-
namespace Golfscript
7+
namespace Golfscript.src.Helpers
78
{
8-
static class Extensions
9+
static class EnumerableHelpers
910
{
1011
public static IEnumerable<T> Difference<T>(this IEnumerable<T> left, IEnumerable<T> right)
1112
{
@@ -16,7 +17,7 @@ public static IEnumerable<T> Difference<T>(this IEnumerable<T> left, IEnumerable
1617
}
1718
}
1819

19-
public static IEnumerable<int> FindAll<T>(this List<T> left, List<T> right, int offset = 0)
20+
public static IEnumerable<int> FindAll<T>(this IList<T> left, IList<T> right, int offset = 0)
2021
{
2122
int match = 0;
2223
for (int i = 0; i < left.Count; i++)
@@ -32,7 +33,7 @@ public static IEnumerable<int> FindAll<T>(this List<T> left, List<T> right, int
3233
}
3334

3435

35-
public static IEnumerable<IEnumerable<T>> Split<T>(this List<T> left, List<T> right)
36+
public static IEnumerable<IEnumerable<T>> Split<T>(this List<T> left, IList<T> right)
3637
{
3738
int start = 0;
3839
foreach (var index in left.FindAll(right))
@@ -50,40 +51,6 @@ public static IEnumerable<T> SymmetricDifference<T>(this IEnumerable<T> left, IE
5051
return union.Except(intersection);
5152
}
5253

53-
public static string Union(this string left, string right)
54-
{
55-
return string.Join("", (left + right).Distinct());
56-
}
57-
58-
public static string Difference(this string left, string right)
59-
{
60-
var sb = new StringBuilder();
61-
foreach (var item in left)
62-
{
63-
if (!right.Contains(item))
64-
sb.Append(item);
65-
}
66-
return sb.ToString();
67-
}
68-
69-
public static string Intersect(this string left, string right)
70-
{
71-
var sb = new StringBuilder();
72-
foreach (var item in left)
73-
{
74-
if (right.Contains(item))
75-
sb.Append(item);
76-
}
77-
return sb.ToString();
78-
}
79-
80-
public static string SymmetricDifference(this string left, string right)
81-
{
82-
var union = left.Union(right);
83-
var intersection = left.Intersect(right);
84-
return union.Difference(intersection);
85-
}
86-
8754
public static string Format<T>(this IEnumerable<T> array)
8855
{
8956
if (!array.Any())
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Linq;
2+
using System.Text;
3+
4+
namespace Golfscript.src.Helpers
5+
{
6+
internal static class StringHelpers
7+
{
8+
9+
public static string Difference(this string left, string right)
10+
{
11+
var sb = new StringBuilder();
12+
foreach (var item in left)
13+
{
14+
if (!right.Contains(item))
15+
sb.Append(item);
16+
}
17+
return sb.ToString();
18+
}
19+
20+
public static string Intersect(this string left, string right)
21+
{
22+
var sb = new StringBuilder();
23+
foreach (var item in left)
24+
{
25+
if (right.Contains(item))
26+
sb.Append(item);
27+
}
28+
return sb.ToString();
29+
}
30+
31+
public static string SymmetricDifference(this string left, string right)
32+
{
33+
var union = left.Union(right);
34+
var intersection = left.Intersect(right);
35+
return union.Difference(intersection);
36+
}
37+
38+
public static string Union(this string left, string right)
39+
{
40+
return string.Join("", (left + right).Distinct());
41+
}
42+
}
43+
}

Project/src/ItemComparer.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Linq;
5+
using System.Numerics;
6+
7+
namespace Golfscript
8+
{
9+
class ItemComparer : IComparer<Item>, IEqualityComparer<Item>
10+
{
11+
public int Compare(Item? x, Item? y)
12+
{
13+
if (ReferenceEquals(x, y) || x == null || y == null)
14+
return 0;
15+
16+
if (x.Type != y.Type)
17+
return 0;
18+
19+
if (x.Type == ItemType.Integer)
20+
return ((BigInteger)x.Value).CompareTo((BigInteger)y.Value);
21+
else if (x.Type == ItemType.String || x.Type == ItemType.Block)
22+
return ((string)x.Value).CompareTo((string)y.Value);
23+
24+
return 0;
25+
}
26+
27+
public bool Equals(Item? x, Item? y)
28+
{
29+
if (ReferenceEquals(x, y) || x == null || y == null)
30+
return true;
31+
32+
if (x.Type != y.Type)
33+
return false;
34+
35+
if (x.Type == ItemType.Integer)
36+
return ((BigInteger)x.Value).Equals((BigInteger)y.Value);
37+
else if (x.Type == ItemType.String || x.Type == ItemType.Block)
38+
return ((string)x.Value).Equals((string)y.Value);
39+
40+
return ((List<Item>)x.Value).SequenceEqual((List<Item>)y.Value, this);
41+
}
42+
43+
public int GetHashCode([DisallowNull] Item obj)
44+
{
45+
return obj.Value.GetHashCode();
46+
}
47+
}
48+
}

Project/src/Operators.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Golfscript.src.Helpers;
2+
using System;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Globalization;
@@ -8,31 +9,13 @@
89

910
namespace Golfscript
1011
{
11-
class ItemComparer : IComparer<Item>
12-
{
13-
public int Compare(Item? x, Item? y)
14-
{
15-
if (ReferenceEquals(x, y) || x == null || y == null)
16-
return 0;
17-
18-
if (x.Type != y.Type)
19-
return 0;
20-
21-
if (x.Type == ItemType.Integer)
22-
return ((BigInteger)x.Value).CompareTo((BigInteger)y.Value);
23-
else if (x.Type == ItemType.String || x.Type == ItemType.Block)
24-
return ((string)x.Value).CompareTo((string)y.Value);
25-
26-
return 0;
27-
}
28-
}
2912

3013
// Ordered naming: First is at the top of the stack
3114
// and last at the bottom of the stack
3215
static class Operators
3316
{
3417
static readonly IComparer<char> CharComparer = Comparer<char>.Default;
35-
static readonly IComparer<Item> ItemComparer = new ItemComparer();
18+
static readonly ItemComparer ItemComparer = new ItemComparer();
3619
static readonly Random RNG = new Random();
3720

3821
static string Sort(string item)
@@ -487,21 +470,35 @@ internal static void Modulus(Stack context)
487470
var value = second.GetInt() % first.GetInt();
488471
result = new IntegerItem(value);
489472
}
473+
// Split
490474
else if (tuple == (ItemType.String, ItemType.String))
491475
{
492476
var separator = first.GetString();
493477
var split = second.GetString().Split(separator);
494478
var value = split.Where(str => str.Length > 0).Select(str => new StringItem(str));
495479
result = new ArrayItem(value);
496480
}
497-
// TODO: Array implementation
498481
else if (tuple == (ItemType.Array, ItemType.Array))
499482
{
500483
var separator = first.GetArray();
501484
var split = second.GetArray().Split(separator);
502485
var value = split.Where(arr => arr.Any()).Select(str => new ArrayItem(str));
503486
result = new ArrayItem(value);
504487
}
488+
// TODO: Select
489+
else if (tuple == (ItemType.Array, ItemType.Integer))
490+
{
491+
var mod = second.GetInt();
492+
var array = first.GetArray();
493+
IEnumerable<Item> enumerable = array;
494+
if (mod < 0)
495+
{
496+
mod = -mod;
497+
enumerable = enumerable.Reverse();
498+
}
499+
var value = enumerable.Where((item, index) => index % mod == 0);
500+
result = new ArrayItem(value);
501+
}
505502
else if (tuple == (ItemType.Block, ItemType.Array))
506503
{
507504
context.PushFrame();
@@ -539,6 +536,7 @@ internal static void Less(Stack context)
539536
}
540537
// TODO: Compare arrays
541538
//else if (tuple == (ItemType.Array, ItemType.Array))
539+
//}
542540
else if (tuple == (ItemType.String, ItemType.Integer))
543541
{
544542
var position = (int)second.GetInt();
@@ -681,8 +679,8 @@ internal static void Equal(Stack context)
681679
}
682680
else if (tuple == (ItemType.String, ItemType.String) || tuple == (ItemType.Block, ItemType.Block))
683681
{
684-
var value = second.GetString().CompareTo(first.GetString());
685-
context.Push(new IntegerItem(value == 0 ? 1 : 0));
682+
var value = second.GetString() == first.GetString();
683+
context.Push(new IntegerItem(value ? 1 : 0));
686684
}
687685
else if (tuple == (ItemType.Array, ItemType.Array))
688686
{
@@ -695,7 +693,7 @@ internal static void Equal(Stack context)
695693
var firstArray = first.GetArray();
696694
var secondArray = second.GetArray();
697695

698-
var value = firstArray.SequenceEqual(secondArray);
696+
var value = firstArray.SequenceEqual(secondArray, ItemComparer);
699697
context.Push(new IntegerItem(value ? 1 : 0));
700698
}
701699
else if (tuple == (ItemType.String, ItemType.Integer))
@@ -1066,8 +1064,9 @@ internal static void Peek(Stack context)
10661064
//}
10671065
//else
10681066
//{
1069-
return;
1067+
// return;
10701068
//}
1069+
result = second;
10711070
}
10721071
else if (first.Type == ItemType.Array)
10731072
{
@@ -1241,6 +1240,7 @@ internal static void Zip(Stack context)
12411240
}
12421241
else if (ItemType.String.MatchAll(matrix.ToArray()))
12431242
{
1243+
12441244
var columnCount = matrix.Count;
12451245
var rowCount = (int)matrix.Max(row => row.Size);
12461246

Project/src/Parser.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Numerics;
45
using System.Text;
56
using System.Text.RegularExpressions;
@@ -129,10 +130,11 @@ static string ParseRawString(string code)
129130
static string ParseString(string code)
130131
{
131132
var sb = new StringBuilder(code, 1, code.Length - 2, code.Length);
133+
132134
foreach (var item in Escaped)
133135
sb.Replace(item.Key, item.Value);
134136

135-
foreach (Match match in EscapedRegex.Matches(sb.ToString()))
137+
foreach (Match match in EscapedRegex.Matches(sb.ToString()).Reverse())
136138
{
137139
var hex = match.Groups["Hex"];
138140
var octal = match.Groups["Oct"];
@@ -151,6 +153,7 @@ static string ParseString(string code)
151153
sb.Insert(match.Index, (char)_byte);
152154
}
153155
}
156+
154157
return sb.ToString();
155158
}
156159

0 commit comments

Comments
 (0)