Skip to content

Commit f3b37ba

Browse files
committed
Fixed FcBlocksTests
Fixed errors caused by changing block size from Vector2 to Vector3
1 parent dedad0d commit f3b37ba

File tree

7 files changed

+46
-82
lines changed

7 files changed

+46
-82
lines changed

FanScript.Cli/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static int runVerb<T>(Func<T, int> action, T arg, string name)
5858
try
5959
{
6060
#endif
61-
return action(arg);
61+
return action(arg);
6262
#if !DEBUG
6363
}
6464
catch (Exception ex)

FanScript.Tests/FCBlocksTests.cs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using FanScript.Compiler.Emit;
33
using FanScript.Compiler.Emit.BlockPlacers;
44
using FanScript.Compiler.Emit.CodeBuilders;
5+
using FanScript.Compiler.Emit.Utils;
56
using FanScript.FCInfo;
67
using MathUtils.Vectors;
78
using System.Collections.Frozen;
@@ -34,16 +35,13 @@ public void Manual_TerminalsAreCorrect()
3435

3536
using (placer.StatementBlock())
3637
{
37-
Block? lastBlock = null;
38+
EmitConnector connector = new EmitConnector(builder.Connect);
3839

3940
foreach (var def in active)
4041
{
4142
Block block = placeAndConnectAllTerminals(def);
4243

43-
if (lastBlock is not null)
44-
builder.Connect(new BlockConnectTarget(lastBlock, lastBlock.Type.After), new BlockConnectTarget(block, block.Type.Before));
45-
46-
lastBlock = block;
44+
connector.Add(new BasicEmitStore(block));
4745
}
4846

4947
foreach (var def in pasive)
@@ -65,23 +63,21 @@ Block placeAndConnectAllTerminals(BlockDef def)
6563

6664
(Block, Terminal)[] connectToTerminals = new (Block, Terminal)[terminals.Length];
6765

68-
using (placer.StatementBlock())
66+
Block block = placer.PlaceBlock(def);
67+
68+
using (placer.ExpressionBlock())
6969
{
70-
for (int i = 0; i < terminals.Length; i++)
70+
for (int i = terminals.Length - 1; i >= 0; i--)
7171
{
7272
Terminal terminal = terminals[i];
7373

7474
WireType type = terminal.WireType;
75-
if (type != WireType.Void)
76-
type = (WireType)((int)type & (int.MaxValue ^ 1)); // xPtr => x
7775

7876
var (_def, _terminal) = terminalDict[(terminal.Type, type)];
7977
connectToTerminals[i] = (placer.PlaceBlock(_def), _terminal);
8078
}
8179
}
8280

83-
Block block = placer.PlaceBlock(def);
84-
8581
for (int i = 0; i < connectToTerminals.Length; i++)
8682
{
8783
var (_block, _terminal) = connectToTerminals[i];
@@ -106,33 +102,33 @@ Block placeAndConnectAllTerminals(BlockDef def)
106102

107103
IEnumerable<KeyValuePair<(TerminalType, WireType), (BlockDef, Terminal)>> generateTerminalDict()
108104
{
109-
foreach (var _type in Enum.GetValues<WireType>())
105+
foreach (var type in Enum.GetValues<WireType>())
110106
{
111-
if (_type == WireType.Error ||
112-
(_type != WireType.Void && (int)_type % 2 == 1))
107+
if (type == WireType.Error)
113108
continue;
114109

115-
BlockDef def = _type == WireType.Void ? Blocks.Variables.Set_Variable_Num : Blocks.Variables.VariableByType(_type);
110+
BlockDef def = type == WireType.Void ? Blocks.Variables.Set_Variable_Num : Blocks.Variables.VariableByType(type);
116111

117-
var type = (WireType)((int)_type | 1);
112+
WireType ptrType = type.ToPointer();
118113

119-
var terminal = def.TerminalArray.First(term => term.Type == TerminalType.Out && term.WireType == type);
114+
var terminal = def.TerminalArray.First(term => term.Type == TerminalType.Out && term.WireType == ptrType);
120115

121-
yield return new KeyValuePair<(TerminalType, WireType), (BlockDef, Terminal)>((TerminalType.In, _type), (def, terminal));
116+
yield return new KeyValuePair<(TerminalType, WireType), (BlockDef, Terminal)>((TerminalType.In, type), (def, terminal));
122117
}
123118

124119
// this *could* be names "type", but for some fuckinf reason if it is, it's always WireType.Error
125-
foreach (var _type in Enum.GetValues<WireType>())
120+
foreach (var type in Enum.GetValues<WireType>())
126121
{
127-
if (_type == WireType.Error ||
128-
(_type != WireType.Void && (int)_type % 2 == 1))
122+
if (type == WireType.Error)
129123
continue;
130124

131-
BlockDef def = _type == WireType.Void ? Blocks.Variables.Set_Variable_Num : Blocks.Variables.Set_VariableByType(_type);
125+
BlockDef def = type == WireType.Void ? Blocks.Variables.Set_Variable_Num : Blocks.Variables.Set_VariableByType(type);
126+
127+
WireType nonPtrType = type.ToNormal();
132128

133-
var terminal = def.TerminalArray.First(term => term.Type == TerminalType.In && term.WireType == _type);
129+
var terminal = def.TerminalArray.First(term => term.Type == TerminalType.In && term.WireType == nonPtrType);
134130

135-
yield return new KeyValuePair<(TerminalType, WireType), (BlockDef, Terminal)>((TerminalType.Out, _type), (def, terminal));
131+
yield return new KeyValuePair<(TerminalType, WireType), (BlockDef, Terminal)>((TerminalType.Out, type), (def, terminal));
136132
}
137133
}
138134
}

FanScript/Compiler/Emit/BlockBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private void calculateMinMax()
160160
BlockDef type = Blocks[i].Type;
161161

162162
min = Vector3I.Min(Blocks[i].Pos, min);
163-
max = Vector3I.Max(Blocks[i].Pos + new Vector3I(type.Size.X, 1, type.Size.Y), max);
163+
max = Vector3I.Max(Blocks[i].Pos + type.Size, max);
164164
}
165165

