OmegaLang

From Esolang
Jump to navigation Jump to search

OmegaLang is designed by PSTF.

Language Overview

OmegaLang is an object-oriented programming language that uses non-extreme-esoteric syntax to programming. It is powerful, and easy to learn but hard to complete mastery its principle. The language most inspired by Python, Kitten4(Logo), LUA and Haskell.

Basic syntax concept

Variable assignment

Recessive type designation

var a = 5
var b = 10
var str = "Hello!"
var bool_var = true
var g = 1.0
var z = 3 + 4j

Explicit type designation

int c = 20
int d = 30
float e = 2.71828182845
string name = "PrySigneToFry"
comp z__ = 3 - 4j
bool FLAG = false
list 列表 = [1, 2, [3, 4], "Hello!"]
tuple 元组 = (1, 2, 3)
tuple Otra_tupla = (4, )
dict 部分間違いコード = {"NonError": 0, "ManualExit": -1}
int *p = 0x1B4 // 该变量指向0x1B4的内容

Variable type

Currently, there are these types:

void

The void type is an empty type that undefined. It is the most basic type when defining variables. It can be converted to a boolean value and return false, or an integer value and return zero.

bool

A boolean is a logical variable that has both TRUE and FALSE properties (either True or False). 0, 0.0000000000, 0j, empty strings, NUL characters(its ASCII is zero), empty lists, empty tuples, empty dictionaries, empty objects (meaning objects that only have __init__ methods, and the only way is an empty function), and blank variables will all return False. All other values return True. The initialized value is False.

Numeric Variable

int

In this programming language, there is no upper and lower bound for integer types. This is the default integer type. The short integer type is from -32768 to 32767. The medium integer type is from -2147483648 to 2147483647, just like the int type in C++. The long integer type is from -9223372036854775808 to 9223372036854775807. There are also longer integer types, which are equivalent to __int128 types in C++. The initialized value is 0.

float

Like integer types, decimal types have no upper and lower bounds. The upper and lower bounds of a single-precision decimal type are equal to the upper and lower bounds of medium integers, and there are 6 to 7 significant decimal places. The upper and lower limits of the double decimal type are equal to the upper and lower limits of the long integer type, and the effective decimal places have reached 13 to 14 digits. There are also even more bizarre types of long double-precision decimals, with upper and lower bounds equal to those of 128-bit long integer types, with 20 to 21 significant decimal places. The initialized value is 0.0 .

comp

A complex number consists of its real and imaginary parts, usually in the form m+nj. If you only want to express imaginary numbers, omit m+. If you want to express the imaginary number 1, just use j. The initialized value is 0+0j.

At the mathematical level, we specify that the square of an imaginary number (0+1J) is minus one (-1+0j), and similarly, the sqrt(x) function multiplies the absolute value of x by the imaginary number when x is negative.

What they all have in common

If you converts 0, 0.0000000000000000000000000000000000, 0.0, 0+0j, 0j to BOOLEAN, you will get a FALSE, otherwise you will get a TRUE. When you try to convert bool to int, float or comp, it will be:

  1. False to int: 0
  2. False to float: 0.0
  3. False to comp: 0+0j
  4. True to int: 1
  5. True to float: 1.0
  6. True to comp: 1+j

string

A string, as the name suggests, is made up of many characters. The string type support escaping characters, which are escaped by backslashes. In particular, lowercase n means new, lowercase t means horizontal tab, lowercase a means character 7 (BEL character), lowercase b means character 127 (DEL character), the number zero represents character 0 (NUL character), " means double-quote, ' means single-quote, variable wrap in a brace means output this variable(Only support by f string).

The string can quote by double quote, single quote, or even sextuple quote and triple quote for document string.

