Skip to main content

A visual programming language that compiles to Python

Project description

Moldo

A visual programming language that compiles to Python, designed to make programming more accessible and intuitive.

Features

  • XML-like syntax for easy visual representation
  • Compiles to Python code
  • Support for Python function imports
  • Basic programming constructs (variables, functions, control flow)
  • Built-in support for Python data types (strings, numbers, lists, dictionaries)
  • Type safety and error handling
  • Block-based loops with nested content

Installation

pip install moldo

Quick Start

  1. Create a .moldo file with your code:
<mblock type="print">Hello, World!</mblock>

<mblock type="input">What's your name? </mblock>
<mblock type="variable">name = input()</mblock>
<mblock type="print">f"Nice to meet you, {name}!"</mblock>
  1. Compile and run:
moldo compile your_file.moldo -o output.py
python output.py

Loop Structures

Moldo supports two different loop syntax options:

1. Simple Loop Syntax (Legacy)

<mblock type="loop">i in range(5)</mblock>
    <mblock type="print">i</mblock>

2. Block-Based Loop Syntax (Recommended)

<mblock type="loop" condition="i in range(5)">
    <mblock type="print">f"Current value: {i}"</mblock>
    <mblock type="variable">squared = i * i</mblock>
    <mblock type="print">f"Squared: {squared}"</mblock>
</mblock>

Simple Counter Example (1 to 6)

<!-- A simple counter that counts from 1 to 6 -->
<mblock type="print">Counting from 1 to 6:</mblock>
<mblock type="loop" condition="count in range(1, 7)">
    <mblock type="print">f"Number: {count}"</mblock>
</mblock>

The block-based syntax (with the condition attribute) is recommended as it:

  • Properly supports nested content
  • Maintains clear scope boundaries
  • Provides better visual representation of the code structure
  • Results in more readable generated Python code

Examples

Counter Example

<mblock type="print">Simple Counter Example</mblock>

<!-- Counter initialization -->
<mblock type="variable">counter = 0</mblock>

<!-- Print header -->
<mblock type="print">Counting from 1 to 10:</mblock>

<!-- Counter for loop using block-based syntax -->
<mblock type="loop" condition="i in range(1, 11)">
    <mblock type="print">f"Count: {i}"</mblock>
    <mblock type="variable">counter += i</mblock>
</mblock>

<!-- Print final counter value -->
<mblock type="print">f"Sum of numbers 1-10: {counter}"</mblock>

Dictionary Operations Example

<!-- Create a dictionary -->
<mblock type="variable">student = {"name": "Alice", "age": 20, "grades": [85, 90, 95]}</mblock>

<!-- Access dictionary values -->
<mblock type="variable">name = student["name"]</mblock>
<mblock type="print">name</mblock>

<!-- Add or modify dictionary entries -->
<mblock type="variable">student["subject"] = "Computer Science"</mblock>

<!-- Print the entire dictionary -->
<mblock type="print">student</mblock>

<!-- Loop through dictionary keys and values -->
<mblock type="loop" condition="key in student">
    <mblock type="variable">value = student[key]</mblock>
    <mblock type="print">f"{key}: {value}"</mblock>
</mblock>

Calculator Example

<!-- Import the math functions module -->
<mblock type="import">examples/math_functions.py</mblock>

<!-- Get input from user -->
<mblock type="input">Enter first number: </mblock>
<mblock type="variable">num1 = float(input())</mblock>

<mblock type="input">Enter second number: </mblock>
<mblock type="variable">num2 = float(input())</mblock>

<!-- Perform calculations -->
<mblock type="print">Addition: </mblock>
<mblock type="call">add(num1, num2)</mblock>

<mblock type="print">Subtraction: </mblock>
<mblock type="call">subtract(num1, num2)</mblock>

<mblock type="print">Multiplication: </mblock>
<mblock type="call">multiply(num1, num2)</mblock>

<mblock type="print">Division: </mblock>
<mblock type="call">divide(num1, num2)</mblock>

Temperature Converter Example

<mblock type="import">examples/temperature_utils.py</mblock>

<mblock type="print">Welcome to Temperature Converter!</mblock>
<mblock type="print">1. Convert Celsius to Fahrenheit</mblock>
<mblock type="print">2. Convert Fahrenheit to Celsius</mblock>

<mblock type="input">Enter your choice (1 or 2): </mblock>
<mblock type="variable">choice = input()</mblock>

<mblock type="variable">
if choice == "1":
    print("Enter temperature in Celsius: ", end="")
    celsius = float(input())
    print(f"{celsius}°C is equal to ", end="")
    result = temperature_utils.celsius_to_fahrenheit(celsius)
    print(f"{result:.1f}°F")
elif choice == "2":
    print("Enter temperature in Fahrenheit: ", end="")
    fahrenheit = float(input())
    print(f"{fahrenheit}°F is equal to ", end="")
    result = temperature_utils.fahrenheit_to_celsius(fahrenheit)
    print(f"{result:.1f}°C")
else:
    print("Invalid choice! Please enter 1 or 2.")
</mblock>

Creating Custom Functions

To create functions that can be used in Moldo, use the @moldo_function decorator:

from moldo.decorators import moldo_function

@moldo_function(reference_name="add")
def add_numbers(a: float, b: float) -> float:
    """Add two numbers together."""
    return a + b

sample snippet

{
  "code": "<mblock type=\"print\">Hello, World!</mblock>",
  "python_modules": {

  }
}

Command Line Interface

The moldo command provides several options:

moldo compile <input_file> -o <output_file>  # Compile Moldo code to Python
moldo run <input_file>                       # Compile and run Moldo code
moldo --help                                 # Show help message

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

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

Author

  • Mutiibwa Grace Peter - GitHub

Acknowledgments

  • ANTLR4 for the parsing infrastructure
  • Python community for inspiration and tools

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moldo-0.3.1.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moldo-0.3.1-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file moldo-0.3.1.tar.gz.

File metadata

  • Download URL: moldo-0.3.1.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for moldo-0.3.1.tar.gz
Algorithm Hash digest
SHA256 1ae719d0cd3cec1117888af6b4a6604e52fc06fe68193f99167f6d1cc78a0d41
MD5 89567ff1e5b2e8ab42db695fc5793798
BLAKE2b-256 905da17461f59634ba8beda91396028496c31a2e985ea696a466ebbbcfdc5c16

See more details on using hashes here.

File details

Details for the file moldo-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: moldo-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for moldo-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dcd34ff0b23bcad3dcaa75072c4193310c73f76c97c9e5f71f4e8e9600c43903
MD5 bb582b08340d598dfcba33321ec58465
BLAKE2b-256 9d0dc57c1acafad051c95c0ce9ca952a1fe5b2fef5b67655b7fddaf619e2b843

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page