Geo7
Geo7
MModular
Created by Geo7 on 10/30/2023 in #questions
How to Implement Recursive data type
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
6 replies
MModular
Created by Geo7 on 10/30/2023 in #questions
How to Implement Recursive data type
is there a way to use memory.unsafe.bitcast to solve this ?
6 replies
MModular
Created by Geo7 on 10/30/2023 in #questions
How to Implement Recursive data type
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()
6 replies
MModular
Created by Geo7 on 10/30/2023 in #questions
How to Implement Recursive data type
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'
6 replies
MModular
Created by Geo7 on 10/30/2023 in #questions
How to Implement Recursive data type
@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?
6 replies
MModular
Created by Geo7 on 10/18/2023 in #questions
How to store Struct(or string) into DynamicVector
Thanks a lot, silly mistake 🙂
7 replies
MModular
Created by Geo7 on 10/18/2023 in #questions
How to store Struct(or string) into DynamicVector
Thanks for the responses, I tried @NKspartan example and it is working, But when I tried to write 2 helper function it does not work anymore what I've done wrong?
struct Person:
var id: Int
var name: String

fn __init__(inout self, i: Int, n: String):
self.id = i
self.name = n

fn __copyinit__(inout self, borrowed other: Self):
self.id = other.id
self.name = other.id


fn append(inout v: DynamicVector[Pointer[Person]], owned p: Person):
let _tmp = __get_lvalue_as_address(p)
v.push_back(_tmp)


fn get(inout v: DynamicVector[Pointer[Person]], index: Int) -> Person:
let _tmp = __get_address_as_lvalue(v[index].address)
return _tmp


fn main():
var v = DynamicVector[Pointer[Person]]()
let p1 = Person(1, "George")
let p2 = Person(2, "Rose")
append(v, p1)
append(v, p2)
let p = get(v, 0)
print(p.name) # It is printing id instead of name !!!
struct Person:
var id: Int
var name: String

fn __init__(inout self, i: Int, n: String):
self.id = i
self.name = n

fn __copyinit__(inout self, borrowed other: Self):
self.id = other.id
self.name = other.id


fn append(inout v: DynamicVector[Pointer[Person]], owned p: Person):
let _tmp = __get_lvalue_as_address(p)
v.push_back(_tmp)


fn get(inout v: DynamicVector[Pointer[Person]], index: Int) -> Person:
let _tmp = __get_address_as_lvalue(v[index].address)
return _tmp


fn main():
var v = DynamicVector[Pointer[Person]]()
let p1 = Person(1, "George")
let p2 = Person(2, "Rose")
append(v, p1)
append(v, p2)
let p = get(v, 0)
print(p.name) # It is printing id instead of name !!!
7 replies