README.md |
Omega
What?
Omega is a language intended for scripting purposes. Omega compiler omegac
reads .og
files which contain Omega-code (specified below), which will then be parsed into bytecode. The parsed bytecode then, unless otherwise stated (via a --no-output
compile-flag), will produce .ogc
files, which function as compiled Omega, which can then be run with the Omega-interpreter omega
. If stated, the omegac
compiler can contain the omega
interpreter with it aswell, and in such case, can run the parsed bytecode right away with a --run
-flag.
Why?
The original version of Omega was written in TypeScript for a NodeJS server for a dungeons and dragons system client. The new server is being written in Rust, so the language must be re-written. To make the process easier, here is the specifications (and technically the documentation) for the re-visited version of the language. To the same repository I will be creating the actual Rust implementation of this language too.
The original version of Omega can be viewed here
Who?
Omega is created by Teascade, original version being written with TypeScript in 2016, and new specification and Rust implementation written in 2017.
License?
Currently Omega has no license, since it is only a specification, but most likely it will be licensed under MIT later.
The Omega specification is simply CC-BY-SA:
Table of Contents
Table of contents for the Omega spec
Examples
Before any of the following examples, a print-function has been defined in the global scope. The print-function takes in a String-type as a parameter.
Example Hello World
print("Hello World!");
Example loop
let max = 15;
for (let i = 0; i < max; i++) {
print("i: " + i);
}
General Syntax
The general syntax of Omega is fairly similar to that of TypeScript or Rust. The syntax is a mix of keywords and expressions displayed such as in the examples.
Expressions
Expressions are a set of values, function calls and operators that Omega interprets and returns a new value out of.
For example 2 + 3
is an expression combined by a +
-operator which will result 5
Function calls
Function calls are also very similar to other languages. If there esists a function called function_one
, it can be called with function_one();
. If there exists a function called function_two
which requires two i32's as arguments, it can be called as function_two(5, 2);
where 5 and 2 are example integer values.
- Function calls must have parenthesises in the end of them to signify the call, and after the parenthesis there must be a semicolon
;
. - Any possible arguments must be given between the parenthesis separated by commas
,
.
Operators
Operators are a number of individual and combined symbols which together form meanings which are interpreted in a special way.
There a few types of operators, and all of these types are either unary of binary operators, although some of them are valid as both:
Unary and binary operators
The difference between unary and binary operators is that unary operators require one value and binary operators require two values.
Unary operators have such examples as:
!
NOT operator, converts!true
intofalse
etc.-
minus operator, negates the next value-5
etc.
Binary operators have such examples as:
&&
AND operator, checks weather both sides of the operator aretrue
.+
plus-operator, adds both sides of the operator together.
Logical operators
These are the operators ofthen called as "conditions" and most commonly used in if-statements and such.
&&
AND binary operator. Checks weather both sides of the operator aretrue
true && true
returns truefalse && true
return false
||
OR binary operator. Checks weather either side of the operator istrue
true || true
returns truetrue || false
return true
^
XOR binary operator. Checks weather only one side of the operator is true.true ^ true
return falsetrue ^ false
returns truefalse ^ false
returns false
==
Equals binary operator. Checks weather both sides of the operator are the same."not" == "test"
returns false3 == 3
returns true
!=
Not equals binary operator. Checks weather both sides of the operator are not the same."not" != "test"
returns true3 == 3
returns false
!
Not unary operator. Negates the value associated with it.!true
returns false!(true ^ true)
returns true
Arithmetic operators
Arithmetic operators are operators used to do math calculations such as addition or multiplication. any integer or float -based types can be used together.
+
Plus binary and unary operator. As a binary operator combines both sides of the operator, as an unary operator simply returns the associated value.+ 3
returns 32 + 3
returns 5
-
Minus binary and unary operator. As binary operator subtracts the latter side of the operator from the first, as an unary operator simply returns the negated associated value.- 3
returns -32 - 3
returns -1
*
Multiplication binary operator. Returns the value of the both sides multiplied.2 * 3
returns 65 * 5
returns 25
/
Division binary operator. Returns the division of the first value with the second.2.0 / 3
returns 0.666..6 / 2
returns 3
%
Modulo binary operator. Returns the remainder of the division between the two values.2 % 3
returns 26 % 2
returns 0
Assignment operators
Assignment operators are a special kind of operator used assign values to variables.
The most basic type of assignment operator being of course =
.
For example:
test_var = 3
sets the value of test_var to 3
=
can be combined with any of the binary arithmetic operators however to create other kinds of assignment operators which work like so:
test_var += 3
would be the same astest_var = test_var + 3
test_var *= 3
would be the same astest_var = test_var * 3
If you however try to use these kinds of assignment operators on variabes which have not been initialized with a value yet, an exception will occur.
Scopes
Scopes are areas of code surrounded by brackets {}
. E.g.
let variable = "text";
{
let this_is_scoped = "scoped_text";
this_is_scoped = variable + "!";
}
variable = this_is_scoped; // Exception! Cannot access inner-scope.
As is visible in the example, variables defined in the scope are no longer accessible outside the scope. Scopes exist in their individual "environments", where they can access the variables in their upper scopes, but not inner scopes.
Values
There are a number of values you can assign to your variables, as of Omega 1.0, only primitive values are possible. Such types are:
string
, a basic piece of text, defined as followes:"String here"
.char
, contains a single character, defined as follows:'c'
i8
(or usually short), a basic 8-bit integer value, such as3
or11
.i32
(or usually int), a basic 32-bit integer value, such as3
or11
.i64
(or usually long), a basic 64-bit integer value, such as3
or11
.f32
(or usually float), a basic 32-bit float value, such as1.5
or6.32
.f64
(or usually double), a basic 64-bit float value, such as1.5
or6.32
.boolean
, contains value oftrue
orfalse
. This also includes conditions.T?
is an optional type. Any type can be an optional type, but the optional type must be checked withvar?
-operator before it can be accessed viaunwrap
, or the execution will crash.T[]
is a primitive array-type. warning: this part is heavily work-in-progress- New arrays may be created as such
T[len]()
, where T is the type of the array, and len is the size of it. - For example:
i32[4]()
would create ani32
-array with 4 slots. - Slots in an array are accessible with the standard
array[i]
syntax.
- New arrays may be created as such
Values in Omega are strongly typed, meaning combining two different types cannot be combined, unless they are successfully cast.
Conditions
For the sake of glossary, conditions can simply be true
or false
, but in all cases where "conditions" are said, logical operators also apply.
Keywords
The following keywords are specified to execute an action.
let
initializes a new variable.if
enters the scope once if the condition is met.else
def
defines a new function.while
functions likeif
, but enters the scope as long as the condition is met.for
initializes a scope which will be ran a number of times specified after thefor
.break
continue
unwrap
some
exists
empty
as
- All operators are also somewhat considered as keywords.
let
Initializes a new variable, as such:
let uninitialized: string; // Initializes this variable as string, but does not give it a value.
let five = 5; // Sets five to 5
let var = "text"; // Sets var to "text"
let five = 6; // Causes an exception, cannot re-define five
five = 3; // (without let-keyword) Re-sets five to 3
- Initialization of new variable must contain
let
, but re-definition of an existing variable, cannot start withlet
. - The name of the variable being defined must follow the
let
after whitespace. - After the name of the variable, there may be a definition of the type of the variable, but it is not necassary. When re-defining a value of a variable, there cannot be a re-definition of the type.
- If there is no type-definition, an initializing value must be set.
- type-definition's form is as follows:
: T
, and it cannot be preceded by whitespace. between the colon and theT
there may be whitespace.
- After whitespace, there may be (or must be, if no type-definition is given), an equals
=
-sign, after which there must be more whitespace, after which the value of the variable is given. This is simply an assignment operator. - After the value of the variable, the
let
-expression must end in a semicolon;
.
if
Defines an if-statement, which will, if the condition is met, enter the scope defined after the if.
if true {
// Executed code
}
if "test" == "not true" {
// Not executed code
}
- begins with an
if
, after which there must be some whitespace. After the whitespace there must be a condition. - After the condition there must be some whitespace, after which there is the scope definition
else
Defines an else-statement, which must proceed after the if-statement's scope. if the if statement's condition was not met, else will be entered. An if-statement can be added immediately after the else
, to chain them up.
if false {
// Not executed code
} else {
// Executed code
}
if false {
// Not executed code
} else if "test1" == "test2" {
// Also not executed code
} else {
// Executed code
}
- The else keyword must follow immediately after the if-statement's body (as seen in the example). Only whitespace is allowed in the middle.
- After the else, there can be a new if-statement, without a body for the else itself.
- If there is no if-statement following the else, there must be a body for the else itself, which will then be executed if the preceding if-statement was not executed.
def
Defines a new function or method as follows:
def first_function() {
// Code
}
def second_function(param1: string) {
// Code
}
def third_function(param1: i32, param2: string) {
// Code
}
- The signature of the function/method must begin with
def
. - After
def
there must be a number of whitespace, after which the name of the defined function must follow. - Immediately after the name of the function, there must be an opening parenthesis
(
. - After the opening bracket there may be parameters listed.
- Format of the parameters follows the
let
format, without thelet
-keyword. - There must also be a type-definition.
- There cannot be any default values. (no assignment operators)
- The parameters are divided by a comma
,
, after which there may be any number of whitespace.
- Format of the parameters follows the
- After the list of parameters there must be a closing bracket
)
. - Between the parenthesis and the parameter-lists, there may be any number of whitespace.
- After the parenthesis and any number of whitespace, there must be the function body.
while
Defines a loop which will be as long as the condition defined after it is met.
while true {
// Runs infinitely.
}
while false {
// Never enters this loop.
}
while true == false {
// Also never enters this loop.
}
- To specify a while, the line must begin with a
while
. - After the
while
, there can be a number of whitespace, after which there must be aboolean
value or otherwise known as a condition. - After the value there must be a scope definition.
for
TODO: Decide weather to use C-style or Rust-style for-loops
Defines a loop very similar to while, but which parameters inside the parenthesis consists of three parts separated by semicolons;
.
for (let i = 0; i < 10; i++) {
// Loop through 0 to 9
}
- The first part (
let i = 0
in this example) is the beginning-expression. It can be any expression, and it will be executed as the loop begins weather or not the scope inside the loop will be accessed. - The second part (
i < 10
in this example) is the condition defining weather the loop-scope will be accessed or not. - The third part (
i++
in this example) is the step-expression, which will be executed after each execution of the loop-scope. - Another difference to while where parenthesis are not necessary, in
for
, te parenthesis around these three parts are necessary. - Otherwise
for
is identical towhile
break
Break is a simple keyword used to break a loop immediately.
while (true) {
break; // The loop only enters once, then leaves.
}
- The break must end with a semicolon
;
. - If there is no loop and break is called, an exception occurs.
continue
Continue is a simple keyword to skip the rest of the loop's body.
while (true) {
continue;
print("Hello!"); // This code is never reached.
}
- The continue must end with a semicolon
;
. - If there is no loop and continue is called, an exception occurs.
unwrap
Unwrap is a keyword used to unwrap an optional variable.
let optional: i32? = some_number as i32;
let number: i32 = unwrap optional;
- unwrap must be followed by an optional variable. If an un-optional variable is given to unwrap, an exception occurs.
- there must be a space between
unwrap
and the optional value.
some
Some is a keyword used to wrap a variable to create an optional variable.
let number: i32 = some_number;
let optional: i32? = some number;
- Some must be followed by a variable. The variable is then wrapped into an optional and the optional is returned.
- There must be a space between the
some
and the variable.
exists
Exists is a keyword that is used to check weather an optional variable contains a value or not.
let optional: i32? = something();
if (optional exists) {
// optional contains a value
} else {
// optional is empty.
}
let op_exists: boolean = optional exists;
- Unlike
unwrap
andsome
,exists
must follow the optional value. Exists-keyword then returns the result as a boolean. - There must be a space between the optional and
exists
- If
exists
is after a non-optional variable, an exeption occurs.
empty
Empty is the keyword used to set an optional as empty (to not contain a value).
let optional: i32? = empty;
if (optional exists) {
// Never executed
} else {
// Executed, since optional is empty
}
- Like
true
orfalse
, empty is used as a value, except it can only be used in an assignment operator - Trying to set a non-optional value as empty will cause an exception.
as
As is the keyword used when you need to cast a variable to another. It will return the casted result as an optional which will be empty if the cast failed.
let long: i64 = 5;
let int_opt: i32? = long as i32;
let int = 0;
if (int_opt exists) {
int = unwrap int_opt;
} else {
// Cast failed
}
- Before
as
there must be a variable, or a value, and after there must be the type which the value is attempted to be cast as.