Geo7
Geo7
MModular
Created by Geo7 on 10/30/2023 in #questions
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;
}
6 replies
MModular
Created by Geo7 on 10/18/2023 in #questions
How to store Struct(or string) into DynamicVector
I want to store struct into DynamicVector at first I tried
from utils.vector import DynamicVector


struct Person:
var first: String
var last: String
var id: Int

fn __init__(inout self, n: String, l: String, id: Int):
self.first = n
self.last = l
self.id = id


fn main() raises:
var x = DynamicVector[Person]()
let me = Person("A", "B", 1)
x.push_back(me)
from utils.vector import DynamicVector


struct Person:
var first: String
var last: String
var id: Int

fn __init__(inout self, n: String, l: String, id: Int):
self.first = n
self.last = l
self.id = id


fn main() raises:
var x = DynamicVector[Person]()
let me = Person("A", "B", 1)
x.push_back(me)
then I tried to change String Pointer[UInt8] now I do not know how to load string into Pointer[UInt8]
from utils.vector import DynamicVector

alias PointerString = Pointer[UInt8]
alias PointerStrings = Pointer[PointerString]
alias BufferPtrType = DTypePointer[DType.uint8]


@value
@register_passable("trivial")
struct Person:
var id: Int
var first: PointerString
var last: PointerString

fn __init__(inout self, id: Int, f: PointerString, l: PointerString) -> Self:
self.id = id
self.first = f
self.last = l
return self


fn main() raises:
var x = DynamicVector[Person]()
let first = PointerString.alloc(100)
first.store(0, 12)
let last = PointerString.alloc(100)
last.store(0, 17)
let id = 1
let me = Person(id, first, last)
x.push_back(me)
from utils.vector import DynamicVector

alias PointerString = Pointer[UInt8]
alias PointerStrings = Pointer[PointerString]
alias BufferPtrType = DTypePointer[DType.uint8]


@value
@register_passable("trivial")
struct Person:
var id: Int
var first: PointerString
var last: PointerString

fn __init__(inout self, id: Int, f: PointerString, l: PointerString) -> Self:
self.id = id
self.first = f
self.last = l
return self


fn main() raises:
var x = DynamicVector[Person]()
let first = PointerString.alloc(100)
first.store(0, 12)
let last = PointerString.alloc(100)
last.store(0, 17)
let id = 1
let me = Person(id, first, last)
x.push_back(me)
7 replies