We write our interpreted hexadecimal programming language for QR codes
Inspired by the Google Glass project, I thought how good it would be to make an extremely simple but powerful tool of augmented reality specifically for such glasses. And why not make it based on such widely used technology as QR codes. Thus, the idea of the QuRava language was born - a set of bytes written in a QR code and interpreted into a full-fledged program that implements part of the capabilities of the Java language.
I want to immediately warn you that all of the following - the alpha version, was made in several evenings. Therefore, do not swear too much about small opportunities and do not ask questions about the lack of lambda calculus.
1. Purpose
It seems to me that most augmented reality tools are completely divorced from reality. They are beautiful, interesting, but useless for practical use. And I wanted to make a technology that would be extremely simple and convenient for everyday use. The best thing that came to mind is to embed the interpreted language into QR codes. They are already widespread, and do not cause rejection in people. They can be printed and attached to any place, be it a cafe table or a shop window. They are read almost instantly, and therefore using Google Glass it will be possible to display programs in real time. ')
The advantages of this approach are obvious:
only the interpreter program and the QR code itself are needed, no additional equipment is needed
interface standardization; people do not need to learn again every time
huge potential opportunities: for example, you can make an order in a cafe, looking at the table, or order tickets for a rock concert, looking at the poster
elementary simplicity and low cost of creating and distributing programs
But there are also disadvantages:
small program size. Solved by pumping additional logic over the network.
perhaps it would be easier to download a code in a scripting language from the Internet via a QR link, even though it is slower
Stephenson's novel "Avalanche" is recalled, in which people, looking at a black and white image, melted their brains
2. Language structure
In QR codes, information is recorded using a sequence of sections of 8 bits each. Therefore, I decided to create a language on the basis of byte calculus, that is, on the basis of hexadecimal characters from 00 to FF. First, the idea of writing a Java bytecode in the barcode came to mind, but I refused it because of the redundancy of this approach. The interpreted language for QR should be very short, so a bar code can contain at best only 2 kilobytes of memory. Yes, and many opportunities this language is not necessary.
So far, only three types of constructions are used in the language: variables, procedures, and Android components. Each object of these three types has its own subtype (for example, for variables it is 00-boolean, 01 is int, 02-float, 03-String), single-byte name and body. For the convenience of the parser, a length byte is placed before each construction. For example, the syntax { 06,01,AA,00,00,00,03 } contains the following:
The first byte is a length byte of 6, and the parser reads 6 bytes after it.
The second byte is the data type byte. It is 01, which means integer
The third byte means the name of the variable. Here the variable is called AA, and afterwards the interpreter will know it precisely under this nickname.
After that comes 4 bytes of the body. Mean that the variable is initially assigned a value of 3
In the procedures, a list of primitive operations is specified. For example { 4,14,,, } means AA = AA + BB;
Since Google Glass has not yet been released, I wrote an interpreter program for Android. Therefore, QuRav now supports Android components. For now, only Button, TextView and Edit Text. You can set their position on the screen, the text on them, and for the buttons to add an indication of the procedure that is called when you click. For example, { 05,09,05,02,03,08 } means the creation of a button (code 09) with the name 05, position on screen 02 (at the top center, using the system of old telephone hardwired keyboards from 1 to 9), with the text string by name 03 and by calling procedure 08 by pressing.
To be honest, now the syntax looks just awful. It is damn complex and has very few features. But this is just a prototype. In the future I plan to stuff all primitive operations on libraries, making them shorter and more convenient. There is still no possibility to call a procedure from another procedure, which is caused by a stupidly written compiler. Now by the way about him.
3. Interpreter
The main interpreter parser reads the program, breaks it into pieces and sends them to the subparsers, which in turn can have their own subparsers. Based on the results of the work of numerous parsers, the program is assembled in pieces at not less than small factories. Finished procedures, variables, and components fall into the Main class. In the Main class, everything is contained in separate HashMaps. An example of such a HashMap is the namesOfVariables:
public Object getVariable(Byte name){ return namesOfVariables.get(name); }
And so that access to variables and components is from procedures and primitive operations, the Procedure class is an inheritor of the ProgramUnit class (in the figure designated as Main), and the primitive operation extends the Procedure class. In the procedure class, the variable taking method is overridden. By adding a couple of lines to it, you can implement the mechanism of local variables.
To be honest, the interpreter is the most boring part of what was done, so enough about it.
4. Example of use
And now I will show you something for which you actually started reading this article. An example of the current program in the language QuRava. In this example, byte numbers are used, not their hex equivalents!
02,03,00 - Announcement of an empty string named 00 02,01,01 - declaration of a variable of type int by name 01 02,01,02 - declaration of a variable of type int by name 02 06,03,03,80,108,117,115 - announcement of the line with the text “Plus”
04,11,04,01,00 - creating a variable text field named 04 05,09,05,02,03,08 - creating a button named 05 calling procedure 08 when pressed 04,11,06,03,00 - creating a variable text field named 06 04,13,07,08,00 - creating an immutable text field named 07
31,04,08 - announcement of procedure by name 08 ........03,42,00,04 - read the text recorded in field 04 and put it in line 00 ........03,41,01,00 - we translate the line 00 to int and write the result in the variable 01 ........03,42,00,06 - read the text recorded in field 06 and put it on line 00 ........03,41,02,00 - we translate the line 00 into int and we write the result in the variable 02 ........04,14,01,01,02 - add variables 01 and 02 and write the result in 01 ........03,40,00,01 - we translate the value 01 into a string and write the result in 00 ........03,43,07,00 - we display in the text field 07 a line 00
Well, actually a demonstration of work: (sorry for the quality)
5. Further development
Now I showed you only the alpha version of the prototype of what I want to do. In the future, I would like to add a lot of interesting things to the QuRava language:
develop a simple and powerful syntax
add regular expressions, arrays, loops and more
to distribute all primitive operations among libraries duplicating the main classes-libraries of Java itself (such as Math or Arrays, for example.
add the ability to call procedures from other procedures and local variables
add work with the network, the function of downloading additional content from the network to the program or even logic
write a bunch of standard libraries
write a compiler for the desktop and a small but convenient IDE to it
write comprehensive language help
put the project on github
Unfortunately so far there is almost nothing to show, so I will not post any links. If it is interesting, then I will work on it and write another article. In the meantime, I want to conduct a survey.