Skip to content

KarsDev/Clarity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clarity Programming Language

Overview

Clarity is a high-level, object-oriented programming language designed to prioritize simplicity and clarity. It features a concise syntax and supports dynamic typing, making it easy to read and write. Clarity is suitable for both beginners and experienced developers who want to create clean, maintainable code.

Table of Contents

  1. Installation
  2. Features
  3. Basic Syntax
  4. Native Libraries
  5. Examples

Installation

To install Clarity, follow these steps based on your operating system:

download Required Files:

Windows: Download install_windows.bat Mac: Download install_mac.sh Linux: Download install_linux.sh

Also, download clarity.jar

Prepare for Installation:

Ensure that install_windows.bat, install_mac.sh, or install_linux.sh, along with clarity.jar, are located in the same directory.

Run the Installer:

Windows: Double-click install_windows.bat to run the installer. Mac: Open a terminal, navigate to the directory containing install_mac.sh and clarity.jar, then run the bash file. Linux: Open a terminal, navigate to the directory containing install_linux.sh and clarity.jar, then the bash file.

Complete Installation:

The installer will place Clarity on your system %userpath%/Clarity and then remove the installer files. You can safely delete clarity.jar and the installer script if they are not automatically removed. (Java will automatically installed if version 8 or higher is not found)

Features

  • Object-Oriented: Support for classes, methods, and properties.
  • Concise Syntax: Minimalist syntax that enhances readability.
  • Dynamic Typing: Variables do not require explicit type declarations.
  • Native Libraries: Provides a standard library with essential functions for I/O operations, mathematical calculations, and error handling.
  • Flexible Main Entry Point: The program does not require a specific main function to run, similar to Python.

Basic Syntax

Variables

Variables are declared using the var keyword. They can be initialized at the time of declaration or assigned values later.

var x
x = 5

var y = 10

Functions

Functions are defined using the fn keyword. A function can return a value or nothing (void).

fn add(a, b) {
    return a + b
}

fn displayMessage() {
    native.println("Hello from Clarity!")
}

Classes

Classes in Clarity are defined using the class keyword and can have properties, a constructor, and methods.

class Person {
    var name
    var age

    constructor(name, age) {
        local.name = name
        local.age = age
    }

    fn greet() {
        native.println("My name is " + local.getName() + " and I'm " + local.getAge() + " years old")
    }

    fn getName() {
        return name
    }

    fn getAge() {
        return age
    }
}

Control Structures

Clarity supports common control structures like loops and conditionals.

For Loop:

// foreach
for i : [1, 2, 3] {
    native.println(i)
}

// for
for var i = 1, i <= 100, i = i + 1 {
    native.print("Iteration: " + i + "\n")
}

While Loop:

var i = 0
var finished = false
while !finished {
    native.println("Not finished")
    i = i + 1
    finished = i > 100
}

Select Statement:

var i = int(native.input("Write a number from 1 to 3: "))
select i {
    when 1 {
        native.println("You wrote 1")
        break
    }
    when 2 {
        native.println("You wrote 2")
        break
    }
    when 3 {
        native.println("You wrote 3")
        break
    }
    default {
        native.println(i + " is not a number between 1 and 3")
    }
}

If Statement:

var x = int(native.input("Write a number: ")) // asking for a number and converting it to integer
if x > 0 {
    native.println("Your number is greater than 0")
} else if x < 0 {
    native.println("Your number is less than 0")
} else {
    native.println("Your number is equal to 0")
}

Assert Statement:

var x = int(native.input("Write a number from 1 to 3: "))
assert x >= 1 && x <= 3 else "The number " + x + " is not between i and 3!"

Is Check:

class Inherited {
}

class Inheritor inherits Inherited {
}

var inheritor = new Inheritor()

assert inheritor is Inherited // does not give any exception since inheritor is indirectly Inherited

Native Libraries

Clarity includes a set of built-in libraries that provide essential functions for various operations. You can include these libraries using the include keyword.

For example:

include native system
include native math

Common Native Functions

    native.println(message): Prints a message with a newline.
    native.print(message): Prints a message without adding a newline.
    native.error.except(): Handles errors or exceptions.

Examples

Simple Program

A Clarity program does not require a specific main function to run. Here's a basic example:

native.println("Hello, World!")

Using a Class

var john = new Person("John", 16)
john.greet()

Looping Over a List

for i : [1, 2, 3] {
    native.println(i)
}

Static Properties and Methods

class Test {
    static const var name = "Clarity"

    static fn getName() {
        return Test.name
    }
}

for var i = 1, i <= 100, i = i++ {
    native.print(Test.getName() + " loves you " + i + " times\n")
}