Skip to content

Commit

Permalink
[C2HEXA] Cast primitives and !
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyTy committed Jul 26, 2024
1 parent 3e67209 commit 1e85727
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions source/toHexa/clang/clangGenerator.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit 1e85727

Please sign in to comment.