Author Topic: Grammer Tutorial  (Read 13194 times)

0 Members and 1 Guest are viewing this topic.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Grammer Tutorial
« on: October 01, 2011, 05:30:48 pm »
This is probably going to gloss over a lot of key aspects, so if you have questions, feel free to ask :)
I realised that I still have yet to actually upload a tutorial anywhere for how to program with Grammer, so here goes :) This first section will describe some of the key aspects and it will provide an example :)

Part 1:
First off, I would like to introduce you to Grammer. Grammer is an interpreted programming language (like TI-BASIC), that can be programmed on your calculator (like TI-BASIC). If you program a lot in TI-BASIC, that will be helpful in learning Grammer, but it might cause some confusion, too. Because of that, I will introduce you to some of the key differences:

Numbers:
(simple) In Grammer, values are integers from 0 to 65535. Anything above or below is looped back around.
(complicated) Grammer uses 16-bit math. This makes things fast and simple for the interpreter while still being useful for games (I could have gone with 8-bit for even faster speed)

Math:
(simple) Math is done right to left and doesn't use order of operations. There are no parantheses. Also, overflow is detected for many operations and the overflow is stored to another var (theta prime)
(extra) Theta prime usually holds remainders or "carry." For example, if adding exceeds 16-bit range, 1 is returned as a "seventeenth bit."

Variables:
(simple) The letters can be used like in BASIC. These are called pointer vars in Grammer. You have A through Theta as well as their primes. For example, A and A' are two different pointer vars. Theta prime is used by the interpreter to return extra data or info so there are 53 vars to use and a system var. There are also strings and programs that can be accessed. To do this, you store them to a pointer var. For example, "HELLO→A is valid.
(complicated) When storing strings to a pointer var, you are actually storing the memory address that the data is located at. This is why they are called pointer vars. They point to the data, they don't actually contain it.

Using this information and a command reference, you will be on your way to making games!
(you can even use this to run algorithms faster than BASIC, if you are interested!)

