add array add, set, get

This commit is contained in:
Qrakhen 2025-11-18 00:21:06 +01:00
parent e86f463de6
commit c097e42c09
5 changed files with 113 additions and 177 deletions

View File

@ -34,116 +34,9 @@ void Init(string[] args)
}
Init(args);
void git(string path, string cmd)
{
Console.WriteLine($" > git {cmd}");
ProcessStartInfo info = new ProcessStartInfo{
WorkingDirectory = path,
FileName = "git",
Arguments = cmd
};
Process.Start(info)?.WaitForExit();
}
void del(string path, string cmd)
{
Console.WriteLine($" > git {cmd}");
ProcessStartInfo info = new ProcessStartInfo{
WorkingDirectory = path,
FileName = "del",
Arguments = $"{cmd} /F /Q /S"
};
Process.Start(info)?.WaitForExit();
}
string x = @"C:\Users\filos\projects";
string[] check = ["node_modules", "bin", "obj", "Library"];
int scanx(string path)
{
foreach (var dir in Directory.EnumerateDirectories(path)) {
try {
var name = dir.Replace(path, "").Replace(@"\", "");
if (!check.Contains(name)) {
scanx(dir);
} else {
Console.WriteLine(dir + " (" + (Directory.GetFiles(dir).Length + Directory.GetDirectories(dir).Length) + " files)");
if ((Directory.GetFiles(dir).Length + Directory.GetDirectories(dir).Length) == 0) {
Console.WriteLine(" skipped (0 files)");
continue;
}
if (name == "Library") {
if (!Directory.Exists(path + "\\Assets")) {
Console.WriteLine(" skipped (not a definite unity library folder)");
continue;
}
}
Console.Write(name == "Library" ? " y/n?" : "");
do {
char c = name == "Library" ? Console.ReadKey(true).KeyChar : 'y';
if (c == 'y') {
Directory.Delete(dir, true);
break;
} else if (c == 'n') {
break;
} else {
Console.WriteLine(string.Join("\n", Directory.EnumerateDirectories(dir).Select(_ => $" + {_}")));
Console.WriteLine(string.Join("\n", Directory.EnumerateFiles(dir).Select(_ => $" - {_}")));
}
} while (true);
}
} catch(Exception e) { Console.WriteLine("ERROR: " + e.Message); }
}
return 0;
}
scanx(x);
return;
string path = @"C:\Users\filos\projects";
string[] ignore = ["node_modules", "bin", "obj", "Debug", "Build", ".vs"];
int scan(string path)
{
foreach (var dir in Directory.EnumerateDirectories(path)) {
var name = dir.Replace(path, "").Replace(@"\", "");
if (ignore.Contains(name))
continue;
if (name == ".git") {
Console.WriteLine(path);
git(path, "status");
Console.Write("y/n? ");
do {
char c = (char)Console.Read();
if (c == 'y') {
Console.Write(" origin [master]? ");
string t = Console.ReadLine() ?? "master";
if (string.IsNullOrEmpty(t))
t = "master";
git(path, "add .");
git(path, "commit -m \"backup commit\"");
git(path, "push origin " + t);
break;
} else if (c == 'n') {
break;
} else if (c > '0' && c < '9') {
return (48 - c);
}
} while (true);
} else {
int e = scan(dir);
if (e < 0)
return e+1;
}
}
return 0;
}
scan(path);
return;
*/
List<char> Ignored = [ '\0', '\b' ];
LoggerService.Default = LogLevel.Error;
List<byte[]> History = [];

View File

@ -111,7 +111,13 @@ public static class ExpressionParser
digester.EmitDynamic(OpCode.Array, length); // digester.MakeConstant(new Value((long)length)));
}
static void ArrayAccessor(Digester digester, bool canAssign)
static void ArrayAdd(Digester digester, bool canAssign)
{
digester.ParseExpression();
digester.Emit(OpCode.ArrayAdd);
}
static void Accessor(Digester digester, bool canAssign)
{
digester.ParseExpression();
digester.Consume(TokenType.ArrayClose, "expected ] after array accessor");
@ -246,9 +252,9 @@ public static class ExpressionParser
_rules[TokenType.GroupClose] = new Rule(null, null, Weight.None);
_rules[TokenType.ContextOpen] = new Rule(null, null, Weight.None);
_rules[TokenType.ContextClose] = new Rule(null, null, Weight.None);
_rules[TokenType.ArrayOpen] = new Rule(Array, ArrayAccessor, Weight.Call);
_rules[TokenType.ArrayOpen] = new Rule(Array, Accessor, Weight.Call);
_rules[TokenType.ArrayClose] = new Rule(null, null, Weight.None);
_rules[TokenType.ArrayAdd] = new Rule(null, null, Weight.None);
_rules[TokenType.ArrayAdd] = new Rule(null, ArrayAdd, Weight.None);
_rules[TokenType.ArrayRemove] = new Rule(null, null, Weight.None);
_rules[TokenType.Colon] = new Rule(null, Dot, Weight.Call);
_rules[TokenType.Comma] = new Rule(null, null, Weight.None);

View File

@ -205,17 +205,43 @@ public class Runner : IDisposable
}
case Op.ArrayGet: {
// attempt to retrieve nth element of array on stack
Value index = Pop();
Value array = Pop();
if (!array.Is(T.Array))
return Error($"Can not access {index} of {array} - it is not an array.");
if (index.IsSigned)
Push(array.Ptr.As<Values.Objects.Array>()!.Get((int)index.Signed));
else if (index.IsUnsigned)
Push(array.Ptr.As<Values.Objects.Array>()!.Get((int)index.Unsigned));
else
return Error($"Can not access {index}, index is not an integer.");
break;
}
case Op.ArraySet: {
// set nth item on array
Value value = Pop();
Value index = Pop();
Value array = Pop();
if (!array.Is(T.Array))
return Error($"Can not access {index} of {array} - it is not an array.");
if (index.IsSigned)
array.Ptr.As<Values.Objects.Array>()!.Set((int)index.Signed, value);
else if (index.IsUnsigned)
array.Ptr.As<Values.Objects.Array>()!.Set((int)index.Signed, value);
else
return Error($"Can not access {index}, index is not an integer.");
break;
}
case Op.ArrayAdd: {
// add to array
Value value = Pop();
Value array = Pop();
if (!array.Is(T.Array))
return Error($"Can not add {value} to {array} - it is not an array.");
array.Ptr.As<Values.Objects.Array>()!.Add(value);
break;
}

View File

@ -320,6 +320,8 @@ public class Reader : IReader<Token>, IDisposable
MakeToken(Equal, buffer + Next()) :
Check(':') ?
MakeToken(Return, buffer + Next()) :
Check('+') ?
MakeToken(ArrayAdd, buffer + Next()) :
Check('<') ?
MakeToken(BitwiseLeft, buffer + Next()) :
Check('=') ?

View File

@ -6,11 +6,13 @@ public class Array(IEnumerable<Value> data) : Obj(ValueType.Array)
public void Set(int index, Value value)
{
AssertWithinBounds(index);
Data[index] = value;
}
public Value Get(int index)
{
AssertWithinBounds(index);
return Data[index];
}
@ -19,5 +21,12 @@ public class Array(IEnumerable<Value> data) : Obj(ValueType.Array)
Data.Add(value);
}
public override string ToString() => $"[{string.Join(", ", Data)}]";
private void AssertWithinBounds(int index)
{
if (index < 0 || index > Data.Count)
throw new QampException($"Can not access element at index {index}, it is outside of this array's bounds.");
}
public override string ToString() => ToString(true);
public string ToString(bool detail) => detail ? $"[{string.Join(", ", Data)}]" : $"Array[{Data.Count}]";
}