Grammer Tutorial
Introduction Grammer is a programming language designed to be powerful, fast, efficient, and safe. However, with all of that said, though it isn't as tough to learn as Assembly, it will probably be a little more difficult than BASIC. One similarity to BASIC is that Grammer is interpreted. This makes it slower than assembly, but it is smaller and more safe. This tutorial will be designed to show Grammer users all the cool features and hacks that Grammer provides, so have fun!
Terminology There are ideas and terms in Grammer that would almost never appear in BASIC. So the first thing that neads to be covered is terminology: Pointer Pointers are used often in Grammer as a means of accessing data. This has a few positive side effects as well as a few negative ones. In easy(ish) terms, all data is located in memory. The "location" is given as a number and on the calulator, this number is from 0 to 65535. A pointer, then, points to the data location. So where are pointers used in Grammer? Say, for example, you want to reference a string. In Grammer, you would do "HELLO→A. The location of the string "HELLO" is stored to A. Sprites A sprite is just an image. In Grammer, sprites are a multiple of 8 pixels wide up to 96 and up to 64 pixels tall. Strings Strings can be any form of data. They can be text, sprite data, label names, even code. Pointer Vars Pointer Vars are the letters A through Z and Ɵ as well as their primes. For example, S and S' are two different pointer vars. These are used to store pointers or values. Pointer vars are 16 bits, so only integers from 0 to 65535 are stored to them.
Subject 1-Numbers In Grammer, values are integers from 0 to 65535. This is one of the key differences between Grammer and TI-BASIC. While this may make some things difficult, this has a few uses and effects not found in BASIC. So first thing up, what happens when you go beyond 65535? The numbers simply loop back to 0! So what happens when you go below 0? You loop back starting at 65535! This is called modular math, so if you understand this section, you will actually be understanding an important concept in number theory. The uses of this will come later...
Subject 2-Math In Grammer, math does not follow Order Of Operations and has some limiting factors (like only working with 16-bit integers). However, there are exploits that can be useful for this. Anyway, math is done from right to left, so as an example, we will look at 3*4+6/4-2: 3*4+6/4-2 3*4+6/2 3*4+3 3*7 21
There are a bunch of math commands at your disposal, too, not just basic math. The tricks, however, come from Ɵ'. There are several operations that modify Ɵ' as a way to return additional information. For example, when adding, if the result exceeds 65535, 1 is stored to Ɵ', otherwise it becomes 0: +: If the result is greater than 65535, Ɵ' is 1, otherwise it is 0. -: If the result is less than 0, Ɵ' is 65535, otherwise it is 0. *: Ɵ' is the upper 16 bits allowing for a 32-bit result. /: Ɵ' is the remainder 2: Same as multiplication See the Math Functions section in the full tutorial for more info on other operations.
If you wanted to do If 24=((A*7)+(B*12)), you would do this: :A*7 :+B*12 :If =24 However, if you want to keep it all on one line (because anything in an If or While statement does not modify Ans), you can use a space instead of a newline: :If A*7 +B*12 =24
Subject 3-Logic Logical operators are used to compare values. If the logic is true, the result is 1 and if it is false, the result is 0. As an example, 3>4 is false because 3 is not greater than 4, so this returns a 0. These are useful for conditional statements used in If , While , and Repeat .
Subject 4-If Blocks If blocks are often used because they are fairly useful. For example, if you want to do something if a condition is true, you will usually want to use an If statement. For example, if you want to increment A if getKey is 9 (if [ENTER] is pressed):
As in TI-BASIC, you can also use an If...Then...End statement to handle multiple lines of code. For example:
1 2 3 4 5 6 7
| If A=1 Then -C→C -D→D End
|
Although usually you can get away with putting it all on one line like this to save a few bytes:
1 2 3 4
| If A=1 -C→C -D→D ;note that there is a space instead of a newline
|
Subject 5-Loops Most games or math algorithms will use loops and Grammer offers three loops that act the same way as in BASIC: Repeat This will repeat the code between Repeat...End until the statement is true. For example, to increment A until A=999:
1 2 3 4 5
| Repeat A=999 A+1→A End
|
Note that this will execute the code first and then check the conditional. While This is a little different from Repeat in that it executes the code in the loop while the condition is true. If the condition is false before it enters the loop, the code inside never gets executed. So for example, to loop so long as A<999:
For( This will increment a variable from a starting point to an ending point. For example, this will cycle starting with A=3, incrementing A by 1 every cycle until it reaches 9:
Subject 6-Labels Labels are a neat feature in Grammer that are very much unlike BASIC. In BASIC, you start a label name with Lbl and it can be up to 2 chars long and must be numbers or letters (or a combination). In Grammer, labels start with a "." and can contain any character except a newline. There is also no size limit, but remember that a label of fifty chars will take longer to locate than one of 5 characters. Here is a label example:
We use Lbl to get the location of the label which is used by Goto. This also lets you store data to a label and get a pointer to it. So as an example:
1 2 3 4 5
| Lbl "HeLLO→Z <> .HeLLO
|
Subject 7-Code Flow Oftentimes, linear code flow just won't work. Sometimes you need to jump around a bit and there are a few commands for this: Goto This is used to jump to wherever a pointer is pointing and execute code from there. Here are two ways to jump to a label:
Or:
Return This returns a pointer to the line following it. I should note that this was created before Repeat and While. So an example of a loop until [CLEAR] is pressed:
1 2 3 4 5
| Return→Z If 15=getKey Goto Z
|
Prgm This will execute a subroutine and then come back. See Subject 10 for info on subroutines. If the subroutine is located at eh label named HeLLO:
Subject 8-Strings Strings are accessed through pointers. The quote token returns the pointer to the string. So for example, here are two ways to display text:
1 2 3 4
| "HELLO WORLD→A Text(0,0,A
|
Or:
Subject 9-Sprites Using sprites is a pretty advanced technique, so don't expect to understand everything here. Sprites are pretty much mini pictures. They are a quick way to get detailed objects that move around making them a powerful tool for graphics. In Grammer, the main sprite commands are Pt-On( and Pt-Off( and both have differences and advantages over the other. Sprite Data Sprite data is in the form of bytes or hexadecimal and you will want to understand binary to hex conversions for this. For example, to draw an 8x8 circle, all the pixels on should be a 1 in binary and each row needs to be converted to hex:
0 0 1 1 1 1 0 0 =3C 0 1 0 0 0 0 1 0 =42 1 0 0 0 0 0 0 1 =81 1 0 0 0 0 0 0 1 =81 1 0 0 0 0 0 0 1 =81 1 0 0 0 0 0 0 1 =81 0 1 0 0 0 0 1 0 =42 0 0 1 1 1 1 0 0 =3C
So the data would be 3C4281818181423C in hexadecimal. Sprite Logic There are 5 forms of sprite logic offered by Grammer, currrently. These tell how the sprite should be drawn and can all be useful in different situations. Overwrite: For an 8x8 sprite, this will erase the 8x8 area on the screen and draw the sprite. AND: This leaves the pixel on the screen on if and only if the sprites pixel is on and the pixel on the screen is on. OR: This will turn a pixel on on the screen if it is already on or the sprite has the pixel on. This never erases pixels. XOR: If the sprites pixel is the ame state as the one on the screen, the pixel is turned off, otherwise, it is turned on. For example, if both pixels are on, the result is off. Erase: Any pixels that are on in the sprite are erased on screen. The pixels that are off in the sprite do not affect the pixels on the graph buffer. Pt-On( This is used to display sprites as tiles. This means it displays the sprite very quickly, but you can only draw to every 8 pixels. Pt-Off( This is a slightly slower sprite routine, but it allows you to draw the sprite to pixel coordinates.
Subject 10-Subroutines Subroutines are very usefl for code organisation and saving memory. If you have a piece of code used multiple times throughout the program, you will probably benefit from this. A subroutine must end with an End token and is called with prgm instead of Goto. So for example, this is a way to call a subroutine:
1 2 3 4 5 6 7 8 9 10 11 12
| Lbl "SUB1→A prgmA prgmA Goto Lbl "Stop .SUB1 Circle(rand/1024,rand/1024,rand/1024,3 DispGraph End .Stop Stop
|
Subject 11-Text(Subject 12-Interrupts An interrupt is similar to a subroutine, but there are a few things to keep in mind. You might want to back up your program before experimenting: -The code cannot take too long to execute (not sure how long too long is) -The code gets automatically executed over 100 times per second -To set an interrupt, use Func -To stop the interrupt, use Func0 So here is an example of an interrupt that updates the LCD automatically:
1 2 3 4 5 6 7
| FuncLbl "INTERRUPT <> .INTERRUPT DispGraph End
|
Subject 13-Data The ability to access data is a powerful tool and if used improperly can cause a crash. So the key here is to use commands as they are intended! Anyway, there are several ways to create and access data. The functions you will want to look at are: int( iPart( ( { Send( Get(
Subject 14-Particles Grammer has its own particle engine built in and this provides for some neat graphics. To use particles, first you need to add them and then you need to recalculate positions and whatnot. So for example:
1 2 3 4 5 6 7 8
| R▶Pr( ;This clears the particle buffer P▶Rx(2,2 ;This adds a particle at pixel coordinate (2,2) Repeat getKey=15 R▶Pθ( ;Updates particle data DispGraph ;Displays tthe graph End
|
|