Skip to content

Commit bdfaa7f

Browse files
author
aafent
committed
Now Arrays are acting as collections (FOREACH,REST,etc)
1 parent 3e648a1 commit bdfaa7f

File tree

12 files changed

+122
-31
lines changed

12 files changed

+122
-31
lines changed

FAST.FBasic.InteractiveConsole/FBasicIC.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using FAST.FBasicInterpreter;
33
using Microsoft.Extensions.Configuration;
44
using System.Data.Odbc;
5-
using System.Runtime.InteropServices;
65

76
namespace FAST.FBasic.InteractiveConsole
87
{

FAST.FBasic.InteractiveConsole/Tests/array.bas

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
REM Test program for CALL and GOTO statements
1+
REM Test program for Arrays
22
REM
33
REM
44

5-
coldim arr1, c1, c2
6-
let [arr1.c1(1)] = "xxx"
7-
85
coldim a1, c1, c2, c3 ' define array a1 with 3 columns
96
coladd a1, c4 ' add one column
107

@@ -26,4 +23,5 @@ print cname("a1",2)
2623
print "Number of columns=";
2724
print cnamescount("a1")
2825

26+
2927
Halt
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
REM Test program for Arrays
2+
REM
3+
4+
coldim arr1, c1, c2
5+
let [arr1.c1(1)]="1,1"
6+
let [arr1.c2(1)]="1,2"
7+
8+
let [arr1.c1(2)]="2,1"
9+
let [arr1.c2(2)]="2,2"
10+
11+
let [arr1.c1(3)]="3,1"
12+
let [arr1.c2(3)]="3,2"
13+
14+
let [arr1.c1(4)]="4,1"
15+
let [arr1.c2(4)]="4,2"
16+
17+
foreach arr1
18+
print [arr1.c1]+" "+EOD("arr1")
19+
endforeach arr1
20+
print "EOD=" +EOD("arr1")
21+
22+
Halt

FAST.FBasicInterpreter/Core/FBasicArray.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace FAST.FBasicInterpreter
1+
using System.Security;
2+
3+
namespace FAST.FBasicInterpreter
24
{
35
public class JaggedArray2D<T>
46
{
@@ -117,7 +119,7 @@ public void CopyTo(JaggedArray2D<T> map)
117119
/// <summary>
118120
/// Reset the array, delete all the content.
119121
/// </summary>
120-
public virtual void Reset()
122+
public virtual void ResetArray()
121123
{
122124
m_values=null;
123125
}
@@ -127,7 +129,7 @@ public virtual void Reset()
127129
/// <summary>
128130
/// FBasic Array
129131
/// </summary>
130-
public class FBasicArray : JaggedArray2D<Value>
132+
public class FBasicArray : JaggedArray2D<Value>, IBasicCollection
131133
{
132134
private string[] columnNames = new string[0] ;
133135

@@ -277,14 +279,43 @@ public void DeleteRow(int rowToDelete)
277279
}
278280

279281
/// <summary>
280-
/// Reset the array, delete all the content.
282+
/// Reset the array, delete all the content. But not the columns
281283
/// </summary>
282-
public override void Reset()
284+
public override void ResetArray()
283285
{
284286
columnNames = new string[0];
285-
base.Reset();
287+
this.Reset(); // reset the IEnumerator part of the class
288+
base.ResetArray();
286289
}
287290

291+
292+
#region (+) IFBasicCollection Interface
293+
294+
private int currentRow = 0;
295+
296+
public bool endOfData { get => currentRow>this.Length ; set { } }
297+
298+
public object Current => m_values[currentRow-1];
299+
300+
public Value getValue(string name)=>this[currentRow,name];
301+
302+
public bool MoveNext()
303+
{
304+
currentRow++;
305+
if (currentRow > this.Length )
306+
{
307+
currentRow = this.Length+1;
308+
}
309+
return endOfData;
310+
}
311+
312+
public void Reset()
313+
{
314+
this.currentRow=0;
315+
}
316+
317+
318+
#endregion (+) IFBasicCollection Interface
288319
}
289320

290321
}

FAST.FBasicInterpreter/Core/IdentifierNotationParser.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ private void standardChecks()
108108
{
109109
if (this.DataElement.ToUpper() != "ITEM")
110110
{
111-
if (!interpreter.collections.ContainsKey(this.DataElement))
112-
{
113-
interpreter.Error("[]", Errors.E111_UndeclareIdentifier(this.DataElement));
114-
return;
115-
}
111+
//if (!interpreter.collections.ContainsKey(this.DataElement))
112+
//{
113+
// interpreter.Error("[]", Errors.E111_UndeclareIdentifier(this.DataElement));
114+
// return;
115+
//}
116116
}
117117
}
118118

FAST.FBasicInterpreter/Core/Interpreter_Elements.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public Value GetIdentifierOrCF(bool permitIdentifier = true,
160160
else if (parser.IsCollection)
161161
{
162162
if (!permitCollection) return Error(Errors.E124_NotPermitted("Collection expressions")).value;
163-
return this.collections[parser.DataContainerName].getValue(parser.DataElement);
163+
return this.GetCollectionOrArray(parser.DataContainerName).getValue(parser.DataElement);
164164
}
165165
else if (parser.IsFunction)
166166
{

FAST.FBasicInterpreter/Core/Interpreter_Extensions.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Runtime.CompilerServices;
1+
using System.Collections;
2+
using System.Runtime.CompilerServices;
23

34
namespace FAST.FBasicInterpreter
45
{
@@ -114,6 +115,29 @@ public static Value GetValue(this Interpreter interpreter, string name)
114115

115116
}
116117

118+
/// <summary>
119+
/// Check if a name is collection or array
120+
/// </summary>
121+
/// <param name="interpreter"></param>
122+
/// <param name="name"></param>
123+
/// <returns></returns>
124+
125+
public static bool IsCollectionOrArray(this Interpreter interpreter, string name)
126+
{
127+
return interpreter.IsCollection(name) || interpreter.IsArray(name);
128+
}
129+
130+
/// <summary>
131+
/// Return the basic collection for the array or the collection
132+
/// </summary>
133+
/// <param name="interpreter"></param>
134+
/// <param name="name"></param>
135+
/// <returns></returns>
136+
public static IBasicCollection GetCollectionOrArray(this Interpreter interpreter, string name)
137+
{
138+
if (interpreter.IsCollection(name)) return interpreter.GetCollection(name);
139+
return interpreter.GetArray(name);
140+
}
117141

118142
/// <summary>
119143
/// Dump the source line at the Market, for debugging purposes

FAST.FBasicInterpreter/Core/Interpreter_Methods.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ public bool IsCollection(string name)
5353
return this.collections.ContainsKey(name);
5454
}
5555

56+
57+
/// <summary>
58+
/// Get reference to a declared Collection
59+
/// </summary>
60+
/// <param name="name"></param>
61+
/// <returns></returns>
62+
public IBasicCollection GetCollection(string name)
63+
{
64+
if (!IsCollection(name))
65+
{
66+
Error(Errors.E112_UndeclaredEntity("Collection", name));
67+
return null; // unnecessary but the compiler will like it...
68+
}
69+
return this.collections[name];
70+
}
71+
5672
/// <summary>
5773
/// Get reference to a declared array
5874
/// </summary>

FAST.FBasicInterpreter/Core/Interpreter_Statements.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ void ForEachStatement()
217217

218218
this.Match(Token.Identifier);
219219
string collectionName = this.lex.Identifier;
220-
if (!this.collections.ContainsKey(collectionName)) Error("FOREACH", "Collection: {collectionName} not found. [E110]");
220+
if (!this.IsCollectionOrArray(collectionName)) Error("FOREACH", "Collection/Array: {collectionName} not found. [E110]");
221221

222222
this.GetNextToken();
223223

@@ -233,7 +233,7 @@ void ForEachStatement()
233233
}
234234

235235
// (v) FETCH
236-
var collection = this.collections[collectionName];
236+
var collection= this.GetCollectionOrArray(collectionName);
237237
collection.MoveNext();
238238
//collection.endOfData = !collection.MoveNext();
239239

@@ -288,7 +288,7 @@ void EndForEachStatement()
288288
string collectionName = lex.Identifier;
289289
string endForEachLoop = collectionName + "$ENDFOREACH";
290290

291-
var collection = this.collections[collectionName];
291+
var collection = this.GetCollectionOrArray(collectionName);
292292
if (collection.endOfData) // leave the EndForEach and go to the next command
293293
{
294294
loops.Remove(collectionName);

FAST.FBasicInterpreter/Libraries/BuiltInsForCollections.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ private static Value EOD(Interpreter interpreter, List<Value> args)
1919
{
2020
if (args.Count != 1) throw new ArgumentException();
2121
string collection = args[0].String;
22-
if (!interpreter.collections.ContainsKey(collection)) interpreter.Error("EOD", $"Collection:{collection} not found");
23-
return new Value(interpreter.collections[collection].endOfData ? 1 : 0);
22+
if (!interpreter.IsCollectionOrArray(collection)) interpreter.Error("EOD", $"Collection/Array:{collection} not found");
23+
return new Value(interpreter.GetCollectionOrArray(collection).endOfData ? 1 : 0);
2424
}
2525

2626
private static void FETCH(Interpreter interpreter)
@@ -34,9 +34,9 @@ private static void FETCH(Interpreter interpreter)
3434
interpreter.GetNextToken();
3535

3636
// (v) do the work
37-
if (!interpreter.collections.ContainsKey(collectionName) )
38-
interpreter.Error("FETCH",$"Collection: ${collectionName} not found.");
39-
var collection = interpreter.collections[collectionName];
37+
if (!interpreter.IsCollectionOrArray(collectionName) )
38+
interpreter.Error("FETCH",$"Collection/Array: ${collectionName} not found.");
39+
var collection = interpreter.GetCollectionOrArray(collectionName);
4040

4141
collection.endOfData=!collection.MoveNext();
4242
}
@@ -53,9 +53,9 @@ private static void RESET(Interpreter interpreter)
5353
//interpreter.lastToken = Token.NewLine;
5454

5555
// (v) implementation
56-
if (!interpreter.collections.ContainsKey(collectionName))
57-
interpreter.Error("RESET", $"Collection: ${collectionName} not found.");
58-
var collection = interpreter.collections[collectionName];
56+
if (!interpreter.IsCollectionOrArray(collectionName))
57+
interpreter.Error("RESET", $"Collection/Array: ${collectionName} not found.");
58+
var collection = interpreter.GetCollectionOrArray(collectionName);
5959

6060
collection.Reset();
6161
collection.endOfData=false;

0 commit comments

Comments
 (0)