The string can only convert to int or float, such as, "24" to be 24, "53.2" to be 53.2, but "0x10" to be an error instead of 16.

  1. \n ---- newline
  2. \t ---- horizontal tabulation
  3. \b ---- backspace
  4. \a ---- alarm
  5. \0 ---- NULL
  6. \x[hhhh] ---- Hexadecimal character
  7. \[ddddd] ---- Decimal character
  8. {var} ---- output the variable
  9. \" ---- double quote
  10. \' ---- single quote
  11. \N[character name] ---- output the character named [character name]. For example, Cyrillic capital letter El outputs Л, Sinhala letter Kantaja Naasikyaya outputs ඞ, CJKV Unified Ideogram 0x3400 outputs 㐀, and so on.
  12. \v ---- vertical tabulation

list

A list is a type that can store several different pieces of data. When using lists, you can modify an element in a list with subscripts, just like Python does. A similar type to a list is an array, which can only store the same type of data.

Control-flow

Sequential structure

var a = input()
a = int(a)
print(a * 2)

Conditional judgment structure

var a = input()
a = int(a)
if a % 3 == 0:
    print("0")
else if a % 3 == 1:
    print("1")
else:
    print("2")

※ An if-elseif-else statement can have multiple else ifs, but only one else. else if can be omitted, and even else can be omitted.

Iterative loop structure

for(int i = 99; i >= 0; i--)
    print(i, end = '\n')

var alphabets = ['a', 'b', 'c', 'd']
for(alphabet in alphabets; ;increase)
    print(alphabet)

Conditional loop structure

i = 0
while(i <= 20):
    print(i)
    i++

j = 20
repeat:
    print(j)
    j--
until(j < 0)

Subroutines and label-jump

Normal function

def greet(string user):
    print(f"Hello, {user}!")

greet("MihaiEso") // Don't ask me why I'm doing this

def add(int x, int y) -> int:
    return x + y

var a = add(5, 3)
print(a) // Should output 8

Label

var a = input()
a = int(a)
if a == 0:
    goto nope
goto yes

:yes
print(1)
goto yes

:nope
print(0)

I don't recommend you to use this, it will break the order in which the program runs.

λ-expressions

a = ((int)(x, y)->return x+y)(5, 3)
print(a) // Should output 8

Error-handling

try:
    var a = 5 / 0
except Exception:
    print("Your program is not working properly.")
    /* In this example, Exception can be replaced with ZeroDivisionError. */
else:
    print(f"The result is: {a}")
finally:
    print("Done.")

Package

// Suppose there is a package that named "math_utils".
import math_utils
a = math_utils.add(1, 1)
print(a) // Should output 2
// Suppose there is a package that named "math_utils".
from math_utils import * // Also can replace * with the things you want included only.
a = add(1, 1)
print(a) // Should output 2

Object and class

The definition of class

// Define a class and create an object
class Person:
    var name: string
    var age: int

    __init__(string name, int age):
        this.name = name
        this.age = age

    def greet() -> string:
        return f"Hello, my name is {this.name} and I am {this.age} years old."

var person = Person("Alice", 30) // "Spawn" a person
print(person.greet())  // Output: Hello, my name is Alice and I am 30 years old.

Inheritance and polymorphism

Let's say we've defined the person class.

class Employee: Person:
    var employeeId: string
 
    __init__(string name, int age, string employeeId):
        super(name, age)
        this.employeeId = employeeId
 
    override def greet() -> string:
        return super.greet() + " My employee ID is " + this.employeeId + "."
        // Adding two strings is equivalent to joining the following string to the tail of the previous string.
 
var employee = Employee("Bob", 40, "E12345")
print(employee.greet())  // Output: Hello, my name is Bob and I am 40 years old. My employee ID is E12345.

Example programs

A+B Problem

var a = int(input("Input the first integer: "))
var b = int(input("Input the second integer: "))
print(f"Their sum is {a + b}.")

Golfed

var a=int(input("Input the first integer: ")), b=int(input("Input the second integer: "));print(f"Their sum is {a + b}.")
// 我不是说不可以这么干,但是这么搞会不规范。

Categories