From 634681ce2bb36cd686943102a0f071cd6a09049f Mon Sep 17 00:00:00 2001 From: Melodi <62381002+Melodi17@users.noreply.github.com> Date: Wed, 27 Aug 2025 21:22:55 +1000 Subject: fixed bugs and revolutionized the universe --- Compiler/visitors/Constants.cs | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Compiler/visitors/Constants.cs (limited to 'Compiler/visitors/Constants.cs') diff --git a/Compiler/visitors/Constants.cs b/Compiler/visitors/Constants.cs new file mode 100644 index 0000000..598c642 --- /dev/null +++ b/Compiler/visitors/Constants.cs @@ -0,0 +1,50 @@ +namespace Q1.Compiler.visitors; + +using System.Text; + +public partial class CCompilerVisitor +{ + public Encoding Encoding; + public override string VisitIntConstant(CGrammarParser.IntConstantContext context) + { + string valueString = context.INT().GetText(); + if (int.TryParse(valueString, out int value)) + return $"{value}"; + else + throw new CompilerException($"Invalid integer value: {valueString}"); + } + public override string VisitHexConstant(CGrammarParser.HexConstantContext context) + { + string valueString = context.HEX().GetText(); + valueString = valueString[2..]; // Remove 0x + return $"${valueString}"; + } + + public override object? VisitCharConstant(CGrammarParser.CharConstantContext context) + { + char c = context.LETTER().GetText()[1]; + return $"{this.Encoding.GetBytes(c.ToString())[0]}"; + } + + public override object? VisitStringConstant(CGrammarParser.StringConstantContext context) + { + string text = context.STRING().GetText()[1..^1]; + + int? arrPtr = null; + + if (text.Length == 0) + throw new CompilerException("Cannot create empty string"); + + foreach (char c in text) + { + int itemPtr = Alloc(CType.Int.Size); + + if (arrPtr == null) + arrPtr = itemPtr; + + this.DynamicMove(CType.Int, $"{this.Encoding.GetBytes(c.ToString())[0]}", $"[${itemPtr:X4}]"); + } + + return $"${arrPtr:X4}"; + } +} \ No newline at end of file -- cgit v1.3.1