summaryrefslogtreecommitdiff
path: root/Compiler/visitors/Constants.cs
diff options
context:
space:
mode:
authorMelodi <62381002+Melodi17@users.noreply.github.com>2025-08-27 21:22:55 +1000
committerMelodi <62381002+Melodi17@users.noreply.github.com>2025-08-27 21:22:55 +1000
commit634681ce2bb36cd686943102a0f071cd6a09049f (patch)
tree6710047aa231d50f57114bdd54a27c559fb20a73 /Compiler/visitors/Constants.cs
parent94ddc02e03ab942d0c881e29053e8df3d2940c30 (diff)
fixed bugs and revolutionized the universe
Diffstat (limited to 'Compiler/visitors/Constants.cs')
-rw-r--r--Compiler/visitors/Constants.cs50
1 files changed, 50 insertions, 0 deletions
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