166166
MinPos = min;

FanScript/Compiler/Emit/CodePlacers/GroundCodePlacer.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,11 @@ public override void ExitStatementBlock()
6565

6666
StatementCodeBlock statement = statements.Pop();
6767

68-
if (statements.Count == 1)
69-
{
70-
if (statement.Parent!.Parent is not null)
71-
throw new Exception("Parent not null.");
72-
73-
// if this is child of the top statement, process and assign x and y position to blocks
74-
LayerStack.Process(statement, BlockXOffset);
75-
}
76-
else if (statements.Count == 0 && statement.AllBlocks.Any())
68+
if (statements.Count == 0 && statement.AllBlocks.Any())
7769
{
7870
// end of function
71+
LayerStack.Process(statement, BlockXOffset);
72+
7973
Builder.AddBlockSegments(statement.AllBlocks);
8074
}
8175
}
@@ -177,9 +171,9 @@ public CodeBlock(GroundCodePlacer placer, int pos)
177171
public Block PlaceBlock(BlockDef blockDef)
178172
{
179173
if (BlockCount != 0)
180-
CurrentPos -= blockDef.Size.Y + blockZOffset;
174+
CurrentPos -= blockDef.Size.Z + blockZOffset;
181175
else
182-
CurrentPos -= blockDef.Size.Y - 1;
176+
CurrentPos -= blockDef.Size.Z - 1;
183177

184178
Height = Math.Max(Height, blockDef.Size.Y);
185179

@@ -191,7 +185,7 @@ public Block PlaceBlock(BlockDef blockDef)
191185
}
192186

193187
protected int CalculatePosOfNextChild()
194-
=> CurrentPos + (LastPlacedBlockType is null ? 0 : LastPlacedBlockType.Size.Y - 1);
188+
=> CurrentPos + (LastPlacedBlockType is null ? 0 : LastPlacedBlockType.Size.Z - 1);
195189

