From 1e85727d3dd0cefc0acfdf7c12a7168cb17cc34f Mon Sep 17 00:00:00 2001 From: Miraculous Ladybugreport <3642643+PeyTy@users.noreply.github.com> Date: Sat, 27 Jul 2024 02:01:16 +0300 Subject: [PATCH] [C2HEXA] Cast primitives and ! --- source/toHexa/clang/clangGenerator.hexa | 31 +++++++++++++++++++------ 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/source/toHexa/clang/clangGenerator.hexa b/source/toHexa/clang/clangGenerator.hexa index 49cc490..ff74fbb 100644 --- a/source/toHexa/clang/clangGenerator.hexa +++ b/source/toHexa/clang/clangGenerator.hexa @@ -349,6 +349,9 @@ class ClangGenerator { case Int(value): if value == 0 { return Node.Bool(false) } + if value == 1 { + return Node.Bool(true) + } case Unop(op, _): switch op { case KNot: return result @@ -367,21 +370,30 @@ class ClangGenerator { } let qualType = node.type.qualType + let desugaredQualType = node.type.desugaredQualType ?? '?' + let primitives = [ + // Also assumes `INT`, `unsigned` and others + "int", "long", "short" + ] // TODO native `bool` and `BOOLEAN` if qualType == "BOOL" { result = Node.Binop(result, Token.Equal, Node.Ident('Bool32.True', null)) } else - if [ - // Also assumes `INT`, `unsigned` and others - "int", "long", "short" - // TODO format - ].includes(qualType.toLowerCase().replace('unsigned ', '')) { + if + primitives.includes(qualType.toLowerCase().replace('unsigned ', '').replace('const ', '')) + or + primitives.includes(desugaredQualType.toLowerCase().replace('unsigned ', '').replace('const ', '')) + { result = Node.Binop(result, Token.Unequal, Node.Int(0)) + } else + + // `!= null` + { + result = Node.Binop(result, Token.Unequal, Node.Null) } - // TODO `!= null` // TODO `char` and `wchar_t` return result @@ -690,6 +702,12 @@ class ClangGenerator { // TODO "isPostfix": false // TODO no need for & for @struct // TODO detect !=0 and !=null and !! + // Logical + if node.opcode == '!' { + // TODO `not != 0` to `== 0` + return Node.Unop(Token.KNot, postfix: false, nodeToBoolean(node.inner[0])) + } + let expr = nodeToNode(node.inner[0]) switch node.opcode { case '~': return Node.Unop(Token.BitwiseNot, postfix: false, expr) @@ -701,7 +719,6 @@ class ClangGenerator { // TODO mutatedNode(expr) return Node.Unop(Token.Decrement, postfix: true, expr) case '*': return Node.Index(expr, Node.Int(0)) - case '!': return Node.Unop(Token.KNot, postfix: false, expr) case '&': return expr } case ReturnStmt: