Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Last active July 29, 2019 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ratstail91/f0095f80aba023e3c2776ddf1be38236 to your computer and use it in GitHub Desktop.
Save Ratstail91/f0095f80aba023e3c2776ddf1be38236 to your computer and use it in GitHub Desktop.
This file is a rough sketch of the toy language - a lot has changed, and the most recent version can be found here: https://github.com/Ratstail91/Toy
//TODO: const
//NOTE: No variable hoisting!
// oneline comment
/* multiline comment */
import "standard"; //imports built-in libraries, or .toy files
import "external.toy";
print("hello world\n"); //print is a keyword, rather than a function - useful for debugging
//seven types of data - null, booleans, strings, numbers, arrays, dictionaries and promises
var n; //undefined variables are "null"
var a = true;
var b = false;
var c = "Hello world"; //only allow double-quotes for strings
var d = 42; //numbers can optionally have trailing decimal places...
var e = [a, b, c, d, "hello world", 42.0]; //mixed types allowed in arrays and dictionaries
var f = { //dictionaries are key-value pairs, essentially JSON; keys must be unique in each dictionary
"hello": "world" //interoperability with JSON files is a must
};
//operators include: = == < <= > >= . ! != += -= ++ -- * / ?:
//inclusion of === is a possibility
//TODO: truthiness and comparison between types
print(f["hello"] == f.hello); //the '.' operator only works for string keys
if (!b) {
//conditional
} else {
//otherwise
}
var sentinel = 0;
while (sentinel < 10) { //braces are optional for if, else, while, for where there is only one statement
sentinel++;
}
for (var i = 0; i < 10; i++) {
//DO STUFF
}
foreach (var element in e) { //array iteration
print(e[0] == element);
}
foreach (var element in f) { //value iteration
print(element); //value in f
}
foreach (var element in c) { //string iteration
print(element); //will spell out "Hello world" one letter at a time
}
foreach (var index of e) { //"of" is used for index/key counting; works for strings, arrays and dictionaries
print(index); // iterates along the indexes
}
//functions can be defined in several ways
var g = function(arguments) {
//
}
var h = (arguments) => {
//
}
var i = x => x; //parenthesis and braces are optional, when there is only one argument or one expression respectfully
//closures are explicitly supported
var createCounter = function() {
var count = 0;
return () => ++count;
}
//an incorrect number of arguments is a runtime error, but the 'rest' parameter is allowed, creating an array
var j = function(...args) {
print(args[0])
}
var { k } = f; //destructuring
var l = async () => { //async/await works as expected
await l(); //don't do this; it's an infinite loop/stack overflow
}
all(l(), l(), l()); //"all" is a keyword that takes promises and returns a promise
l()
.then(x => x)
.then(x => x)
//there is no catch clause, since there are no exceptions being thrown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment