Calculator Community > Grammer

Grammer Tutorial

(1/3) > >>

Xeda112358:
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: ---.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

--- End code ---
Press Clear or ON to exit :)

Xeda112358:
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: ---If C=1 and getKey=105

--- End code ---
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: ---If C=1 *getKey=9

--- End code ---
Similarly, with OR logic we use a + instead of *:

--- Code: ---If C=1 +getKey=9

--- End code ---

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: --- Repeat getKey=9
End
--- End code ---
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: ---While getKey=0
End
--- End code ---
Note that this will not execute the code in the loop if the condition is false upon entering. For example:

--- Code: ---3→A
While A=4
<<code that never gets executed>>
End
--- End code ---

For(
This works the same way as in BASIC. For example:

--- Code: ---For(A,0,99
End
--- End code ---
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 :)

mrmprog:
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?

Yeong:
yes, it is.

Xeda112358:

--- Quote from: yeongJIN_COOL on October 16, 2011, 06:54:51 am ---yes, it is.

--- End quote ---
Awesome, I wasn't sure so thanks for letting us know that! Um, also, here is a more complete tutorial XD

Navigation

[0] Message Index

[#] Next page

Go to full version