Cookeylang Tutorial
This has been the tutorial we have all been waiting on. The creators of CookeyLang finally creating a tutorial about it! Unsurprisingly, this tutorial was built and compiled in CookeyLang.
Table of Contents
- Introduction
- About CookeyLang
- Get Started
- Usage on repl.it
- CookeyLang versioning
- Why did we start on 2?
- Basic Syntax
- comments
- motivation of
%
- motivation of
- if statements
- motivation of
el
- motivation of
- type casting
- future of type casting
- comments
- Variables
- Dynamic variables
- Assignment
- Constant variables
- Types
- Special Cast
- Dynamic variables
- Functions
- Builtin Functions
- Creating Functions
- Motivation of
ret
- Motivation of
- Logic
- Conditionals
- Motivation of
or
andand
- Motivation of
- If statements
- Loops
- Conditionals
- More
- CamelCase or snake_case?
main.clf
vsindex.clf
- How to contribute
Introduction
Before we start learning CookeyLang, one must first understand the motivations of this language.
About CookeyLang
CookeyLang, or Cookey was created as a simple language for beginners. Don't worry, we are still developing CookeyLang3! (@Codemonkey51, @RaidTheWeb, @Summit). The github source code can be found here.
Get Started
Coding in CookeyLang requires minimal setup. All you need is the node interpreter, and npm. To make sure these things, work, try doing something like (this is bash):
echo "printLine('Hello, World!');" > index.clf
npx cookeylang index.clf
Usage on repl.it
Using cookeylang on repl.it is more than easy! Create a bash repl, and add this to your main.sh
:
npx cookeylang index.clf
Create an index.clf
, and you are ready to code some Cookey!
CookeyLang Versioning
CookeyLang 1 was never a thing. CookeyLang 2 is the current version of CookeyLang. CookeyLang3 is still in the works and will incorporate many new features and will fix old concept errors and confusing aspects of the language. We will also aim to make hard things easy, and impossible things (http requests, among others), easy, or in the worst case, hard.
Why did we start on 2?
We actually do have a version one (no version 0) as the originally implementers wrote it in ohm, and there was no specification yet. Evidently, we could not support chaos, so the version was deprecated. Version one however, still inspires the many features we have today and in version 3.
Basic Syntax
Before we start coding things with CookeyLang, we must first understand CookeyLang.
Comments
Single line comments look like this:
%% Single Line comment
Multiline comments look like this:
%*
Multiple
lines
wow!
*%
Motivation for %
The %
operator is used only for modulous in most C-family languages. This is a waste of such a character. We adapted the %
character for comments because they can clearly state where a comment starts and ends.
if
statements
if
statements will be covered in more depth in logic
. For now, just know:
if (expression) {
} el if (expression if first was false) {
} el {
%% if all expressions were false
}
Seem familiar? This is because CookeyLang is a C-family langauge.
Motivation of el
The el
keyword was used because it has the same length as if
, making it easier to see the code.
Type casting
It may be apparant we can't do:
5 * "6";
In Cookey, we have type casting so we can make this work:
5 * @"number":"6";
We will cover more of this in variables
.
Future of type casting
Soon, we will make the casts get classes in CookeyLang 3:
5 * @Number:"6";
The motivation for this is so we can allow custom classes to be able to be casted as well.
Variables
Where more to store memory than in variables?
Dynamic variables
This is the most common type of a variable you will see.
var [variable name] = [value];
An example variable is:
var myVar = 5;
Assignment
To change variables, we have many types of operators.
Assignment
The most simple way would be:
var myVar = 5;
myVar = 10;
Do note assignment only works for variables that were already defined!
Assignment equals
We also have some handy assignment equals:
var myVar = 5;
myVar += 5; %% myVar is 10
myVar -= 5; %% myVar is 5
myVar *= 5; %% myVar is 25
myVar /= 5; %% myVar is 5
myVar ^= 5; %% myVar is 3125
The
^
operator is exponentation, like in math class.
Postfix
Finally, we have some postfix operators:
var myVar = 5;
myVar++; // myVar is 6
myVar--; // myVar is 5
There are no prefix operators, but postfix operators are known to act like prefix operators. Such undefined behavior will be fixed in CookeyLang3!
Constants
Variables can be constant too.
final PI = 3.141592653589793238462643383279502884197;
As you can test,
PI++
will throw an error!
Types
There are many types in cookey.
| Type | Description |
|:------:|:------------:|
| NaV
| equivalent to null
in C |
| true
, false
| booleans |
| 1
, -1
, 0.1
| numbers |
| ""
, ''
| strings. Do note that they can be multiline. |
You can find more descriptions of them in the official website
Special Cast
There is a special thing with casting types. In the development of the language, I wanted to make undefined variables throw errors, but still have an ability for users who knew the variable didn't exist to still use them. This was the NaV
cast.
printLine(@"NaV":undefinedVariable);
printLine(@"NaV":5);
It returns the value unless the value is not defined.
Functions
Functions allow us to recreate code and write less code.
Builtin Functions
There are many builtin functions in CookeyLang. We will explore the most important ones.
print("Hello"); %% print without newline
printLine("Hello"); %% print with newline
exit 1; %% Exits with a code.
exit
is not necessarily a function, but it is too important to be left out.
Creating Functions
You can create functions quite simply. The syntax looks like,
function [name]([arg1, [arg2, [... arg3]]]) {
%% code
}
An example function looks like:
function sum(a, b) {
ret a + b;
%% code here won't be executed!
}
printLine(sum(2, 4)); %% 6
Note that the
ret
is used to return code.
Motivation for ret
This was more of a choice from the el
of else
for consistency, as well as the fact that return
has always been a variable name we have most likely wanted to use at some point in coding.
Logic
What is a good programming language with logic?
Conditionals
We have two short-circuiting functions, the ubiquitous or
and and
.
printLine(true and true); %% true
printLine(false or true); %% false
Motivation of or
and and
This was an inspiration from ruby's or
and and
as the choice of &&
and ||
were arbitrary. We still do have the unary !
operator so one never has to add more spaces and words than necessary, as in this example:
var bool = true;
if (!bool and false) {
}
CookeyLang wants to promote good practices, but we don't want to make it a pain to do that.
If statements
If statements look something like:
if ([expression]) [{] [code] [}] [el] [{] [code] [}]
As can be seen, curly braces (
{
) can be omitted for shorter statements.
Valid if
statements include:
var statement = true;
if (statement) {
printLine("statement was true!");
}
var number = 5;
if (number < 5) printLine("Number is less than 5");
el {
if (number > 5) printLine("Number is greater than 5");
el printLine("Number is equal to 5");
}
The second example is to show exactly what
else if
(in other languages) really represent.
Loops
There are 4 kinds of loops. First, we have the common loops:
{
for (var i = 0; i < 10; i++) printLine(i);
}
{
var i = 0;
while((i++) < 10) printLine(i);
}
{
do {
printLine("I happen at least once!");
} while (false);
}
The syntax follows that of most standard languages. The less intuitive loop, the for
loop has the syntax of:
for ([initializer]; [condition]; [incrementer])
the [initializer]
initializes a variable, [condition]
is the end value, and [incremeneter]
is how much the loop should step. Of course, all the values are optional, and this would be completely valid:
for(;;) {
printLine("Only once!");
break; %% this is how you break out of a loop.
}
The
break
statement allows you to break out of loops.
Now, for the new loop only CookeyLang has!
forrep (i, 0, 11, 1) {
printLine(i);
}
The syntax looks like:
forrep ([variableName], [startValue (inclusive)], [endValue (exclusive)], [incrementer])
The loop has a few overloads as well, but if you are inclined to learning them, visit the website documentation.
Evidently, the choice of passing in an argument comes off as a very interesting choice for variables are rarely declared this way. Because of this, in CookeyLang 3, the
forrep
loop will require you to make sure the variable is defined beforehand.
More
Thank you for reading this tutorial! Evidently, I have not covered everything in this small but powerful language, and I never intended to. Here are some resources and practices to further your learning on this language.
CamelCase or snake_case?
As the language suggest, 'CookeyLang', we are a CamelCased language. This doesn't mean you can't code in snake_case, but we prefer if you were consistent with our standards.
- Constants should be capitalized (lol I don't even follow this)
- Classes should be Capitalized. (
MyClass
instead ofmyClass
) - Variable names should describe themselves. (
Joe
instead ofHuman
instead ofHomoSapien
) - Single line comments unless you need more lines.
main.clf
vs index.clf
When in developing this language, parts of our team were python coders. This meant they were accustomed to main.py
. However, this language was written in NodeJS, which uses index.js
as the starting point. So, which one should you use? I usually use index.clf
, but main.clf
is used sometimes too (like in the compilation of this tutorial). It is still under debate, and it may never be resolved. Use what comforts you the most, and if it helps, cookey.clf
might be the entry point instead.
How to contribute
Thank you for you endeavors! Feel free to fork the main repo here. If you are at any point confused, there is also a discord server so you can chat with the devs!
Conclusion
Thank you for reading this quick CookeyLang tutorial! As promised, the code was compiled in CookeyLang (!!) and the process was very easy :D
Hopefully after reading this tutorial you understand how main.clf
works (or at least partially lol)
Special thanks to the CookeyLang team who made CookeyLang the awesome language it is today!
Pings: @elipie @k9chelsea2
Well, when life gives you lemons...
...you create a whole new coding language.
wow CookeyLang3 already? I'm so old lol I was around when CookeyLang1 was released lol
You can beat Python with this code:
if (expression) {
} elf (expression if first was false) {
} el {
}
if you do it:
a) I'll laugh because it seems oddly humourous
b) It'll take fewer characters than Python
Type | Description |
---|---|
NaV | equivalent to null in C |
true , false | booleans |
1 , -1 , 0.1 | numbers |
"" , '' | strings. Do note that they can be multiline. |
| Type | Description |
|:------:|:------------:|
| `NaV` | equivalent to `null` in C |
| `true`, `false` | booleans |
| `1`, `-1`, `0.1` | numbers |
| `""`, `''` | strings. Do note that they can be multiline. |
I fixed your table now.
oh lmao @TheForArkLD
thanks rpel.it so mean @TheForArkLD
@Coder100 wait, you forgot to put newline before table.
hey @coder100 and anybody else I'm looking for a team to help out. Assembling one that is. https://join.slack.com/t/directory0/shared_invite/zt-j2tap4lu-PUgYnymFwnFZKA7cjY1YWg
You can join if you want, just make sure to let me know and we'll go from there! Thanks repl.it
@peternielsen112 sure, what's it for?
@Coder100 everything and nothing... web development, mainly but I think it would be a great way to deploy projects to the world. Repl.it is great too but there is more potential through GitHub... if that makes sense. We can still link into Repl using GitHub if you want, anything works. No requirements but I think it would be cool...
If you haven't used Slack before you might need an account but I think that won't be a problem... it's free.
The link does expire in a week tho so if anyone's interested... now or never :D
@peternielsen112 are you working for a company, or is this just for fun? Also why not make a team on Repl.it and then link to Github?
@RayhanADev I'm working for fun
and I can link GitHub to a repl team
@peternielsen112 ah. Just wondering.
@RayhanADev you want in? the link still works
@peternielsen112 mhhh I prefer to freelance my own projects, but if you have an idea I’d love to drop in every now and then!
@RayhanADev we're designing a webapp to sort of give coding advice and all that but I will keep it in mind. You could still join the slack if you want
I was pung
Yay a notification
WeirdChamp
POGCHAMP @tsunami21
cool
yey thanks @PDanielY
thanks! @adsarebbbad
how do you even make a programming language? @Coder100
Nice, but why on earth was this made in node.js though? I mean, it must be terribly slow...
@fuzzyastrocat lmao I was wondering the same thing
lol maybe @fuzzyastrocat
@Coder100 Not maybe, definitely lol