M
Modular13mo ago
Geo7

How to Implement Recursive data type

I want to Implement abstract syntax tree in mojo, so far no luck, Something simple as this in C : https://github.com/orangeduck/mpc/blob/master/mpc.h#L291
struct ast {
char *tag;
char *contents;
int children_num;
struct ast** children;
}
struct ast {
char *tag;
char *contents;
int children_num;
struct ast** children;
}
GitHub
mpc/mpc.h at master · orangeduck/mpc
A Parser Combinator library for C. Contribute to orangeduck/mpc development by creating an account on GitHub.
2 Replies
Geo7
Geo7OP13mo ago
@value
@register_passable("trivial")
struct UnsafeString:
var data: Pointer[Int8]
var len: Int

fn __init__(str: StringLiteral) -> UnsafeString:
let l = str.__len__()
let s = String(str)
let p = Pointer[Int8].alloc(l)
for i in range(l):
p.store(i, s._buffer[i])
return UnsafeString(p, l)

fn to_string(self) -> String:
let s = String(self.data, self.len)
return s


@value
@register_passable("trivial")
struct AST:
var tag: UnsafeString
var content: UnsafeString
var child_no: Int
var children: Pointer[AST]


fn main():
print("AST Started")
var child1 = AST("Ident", "book", 0, 0)
@value
@register_passable("trivial")
struct UnsafeString:
var data: Pointer[Int8]
var len: Int

fn __init__(str: StringLiteral) -> UnsafeString:
let l = str.__len__()
let s = String(str)
let p = Pointer[Int8].alloc(l)
for i in range(l):
p.store(i, s._buffer[i])
return UnsafeString(p, l)

fn to_string(self) -> String:
let s = String(self.data, self.len)
return s


@value
@register_passable("trivial")
struct AST:
var tag: UnsafeString
var content: UnsafeString
var child_no: Int
var children: Pointer[AST]


fn main():
print("AST Started")
var child1 = AST("Ident", "book", 0, 0)
How could I create child1 without any children? OK, I've done that now I could not implement add_child()
@value
@register_passable("trivial")
struct UnsafeString:
var data: Pointer[Int8]
var len: Int

fn __init__(str: StringLiteral) -> UnsafeString:
let l = str.__len__()
let s = String(str)
let p = Pointer[Int8].alloc(l)
for i in range(l):
p.store(i, s._buffer[i])
return UnsafeString(p, l)

fn to_string(self) -> String:
let s = String(self.data, self.len)
return s


struct AST:
var tag: UnsafeString
var content: UnsafeString
var child_no: Int
var children: Pointer[AST]

fn __init__(
inout self,
tag: UnsafeString = "ILLEGAL",
content: UnsafeString = "un",
child_no: Int = 0,
):
self.tag = tag
self.content = content
self.child_no = child_no
self.children = Pointer[AST].alloc(child_no * 2)

fn add_child(inout self, owned other: Self):
self.children.store(0, other)


fn main():
print("AST Started")
var child1 = AST()
var child2 = AST()
var parent = AST()
@value
@register_passable("trivial")
struct UnsafeString:
var data: Pointer[Int8]
var len: Int

fn __init__(str: StringLiteral) -> UnsafeString:
let l = str.__len__()
let s = String(str)
let p = Pointer[Int8].alloc(l)
for i in range(l):
p.store(i, s._buffer[i])
return UnsafeString(p, l)

fn to_string(self) -> String:
let s = String(self.data, self.len)
return s


struct AST:
var tag: UnsafeString
var content: UnsafeString
var child_no: Int
var children: Pointer[AST]

fn __init__(
inout self,
tag: UnsafeString = "ILLEGAL",
content: UnsafeString = "un",
child_no: Int = 0,
):
self.tag = tag
self.content = content
self.child_no = child_no
self.children = Pointer[AST].alloc(child_no * 2)

fn add_child(inout self, owned other: Self):
self.children.store(0, other)


fn main():
print("AST Started")
var child1 = AST()
var child2 = AST()
var parent = AST()
I'm getting this error:
candidate not viable: method argument #1 cannot bind generic !mlirtype to memory-only type 'AST'
candidate not viable: method argument #1 cannot bind generic !mlirtype to memory-only type 'AST'
I can not convert AST to value & register_passable, then I get an other error: no matching function in call to 'init': var child1 = AST() ~~~^~ candidate not viable: callee expects at least 1 argument, but 0 were specified
@value
@register_passable("trivial")
struct UnsafeString:
var data: Pointer[Int8]
var len: Int

fn __init__(str: StringLiteral) -> UnsafeString:
let l = str.__len__()
let s = String(str)
let p = Pointer[Int8].alloc(l)
for i in range(l):
p.store(i, s._buffer[i])
return UnsafeString(p, l)

fn to_string(self) -> String:
let s = String(self.data, self.len)
return s


@value
@register_passable("trivial")
struct AST:
var tag: UnsafeString
var content: UnsafeString
var child_no: Int
var children: Pointer[AST]

fn __init__(
inout self,
tag: UnsafeString = "ILLEGAL",
content: UnsafeString = "un",
child_no: Int = 0,
) -> AST:
self.tag = tag
self.content = content
self.child_no = child_no
self.children = Pointer[AST].alloc(child_no * 2)
return self

fn add_child(inout self, owned other: Self):
self.children.store(0, other)


fn main():
print("AST Started")
var child1 = AST()
var child2 = AST()
var parent = AST()
@value
@register_passable("trivial")
struct UnsafeString:
var data: Pointer[Int8]
var len: Int

fn __init__(str: StringLiteral) -> UnsafeString:
let l = str.__len__()
let s = String(str)
let p = Pointer[Int8].alloc(l)
for i in range(l):
p.store(i, s._buffer[i])
return UnsafeString(p, l)

fn to_string(self) -> String:
let s = String(self.data, self.len)
return s


@value
@register_passable("trivial")
struct AST:
var tag: UnsafeString
var content: UnsafeString
var child_no: Int
var children: Pointer[AST]

fn __init__(
inout self,
tag: UnsafeString = "ILLEGAL",
content: UnsafeString = "un",
child_no: Int = 0,
) -> AST:
self.tag = tag
self.content = content
self.child_no = child_no
self.children = Pointer[AST].alloc(child_no * 2)
return self

fn add_child(inout self, owned other: Self):
self.children.store(0, other)


fn main():
print("AST Started")
var child1 = AST()
var child2 = AST()
var parent = AST()
is there a way to use memory.unsafe.bitcast to solve this ?
Geo7
Geo7OP13mo ago
Again I changed it and now getting new error, I can not post it here (I reached the limit) I posted it on github discussion https://github.com/modularml/mojo/discussions/1194
GitHub
How to Implement Recursive data type? · modularml mojo · Discussion...
NOTICE: I've read this post #69 anyway I want to Implement this with current unsafe features I want to Implement abstract syntax tree in mojo, so far no luck, Something simple as this in C : ht...
Want results from more Discord servers?
Add your server