Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Update 2 - Our program can now identify numbers.

So previously, our program considered every single word we typed separate. This is very useful in tokenizing, but there are certain cases where this is a problem. One of them is when the interpreter has to recognize numbers. As an example, if I were to type in 22.5; in our previous program, the outputted line array would look like this:

[22,.,5]

We obviously don't want that. We want the program to be able to recognize that the dot in the middle is a binding character and that these three are not three separate tokens but a single number. On the first glance, the fix seems easy. Just check if the next token is a dot and if the one after that is a number. If true, combine all these into a single token. But this does not actually solve the problem. This is because, if the above solution is implemented, the program output to the line '22.3.4;' would be:

[22.3.4]

And this is not what we want either. In the above scenario, we want the program to return an error. The fix to this, too, is actually not that complicated. There are a couple of ways, in fact, to work around this problem. I'll describe the method I used. So suppose I typed in 22.3.4; into the editor. The interpreter function discussed earlier would output the array:

[22,.,3,.,4]

Now first, we create a new array called N in a new function called makeNumber. This is the array that shall be returned by the makeNumber function, which identifies numbers in an array. The makeNumber function loops through the entire array. In each iteration, it does the following:

If the element is a dot and the next element is a number, It appends 0 + dot + the next element into the new array as a single element.

[.,3] -> [0.3]

If the element is a dot but the next element is not a number, It returns an error.

[.,a] -> [error]

If the element is a number and the next element is not a dot, it appends the element to the new array.

[3,a] -> [3,a]

If the element is a number and the next element is a dot, and the one after that is a number, it combines the whole and appends to the new array as one element.

[3,.,1] -> [3.1]

If the element is a number and the next element is a dot, but the one after that is not a number, it returns an error.

[3,.,a] -> [error]

Now this is not the exact flow of logic, but it is something similar. In the end, the result would be something like this:

In the right in the editor, there are three commands:

22.2 + 3.1;
23.1;
24.3.2;

In the output, we can see that the first like is the tokens array which is:

[22,.,2,+,3,.,1,;,23,.,1,;,24,.,3,.,2,;]

The next three lines are the results makeNumber function returned when it was passed this array.

[22.2,+,3.1]
[23.1]
Error

Just as we needed, it was able to successfully recognize single numbers separated by dots and combine them into one floating point value. This took about an hour and a half. Also, I just realized I am creating a game engine and its interpreted language, in another game engine using its interpreted language.