Skip to content

Commit

Permalink
[C2HEXA] Recognize ArrayByValue
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyTy committed Jul 20, 2024
1 parent f94e38e commit 566bdd2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
7 changes: 6 additions & 1 deletion source/data/nodeType.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ enum NodeType {
Optional(type NodeType)

/// `T` or `path.T`
// TODO `(params [NodeType]? = null)` + `case Type(..., params: null)`
Type(name String, path String?)

/// `T<K, V>`
// TODO delete Type and always just use ParametricType with params==null`null!!!!`
// TODO delete Type and always just use ParametricType with params==null
ParametricType(name String, params [NodeType], path String?)

/// `(T, T) => T`
Expand Down Expand Up @@ -56,6 +57,9 @@ enum NodeType {
/// Readable string representation
static fun stringify(nodeType NodeType) String {
switch nodeType {
// TODO more
case Int(value):
return '' + value
case Optional(type):
return NodeType.stringify(type) + '?'
case Object(names, types):
Expand All @@ -77,6 +81,7 @@ enum NodeType {
/// Deep cloning, normally used for generic instances
static fun clone(nodeType NodeType) NodeType {
switch nodeType {
// TODO more
case Type(name, path):
return NodeType.Type(name, path)
case ParametricType(name, params, path):
Expand Down
1 change: 1 addition & 0 deletions source/server/format.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ fun autoFormatWholeFile(file: String): String {
lastToken != Token.Comma and
lastToken != Token.Equal and
lastToken != Token.Unequal and
// Includes `??`
lastToken != Token.Question and

// `@demo()` but not `@demo ()`
Expand Down
26 changes: 21 additions & 5 deletions source/toHexa/clang/clangGenerator.hexa
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ class ClangGenerator {
fun typeToNodeType(qualType String) NodeType {
let qualType = desugarKnownType(qualType)

// Array
if qualType.endsWith(']') {
let parts = qualType.replace(']', '').split('[')

return NodeType.ParametricType(
'ArrayByValue', [typeToNodeType(parts [0]), NodeType.Int(parseInt(parts [1]))],
null
)
}

// Function calling convention
if qualType.endsWith(' __attribute__((stdcall))') {
let clear = qualType.substr(0, qualType.length - ' __attribute__((stdcall))'.length).trim()
Expand Down Expand Up @@ -272,6 +282,10 @@ class ClangGenerator {
return Node.Call(Node.NodeTypeValue(varType), [], [])
}

if str.startsWith('ArrayByValue<') {
return Node.Array([Node.Int(0)])
}

if not zeroInitialize.includes(str) {
return Node.Null
}
Expand Down Expand Up @@ -324,7 +338,7 @@ class ClangGenerator {
if let decl = previousRecordDecl {
previousRecordDecl = null

let className = NodeType.Type(node.name, null)
let className = NodeType.Type(desugarQualType.get(node.name) ?? node.name, null)
let extend NodeType? = null
let implement [NodeType] = []
let kind = ClassKind.Class
Expand Down Expand Up @@ -352,7 +366,7 @@ class ClangGenerator {
var returnType NodeType = typeToType(typeOfReturn(node.type))
var body Node? = null
// TODO FORMAT
let nodes = node.inner ??([] as! [ClangNode] /*TODO*/)
let nodes = node.inner ?? ([] as! [ClangNode] /*TODO*/)

for functionDecl in nodes {
switch functionDecl.kind {
Expand Down Expand Up @@ -504,8 +518,8 @@ class ClangGenerator {

switch condition {
case Int(value): if value == 0 {
return Node.Block([])
}
return Node.Block([])
}
}

return Node.While(condition, wrapToBlockIfNone(nodeToNode(node.inner[0])), true)
Expand Down Expand Up @@ -789,7 +803,9 @@ class ClangGenerator {
// TODO `case`-body still contains empty blocks
fun noEmptyBlocks(expr Node) Bool {
switch expr {
case Block(e) if e.length == 0: return false
// TODO format bug
// case Block(e) if e.length == 0: return false
case Block(e): if e.length == 0 { return false }
}

return true
Expand Down

0 comments on commit 566bdd2

Please sign in to comment.