Getting Started
Introduction
Welcome to the Comet docs! The Comet docs are a tutorial and specification for the syntax of Comet.
"What is Comet?"
Comet is an object orientated, compiled programming language designed to be the solution to many of the issues with other languages. It has a few core goals:
- Don't be C++, because C++ has terrible names for everything.
- Don't be Rust (or Zig), because Rust is ugly.
- Don't be Go, because Go is garbage collected.
- Don't be Python, because Python is slow.
- Don't be C, because C isn't object orientated.
- Don't be Java, because Java is FAR too verbose.
- Don't be JavaScript, because JavaScript isn't type safe.
Warning
Comet is still very, VERY early in development! Here be dragons!
Installation
Because of the beta state of Comet, there is no installer of installation script available at the moment. The only way to install is by building from source.
Building From Source
Note
Comet is not entirely supported on Windows yet. (due to argp)
To build comet, you first have to get the repo. Make sure you have Git and LLVM installed before anything else. Once you have git installed, type these commands in a terminal to clone the repository, build Comet, and install it:
git clone https://chookspace.com/comet/comet && cd comet && make && sudo make install
If you didn't see any errors... hooray! Comet is now installed!
cometc
cometc is the Comet compiler. It can take a few command line arguments: (the first argument is the Comet file you want to compile)
- -o or --output: the file to output to
- -v or --version: print version number and exit
Example usage:
cometc myFile.comet -O3 -l -o myFile.ll
"Hello, World!"
This is a basic "Hello, World!" program in Comet:
import io
func main() -> int {
io.print("Hello, World!")
}
Save this to a file called "helloWorld.comet". We can compile this program to an executable file with the following command:
cometc helloWorld.comet -o helloWorld.cvm
And then we can run our program on the Comet VM:
comet helloWorld.cvm
Variables
This is the syntax for defining a variable in Comet:
type name = value
In this example:
- "type" is the type of the variable. It can either be an inbuilt type or a struct. (See structs)
- "name" is the name of the variable. Variable names are in camelCase, can't start with numbers, and must be alphanumeric.
- "value" is what you want to set the variable to. This can be any expression. That means a variable can be set to a number, an instance of a struct, another variable, etc...
Built-in Types
Comet has a few built in types:
- small - An 8 bit integer. | e.g: 123
- int - A 32 bit integer. This is what you would use for most calculations. | e.g: 123456
- big - A 64 bit integer. | e.g: 123456789...
- float - A single precision floating point number. | e.g: 0.123456
- double - A double precision floating point number. | e.g: 0.123456789...
- bool - Either "true" or "false". | e.g: true
- void - Nothing. You can't make a variable be of type void. Use this when you want to make a function that doesn't return anything.
Variables can also be uninitialized, however this is bad practice and should only be used when needed. Uninitialized variables have no default value and thus contain garbage until you change them.
An example of creating an uninitialized variable:
int myNumber
Functions
Function definitions follow this syntax:
func functionName(type arg1, type arg2) -> returnType {
...
}
Calling a function follows the same syntax as many other languages:
functionName(123, true, 0.456)
And you can use the return value of the function in expressions:
int x = add(4, 5)
Inline Functions
An inline function is a function which's body is an expression. Inline functions are great for simple, repeated calculations you want to avoid repeating. The syntax for an inline function is as follows:
func foo(type arg1, type arg2) -> returnType => (expression)
As an example, the square operation could be represented as this inline function:
func square(int x) -> int => x * x
Loops
While-Loops
The syntax for a while-loop in Comet is:
while condition {
...
}
"condition" is any expression that evaluates to a boolean. The while loop will continue until the condition is false.
Example syntax:
while true {
io.print("To infinity and beyond!\n")
}
The above code will print "To infinity and beyond!" forever.
For-Loops
The syntax for a for-loop in Comet is:
for type varName in start .. end {
...
}
Where:
- "type" is the type of the iterator variable
- "varName" is the name of the iterator variable
- "start" is the number to start counting from
- "end" is the number to stop counting at
You can also include an optional step which will change how much the loop counts by. If the start is bigger than the end, you will need to include a step to make the loop count down.
Example syntax:
for int i in 10 .. 0 step -1 {
io.print("%d\n", i)
}
The above code outputs the numbers 10 to 1.