196190
protected void IncrementXOffsetOfStatementParent()
197191
{
@@ -249,8 +243,7 @@ public override ExpressionCodeBlock CreateExpressionChild()
249243

250244
protected class ExpressionCodeBlock : CodeBlock
251245
{
252-
public static readonly int BlockZOffset = 0;
253-
protected override int blockZOffset => BlockZOffset;
246+
protected override int blockZOffset => 0;
254247

255248
public ExpressionCodeBlock(GroundCodePlacer placer, int pos, CodeBlock parent, int layerOffset)
256249
: base(placer, pos, parent, layerOffset)
@@ -309,7 +302,7 @@ public static void Process(CodeBlock block, int blockXOffset)
309302
foreach (var (_, list) in xToBlocks)
310303
{
311304
// sort in descending order - blocks start and zero and go down
312-
list.Sort((a, b) => b.StartPos.CompareTo(a.StartPos));
305+
list.Sort((a, b) => b.StartZ.CompareTo(a.StartZ));
313306

314307
List<int> positions = new();
315308

@@ -323,12 +316,14 @@ public static void Process(CodeBlock block, int blockXOffset)
323316
int yPos = -1;
324317

325318
for (int j = 0; j < positions.Count; j++)
319+
{
326320
if (positions[j] > item.StartZ)
327321
{
328322
positions[j] = item.CurrentPos;
329323
yPos = j;
330324
break;
331325
}
326+
}
332327

333328
if (yPos == -1)
334329
{

FanScript/FCInfo/BlockDef.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ private void initTerminals()
8181
}
8282

8383
// if a block has less/more in/out terminals, one of the sides will start higher
84-
countIn = Size.Y - countIn;
85-
countOut = Size.Y - countOut;
84+
countIn = Size.Z - countIn;
85+
countOut = Size.Z - countOut;
8686

8787
int outXPos = Size.X * 8 - 2;
8888

@@ -99,7 +99,7 @@ private void initTerminals()
9999
if (Type == BlockType.Active)
100100
{
101101
After.Init(0, new Vector3I(3, 1, 0));
102-
Before.Init(TerminalArray.Length - 1, new Vector3I(3, 1, Size.Y * 8 - 2));
102+
Before.Init(TerminalArray.Length - 1, new Vector3I(3, 1, Size.Z * 8 - 2));
103103
}
104104
}
105105

FanScript/FCInfo/Blocks.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static class Math
101101
{
102102
public static BlockDef EqualsByType(WireType type)
103103
{
104-
switch (type)
104+
switch (type.ToNormal())
105105
{
106106
case WireType.Float:
107107
return Equals_Number;
@@ -117,7 +117,7 @@ public static BlockDef EqualsByType(WireType type)
117117
}
118118
public static BlockDef BreakByType(WireType type)
119119
{
120-
switch (type)
120+
switch (type.ToNormal())
121121
{
122122
case WireType.Vec3:
123123
return Break_Vector;
@@ -129,7 +129,7 @@ public static BlockDef BreakByType(WireType type)
129129
}
130130
public static BlockDef MakeByType(WireType type)
131131
{
132-
switch (type)
132+
switch (type.ToNormal())
133133
{
134134
case WireType.Vec3:
135135
return Make_Vector;
@@ -223,7 +223,7 @@ public static BlockDef ValueByType(object value)
223223
}
224224
public static BlockDef InspectByType(WireType type)
225225
{
226-
switch (type)
226+
switch (type.ToNormal())
227227
{
228228
case WireType.Float:
229229
return Inspect_Number;
@@ -259,7 +259,7 @@ public static class Variables
259259
{
260260
public static BlockDef VariableByType(WireType type)
261261
{
262-
switch (type)
262+
switch (type.ToNormal())
263263
{
264264
case WireType.Float:
265265
return Variable_Num;
@@ -279,7 +279,7 @@ public static BlockDef VariableByType(WireType type)
279279
}
280280
public static BlockDef Set_VariableByType(WireType type)
281281
{
282-
switch (type)
282+
switch (type.ToNormal())
283283
{
284284
case WireType.Float:
285285
return Set_Variable_Num;
@@ -299,7 +299,7 @@ public static BlockDef Set_VariableByType(WireType type)
299299
}
300300
public static BlockDef ListByType(WireType type)
301301
{
302-
switch (type)
302+
switch (type.ToNormal())
303303
{
304304
case WireType.Float:
305305
return List_Num;
@@ -319,7 +319,7 @@ public static BlockDef ListByType(WireType type)
319319
}
320320
public static BlockDef Set_PtrByType(WireType type)
321321
{
322-
switch (type)
322+
switch (type.ToNormal())
323323
{
324324
case WireType.Float:
325325
return Set_Ptr_Num;

FanScript/FCInfo/WireType.cs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,12 @@ public enum WireType : byte
2222
public static class WireTypeE
2323
{
2424
public static WireType ToPointer(this WireType wireType)
25-
=> wireType switch
26-
{
27-
WireType.Float => WireType.FloatPtr,
28-
WireType.Vec3 => WireType.Vec3Ptr,
29-
WireType.Rot => WireType.RotPtr,
30-
WireType.Bool => WireType.BoolPtr,
31-
WireType.Obj => WireType.ObjPtr,
32-
WireType.Con => WireType.ConPtr,
33-
_ => wireType,
34-
};
25+
=> wireType == WireType.Error ? wireType : (WireType)((int)wireType | 1);
3526

3627
public static WireType ToNormal(this WireType wireType)
37-
=> wireType switch
38-
{
39-
WireType.FloatPtr => WireType.Float,
40-
WireType.Vec3Ptr => WireType.Vec3,
41-
WireType.RotPtr => WireType.Rot,
42-
WireType.BoolPtr => WireType.Bool,
43-
WireType.ObjPtr => WireType.Obj,
44-
WireType.ConPtr => WireType.Con,
45-
_ => wireType,
46-
};
28+
=> wireType == WireType.Void ? wireType : (WireType)((int)wireType & (int.MaxValue ^ 1));
4729

4830
public static bool IsPointer(this WireType wireType)
49-
=> wireType switch
50-
{
51-
WireType.FloatPtr => true,
52-
WireType.Vec3Ptr => true,
53-
WireType.RotPtr => true,
54-
WireType.BoolPtr => true,
55-
WireType.ObjPtr => true,
56-
WireType.ConPtr => true,
57-
_ => false
58-
};
31+
=> wireType == WireType.Void ? false : ((int)wireType & 1) == 1;
5932
}
6033
}

0 commit comments

Comments
 (0)