## 10 POKE53281,0

# How #Genesis64 parses C64 BASIC (Part 2)

Or: how operators work and mathematical equations are solved

The last post was about translating your input into something that can be run in #Genesis64, this time I'll try to shed some light on the black art of using operators and math.

Let's start with a simple BASIC program:

10 v=53280 20 poke v+1,0 30 print 2+2*2

We can skip line 10 and look directly at the parameter part of the POKE command:` v+1`

.

Anyone can understand it and the trick (as so often) is to make the program to understand it. The term `v+1`

is pushed into the tokenizer after splitting (see Part 1) and again I use a simple regex to see what it is we want to tokenize:

/^(.*)\+(.*)$/

This translates to: "Take everything until you hit a '+' and group it, then take the rest and group it as well". We are using a '+' operator, and the two groups we get are fed into the Tokenizer again to see what they are, namely a variable: '`v`

' (a numerical one at that) and a number '`1`

'.

So the token for '`+`

' looks like:

When the program is run and the token for '`+`

' is executed, it simply takes its two parameters and adds their Num value and then stores it in its Num value (which is then used by the `poke`

token.

## More complex expressions

2 + 2 * 2

Luckily these work exactly like the simple ones, we just have to keep an eye on the order. The Tokenizer will return two tokens: a "multiplication" token and an "addition" token.

(excuse the crude drawing)

#Genesis64 parses expressions in reverse order (so lower-order mathematical operations first), but executes them back in reverse again:

// parsing 2+2*2 -> [0] "+": 2, 2*2 2*2 --> [1] "*": 2, 2 // running [1]: "*", 2, 2 => 4 [0]: "+" 2, [1] => 6

This way all other operators are parsed, too: `and`

, `or`

(but not: `not`

), `<`

, `>`

, etc.

The next part(s) will be about the more complex commands like `if`

/`then`

, `goto`

/`gosub`

, and `for`

/`to`

/`step`

as they all share the "problem" that they need to jump around the token list.

You can try out your C64 BASIC skills in #Genesis64. You can get a list of all working c64 BASIC commands and functions by typing "help" and hit enter.

Happy coding,

nGFX