Files
metabuilder/mojo/compiler/examples/structs.mojo
johndoe6345789 83f1533bce feat(mojo): integrate Modular Mojo compiler implementation
Extracted from modular repo and reorganized:

Compiler Implementation:
- 21 compiler source files (frontend, semantic, IR, codegen, runtime)
- 15 comprehensive test files (lexer, parser, type checker, backend, etc.)
- 9 compiler usage example programs

Architecture (5 phases):
- Frontend: Lexer, parser, AST generation (lexer.mojo, parser.mojo, ast.mojo)
- Semantic: Type system, checking, symbol resolution (3 files)
- IR: MLIR code generation (mlir_gen.mojo, mojo_dialect.mojo)
- Codegen: LLVM backend, optimization passes (llvm_backend.mojo, optimizer.mojo)
- Runtime: Memory mgmt, reflection, async support (3 files)

File Organization:
- mojo/compiler/src/: Compiler implementation (21 files, 952K)
- mojo/compiler/tests/: Test suite (15 files)
- mojo/compiler/examples/: Usage examples (9 files)
- mojo/samples/: Mojo language examples (37 files, moved from examples/)

Documentation:
- mojo/CLAUDE.md: Project-level guide
- mojo/compiler/CLAUDE.md: Detailed architecture documentation
- mojo/compiler/README.md: Quick start guide
- mojo/samples/README.md: Example programs guide

Status:
- Compiler architecture complete (Phase 4)
- Full test coverage included
- Ready for continued development and integration

Files tracked:
- 45 new compiler files (21 src + 15 tests + 9 examples)
- 1 moved existing directory (examples → samples)
- 3 documentation files created
- 1 root CLAUDE.md updated

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-01-23 19:05:44 +00:00

57 lines
1.5 KiB
Mojo

#!/usr/bin/env mojo
# Example: Struct definitions and methods
struct Point:
"""A 2D point with x and y coordinates."""
var x: Int
var y: Int
fn __init__(inout self, x: Int, y: Int):
"""Initialize a point with coordinates."""
self.x = x
self.y = y
fn distance_from_origin(self) -> Float:
"""Calculate distance from origin."""
return sqrt(Float(self.x * self.x + self.y * self.y))
fn move(inout self, dx: Int, dy: Int):
"""Move the point by dx, dy."""
self.x = self.x + dx
self.y = self.y + dy
struct Rectangle:
"""A rectangle defined by width and height."""
var width: Int
var height: Int
fn __init__(inout self, width: Int, height: Int):
"""Initialize a rectangle."""
self.width = width
self.height = height
fn area(self) -> Int:
"""Calculate the area."""
return self.width * self.height
fn perimeter(self) -> Int:
"""Calculate the perimeter."""
return 2 * (self.width + self.height)
fn is_square(self) -> Bool:
"""Check if the rectangle is a square."""
return self.width == self.height
fn main():
var p = Point(3, 4)
print("Point:", p.x, p.y)
print("Distance from origin:", p.distance_from_origin())
p.move(1, 1)
print("After moving:", p.x, p.y)
let rect = Rectangle(10, 5)
print("Rectangle area:", rect.area())
print("Rectangle perimeter:", rect.perimeter())
print("Is square:", rect.is_square())