So no we will make a simple program that moves a cursor around the screen! To do this, we will use the rectangle commands, getKey, and a Repeat loop. Depending on how you want the program to run will determine the header. For now, use .0:Asm(prgmGRAMMER as that will work universally for any method:
Code: [Select]
.0:Asm(prgmGRAMMER         ;This is the start of the program
0→X →Y                     ;Initiallises the coordinates. Note the space.
Repeat A=15                ;A will hold have the keypress, so this checks for [CLEAR]
Line(X,Y,6,6,2             ;draws an inverted 8x8 rectangle
DispGraph                  ;updates the LCD screen
Line(X,Y,6,6,2             ;Reinverts the rectangle.
Repeat A                   ;Repeats loop until A is not 0
getKey→A                   ;Stores the key code to A
End                        ;Ends the Repeat loop
X+A=3 -A=2                 ;Updates X coordinate based on key press, Result in Ans
If <91                     ;If it goes below zero, so this checks for going offscreen!
→X                         ;If it is on screen still, the new value is stored to X
Y+A=1 -A=4                 ;Updates X coordinate based on key press, Result in Ans
If <59                     ;Checks if the cursor would be offscreen
→Y                         ;If it is on screen still, the new value is stored to Y
End

Stop                       ;Ends the program
Press Clear or ON to exit :)

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Grammer Tutorial
« Reply #1 on: October 06, 2011, 12:18:48 pm »
This section will deal with how to use logic expressions and loops.
Logic:
Say I wanted to test if Enter was being pressed AND if C is 1. In BASIC, this would be simply:
Code: [Select]
If C=1 and getKey=105
However, in Grammer, there are two key differences: the getKey codes are different and there isn't an and token! So instead, we must use math. As in BASIC, the condition is considered "false" if 0 is returned and true for anything else returned. So in Grammer:
C=1 returns 0 if C is not 1, 1 if C=1
getKey=9 returns 0 if Enter isn't pressed, 1 if it is pressed

So we want to return 1 if and only if both statements are 1. How do we do this? We multiply! If either statement is false, multiplying them returns false, too. But how can we do this in Grammer? It doesn't have order of operations or parentheses! Well not to worry, I try to give the bad news before the good news :) The good news is that you can use spaces or colons to separate chunks of math! So here is the final code:
Code: [Select]
If C=1 *getKey=9
Similarly, with OR logic we use a + instead of *:
Code: [Select]
If C=1 +getKey=9

Loops:
The loops in Grammer act the same way as in BASIC (except for one case with the For( loop)

So here is an explanation of the loops:
Repeat
This uses a conditional expression and loops until it is true. For example, to loop until Enter is pressed:
Code: [Select]
Repeat getKey=9
End
Note that this executes all of the code between the Repeat and End first, then it checks the condition.

While
This also uses a condition, but it only executes the loop as long as the condition is true. For example, to loop while a key is not pressed
Code: [Select]
While getKey=0
End
Note that this will not execute the code in the loop if the condition is false upon entering. For example:
Code: [Select]
3→A
While A=4
<<code that never gets executed>>
End

For(
This works the same way as in BASIC. For example:
Code: [Select]
For(A,0,99
End
However, if you try loop from 0 to 0, for example, the loop will go 65536 times.

So if anybody has any questions, wants help with some code, or wants a specific example, feel free to ask :)

Offline mrmprog

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 559
  • Rating: +35/-1
    • View Profile
Re: Grammer Tutorial
« Reply #2 on: October 15, 2011, 05:29:37 pm »
Thanks for the tutorial! I didn't know that so much could be done with Grammer at this point. The getkeys are the same as axe, right?

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Grammer Tutorial
« Reply #3 on: October 16, 2011, 06:54:51 am »
yes, it is.
Sig wipe!

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Grammer Tutorial
« Reply #4 on: October 16, 2011, 09:34:14 am »
yes, it is.
Awesome, I wasn't sure so thanks for letting us know that! Um, also, here is a more complete tutorial XD

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Grammer Tutorial
« Reply #5 on: October 16, 2011, 11:24:49 am »
How do you save hexadecimals to a pointer? [FFFFFFFFFF->POINTER? or what? And with PT-On can you take as pointer e.g. D+8?

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Grammer Tutorial
« Reply #6 on: October 16, 2011, 03:02:23 pm »
so put sprite data in lbl:
.SPRITE
FFFFFFFFFFFFFFFF
FFFFFFFFFFDCDCFF

and do this:

Lbl"SPRITE->A
Pt-Off(8,A,0,0,1,8
DispGraph

you add 16 to A if you wanna display next one
Sig wipe!

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Grammer Tutorial
« Reply #7 on: October 16, 2011, 03:04:04 pm »
Nice to see a grammer tutorial Xeda. If it wasn't for the fact each portions are available in one single topic instead of separate ones, I would have suggested you also submit it to http://www.omnimaga.org/index.php?action=articles;cat=11 , although you could always just submit the first part there with a link to this topic.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Grammer Tutorial
« Reply #8 on: October 17, 2011, 08:30:49 am »
@DJ: Okay, would it be okay if I instead posted the whole tutorial there and posted a link to it here?
@Suronome: Yeong has it correct XD
@Yeong: You are getting a pretty solid understanding of the language :) Thanks for helping folks out!

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Grammer Tutorial
« Reply #9 on: October 17, 2011, 03:59:19 pm »
There is no problem, although I think the character limit is 65536 there (like in posts).

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Grammer Tutorial
« Reply #10 on: October 18, 2011, 11:56:27 am »
Okay, I added it :) It seems to have a different format after a certain point, though, and I could figure out why :/ Also, I put it under general tutorials.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Grammer Tutorial
« Reply #11 on: October 23, 2011, 10:32:22 pm »
So here is a little note on programming when it comes to creating a variable. When you want to use some RAM for long term (like saving scores and data and whatnot), you should store to an appvar or a program with a weird name. This way, it wont be as easy to access the data for inexperienced calc users.

However, if you only need the data for the duration of the game, you should use Temporary Programs because these get deleted automatically by the OS. This means it won't eat up RAM with useless data and it does a few other useful things (Send( will not delete an existing var, so if the var exists at 17 bytes and you need 47, you will have an issue).

Also note that if you do this, you will want to name it something weird, that way you can be sure no program will have that name. For example, creating a temp prog with a name BuF-1 is good since programs don't use lowercase letters or the inverse power under normal circumstances.

So to create an ___ variable named BuF with 7 bytes:
Code: [Select]
Send(7,"UBuF     ;Creates an AppVar
Send(7,"EBuF     ;Creates a Program
Send(7,"FBuF     ;Creates a Protected Program
Send(7,"VBuF     ;Creates a Temp Program
So I hope this helps!