Files
metabuilder/mojo/compiler/tests/test_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

135 lines
3.1 KiB
Mojo

#!/usr/bin/env mojo
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2025, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
"""Test struct parsing (Phase 2)."""
from src.frontend.parser import Parser
fn test_simple_struct():
"""Test simple struct definition."""
print("Testing simple struct...")
let source = """
struct Point:
var x: Int
var y: Int
"""
var parser = Parser(source)
_ = parser.parse()
print("✓ Simple struct parsed successfully")
print()
fn test_struct_with_methods():
"""Test struct with methods."""
print("Testing struct with methods...")
let source = """
struct Rectangle:
var width: Int
var height: Int
fn area(self) -> Int:
return self.width * self.height
fn perimeter(self) -> Int:
return 2 * (self.width + self.height)
"""
var parser = Parser(source)
_ = parser.parse()
print("✓ Struct with methods parsed successfully")
print()
fn test_struct_with_init():
"""Test struct with __init__ method."""
print("Testing struct with __init__...")
let source = """
struct Vector:
var x: Float
var y: Float
var z: Float
fn __init__(inout self, x: Float, y: Float, z: Float):
self.x = x
self.y = y
self.z = z
fn magnitude(self) -> Float:
return sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
"""
var parser = Parser(source)
_ = parser.parse()
print("✓ Struct with __init__ parsed successfully")
print()
fn test_struct_with_default_values():
"""Test struct with default field values."""
print("Testing struct with default values...")
let source = """
struct Config:
var name: String = "default"
var count: Int = 0
var enabled: Bool = True
"""
var parser = Parser(source)
_ = parser.parse()
print("✓ Struct with default values parsed successfully")
print()
fn test_nested_struct_types():
"""Test struct with field types that are other structs."""
print("Testing nested struct types...")
let source = """
struct Inner:
var value: Int
struct Outer:
var inner: Inner
var count: Int
"""
var parser = Parser(source)
_ = parser.parse()
print("✓ Nested struct types parsed successfully")
print()
fn main():
print("=== Mojo Compiler Phase 2 - Struct Parsing Tests ===")
print()
test_simple_struct()
test_struct_with_methods()
test_struct_with_init()
test_struct_with_default_values()
test_nested_struct_types()
print("=== All struct parsing tests passed! ===")