From 4fff98b010026f2cc80eee2ced69af03761a088a Mon Sep 17 00:00:00 2001 From: nub31 Date: Mon, 7 Jul 2025 19:16:06 +0200 Subject: [PATCH] ... --- example/src/main.nub | 64 ++++++++++--------- .../NubLang/Generation/QBE/QBEGenerator.cs | 31 ++++----- 2 files changed, 47 insertions(+), 48 deletions(-) diff --git a/example/src/main.nub b/example/src/main.nub index 1ee51c6..5dc53c4 100644 --- a/example/src/main.nub +++ b/example/src/main.nub @@ -1,41 +1,47 @@ -// struct name -// { -// first: cstring -// last: cstring -// } +struct name +{ + first: cstring + last: cstring +} -// struct human -// { -// name: name -// age: i64 -// } +struct human +{ + name: name + age: i64 +} -// trait printable -// { -// func print() -// } +trait printable +{ + func print() +} -// impl printable for human -// { -// func print() { -// c::puts(this.name.first) -// } -// } +impl printable for human +{ + func print() { + c::puts(this.name.first) + } +} func main(args: []cstring): i64 { - // let human = alloc human - // { - // name = alloc name { - // first = "john" - // last = "doe" - // } - // age = 23 - // } + let human = alloc human + { + name = alloc name { + first = "john" + last = "doe" + } + age = 23 + } - // human.print() + human.print() print_result(12, func(num) { return num == 12 }) + let x: cstring = "test" + + let addr = x& + + c::puts(addr^) + return 0 } diff --git a/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs b/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs index 55fb85e..418a997 100644 --- a/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs +++ b/src/compiler/NubLang/Generation/QBE/QBEGenerator.cs @@ -97,28 +97,21 @@ public partial class QBEGenerator return _writer.ToString(); } - private string QBEAssign(NubType type) + private static string QBEAssign(NubType type) { - return type switch + if (type.IsSimpleType(out var simpleType, out _)) { - NubComplexType => "=l", - NubSimpleType simpleType => simpleType switch + return simpleType.StorageSize switch { - NubFuncType or NubFuncType => "=l", - NubPrimitiveType primitiveType => primitiveType.Kind switch - { - PrimitiveTypeKind.I64 or PrimitiveTypeKind.U64 => "=l", - PrimitiveTypeKind.I32 or PrimitiveTypeKind.U32 => "=w", - PrimitiveTypeKind.I16 or PrimitiveTypeKind.U16 => "=w", - PrimitiveTypeKind.I8 or PrimitiveTypeKind.U8 or PrimitiveTypeKind.Bool => "=w", - PrimitiveTypeKind.F64 => "=d", - PrimitiveTypeKind.F32 => "=s", - _ => throw new ArgumentOutOfRangeException() - }, - _ => throw new NotSupportedException($"'{type}' type cannot be used in variables") - }, - _ => throw new UnreachableException() - }; + StorageSize.I8 or StorageSize.U8 or StorageSize.I16 or StorageSize.U16 or StorageSize.I32 or StorageSize.U32 => "=w", + StorageSize.I64 or StorageSize.U64 => "=l", + StorageSize.F32 => "=s", + StorageSize.F64 => "=d", + _ => throw new ArgumentOutOfRangeException(nameof(simpleType.StorageSize)) + }; + } + + return "=l"; } private void EmitStore(NubType type, string value, string destination)