-
int: 64 bit signed integer -
float: 64 bit signed float -
bool: 64 bit integer representing a bool (value of 1 for true, 0 for false) -
str: Represents a string of characters, you can calllen(str)to get the length, but strings are immutable under the hood, so be aware of that StructStructs are custom heap allocated data structures created by the user. If you create a struct Point, you can also do Point[], the same way you can with any other type- I touched on it above but all first class types can have n dimensional arrays declared like
int[][]for an integer matrix. Lengths are not necessary and the arrays behave like vectors in the strict sense, as they are contiguously allocated in (heap) memory, but can grow, and will be able to shrink in the future
-
Void: represents a function that does not return, stability and compilation are guaranteed when using it as a return type from a function, and for no other place -
Any: Will disable the typechecker when used, so it assumes you have manually checked your types, if they are wrong you will get a nasty error, while the following is allowed: functions may returnany, and takeanyas parameters, variables may assigned to any, and you may makeany[]'s it is highly unstable for all other use cases and should be avoided if at all possible - Functions: This language has 0, nada, nil functional features. Maybe they will be added later but for now treat all functions like C functions. Also DO NOT USE COLONS IN YOUR FUNCTION NAMES!!! THey will confuse the name mangler
To declare a variable, use:
let x = 9;
The type is automatically inferred. If you want to explicitly specify a type:
let x: str = "hello world";
Notice the semicolons!!!
Variable reassignment and compound operators are also supported
let x: float = 9.2;
x += 315.2;
You can use an if statement as follows
let x = true && !false;
if x {
println("it worked");
} else {
println("it failed");
}
Else is supported, but else-if chains are not.
You already saw a function call above but here is the general syntax for functions
fn add(a: int, b: int): int {
return a + b;
}
let x = add(9, 3);
All function parameters and return types (including void) must be stated explicitly, here is a void example
fn my_println(s: str): void {
println(s);
}
my_println("hi mom!!!");
You can see a list of builtin functions at the bottom
Currently only while loops are supported, but iteration loops (for) will be in the (very) distant future
let i = 0;
while i < 7 {
if i == 3{
continue;
}
if i == 6 {
break;
}
println(i);
}
will print 1, 2, 4, 5,
Arrays are also fully supported
let arr = [0, 1, 2, 3, 4];
arr = [9, 2];
let x = arr[0];
println(arr);
prints [9, 2]
Structs are also fully supported
struct Point{
x: float,
y: float
}
let origin = Point{x: 0.0, y: 0.0};
fn print_point(p: Point): void{
print("Point{x: ");
print(p.x);
print(", y: ");
print(p.y);
print("};");
}
You can bind methods to structs like this, lets redo that print_point
struct Point{
x: float,
y: float
}
for Point {
fn print_point(){// note the inferred return type of void
print("Point{x: ");
print(this.x);//notice this keyword
print(", y: ");
print(this.y);
print("};");
}
}
let origin = Point{x: 0.0, y: 0.0};
origin.print_point(); //outputs Point{x: 0.0000, y: 0.0000};
-
print(s: any): voidprints an output to the standard output -
println(s: any): voidprints an output to the standard output with a newline -
len(v: any): intthe signature takes an any but can only be called on a string or an array, will return the number of characters or elements respectively -
int(i: any): intreturns the integer value of a value an object if it is a string of "17" it will become 17 otherwise it will panic, and for floats it will round -
str(s: any): strreturns the string value of any convertible object, for booleans false => "false", true is the same, for an integer 7 => "7", and same with floats -
float(f: any): floatreturns a float from a convertible value, will turn "5.3" => 5.3, false => 0.0, true => 1.0, will turn 1 => 1.0 -
bool(b: any): boolwill turn "true" => true, same with false and will panic for any other string, will turn 1 => true, 0 => false and wil panic otherwise, and will round a float and use do the same
read_file(path: str): strreads all bytes from a file as a UTF-8 stringwrite_file(path: str, content: str)overwrites all concent in the file with the specified valueappend_file(path: str, content: str)appends content to the back of a file
ms_since_unix_epoch(): intreturns the number of milliseconds since 12:00 AM, Jan 1, 1970now(): intalias forms_since_unix_epoch()current_year(): intreturns the current yearcurrent_month(): intreturns 1 for january and 12 for decembercurrent_month_name(): strreturns the name of the current monthcurrent_day(): intreturns the current day of the monthcurrent_date(): Datereturns a struct containing year, month, day as ints, and has a .to_str methodsleep(ms: int)will pause all program execution for the specified amount of tme
exit(code: int)exits the current process with the provided exit codeabort()immediately terminates the current process with exit code-1panic(message: str)prints the message and then aborts the processget_pid(): intreturns the current process IDargv(): str[]returns the process argument vector as an array of stringsargc(): intreturns the number of command line argsos_name(): strreturns the operating system namecore_count(): intreturns the number of available CPU coresversion(): strreturns the toy lang version string (currently0.0.2)version_info(): strreturns the extended release tag (currentlyALPHA_0.02_GRAY)is_little_endian(): boolreturns true when running on a little-endian systemis_big_endian(): boolreturns true when running on a big-endian system
A brief note, if you are worried about security, this uses libcurl and will imbed a trusted public key as binary into your program. That is what the compiler does, security beyond that is your responsibility. If you are doing anything sensitive PLEASE use the ffi and do it in C. This language is not tested enough yet.
get_url(url: str): strwill curl the specified url and return a string of the resultsconfigure_http_server(port: int, timeout: int)will configure the global server, MUST BE CALLED BEFORE CONNECTION REQUESTED. Timeout is in ms.connection_requested(): boolwill return true when a connection is requested, false otherwiseHttpRequest{method: str, path: str, client_ip: str, body: str}Is used to represent an incoming http requestread_request(): HttpRequestOnce a connection has been requested you can use read_request to read it, and can treat it like any other structclose_client()Will close the client connection, but not the server.write_response(code: int, content_type: int, body: str)Will write a response back to the open client. Code is the http status code (200 = good, 500/503 = server error, 404 = page not found). Content_type is 1 = text/plain, 2 = text/html 3 = application/json 4 = application/javascript. Body is the text of the response
-
If you do not have the build system setup (mys2 - clang/llvm), rust on the correct toolchain, cmake, and ninja run the following.
python setup_build_system.py #will install build system
cargo run -- --repl # will get you a repl
cargo run -- PATH_TO_FILE # will compile a .toy file