Skip to content

Anthony4m/mars

Repository files navigation

Mars Programming Language

Go License Tests LeetCode

Mars is a modern, statically-typed programming language that compiles to Go. It combines the safety and performance of Go with a more expressive syntax and additional features.

Mars 1.0: MISSION ACCOMPLISHED

Mars 1.0 is a fully working programming language capable of solving real algorithmic challenges.

Major Achievements

  • Complete Language Implementation: All core features working
  • Algorithmic Problem Solving: Successfully solved 5+ LeetCode problems
  • Hard Problem Capability: Can handle LeetCode Hard problems
  • Production Ready: Language is fully working for practical use
  • Extensible Architecture: Ready for future enhancements

LeetCode Problems Solved

Easy/Medium Problems - ALL SOLVED

  • Two Sum - Array manipulation, nested loops, equality comparisons
  • Three Sum - Triple nested loops, array return types
  • Trapping Rain Water - Complex array logic, mutable variables, accumulator pattern
  • Binary Search - While loops, array indexing, logarithmic complexity

Hard Problems - MAJOR SUCCESS

  • Maximum Subarray (Kadane's Algorithm) - Dynamic programming, O(n) solution
  • Best Time to Buy and Sell Stock III - Greedy algorithms, multiple transactions
  • Median of Two Sorted Arrays - Two-pointer technique, O(log(min(m,n))) solution

Features

  • Static Typing: Strong type system with type inference
  • Immutability by Default: Variables are immutable unless explicitly marked as mutable
  • Memory Safety: Garbage collection with optional manual memory management
  • Go Interop: Seamless integration with Go code and ecosystem
  • Modern Syntax: Clean and expressive syntax with modern language features
  • Algorithmic Problem Solving: Full support for complex algorithms and data structures
  • Structs (beta): Struct declarations, struct literals, and field access

Quick Start

Installation

# Clone the repository
git clone https://github.com/yourusername/mars.git
cd mars

# Run Mars programs directly
go run cmd/mars/*.go run your_program.mars

Hello, World!

Create a file named hello.mars:

func main() {
    println("Hello, Mars!");
}

Run it:

go run cmd/mars/*.go run hello.mars

Algorithmic Problem Example

func two_sum(nums : []int, target : int) -> []int {
    for i := 0; i < len(nums); i = i + 1 {
        for j := i + 1; j < len(nums); j = j + 1 {
            if nums[i] + nums[j] == target {
                return [i, j];
            }
        }
    }
    return [-1, -1];
}

func main() {
    result := two_sum([2, 7, 11, 15], 9);
    println("Two Sum Result:");
    println(result);
}

Structs (beta)

struct Point {
    x: int;
    y: int;
}

func main() {
    p := Point{x: 1, y: 2};
    println(p.x); // 1
}

Notes:

  • Struct literals are parsed only in expression context (e.g., right-hand side of assignments, arguments).
  • The parser uses context-aware disambiguation (similar to Go): IDENT { IDENT : ... } → struct literal; if cond { → block.
  • Field access via obj.field works at runtime.
  • Current limitations: analyzer validation for struct fields/types is pending; field mutation (p.x = 3) is not yet implemented.

Documentation

Project Structure

mars/
├── lexer/          # Token definitions and lexical analysis
├── parser/         # Syntax analysis and AST construction
├── analyzer/       # Static analysis and type checking
├── evaluator/      # Runtime evaluation and execution
├── errors/         # Error handling and reporting
├── ast/            # Abstract Syntax Tree definitions
├── cmd/mars/       # Mars compiler and runtime
├── examples/       # Example programs and LeetCode solutions
└── docs/           # Documentation

Development

Prerequisites

  • Go 1.21 or later
  • Make (optional, for using Makefile)

Building

# Run Mars programs
go run cmd/mars/*.go run your_program.mars

# Run tests
go test ./...

# Run linter
go vet ./...

Contributing

We welcome contributions! Please see our Contributing Guide for detailed information on:

  • Development setup and environment
  • Coding standards and guidelines
  • Testing requirements
  • Contribution workflow
  • Current development priorities
  • How to report issues and request features

Quick start:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

For questions or help, please open an issue or start a discussion.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Community

Implementation Status

Fully Implemented (Mars 1.0)

Core Language Features:

  • Lexer: Complete token recognition for all language constructs
  • Parser: Full recursive descent implementation with error recovery
  • AST: Complete node definitions with position tracking
  • Error Reporting: Structured error system with user-friendly messages, context, and line/column info
  • Basic Types: int, float, string, bool, null
  • Variable Declarations: Type inference and explicit typing with := syntax
  • Mutable Variables: mut keyword for reassignable variables
  • Arithmetic Operations: +, -, *, /, %
  • Comparison Operations: ==, !=, <, >, <=, >=
  • Logical Operations: &&, ||, !
  • Control Flow: if/else, for loops, while loops, break/continue
  • Functions: Declaration, parameters, return types, automatic main() execution
  • Function Calls: User-defined function execution
  • Built-in Functions: len(), println(), print()
  • Block Statements: Nested scopes and variable shadowing
  • Type System: Advanced type checking with compatibility and aliases
  • Array Types: Fixed-size and dynamic arrays with full support
  • Array Indexing: Complete array[index] access support
  • Array Return Types: Functions can return arrays and nested arrays

Advanced Features:

  • Algorithmic Problem Solving: Full support for complex algorithms
  • Dynamic Programming: Kadane's algorithm and similar patterns
  • Greedy Algorithms: Stock trading and optimization problems
  • Array Processing: Complex array operations and manipulation
  • Error Handling: Comprehensive error reporting with user-friendly messages and debugging
  • Testing: Extensive test suite with real-world problem validation

What's Next (Mars 1.1+)

  • String Operations: Enhanced string manipulation and processing
  • Advanced Data Structures: Hash maps, trees, graphs, linked lists
  • Standard Library: More built-in functions and utility modules
  • Package System: Module imports and dependency management
  • Concurrency Support: Goroutines and channels
  • Build System Integration: Dependency management and compilation
  • IDE Support: Language server and development tooling
  • Performance Optimizations: Code optimization and compilation passes
  • Code Generation: Transpiler to generate Go code

Current Capabilities

Production Ready Features

  • Complete Syntax Support: All core language constructs working
  • Robust Type System: Advanced type checking and compatibility
  • Advanced Control Flow: Nested loops, complex conditions, variable scope
  • Algorithmic Problem Solving: Can solve LeetCode Hard problems
  • Error Handling & Debugging: Comprehensive error reporting with user-friendly messages
  • Real-World Applications: Ready for practical programming tasks

Algorithmic Problem Solving

Mars 1.0 has successfully solved:

  • Two Sum: Array manipulation and nested loops
  • Three Sum: Triple nested loops and array return types
  • Trapping Rain Water: Complex array logic and mutable variables
  • Binary Search: While loops, array indexing, logarithmic complexity
  • Maximum Subarray: Dynamic programming with Kadane's algorithm
  • Best Time to Buy and Sell Stock III: Greedy algorithms and optimization
  • Median of Two Sorted Arrays: Two-pointer technique, O(log(min(m,n))) solution

Status

Mars 1.0 is a fully working programming language with a solid foundation and extensive capabilities. The language can execute complex programs with variables, functions, control flow, arrays, and algorithmic problem solving. All core features are implemented and working, making Mars ready for practical use and further development.

Stability: 1.0

  • Syntax and semantics for 1.0 are frozen.
  • Bugfixes and security updates only on the 1.0-maint branch.
  • New features go through the 1.x proposals queue (see docs/proposals/).

Mars 1.0: MISSION ACCOMPLISHED

About

an interpreter written in golang

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages