Hammad Ali
Hammad Ali
MModular
Created by Hammad Ali on 11/10/2024 in #questions
Struct containing a list of itself?
Hi, I'm trying to creating a node-like tree which can contain a child (or children), but the mojo compiler doesn't really let me do that:
@value
struct Node:
var value: Int
var children: List[Node] # A list of Node objects

fn __init__(inout self, value: Int):
self.value = value
self.children = List[Node]() # Initialize an empty list

fn add_child(inout self, child: Node):
self.children.append(child) # Add a child node to the list

fn main():
var root = Node(1)
var child1 = Node(2)
var child2 = Node(3)

root.add_child(child1)
root.add_child(child2)

print(root.value) # Output: 1
@value
struct Node:
var value: Int
var children: List[Node] # A list of Node objects

fn __init__(inout self, value: Int):
self.value = value
self.children = List[Node]() # Initialize an empty list

fn add_child(inout self, child: Node):
self.children.append(child) # Add a child node to the list

fn main():
var root = Node(1)
var child1 = Node(2)
var child2 = Node(3)

root.add_child(child1)
root.add_child(child2)

print(root.value) # Output: 1
error: function has recursive call to 'always_inline' function struct Node: ^ Mojo/USL/shaderlab/libutils/utils_methods.mojo:87:8: note: through call here /Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/stdlib/collections/list.mojo:186:8: note: to function marked 'always_inline' here /Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/stdlib/memory/unsafe_pointer.mojo:413:9: note: call here recurses Mojo/USL/shaderlab/libutils/utils_methods.mojo:87:8: note: back to function here struct Node: ^ mojo: error: failed to run the pass manager
5 replies
MModular
Created by Hammad Ali on 9/14/2024 in #community-showcase
USL: A Shader transpiler written in mojo
No description
1 replies
MModular
Created by Hammad Ali on 9/8/2024 in #questions
always_inline and struct problem
I'm trying to create a transpiler and I've created an architecutre in which every parsed token of the AST is repersented as ShaderOperation
struct ShaderOperation(PPrintable, CollectionElement):
var tokens: List[Token]
var name: String
var arguments: List[ShaderOperation]
var type: String

fn __init__(inout self, tokens: List[Token], name: String, arguments: List[ShaderOperation], type: String):
self.tokens = tokens
self.name = name
self.arguments = arguments
self.type = type


fn getName(inout self) -> String:
return self.name

fn setName(inout self, name:String) :
self.name = name


fn repr(inout self) -> String:
return 'ShaderOperation$(name=' + self.name + ', type=' + self.type + ')'


fn __copyinit__(inout self, existing: Self):
self.arguments = existing.arguments
self.type = existing.type
self.name = existing.name
self.tokens = existing.tokens


fn __moveinit__(inout self, owned existing: Self):
self.arguments = existing.arguments
self.type = existing.type
self.name = existing.name
self.tokens = existing.tokens
struct ShaderOperation(PPrintable, CollectionElement):
var tokens: List[Token]
var name: String
var arguments: List[ShaderOperation]
var type: String

fn __init__(inout self, tokens: List[Token], name: String, arguments: List[ShaderOperation], type: String):
self.tokens = tokens
self.name = name
self.arguments = arguments
self.type = type


fn getName(inout self) -> String:
return self.name

fn setName(inout self, name:String) :
self.name = name


fn repr(inout self) -> String:
return 'ShaderOperation$(name=' + self.name + ', type=' + self.type + ')'


fn __copyinit__(inout self, existing: Self):
self.arguments = existing.arguments
self.type = existing.type
self.name = existing.name
self.tokens = existing.tokens


fn __moveinit__(inout self, owned existing: Self):
self.arguments = existing.arguments
self.type = existing.type
self.name = existing.name
self.tokens = existing.tokens
But I get the error:
/Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/stdlib/collections/list.mojo:186:8: error: function has recursive call to 'always_inline' function
/Users/hammad/Documents/htdocs/Hammad-Subhtdocs/Mojo/USL/shaderlab/libutils/Typing.mojo:93:8: note: to function marked 'always_inline' here
struct ShaderOperation(PPrintable, CollectionElement):
/Users/ec2-user/actions-runner/_work/modular/modular/Kernels/mojo/stdlib/collections/list.mojo:186:8: error: function has recursive call to 'always_inline' function
/Users/hammad/Documents/htdocs/Hammad-Subhtdocs/Mojo/USL/shaderlab/libutils/Typing.mojo:93:8: note: to function marked 'always_inline' here
struct ShaderOperation(PPrintable, CollectionElement):
I need the arguments property to be a list of ShaderOperations because AST nodes are usually nested inside each other
7 replies
MModular
Created by Hammad Ali on 8/6/2024 in #questions
Error while importing local python file
Hi I was doing some experiements with python and mojo but when I tried to import the local file I got the following error:
Unhandled exception caught during execution: No module named 'python_curl'
mojo: error: execution exited with a non-zero result: 1
Unhandled exception caught during execution: No module named 'python_curl'
mojo: error: execution exited with a non-zero result: 1
Here is the code:
from python import Python

