Author Topic: Lolcode Tutorial  (Read 11916 times)

0 Members and 1 Guest are viewing this topic.

Offline miotatsu

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 332
  • Rating: +11/-1
    • View Profile
Lolcode Tutorial
« on: April 27, 2010, 09:24:35 pm »
I started writing a tutorial for lolcode, the tutorial will be geared towards people with little to no programming experience
it is being cross posted from http://s1.zetaboards.com/mionet/topic/3249388/1/?x=0#post246898
the tutorial is still a work-in-progress

What is it?
Lolcode is an esoteric programming language.
It is a simple language that at one time had a decent sized community.
That community is now pretty much dead and the language is no longer improved.
This is sad because the language has so much potential, but it makes for a great language to learn to program with.

So how do I do it?
You will need either an interpreter or compiler that is compliant with the 1.2 spec

Hold-up, What?
An interpreter is a program that reads code that you wrote and translates it into an action that the computer can perform during run-time.
A Compiler is a program that does all that converting and then packages it into a standalone file so that you can run your program without needing to have an interpreter, it is also a lot faster because your program does not have to be "translated" as it is running.

Okay so where do I get one, and what was that about 1.2 something or other?
I have had good luck with Interpreters, you can find a good one here:
http://www.microngamestudios.com/lolexec.html

And the 1.2 thing?
A specification (spec) is just a standardized definition of what the language should include and information about it. lolcode has gone through 1 major spec and is on the second minor revision, there is also an incomplete 1.3 spec but I recommend you stick with 1.2, incomplete = naughty naughty you dirty boy, stay away from there. ;O

What is the point of having a spec?
You need rules to play The Game. ;O
Different interpreters and compilers will "translate" your code in different ways. If you make sure your interpreter of compiler is 1.2 compliant that means that if you type in code that is compliant with the 1.2 spec your program will run just fine. You don't want icky bugs in your program because of a non-standard compiler or interpreter!

Oh Oh I see :O but where do I find the 1.2 spec?
It can be found here:
http://lolcode.com/specs/1.2

Okay now I have all the stuff i need, i opened the spec in a tab of a good browser like Firefox, bookmarked it, and downloaded that interpreter, so how do i start programming?
Lets start by making an Example program:
Code: [Select]
HAI 1.2
I HAS A NAME
VISIBLE "NAME::"!
GIMMEH NAME
VISIBLE "HERRO " NAME "!"
KTHXBYE


What?
lets start by setting up that interpreter, go to the zip file and find the executable file inside, copy it wherever, I put mine on the Desktop
next we will want to associate lolcode files with the interpreter, this just means that we can double click the source code file and it will automatically run it, heres how:
copy that example program i made up there into a blank text file with a text editor like Notepad.
Save the file as <name>.lol (.lol is the standard file extension for lolcode programs)
and set the file type to "All files"
Right click the newly created file and choose properties
Under the General tab you will see the option
Opens with: blahblahblah [change...]
click the change button and then the browse button in the next window, now just find the interpreter wherever you saved it.
Click Apply and then OK.

Okay I did that but whats that program you made do?
Lets start by double clicking it and we can see for ourselves :{D
A Console window should open with the text "NAME:" on the screen
enter your name and press enter
it should return:
"HERRO (the name you entered here)!"
"Press ENTER to continue..." (this line may vary between compilers and interpreters)
If it worked good, if not make sure you set everything up correctly, if you did and it still does not work try contacting me under the CHAT page, I am usually on. (and if i am not you can always leave a message on the forums!)

Thats cool! but how did you do it?
Lets take a look at that spec.
Formating>file creation
It explains that all code is put between HAI and KTHXBYE commands, these define the start and end of a program.
also note that the 1.2 is ignored, you put it in there to signify that your code is 1.2 compliant.
Next up is
I HAS A NAME
Whenever you want to store a value you must first declare the Variable you want to store it to, I created a variable called NAME, we will store the users name in it.
you could now store something to it by saying
NAME R (number or string here)
you can also give a value to a variable as soon as you declare it, this is done with ITZ
I HAS A NAME ITZ (value here)
We want the user to input their name so we will just leave it null for now (nothing assigned to it yet)
Next up is
VISIBLE "NAME::"!
VISIBLE is the command to output something to the console, you can include multiple things to output and there are some ways to format it, we will want it to say "NAME:(input)"
first thing you will probably notice is the ::, : is used to define some formats, here is a list:
:) represents a newline (\n)
:> represents a tab (\t)
:o represents a bell (beep) (\g)
:” represents a literal double quote (“)
:: represents a single literal colon (:)
note that there are 3 more as well but at least 2 of them are not supported in lolexec, they aren't of major importance so i won't cover them, you can look them up if you want
in the spec.
you will see that :: just means :, this is because : is used for stuff but if you want to literally display :) it would think you meant new line, the extra : tells it that thats not what you want and so it is used to make sure that everything works nice and dandy.
the only other major shocker here is the ! at the end, this just tells it to stay on the same line, normally it will automatically stick a :) on the end of the VISIBLE command
alright so we covered that, next up is the input:
GIMMEH NAME
GIMMEH is a simple command, it only will accept one input per command and only a variable
we have a variable don't we? yeah thats right, we called it NAME!
this will cause the program for you to wait until there is input and the enter key is pressed
then it stores the entered stuff into the variable as a YARN (a YARN is a string of characters)
so now we have a YARN called NAME and it has a name stored in it, lets output it!
VISIBLE "HERRO " NAME "!"
back to VISIBLE, its the same as before just note that it can do multiple expressions. (we did 3 here) also note that "" defines text, if you don't have "" it will assume its a variable. If you output a variable it will dump the contents of the variable on the screen.
Finally we end the program with KTHXBYE.
« Last Edit: April 27, 2010, 09:26:11 pm by miotatsu »

