clean up code a bit and revert back to .net8 in a desperate attempt to fix the profiler

This commit is contained in:
Qrakhen 2025-11-11 18:04:56 +01:00
parent dc94c16b03
commit 9e4fbd5906
42 changed files with 166 additions and 81 deletions

View File

@ -1,5 +1,4 @@
 using Qrakhen.Qamp.Core.Collections.Abstractions;
using Qrakhen.Qamp.Core;
using Qrakhen.Qamp.Core.Tokenization; using Qrakhen.Qamp.Core.Tokenization;
using System.Text; using System.Text;

View File

@ -4,15 +4,18 @@ using Qrakhen.Qamp.Core.Execution;
using Qrakhen.Qamp.Core.Logging; using Qrakhen.Qamp.Core.Logging;
using System.Text; using System.Text;
char[] Ignored = [ '\0', '\b' ]; // do args here too, keep console, etc. output with exit, all the spicy things
// if no file is given for example in args just keep cli live until quit
List<char> Ignored = [ '\0', '\b' ];
LoggerService.Default = LogLevel.Error; LoggerService.Default = LogLevel.Error;
Stack<byte[]> History = []; List<byte[]> History = [];
int historyCursor = -1;
ConsoleKeyInfo input; ConsoleKeyInfo input;
bool useSyntaxHighlighting = false; bool useSyntaxHighlighting = false;
(int x, int y) cursor = (0, 0); (int x, int y) cursor = (0, 0);
ConsoleCode code = ConsoleCode.Error; ConsoleCode code = ConsoleCode.Error;
Runner runner = new Runner(new Options()); Runner runner = new(new Options());
ConsoleWriter cli = new ConsoleWriter(Encoding.Default);
do { do {
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
MemoryStream stream = new MemoryStream(); MemoryStream stream = new MemoryStream();
@ -53,6 +56,18 @@ do {
continue; continue;
} }
if (input.Key == ConsoleKey.UpArrow) {
historyCursor = Math.Min(History.Count - 1, historyCursor + 1);
byte[] bytes = History[historyCursor];
continue;
}
if (input.Key == ConsoleKey.DownArrow) {
historyCursor = Math.Max(0, historyCursor - 1);
byte[] bytes = History[historyCursor];
continue;
}
if (input.Key == ConsoleKey.Backspace && stream.Position > 0) { if (input.Key == ConsoleKey.Backspace && stream.Position > 0) {
Console.CursorLeft -= 1; Console.CursorLeft -= 1;
Console.Write(' '); Console.Write(' ');
@ -72,8 +87,10 @@ do {
} }
} while (input.Key != ConsoleKey.Enter || input.Modifiers == ConsoleModifiers.Shift); } while (input.Key != ConsoleKey.Enter || input.Modifiers == ConsoleModifiers.Shift);
try { try {
History.Insert(0, stream.GetBuffer());
Console.WriteLine(); Console.WriteLine();
runner.Run(stream); runner.Run(stream);
stream.Dispose();
} }
catch (QampException e) { catch (QampException e) {
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<BaseOutputPath>..\Build\</BaseOutputPath> <BaseOutputPath>..\Build\</BaseOutputPath>

View File

@ -1,6 +1,6 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Abstractions;
public interface IDebug<out T> public interface IDebug<out T>
{ {

View File

@ -1,4 +1,4 @@
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Abstractions;
public interface ISerialize<TSelf> public interface ISerialize<TSelf>
{ {

View File

@ -1,6 +1,6 @@
using System.Numerics; using System.Numerics;
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface IAdd<out TKey, in TValue> public interface IAdd<out TKey, in TValue>
{ {

View File

@ -1,4 +1,4 @@
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface ICaptureBuffer<TData, THandle> public interface ICaptureBuffer<TData, THandle>
{ {

View File

@ -1,4 +1,4 @@
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface IConsumable<in T> public interface IConsumable<in T>
{ {

View File

@ -1,4 +1,4 @@
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface IGet<in TKey, out TValue> public interface IGet<in TKey, out TValue>
{ {

View File

@ -1,3 +1,3 @@
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface IGetSet<in TKey, TValue> : IGet<TKey, TValue>, ISet<TKey, TValue>; public interface IGetSet<in TKey, TValue> : IGet<TKey, TValue>, ISet<TKey, TValue>;

View File

@ -1,4 +1,4 @@
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface IPeekable<out T> public interface IPeekable<out T>
{ {

View File

@ -1,4 +1,4 @@
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface IReadable<out T> public interface IReadable<out T>
{ {

View File

@ -1,6 +1,6 @@
using Qrakhen.Qamp.Core.Tokenization; using Qrakhen.Qamp.Core.Tokenization;
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface IReader<out T> public interface IReader<out T>
{ {

View File

@ -1,4 +1,4 @@
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface ISeekable<TKey, out TValue> public interface ISeekable<TKey, out TValue>
{ {

View File

@ -1,4 +1,4 @@
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface ISet<in TKey, in TValue> public interface ISet<in TKey, in TValue>
{ {

View File

@ -1,4 +1,4 @@
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface IStack<out TKey, TValue> : IPush<TKey, TValue>, IPop<TValue>; public interface IStack<out TKey, TValue> : IPush<TKey, TValue>, IPop<TValue>;

View File

@ -1,4 +1,4 @@
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface ISteppable<out T> public interface ISteppable<out T>
{ {

View File

@ -0,0 +1,6 @@
namespace Qrakhen.Qamp.Core.Collections.Abstractions;
public interface IToArray<out T>
{
T[] ToArray();
}

View File

@ -1,4 +1,5 @@
using System.Collections; using Qrakhen.Qamp.Core.Collections.Abstractions;
using System.Collections;
namespace Qrakhen.Qamp.Core.Collections; namespace Qrakhen.Qamp.Core.Collections;

View File

@ -1,4 +1,5 @@
using System.Collections; using Qrakhen.Qamp.Core.Collections.Abstractions;
using System.Collections;
namespace Qrakhen.Qamp.Core.Collections; namespace Qrakhen.Qamp.Core.Collections;

View File

@ -1,4 +1,6 @@
namespace Qrakhen.Qamp.Core.Collections; using Qrakhen.Qamp.Core.Collections.Abstractions;
namespace Qrakhen.Qamp.Core.Collections;
/// <summary> /// <summary>
/// Dynamic Pointer able to point to any object implementing the <see cref="IGetSet{long, TValue}"/> interface. /// Dynamic Pointer able to point to any object implementing the <see cref="IGetSet{long, TValue}"/> interface.

View File

@ -1,4 +1,5 @@
using System.Collections; using Qrakhen.Qamp.Core.Collections.Abstractions;
using System.Collections;
namespace Qrakhen.Qamp.Core.Collections; namespace Qrakhen.Qamp.Core.Collections;

View File

@ -1,4 +1,6 @@
namespace Qrakhen.Qamp.Core.Collections; using Qrakhen.Qamp.Core.Collections.Abstractions;
namespace Qrakhen.Qamp.Core.Collections;
/// <summary> /// <summary>
/// Push-only Stack that is based on <see cref="Expander{T}"/>. /// Push-only Stack that is based on <see cref="Expander{T}"/>.

View File

@ -1,4 +1,5 @@
using System.Collections; using Qrakhen.Qamp.Core.Collections.Abstractions;
using System.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
namespace Qrakhen.Qamp.Core.Collections; namespace Qrakhen.Qamp.Core.Collections;

View File

@ -1,4 +1,6 @@
namespace Qrakhen.Qamp.Core.Collections; using Qrakhen.Qamp.Core.Collections.Abstractions;
namespace Qrakhen.Qamp.Core.Collections;
/// <summary> /// <summary>
/// Stack-like implementation with a few added features like seeking and setting values. /// Stack-like implementation with a few added features like seeking and setting values.

View File

@ -1,4 +1,5 @@
using Qrakhen.Qamp.Core.Collections; using Qrakhen.Qamp.Core.Abstractions;
using Qrakhen.Qamp.Core.Collections;
using Qrakhen.Qamp.Core.Execution; using Qrakhen.Qamp.Core.Execution;
using Qrakhen.Qamp.Core.Values; using Qrakhen.Qamp.Core.Values;

View File

@ -8,6 +8,7 @@ using Qrakhen.Qamp.Core.Compilation.Builders;
using Qrakhen.Qamp.Core.Values.Objects; using Qrakhen.Qamp.Core.Values.Objects;
using Qrakhen.Qamp.Core.Tokenization; using Qrakhen.Qamp.Core.Tokenization;
using Qrakhen.Qamp.Core.Logging; using Qrakhen.Qamp.Core.Logging;
using Qrakhen.Qamp.Core.Collections.Abstractions;
namespace Qrakhen.Qamp.Core.Compilation; namespace Qrakhen.Qamp.Core.Compilation;

View File

@ -107,17 +107,11 @@ public static class ExpressionParser
else else
break; break;
} }
digester.Consume(TokenType.ArrayClose, "Expected ']' after array definition"); digester.Consume(TokenType.ArrayClose, "Expected ']' after array declaration");
digester.EmitDynamic(OpCode.Array, length); // digester.MakeConstant(new Value((long)length))); digester.EmitDynamic(OpCode.Array, length); // digester.MakeConstant(new Value((long)length)));
} }
static void ArrayAdd(Digester digester, bool canAssign) static void ArrayAccessor(Digester digester, bool canAssign)
{
digester.ParseExpression();
digester.Emit(OpCode.ArrayAdd);
}
static void Index(Digester digester, bool canAssign)
{ {
digester.ParseExpression(); digester.ParseExpression();
digester.Consume(TokenType.ArrayClose, "expected ] after array accessor"); digester.Consume(TokenType.ArrayClose, "expected ] after array accessor");
@ -252,7 +246,7 @@ public static class ExpressionParser
_rules[TokenType.GroupClose] = new Rule(null, null, Weight.None); _rules[TokenType.GroupClose] = new Rule(null, null, Weight.None);
_rules[TokenType.ContextOpen] = 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.ContextClose] = new Rule(null, null, Weight.None);
_rules[TokenType.ArrayOpen] = new Rule(Array, Index, Weight.Call); _rules[TokenType.ArrayOpen] = new Rule(Array, ArrayAccessor, Weight.Call);
_rules[TokenType.ArrayClose] = new Rule(null, null, Weight.None); _rules[TokenType.ArrayClose] = new Rule(null, null, Weight.None);
_rules[TokenType.ArrayAdd] = new Rule(null, null, Weight.None); _rules[TokenType.ArrayAdd] = new Rule(null, null, Weight.None);
_rules[TokenType.ArrayRemove] = new Rule(null, null, Weight.None); _rules[TokenType.ArrayRemove] = new Rule(null, null, Weight.None);

View File

@ -1,4 +1,5 @@
using Qrakhen.Qamp.Core.Tokenization; using Qrakhen.Qamp.Core.Collections.Abstractions;
using Qrakhen.Qamp.Core.Tokenization;
using Qrakhen.Qamp.Core.Values; using Qrakhen.Qamp.Core.Values;
namespace Qrakhen.Qamp.Core; namespace Qrakhen.Qamp.Core;

View File

@ -1,4 +1,6 @@
namespace Qrakhen.Qamp.Core.Execution; using Qrakhen.Qamp.Core.Collections.Abstractions;
namespace Qrakhen.Qamp.Core.Execution;
public class Instructions<T>(IEnumerable<T> instructions) : public class Instructions<T>(IEnumerable<T> instructions) :
ISteppable<T>, ISteppable<T>,

View File

@ -192,6 +192,26 @@ public class Runner : IDisposable
break; break;
} }
case Op.Array: {
// essentially collect all pushed values and create an array, no?
break;
}
case Op.ArrayGet: {
// attempt to retrieve nth element of array on stack
break;
}
case Op.ArraySet: {
// set nth item on array
break;
}
case Op.ArrayAdd: {
// add to array
break;
}
case Op.Jump: { case Op.Jump: {
long delta = call.Instruction.NextLong(); long delta = call.Instruction.NextLong();
call.Instruction.Cursor += delta; call.Instruction.Cursor += delta;
@ -260,7 +280,7 @@ public class Runner : IDisposable
break; break;
} }
case Op.Return: // todo: not done case Op.Return:
Value result = Pop(); Value result = Pop();
CloseOuters(call.StackPtr.Branch()); CloseOuters(call.StackPtr.Branch());
Calls.Pop(); Calls.Pop();
@ -288,7 +308,7 @@ public class Runner : IDisposable
if (!inheritingClass.Members.Has(member.Key)) if (!inheritingClass.Members.Has(member.Key))
inheritingClass.Members.Add(member.Key, member.Value); inheritingClass.Members.Add(member.Key, member.Value);
} }
Pop(); // Pop();
break; break;
} }
@ -321,14 +341,14 @@ public class Runner : IDisposable
case Op.PrintStack: { case Op.PrintStack: {
for (int i = 0; i < Cursor; i++) { for (int i = 0; i < Cursor; i++) {
Console.WriteLine($"{Stack[i].ToString(true)}"); IO.Console.Write($"{Stack[i].ToString(true)}");
} }
break; break;
} }
case Op.PrintGlobals: { case Op.PrintGlobals: {
foreach (var value in Globals) { foreach (var value in Globals) {
Console.WriteLine($"{value.Key}: {value.Value.ToString(true)}"); IO.Console.Write($"{value.Key}: {value.Value.ToString(true)}");
} }
break; break;
} }

View File

@ -1,4 +1,5 @@
using Qrakhen.Qamp.Core.Collections; using Qrakhen.Qamp.Core.Abstractions;
using Qrakhen.Qamp.Core.Collections;
using Qrakhen.Qamp.Core.Values; using Qrakhen.Qamp.Core.Values;
using Qrakhen.Qamp.Core.Values.Objects; using Qrakhen.Qamp.Core.Values.Objects;

View File

@ -9,6 +9,7 @@ public static class Extensions
{ {
if (string.IsNullOrEmpty(str)) if (string.IsNullOrEmpty(str))
return 0; return 0;
byte[] bytes = Encoding.ASCII.GetBytes(str); byte[] bytes = Encoding.ASCII.GetBytes(str);
unchecked { unchecked {
uint hash = 216613661u; uint hash = 216613661u;
@ -73,10 +74,8 @@ public static class Extensions
public static int GetPrimitiveLength(this long value) public static int GetPrimitiveLength(this long value)
{ {
int length = 8; int length = 8;
for (int i = 1; i < 8; i++) for (int i = 1; i < 8; i++) {
{ if (value <= (1L << (i * 8))) {
if (value <= (1L << (i * 8)))
{
length = i; length = i;
break; break;
} }
@ -84,7 +83,6 @@ public static class Extensions
return length; return length;
} }
public static byte[] GetBytes(this short value) => BitConverter.GetBytes(value); public static byte[] GetBytes(this short value) => BitConverter.GetBytes(value);
public static byte[] GetBytes(this ushort value) => BitConverter.GetBytes(value); public static byte[] GetBytes(this ushort value) => BitConverter.GetBytes(value);
public static byte[] GetBytes(this int value) => BitConverter.GetBytes(value); public static byte[] GetBytes(this int value) => BitConverter.GetBytes(value);

View File

@ -1,6 +0,0 @@
namespace Qrakhen.Qamp.Core;
public interface IToArray<out T>
{
T[] ToArray();
}

View File

@ -1,4 +1,5 @@
using Qrakhen.Qamp.Core.Logging; using Qrakhen.Qamp.Core.Collections.Abstractions;
using Qrakhen.Qamp.Core.Logging;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using static Qrakhen.Qamp.Core.Tokenization.TokenType; using static Qrakhen.Qamp.Core.Tokenization.TokenType;

View File

@ -1,4 +1,6 @@
namespace Qrakhen.Qamp.Core.Tokenization; using Qrakhen.Qamp.Core.Abstractions;
namespace Qrakhen.Qamp.Core.Tokenization;
public class Tokens : IDebug<string> public class Tokens : IDebug<string>
{ {

View File

@ -4,5 +4,20 @@ public class Array(IEnumerable<Value> data) : Obj(ValueType.Array)
{ {
public List<Value> Data = [..data]; public List<Value> Data = [..data];
public override string ToString() => $"Array [{Data.Count}]"; public void Set(int index, Value value)
{
Data[index] = value;
}
public Value Get(int index)
{
return Data[index];
}
public void Add(Value value)
{
Data.Add(value);
}
public override string ToString() => $"[{string.Join(", ", Data)}]";
} }

View File

@ -0,0 +1,20 @@
namespace Qrakhen.Qamp.Core.Values.Objects;
public class Dictionary(IEnumerable<KeyValuePair<string, Value>> data) : Obj(ValueType.Object)
{
public Dictionary<string, Value> Data = new(data);
public void Set(string key, Value value)
{
Data[key] = value;
}
public Value Get(string key)
{
if (Data.TryGetValue(key, out Value value))
return value;
return Value.Void;
}
public override string ToString() => $"[{string.Join(", ", Data)}]";
}

View File

@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Qrakhen.Qamp.Core.Abstractions;
using Qrakhen.Qamp.Core.Values.Objects; using Qrakhen.Qamp.Core.Values.Objects;
using String = Qrakhen.Qamp.Core.Values.Objects.String; using String = Qrakhen.Qamp.Core.Values.Objects.String;
using T = Qrakhen.Qamp.Core.Values.ValueType; using T = Qrakhen.Qamp.Core.Values.ValueType;

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net10.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<BaseOutputPath>..\Build\</BaseOutputPath> <BaseOutputPath>..\Build\</BaseOutputPath>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net10.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>

View File

@ -38,12 +38,13 @@ All of the following examples are written in the classic dialect `--dialect=sqr`
### Syntax ### Syntax
Most basic Usage: Most basic Usage:
``` ```cs
# This is a comment. # this is a comment.
*~ q <~ 12; *~ q <~ 12; # q == 12
*~ f <~ (n) <: n < 2 ? n : f(n-1) + f(n-2); *~ f <~ (n) <: n < 2 ? n : f(n-1) + f(n-2); # fibonacci
*~ a <~ [1, 2, 3]; f(12); # 233
*~ x <~ a:0 + a:2; # = 4 *~ a <~ [1, 2, 3]; # a = [1, 2, 3]
*~ x <~ a:0 + a:2; # x == 4
``` ```
### Assignments ### Assignments
@ -51,6 +52,7 @@ Most basic Usage:
**Q&** has two distinct ways to assign a value to a variable: **Q&** has two distinct ways to assign a value to a variable:
- By reference `<&`, or - By reference `<&`, or
- By value `<=`. - By value `<=`.
Using the auto-assignment operator `<~`, **Q&** will choose the correct assignment based on what Using the auto-assignment operator `<~`, **Q&** will choose the correct assignment based on what
type the right-hand expression consists of. type the right-hand expression consists of.
Primitive types will be assigned by value, any object values will be assigned by reference. Primitive types will be assigned by value, any object values will be assigned by reference.