Compiler hints

Be aware that some EV3 Basic programs that run perfectly in 'PC mode' (launched within Small Basic) will not compile correctly to the brick and therefore cannot be run in 'Brick mode' (launched from within the brick's menus). Sometimes the reason is obvious: of course brick mode will not work if the program makes use of Small Basic's graphic window or text window or tries to play sounds on the PC's speaker. Sometimes the reason is less obvious, caused by programming practices that are compatible with Small Basic but not with the compiler that tries to convert the Small Basic files into the RBF format that the brick understands. Here are some tips to minimise the risk of problems occurring when you try to compile your programs so that they can be run in brick mode:

  1. Be aware that any program that plays sounds on the PC speaker or uses the Timer functions or the Small Basic graphics window or text window will not be compatible with the EV3 Explorer compiler. The closest compiler-compatible equivalent to the Timer function is the Program.Delay() function.
  2. Small Basic allows you to use a variable before it has been assigned a value but Explorer's compiler does not allow that. For example, this code would work in Small Basic but would not compile: 
       Sub Move
         Motor.Move("BC",20,angle,"True")
       EndSub
       angle=360
       Move()
    So, for compatibility with the compiler, you must assign a value to the variable before you use it, like this:
       angle=360
       Sub Move
         Motor.Move("BC",20,angle,"True")
       EndSub
       Move()
    Here's another example of a program that works in Small Basic but which won't compile to the brick (a silly example because you would use For rather than While in this case):
       While n<10
         Speaker.Tone(100,n*200,500)
         Speaker.Wait()
         n=n+1
       EndWhile
    By the way, can you guess why the above code will begin by playing a 250Hz tone twice?
  3. Similarly, put subprogram definitions BEFORE the main program.
  4. Don't use this method of making arrays: Colors="0=UNKNOWN;1=BLACK;2=BLUE;3=GREEN"
  5. Don't use strings as array indices, like this: address["John"]. Use only numbers as array indices, such as employee[7]
  6. Don't use file names with more than 29 characters (including the .sb extension). Use only these characters in the file names: 
  7. ABCDEFGHIJKLMNOPQRSTUVWXYZ
    abcdefghijklmnopqrstuvwxyz
    0123456789 -_  (spaces are OK)
The fourth tip above is NOT followed by this program below which is intended to recognise standard colors and then display the corresponding numerical color code and the name of the actual color:

Sensor.SetMode(3,2)

Make an array containing the colors, matched to the array index values
Colors="0=UNKNOWN;1=BLACK;2=BLUE;3=GREEN;4=YELLOW;5=RED;6=WHITE;7=BROWN"

While "True"
  LCD.StopUpdate()
  LCD.Clear()
  code=Sensor.ReadRawValue(3, 0)
  LCD.Text(1,33,40, 2, "Color "+ Code)
  LCD.Text(1,33,75, 2, Colors[Code])
  LCD.Update()
  Program.Delay(200)
EndWhile

The above program runs fine in PC mode but will not compile for download to the brick. Why? Because  the method used to make the array called 'Colors' is not acceptable to the compiler. So the solution, which CAN be compiled to the brick and run in brick mode, is this:

Sensor.SetMode(3,2)

'Make an array containing the colors, matched to the array index values
Colors[0]="
UNKNOWN"
Colors[1]="BLACK"
Colors[2]="BLUE"
Colors[3]="GREEN"
Colors[4]="YELLOW"
Colors[5]="RED"
Colors[6]="WHITE"
Colors[7]="BROWN"

While "True"
    LCD.StopUpdate()
    LCD.Clear()
    code=Sensor.ReadRawValue(3, 0)
    LCD.Text(1,33,40, 2, "Color "+ Code)
    LCD.Text(1,33,75, 2, Colors[Code])
    LCD.Update()
    Program.Delay(200)
EndWhile

Further details, as explained by the developer:

The original behaviour of Small Basic has been mimicked in the EV3-compiler as faithfully as possible, but there are some things that simply can not be done with the EV3 byte code interpreter.

Variables are typed

While Small Basic has just one datatype which is a Unicode string into which all possible values (even arrays) are mangled, EV3 Basic has 4 distinct datatypes for variables:

  • Number (a 32 bit floating point)
  • Text strings
  • Array of Numbers
  • Array of Text strings

Every variable has one of these types (which is determined by its first use in the program code) and cannot be used to store anything else.

Operators and functions work on compile-type types

Arithmetic or logic operators need to be provided with either number or text parameters and deliver a result of also a defined type (for example, the "<" needs two numbers and delivers a text). One exception is the "=" which will perform either a numerical or a textual comparison, depending on the compile-time type of the operands. Another exception is the "+" that will do a numerical add when provided with two numbers, but will do a text concatenation if one of the operands is text. Similar requirements are there for function parameters, which also have a defined compile-time type. To make things easier, an automatic conversion from number to text is done if a parameter needs a text (but not the other way round).

Limitation for text

Any text variable can only hold up to 251 characters and not the full Unicode range is supported, but only the codes 1 - 255 (they are just plain C-style char strings). The same holds for all elements of text arrays.

Comments