_player1537

  • Guest
Re: Lolcode Tutorial
« Reply #1 on: April 27, 2010, 10:07:49 pm »
hmm, I saw this a while back, but didn't want to deal with getting the compiler.  Is there a linux version of the interpreter/compiler?  Else I'll just use windows, either way, looking forward to it.  Good Luck

Offline miotatsu

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 332
  • Rating: +11/-1
    • View Profile
Re: Lolcode Tutorial
« Reply #2 on: April 27, 2010, 10:14:41 pm »
yes, i will post a link to the interpreter i use on linux soon

_player1537

  • Guest
Re: Lolcode Tutorial
« Reply #3 on: April 27, 2010, 10:24:15 pm »
kthxbai :D

(I'm in ur loop uppin ur var)
« Last Edit: April 27, 2010, 10:24:35 pm by _player1537 »

Offline miotatsu

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 332
  • Rating: +11/-1
    • View Profile
Re: Lolcode Tutorial
« Reply #4 on: April 28, 2010, 05:36:03 pm »
here is the interpreter i use on linux: http://www.icanhaslolcode.org/

Offline miotatsu

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 332
  • Rating: +11/-1
    • View Profile
Re: Lolcode Tutorial
« Reply #5 on: May 11, 2010, 06:24:57 pm »
part II is out!
Okay so how do we make anything useful with it?

Welcome to Part II of the lolcode tutorial:
making something almost sort of useful

Lets make a small example program, how about a program that
calculates the amount of xp needed for any given level in
the game Volund?

The first thing you will need when making a mathematical
program is the formula or equation that the problem uses.
In this case we are lucky, people have already figured out
what the equation for calculating xp in volund is, it is as
follows:

xp = ((((level*(level+1))/2)*10)+600)

that sure is a mouthful isn't it?
so lets start off by making a simple Input/Output system.
What do we need to start?
We need variables.
to be exact we need two variables, one for xp and one for
level. The user will input the level and it will output the
xp.
okay now lets start coding!

OBTW DIS PROGRAM CALCULATEZ TEH XP UP TO AT LEAST LVL 100
LOLOLO
DIS CODEZ IZ PROPERTIEZ OF TEH ELITE MIOTATSUZ
TLDR
HAI 1.2
I HAS A XP
I HAS A LVL

Woah woah woah, whats that stuff in the beginning?
don't worry, its just a comment.
OBTW declares the start of a comment, anything inside it is
ignored by the interpretter or compiler, it does nothing.
so why put it there? because commenting code can help you
keep track of things, leave notes, explain what a program
does, etc. It is mainly for personal reference.
okay so we started a comment but how does it know when the
comment is done?
with TLDR
anything between OBTW and TLDR is a comment.
there is more than one way to comment however!
you can start an OBTW only on an empty line OR after a line
with code on it if there is a comma, such as:

I HAS A VAR ITZ 12,  OBTW this is a long comment block
see, i have more comments here
and here
TLDR, I HAS A FISH ITZ BOB

as you can see the same applies to TLDR but before the next
line of code.

There is even more to it than OBTW/TLDR though!
welcome to the wonderous world of single line comments.

examples:

I HAS A VAR ITZ 12 BTW VAR = 12
I HAS A VAR ITZ 12,BTW VAR = 12
I HAS A VAR ITZ 12
BTW VAR = 12

as you can see you can do the same thing with just BTW but
keep in mind that it can only be one line long and at the
end. You can use it by setting it off by a space, comma, or
new line.
Next up is the start of the program, we enter the program
at HAI 1.2
and then we declare to variables, XP and LVL

next up we want to ask the user to input a level:

VISIBLE "Lvl:: "!
GIMMEH LVL

this shouldn't be anything new, its just VISIBLE and GIMMEH
and now we are ready for the real magic:

XP R SUM OF LVL AN 1
XP R PRODUKT OF XP AN LVL
XP R QUOSHUNT OF XP AN 2
XP R PRODUKT OF XP AN 10
XP R SUM OF XP AN 600

Wait what?
this is wheer we calculate the Xp, remember that formula i
posted up there?
((((level*(level+1))/2)*10)+600)
yeah that.
Lolcode has a rather annoying way of doing math, but it
works.
You should not use conventional math strategies if you want
to stay 1.2 compliant, stick to what is given in the spec:

SUM OF <x> AN <y>       BTW  +
DIFF OF <x> AN <y>      BTW  -
PRODUKT OF <x> AN <y>   BTW  *
QUOSHUNT OF <x> AN <y>  BTW  /
MOD OF <x> AN <y>       BTW  modulo
BIGGR OF <x> AN <y>     BTW  max
SMALLR OF <x> AN <y>    BTW  min

you can find other useful things in the spec such as
boolean operations, i will just cover these however.
in order to make this work we will work from the inner most
parenthesis outward, following algebraic order.
the inner most set of parenthesis are:
(level+1)
so that is what we will do, this will be stored into XP as
a temporary result:

XP R SUM OF LVL AN 1
next we have:
(level*(level+1))
which is now
(level*(XP))
or simply
XP R PRODUKT OF XP AN LVL note that yet again we are
storing to XP with R
next up we divide the result by two, which i'm sure you can
see will be:
XP R QUOSHUNT OF XP AN 2
finally we finish it off by multiplying it by 10 and adding
600.
XP R PRODUKT OF XP AN 10
XP R SUM OF XP AN 600

so thats how we do math. exciting, i know.
now we just display it and end the program
VISIBLE "XP:: " XP
KTHXBYE
this isn't very useful though is it? we can only do one
calculation every time we run it!
so lets make it loop, or that is to say.
once the program is finished lets make it jump back to the
start of the program instead of exit.
how would we do that?
check out what the spec has to say about loops.

IM IN YR <label> <operation> YR <variable> [TIL|WILE
<expression>]
<code block>
IM OUTTA YR <label>

Okay so what does this say? we can use IM IN YR <label> to
loop and stuff. what about all that other stuff? the
operation variable combo gives it something to test and TIL
and WILE for testing and what not but I won't cover that just yet, we just want it to jump back to the top no matter what as long as the program is done
lets make a label called BASE, whenever it hits IM OUTTA YR BASE
it will jump back to IM IN YR BASE
but keep in mind that where you place these commands is very, VERY important.
take a look:
Code: [Select]
OBTW DIS PROGRAM CALCULATEZ TEH XP UP TO AT LEAST LVL 100 LOLOLO
DIS CODEZ IZ PROPERTIEZ OF TEH ELITE MIOTATSUZ
TLDR
HAI 1.2
IM IN YR BASE
I HAS A XP
I HAS A LVL
VISIBLE "Lvl:: "!
GIMMEH LVL
XP R SUM OF LVL AN 1
XP R PRODUKT OF XP AN LVL
XP R QUOSHUNT OF XP AN 2
XP R PRODUKT OF XP AN 10
XP R SUM OF XP AN 600
VISIBLE "XP:: " XP
IM OUTTA YR BASE
KTHXBYE

note that the loop (label) is placed at the very beginning of the program and the ending label (goto) is placed at the end right before the KTHXBYE, this means that the program will never end, you have to hit the X to close it.

next tutorial i will cover more advanced uses of looping.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Lolcode Tutorial
« Reply #6 on: May 11, 2010, 06:46:00 pm »
I did not pay attention to this much but the syntax sure looks weird o.o

Offline miotatsu

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 332
  • Rating: +11/-1
    • View Profile
Re: Lolcode Tutorial
« Reply #7 on: May 11, 2010, 07:21:29 pm »
its programming but for lolcats.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Lolcode Tutorial
« Reply #8 on: May 11, 2010, 07:37:33 pm »
what would be the advantage of this language over another language, for example, for game dev? Or is it more like some new internet meme I don't know about?

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Lolcode Tutorial
« Reply #9 on: May 11, 2010, 08:08:58 pm »
Just an esoteric programming language. For fun. I doubt anybody uses it for serious work.
Edit: Links for more general info:
http://en.wikipedia.org/wiki/Esoteric_programming_language
http://esolangs.org/wiki/Main_Page
« Last Edit: May 11, 2010, 08:12:02 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline miotatsu

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 332
  • Rating: +11/-1
    • View Profile
Re: Lolcode Tutorial
« Reply #10 on: May 12, 2010, 01:47:14 am »
exactly^ however there is one benefit to it besides just for fun/jokes: it is very simple and very small, i would say it is even easier than ti-basic, which makes it a good language for anyone starting out with programming imo

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Lolcode Tutorial
« Reply #11 on: May 12, 2010, 01:59:51 am »
Yeah true, I notice the syntax is kinda simple so it shouldn't be too hard to learn

Offline mapar007

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 550
  • Rating: +28/-5
  • The Great Mata Mata
    • View Profile
Re: Lolcode Tutorial
« Reply #12 on: May 27, 2010, 12:19:07 pm »
It's like brainfuck but easier. :P