added some classic sqript spice, fixed a few bugs

This commit is contained in:
Qrakhen 2025-11-09 10:44:49 +01:00
parent 3013a338ed
commit 298a2ffa67
6 changed files with 75 additions and 57 deletions

View File

@ -30,7 +30,7 @@ public class ConsoleRenderer
if (token.Type.IsBoolean())
color = ConsoleColor.Blue;
if (token.Type.IsString())
color = ConsoleColor.Magenta;
color = ConsoleColor.Cyan;
if (token.Type.IsNumber())
color = ConsoleColor.DarkCyan;
if (token.Type.IsOperator())

View File

@ -148,6 +148,10 @@ public class Digester : ISteppable<Token>
{
_logger.Method();
ParseExpression();
if (Builder.Type == FunctionType.Code && Check(T.Eof))
Emit(Op.Print); // if we got presented with only an expression on top-level code and no ; at the end,
// we assume the user wants to see the value.
else {
Consume(T.Semicolon, "Expected ';' after statement");
Emit(Op.Pop); // in an expression statement, we definitely do not want to remain the value on stack,
// as it would fuck over the entire stack for the rest of the execution.
@ -155,6 +159,7 @@ public class Digester : ISteppable<Token>
//
// a tad bit older me: it's a terrible idea as return a = b; wouldn't work anymore
}
}
internal void ParseExpression() => WeightedDigest(Weight.Assign);

View File

@ -1,26 +1,18 @@
namespace Qrakhen.Qamp.Core.Tokenization;
using Qrakhen.Qamp.Core.Collections;
using static TokenType;
public class Dialect
{
private char[][] _sequences;
public Dialect()
{
_sequences = new char[0x80][];
}
// suuuper slow but meh, who cares
private Register<string, TokenType> _register = new();
public void Define(TokenType type, string sequence)
=> Define(type, sequence.ToCharArray());
=> _register[sequence] = type;
public void Define(TokenType type, char[] sequence)
{
_sequences[(int)type] = sequence;
}
public char[] Get(TokenType type)
=> _sequences[(int)type];
public TokenType Get(string sequence)
=> _register[sequence];
}
public class DefaultDialect : Dialect

View File

@ -23,13 +23,16 @@ internal static partial class ReaderPatterns
Keywords["false"] = False;
Keywords["true"] = True;
Keywords["null"] = Null;
Keywords["void"] = Null;
Keywords["and"] = And;
Keywords["else"] = Else;
Keywords["for"] = For;
Keywords["if"] = If;
Keywords["or"] = Or;
Keywords["this"] = This;
Keywords[".~"] = This;
Keywords["var"] = Var;
Keywords["*~"] = Var;
Keywords["while"] = While;
Keywords["do"] = Do;
Keywords["ref"] = Ref;
@ -38,10 +41,14 @@ internal static partial class ReaderPatterns
Keywords["fq"] = Function;
Keywords["funq"] = Function;
Keywords["return"] = Return;
Keywords["<:"] = Return;
Keywords["class"] = Class;
Keywords["base"] = Base;
Keywords["^~"] = Base;
Keywords["typeof"] = TypeOf;
Keywords["?:"] = TypeOf;
Keywords["print"] = Print;
Keywords["::"] = Var;
Keywords["globals"] = PrintGlobals;
Keywords["stack"] = PrintStack;
Keywords["expr"] = PrintExpr;
@ -255,14 +262,20 @@ public class Reader : IReader<Token>, IDisposable
'}' => MakeToken(ContextClose, buffer),
'(' => MakeToken(GroupOpen, buffer),
')' => MakeToken(GroupClose, buffer),
'.' => MakeToken(Dot, buffer),
'.' => Check('~') ?
MakeToken(This, buffer + Next()) :
MakeToken(Dot, buffer),
',' => MakeToken(Comma, buffer),
';' => MakeToken(Semicolon, buffer),
':' => MakeToken(Colon, buffer),
':' => Check(':') ?
MakeToken(Print, buffer + Next()) :
MakeToken(Colon, buffer),
'&' => Check('&') ?
MakeToken(And, buffer + Next()) :
MakeToken(BitwiseAnd, buffer),
'^' => MakeToken(BitwiseXor, buffer),
'^' => Check('~') ?
MakeToken(Base, buffer + Next()) :
MakeToken(BitwiseXor, buffer),
'%' => Check('=') ?
MakeToken(ModuloEqual, buffer + Next()) :
MakeToken(Modulo, buffer),
@ -287,11 +300,17 @@ public class Reader : IReader<Token>, IDisposable
MakeToken(Slash, buffer),
'*' => Check('=') ?
MakeToken(StarEqual, buffer + Next()) :
Check('~') ?
MakeToken(Var, buffer + Next()) :
MakeToken(Star, buffer),
'=' => Check('=') ?
MakeToken(EqualEqual, buffer + Next()) :
MakeToken(Equal, buffer),
'<' => Check('<') ?
'<' => Check('~') ?
MakeToken(Equal, buffer + Next()) :
Check(':') ?
MakeToken(Return, buffer + Next()) :
Check('<') ?
MakeToken(BitwiseLeft, buffer + Next()) :
Check('=') ?
MakeToken(LessEqual, buffer + Next()) :
@ -304,6 +323,8 @@ public class Reader : IReader<Token>, IDisposable
'~' => MakeToken(BitwiseNot, buffer),
'?' => Check('?') ?
MakeToken(DoubleQuestion, buffer + Next()) :
Check(':') ?
MakeToken(TypeOf, buffer + Next()) :
MakeToken(Question, buffer),
_ => MakeToken(Error, buffer) //throw new ReaderException($"Could not identify operator <{buffer}>", this)
};

View File

@ -114,7 +114,7 @@ public static class TokenTypeExtensions
=> type is Identifier;
public static bool IsControl(this TokenType type)
=> type is If or Else or For or While or Do or Return or And or Or;
=> type is If or Var or Class or Else or For or While or Do or Return or And or Or or TypeOf or Print;
public static bool IsBoolean(this TokenType type)
=> type is True or False;

View File

@ -4,5 +4,5 @@ public class Array(IEnumerable<Value> data) : Obj(ValueType.Array)
{
public List<Value> Data = [..data];
public override string ToString() => $"Array<{Data.Count}>";
public override string ToString() => $"Array [{Data.Count}]";
}