Skip to content

Commit 22d8a67

Browse files
committed
Zip added
1 parent 860bb23 commit 22d8a67

File tree

3 files changed

+85
-45
lines changed

3 files changed

+85
-45
lines changed

Project/src/Golfscript.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public void ResetVariables()
9797
SetVariable("until", Operators.Until);
9898
SetVariable("rand", Operators.Rand);
9999
SetVariable("while", Operators.While);
100+
SetVariable("zip", Operators.Zip);
100101

101102
SetVariable("n", new StringItem("\n"));
102103
SetVariable("p", new BlockItem("`puts"));

Project/src/Operators.cs

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Globalization;
35
using System.Linq;
46
using System.Numerics;
57
using System.Text;
@@ -458,10 +460,10 @@ internal static void Division(Stack context)
458460
context.Push(result);
459461
}
460462

461-
internal static void Each(Stack context, Item first, Item second)
463+
internal static void Each(Stack context, Item str, Item array)
462464
{
463-
var code = first.GetString();
464-
foreach (var item in second.GetArray())
465+
var code = str.GetString();
466+
foreach (var item in array.GetArray())
465467
{
466468
context.Push(item);
467469
context.Golfscript.Run(code);
@@ -492,6 +494,14 @@ internal static void Modulus(Stack context)
492494
var value = split.Where(str => str.Length > 0).Select(str => new StringItem(str));
493495
result = new ArrayItem(value);
494496
}
497+
// TODO: Array implementation
498+
else if (tuple == (ItemType.Array, ItemType.Array))
499+
{
500+
var separator = first.GetArray();
501+
var split = second.GetArray().Split(separator);
502+
var value = split.Where(arr => arr.Any()).Select(str => new ArrayItem(str));
503+
result = new ArrayItem(value);
504+
}
495505
else if (tuple == (ItemType.Block, ItemType.Array))
496506
{
497507
context.PushFrame();
@@ -1046,7 +1056,7 @@ internal static void Peek(Stack context)
10461056
if (context.Size == 0)
10471057
return;
10481058

1049-
//var second = context.Pop();
1059+
var second = context.Pop();
10501060
// TODO: Mapping sort
10511061
//if (second.Type == ItemType.String)
10521062
//{
@@ -1187,6 +1197,76 @@ internal static void While(Stack context)
11871197
}
11881198
}
11891199

1200+
1201+
internal static void Zip(Stack context)
1202+
{
1203+
if (context.Size == 0)
1204+
return;
1205+
1206+
var first = context.Pop();
1207+
1208+
if (first.Type != ItemType.Array)
1209+
{
1210+
Console.WriteLine("It must be an array");
1211+
return;
1212+
}
1213+
1214+
var matrix = first.GetArray();
1215+
1216+
if (ItemType.Array.MatchAll(matrix.ToArray()))
1217+
{
1218+
var columnCount = matrix.Count;
1219+
var rowCount = (int)matrix.Max(row => row.Size);
1220+
1221+
var rows = new List<List<Item>>(rowCount);
1222+
1223+
for (int i = 0; i < rowCount; i++)
1224+
rows.Add(new List<Item>(matrix.Count));
1225+
1226+
for (int i = 0; i < columnCount; i++)
1227+
{
1228+
var row = matrix[i].GetArray();
1229+
for (int j = 0; j < row.Count; j++)
1230+
{
1231+
var item = row[j];
1232+
rows[j].Add(item);
1233+
}
1234+
}
1235+
1236+
context.PushFrame();
1237+
for (int i = 0; i < rowCount; i++)
1238+
context.Push(new ArrayItem(rows[i]));
1239+
1240+
context.PopFrame(true);
1241+
}
1242+
else if (ItemType.String.MatchAll(matrix.ToArray()))
1243+
{
1244+
var columnCount = matrix.Count;
1245+
var rowCount = (int)matrix.Max(row => row.Size);
1246+
1247+
var rows = new List<StringBuilder>(rowCount);
1248+
1249+
for (int i = 0; i < rowCount; i++)
1250+
rows.Add(new StringBuilder(matrix.Count));
1251+
1252+
for (int i = 0; i < columnCount; i++)
1253+
{
1254+
var row = matrix[i].GetString();
1255+
for (int j = 0; j < row.Length; j++)
1256+
{
1257+
var item = row[j];
1258+
rows[j].Append(item);
1259+
}
1260+
}
1261+
1262+
context.PushFrame();
1263+
for (int i = 0; i < rowCount; i++)
1264+
context.Push(new StringItem(rows[i].ToString()));
1265+
1266+
context.PopFrame(true);
1267+
}
1268+
}
1269+
11901270
#endregion
11911271

11921272
#region Utils

Project/src/Program.cs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class Program
1010
static void Main(string[] args)
1111
{
1212
//Format();
13-
1413
var golfscript = new Golfscript();
1514

1615
if (args.Length > 0)
@@ -44,45 +43,5 @@ static void Main(string[] args)
4443
} while (line == null || !line.StartsWith("quit"));
4544

4645
}
47-
48-
const string SEPARATOR = "->";
49-
50-
static void Format()
51-
{
52-
Console.WriteLine("File?");
53-
string file = Console.ReadLine();
54-
55-
var lines = File.ReadAllLines(file);
56-
57-
Console.WriteLine("Save to?");
58-
file = Console.ReadLine();
59-
60-
using var stream = new StreamWriter(file);
61-
stream.WriteLine("[Fact]");
62-
stream.WriteLine("void Function(){");
63-
64-
foreach (string line in lines)
65-
{
66-
if (line.StartsWith("args"))
67-
{
68-
stream.WriteLine("}\n\n");
69-
stream.WriteLine("[Fact]");
70-
stream.WriteLine("void Function(){");
71-
}
72-
73-
if (!line.Contains(SEPARATOR))
74-
continue;
75-
76-
var split = line.Split(SEPARATOR);
77-
var code = split[0].Trim().Replace("\"", "\\\"").Replace("\\", "\\\\");
78-
var expected = split[1].Trim().Replace("\"", "\\\"").Replace("\\", "\\\\");
79-
80-
stream.Write(" Test(\"");
81-
stream.Write(code);
82-
stream.Write("\", \"");
83-
stream.Write(expected);
84-
stream.WriteLine("\");");
85-
}
86-
}
8746
}
8847
}

0 commit comments

Comments
 (0)