fn main() raises:
Python.add_to_path(".")
var module = Python.import_module("python_curl.py")
from python import Python

fn main() raises:
Python.add_to_path(".")
var module = Python.import_module("python_curl.py")
Python file (python_curl.py)
import pycurl
from io import BytesIO

def curl_request():
# Create a buffer to store the response
buffer = BytesIO()

# Create a cURL object
c = pycurl.Curl()

# Set the URL to fetch
c.setopt(c.URL, 'https://httpbin.org/')

# Write the response data to the buffer
c.setopt(c.WRITEDATA, buffer)

# Perform the request
c.perform()

# Close the cURL object
c.close()

# Get the response content
response_body = buffer.getvalue().decode('utf-8')

# Print the response
return response_body
import pycurl
from io import BytesIO

def curl_request():
# Create a buffer to store the response
buffer = BytesIO()

# Create a cURL object
c = pycurl.Curl()

# Set the URL to fetch
c.setopt(c.URL, 'https://httpbin.org/')

# Write the response data to the buffer
c.setopt(c.WRITEDATA, buffer)

# Perform the request
c.perform()

# Close the cURL object
c.close()

# Get the response content
response_body = buffer.getvalue().decode('utf-8')

# Print the response
return response_body
6 replies
MModular
Created by Hammad Ali on 4/5/2024 in #questions
Less Ugly way to concatenate strings
I have been working on a plotting library in mojo that is based on gnuplot. The code works but the string parts are extremely ugly:
...
fn plotArray(inout self, *data: Float32, lineType:Int32=0, lineColor:Int32=7, title:String="Plot") raises:
with open("m1v1tf01_temp.dat", "w") as f:
var string: String = ""
for dat in data:
string += FloatToString(dat) + ",\n"
f.write(string)
self.gnuplot_gateway.callGNUPlotCommand("plot \"m1v1tf01_temp.dat\" title " + title + " lt " + IntToString(lineType) + " lc " + IntToString(lineColor)) # <<< Ugly strings
...
...
fn plotArray(inout self, *data: Float32, lineType:Int32=0, lineColor:Int32=7, title:String="Plot") raises:
with open("m1v1tf01_temp.dat", "w") as f:
var string: String = ""
for dat in data:
string += FloatToString(dat) + ",\n"
f.write(string)
self.gnuplot_gateway.callGNUPlotCommand("plot \"m1v1tf01_temp.dat\" title " + title + " lt " + IntToString(lineType) + " lc " + IntToString(lineColor)) # <<< Ugly strings
...
So does anyone know a good way to concatenate strings? Does mojo have f-strings yet?
5 replies
MModular
Created by Hammad Ali on 3/10/2024 in #questions
unable to load package '/Users/hammad/.modular/pkg/packages.modular.com_mojo/lib/mojo/stdlib.mojopkg
I just started working on an educational physics engine when suddenly mojo interpreter got angry at me for no reason and started throwing this error:
unable to load package '/Users/hammad/.modular/pkg/packages.modular.com_mojo/lib/mojo/stdlib.mojopkg'mojo
unable to load package '/Users/hammad/.modular/pkg/packages.modular.com_mojo/lib/mojo/stdlib.mojopkg'mojo
Code:
struct System:
elasticPotentialEnergy = 0.0
kineticEnergy = 0.0
gravitaionalPotentialEnergy = 0.0
...
struct System:
elasticPotentialEnergy = 0.0
kineticEnergy = 0.0
gravitaionalPotentialEnergy = 0.0
...
What's wrong?
6 replies
MModular
Created by Hammad Ali on 2/23/2024 in #questions
Will Mojo Support constants after "let" is gone?
Given that mojo is removing the let keyword, will Mojo no longer have constants? Or will there be some other way to declare them?
2 replies
MModular
Created by Hammad Ali on 2/9/2024 in #questions
Will Mojo ever become Open Source?
Mojo is a great language and has amazing potential but the fact that it is NOT open source like it's predecessor Python, is not cool. So will mojo become open source later on in it's development or will it remain proprietary and closed-source forever?
16 replies