Update to version 1.0.0 #3

Merged
Booklordofthedings merged 23 commits from dev into main 2024-08-26 13:33:38 +02:00
10 changed files with 421 additions and 92 deletions
Showing only changes of commit 11145e278b - Show all commits

View file

@ -2,4 +2,5 @@ FileVersion = 1
[Project]
Name = "Bofa"
TargetType = "BeefLib"
StartupObject = "Bofa.Program"

View file

@ -1,6 +1,7 @@
namespace Bofa;
using System;
using System.Collections;
class Bofa
{
@ -12,6 +13,134 @@ class Bofa
public StringView Typename;
public BofaValue Value;
public override void ToString(String strBuffer)
{
int64 depth = 0;
Bofa target = this;
List<(int64, Bofa)> toProcess = new .();
defer delete toProcess;
repeat
{
target.ToString(strBuffer, depth);
if(target.Type == .Array)
{
depth++;
for(var i in Value.Array.Reversed)
toProcess.Add((depth, i));
}
else if(target.Type == .Object)
{
depth++;
for(var i in Value.Object)
toProcess.Add((depth, i.value));
}
if(toProcess.IsEmpty)
target = null;
else
{
var t = toProcess.PopBack();
depth = t.0;
target = t.1;
strBuffer.Append('\n');
}
} while(target != null);
}
private void ToString(String strBuffer, int64 pDepth)
{
strBuffer.Append(' ', pDepth);
switch(Type)
{
case .Array:
strBuffer.Append(scope $"a {Name}");
case .BigInteger:
strBuffer.Append(scope $"bi {Name} {Value.BigInteger}");
case .BigNumber:
strBuffer.Append(scope $"bn {Name} {Value.BigNumber}");
case .Boolean:
strBuffer.Append(scope $"b {Name} {Value.Boolean ? "true" : "false"}");
case .Custom:
strBuffer.Append(scope $"c {Typename} {Name} {Value.Custom}");
case .Integer:
strBuffer.Append(scope $"i {Name} {Value.Integer}");
case .Line:
strBuffer.Append(scope $"l {Name} {Value.Line}");
case .Number:
strBuffer.Append(scope $"n {Name} {Value.Number}");
case .Object:
strBuffer.Append(scope $"o {Name}");
case .Text:
strBuffer.Append(scope $"t {Name} ");
for(var i in Value.Text.Split('\n', .None))
{
strBuffer.Append(i);
if(@i.HasMore)
{
strBuffer.Append('\n');
strBuffer.Append(' ', pDepth);
strBuffer.Append("- " );
}
}
}
}
///Deletes this object without filling the stack
public void DeepDeletion()
{
if(this.Type == .Array || this.Type == .Object)
{
List<Bofa> toDelete = new .();
List<Bofa> toProcess = new .();
toDelete.Add(this);
Bofa target = this;
repeat
{
if(target.Type == .Object)
{
for(var i in target.Value.Object)
{
toDelete.Add(i.value);
toProcess.Add(i.value);
}
target.Value.Object.Clear();
}
else if(target.Type == .Array)
{
for(var i in target.Value.Array)
{
toDelete.Add(i);
toProcess.Add(i);
}
target.Value.Array.Clear();
}
if(toProcess.Count > 0 && target != null)
target = toProcess.PopBack();
else
target = null;
} while(target != null);
for(var i in toDelete.Reversed)
delete i;
delete toDelete;
delete toProcess;
}
else
{
if(_line != null)
delete _line;
if(Type == .Text)
delete Value.Text;
}
}
public ~this()
{
if(_line != null)

View file

@ -9,18 +9,16 @@ using Bofa.Parser;
class BofaParser
{
public static void Parse(StringView pToParse, List<Bofa> pResult, List<int64> pErrors)
public static void Parse(StringView pToParse, Dictionary<StringView, Bofa> pResult, List<int64> pErrors)
{
StringStream stream = new .(pToParse, .Reference);
defer delete stream;
Parse(stream, pResult, pErrors);
}
public static void Parse(Stream pToParse, List<Bofa> pResult, List<int64> pErrors)
public static void Parse(Stream pToParse, Dictionary<StringView, Bofa> pResult, List<int64> pErrors)
{
HashSet<SHA256Hash> lowLevelObjects = new .(100);
defer delete lowLevelObjects;
Bofa last = null;
if (pToParse == null || pResult == null)
{ //Cannot parse what doesnt exist
pErrors.Add(-1);
@ -36,6 +34,7 @@ class BofaParser
String l = new .();
if (reader.ReadLine(l) case .Err)
{ //Despite data being here, we cannot read it
delete l;
pErrors.Add(-2);
return;
}
@ -49,16 +48,18 @@ class BofaParser
continue;
}
if(entry.Type == .Error)
{
pErrors.Add(line);
if(entry.Type == .Text)
defer delete l;
continue;
}
//If we are on the lowest level we can just add them here
if(entry.Depth == 0 && entry.Type ==.Bofa)
{
if(lowLevelObjects.Add(SHA256.Hash(cast!<Span<uint8>>(entry.Data.Bofa.Name))))
if(!pResult.ContainsKey(entry.Data.Bofa.Name))
{
pResult.Add(entry.Data.Bofa);
last = entry.Data.Bofa;
pResult.Add(entry.Data.Bofa.Name, entry.Data.Bofa);
}
else
{
@ -68,27 +69,29 @@ class BofaParser
}
else if(entry.Depth == 1 && entry.Type == .Text)
{
if(pResult[pResult.Count-1].Type == .Text)
pResult[pResult.Count-1].Value.Text.Append(scope $"\n{entry.Data.Text}");
if(last.Type == .Text)
last.Value.Text.Append(scope $"\n{entry.Data.Text}");
else
pErrors.Add(entry.Line); //Bad text error
}
else
{
entry.Depth--;
if(pResult[pResult.Count-1].[Friend]_Insert(&entry) case .Err)
if(last.[Friend]_Insert(&entry) case .Err)
{
pErrors.Add(line);
delete entry.Data.Bofa;
}
}
if(entry.Type == .Text)
delete l;
line++;
}
return; //Being done normally
}
[Inline]
private static ParserEntry ParseLine(String pLine, int64 pLineNumber)
{
StringView line = pLine;
@ -129,8 +132,6 @@ class BofaParser
if (type.0 == "") //This should never be reached
{
Runtime.FatalError("Unreachable code reached");
toReturn.Type = .Error;
return toReturn;
}
else if (type.0 == "-")
{

10
src/Extension.bf Normal file
View file

@ -0,0 +1,10 @@
namespace System
{
static
{
public static mixin cast<T>(var v)
{
*(T*)(void*)&v
}
}
}

View file

@ -6,8 +6,43 @@ using System;
extension Bofa
{
[Inline]
private Result<void> _Insert(ParserEntry* pToAdd)
{
Bofa target = this;
while(!(pToAdd.Depth == 0 || (pToAdd.Type == .Text && pToAdd.Depth == 1)))
{
if(target == null || !(target.Type == .Object || target.Type == .Array))
return .Err;
pToAdd.Depth--;
target = target._lastObject;
}
if(pToAdd.Type == .Text && target.Type == .Text)
{
target.Value.Text.Append(scope $"\n{pToAdd.Data.Text}");
//delete pToAdd.Data.Text;
return .Ok;
}
else if(target.Type == .Object)
{
if(target.Value.Object.ContainsKey(pToAdd.Data.Bofa.Name))
return .Err;
target.Value.Object.Add(pToAdd.Data.Bofa.Name, pToAdd.Data.Bofa);
target._lastObject = pToAdd.Data.Bofa;
return .Ok;
}
else if(target.Type == .Array)
{
target.Value.Array.Add(pToAdd.Data.Bofa);
target._lastObject = pToAdd.Data.Bofa;
return .Ok;
}
return .Err;
/*
//See if we can insert and do so
if(pToAdd.Depth == 0 || (pToAdd.Type == .Text && pToAdd.Depth == 1))
{
@ -41,5 +76,6 @@ extension Bofa
pToAdd.Depth--;
return _lastObject._Insert(pToAdd);
*/
}
}

View file

@ -1,39 +0,0 @@
using System;
namespace Bofa
{
class Program
{
public static void Main()
{
Bofa.Testing.Profiling.Profiling_Normal_Large();
}
}
}
namespace System.Security.Cryptography
{
extension SHA256Hash : IHashable
{
public int GetHashCode()
{
return this.mHash[0];
}
}
}
namespace System
{
static
{
public static mixin cast<T>(var v)
{
*(T*)(void*)&v
}
}
}

View file

@ -0,0 +1,45 @@
namespace Bofa.Testing;
using Bofa;
using System;
using System.Collections;
class Consistency
{
[Test(Name="Reverse simple")]
public static void Standart()
{
String content = """
l one line of text
b bool true
t textgoes here
- Text addendum
n tone 12.5
bn biggie 6.56456e+10
i int 234345
bi bint 38432847329847329
o object
b content true
a array
b content true
b content true
""";
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(content, output, errors);
String s = scope .();
for(var i in output)
{
i.value.ToString(s);
s.Append('\n');
}
s.RemoveFromEnd(1);
Test.Assert(content == s);
DeleteDictionaryAndValues!(output);
delete errors;
}
}

View file

@ -29,16 +29,14 @@ class Default
b content true
b content true
""";
List<Bofa> output = new .();
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(content, output, errors);
Test.Assert(output.Count == 9);
Test.Assert(output[7].Value.Object.Count == 1);
Test.Assert(output[8].Value.Array.Count == 2);
Test.Assert(errors.Count == 0);
DeleteContainerAndItems!(output);
DeleteDictionaryAndValues!(output);
delete errors;
}
@ -53,14 +51,14 @@ class Default
i integer 21324
bi biginteger 565765765765765
""";
List<Bofa> output = new .();
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(content, output, errors);
Test.Assert(output.Count == 5);
Test.Assert(errors.Count == 0);
DeleteContainerAndItems!(output);
DeleteDictionaryAndValues!(output);
delete errors;
}
@ -76,14 +74,14 @@ class Default
- that stuff has to be appended
- but thats fine
""";
List<Bofa> output = new .();
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(content, output, errors);
Test.Assert(output.Count == 1);
Test.Assert(errors.Count == 0);
DeleteContainerAndItems!(output);
DeleteDictionaryAndValues!(output);
delete errors;
}
@ -102,14 +100,14 @@ class Default
o even deeper
n member 1
""";
List<Bofa> output = new .();
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(content, output, errors);
Test.Assert(output.Count == 2);
Test.Assert(errors.Count == 0);
DeleteContainerAndItems!(output);
DeleteDictionaryAndValues!(output);
delete errors;
}
@ -125,14 +123,14 @@ class Default
n member 1
n member 2
""";
List<Bofa> output = new .();
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(content, output, errors);
Test.Assert(output.Count == 2);
Test.Assert(errors.Count == 2);
DeleteContainerAndItems!(output);
DeleteDictionaryAndValues!(output);
delete errors;
}
}

45
src/Testing/Fuzzing.bf Normal file
View file

@ -0,0 +1,45 @@
namespace Bofa.Testing;
using Bofa;
using System;
using System.Collections;
class Fuzzing
{
[Test(Name = "Fuzzing - 10 * 1000000 random characters")]
public static void Fuzzin_Random()
{
for (int i < 10)
{
String toParse = new .(10 * 1500);
for (int ii < 250000)
toParse.Append(cast!<uint8[4]>(gRand.NextI32()));
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(toParse, output, errors);
DeleteDictionaryAndValues!(output);
delete errors;
delete toParse;
}
}
[Test(Name = "Fuzzing - 10 * 10000000 random characters")]
public static void Fuzzin_Random_Large()
{
for (int i < 10)
{
String toParse = new .(10 * 1500);
for (int ii < 2500000)
toParse.Append(cast!<uint8[4]>(gRand.NextI32()));
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(toParse, output, errors);
DeleteDictionaryAndValues!(output);
delete errors;
delete toParse;
}
}
}

View file

@ -12,28 +12,32 @@ class Profiling
{
for (int i < 1000)
{
String content = """
l one line of text
b bool true
# saoidsaodsad
t text goes here
- Text addendum
n tone 12.5
#husdhfiudshfds
bn biggie 65645645645.6
i int 234345
bi bint 38432847329847329
o object
b content true
a array
b content true
b content true
String toParse = scope .();
toParse.Append(scope $"""
l one line of text
b bool true
# saoidsaodsad
t text goes here
- Text addendum
n tone 12.5
#husdhfiudshfds
bn biggie 65645645645.6
i int 234345
bi bint 38432847329847329
o object
b content true
a array
b content true
b content true
""";
List<Bofa> output = new .();
""");
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(content, output, errors);
DeleteContainerAndItems!(output);
BofaParser.Parse(toParse, output, errors);
DeleteDictionaryAndValues!(output);
delete errors;
}
}
@ -67,10 +71,10 @@ b content true
""");
}
List<Bofa> output = new .();
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(toParse, output, errors);
DeleteContainerAndItems!(output);
DeleteDictionaryAndValues!(output);
delete errors;
delete toParse;
}
@ -78,11 +82,11 @@ b content true
[Test(Name = "Profiling: normal 1 * 600000")]
[Test(Name = "Profiling: normal 1 * 300000")]
public static void Profiling_Normal_Large()
{
String toParse = new .(10 * 150000);
for (int ii < 40000)
for (int ii < 20000)
{
toParse.Append(scope $"""
l one{ii} line of text
@ -105,11 +109,110 @@ b content true
""");
}
List<Bofa> output = new .();
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(toParse, output, errors);
DeleteContainerAndItems!(output);
DeleteDictionaryAndValues!(output);
delete errors;
delete toParse;
}
[Test(Name = "Profiling: Depth testing 100 * 100")]
public static void Profiling_Depth_Testing()
{
for (int i < 100)
{
String toParse = scope .();
for(int ii < 100)
{
toParse.Append(' ', ii);
toParse.Append(scope $"o obj\n");
}
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(toParse, output, errors);
DeleteDictionaryAndValues!(output);
delete errors;
}
}
[Test(Name = "Profiling: Depth testing 100 * 1000")]
public static void Profiling_Depth_Testing_Medium()
{
for (int i < 100)
{
String toParse = scope .();
for(int ii < 1000)
{
toParse.Append(' ', ii);
toParse.Append(scope $"o obj\n");
}
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(toParse, output, errors);
DeleteDictionaryAndValues!(output);
delete errors;
}
}
[Test(Name = "Profiling: Depth testing 10 * 5000")]
public static void Profiling_Depth_Testing_Large()
{
for (int i < 10)
{
String toParse = scope .();
for(int ii < 5000)
{
toParse.Append(' ', ii);
toParse.Append(scope $"o obj\n");
}
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(toParse, output, errors);
output["obj"].DeepDeletion();
delete output;
delete errors;
}
}
[Test(Name = "Profiling: Texts 10 * 1000")]
public static void Profiling_Texts()
{
for (int i < 10)
{
String toParse = new .(10 * 1500);
toParse.Append("t text goes here");
for (int ii < 1000)
toParse.Append("- Addendum");
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(toParse, output, errors);
DeleteDictionaryAndValues!(output);
delete errors;
delete toParse;
}
}
[Test(Name = "Profiling: Large Texts 10 * 100000")]
public static void Profiling_Texts_Large()
{
for (int i < 10)
{
String toParse = new .(10 * 1500);
toParse.Append("t text goes here");
for (int ii < 100000)
toParse.Append("- Addendum");
Dictionary<StringView, Bofa> output = new .();
List<int64> errors = new .();
BofaParser.Parse(toParse, output, errors);
DeleteDictionaryAndValues!(output);
delete errors;
delete toParse;
}
}
}