Δευτέρα 27 Δεκεμβρίου 2021

Making the Karel Ide (part one)

This is the first part, of my try to make a Karel Ide. This is the least program to display with syntax color, Karel programs. The Karel_Paf is an EditBox, one of M2000 controls for user forms. This control has a special part, for applying syntax color.

The first problem was from the karel commands (or statements) which utilize the hyphen as a character. So the word BEGINNING-OF-PROGRAM has to have one color. The control's state of the art routine, knows that hyphen is a symbol for expressions (in any language, except Karel). There is a property in the control named  WordCharRightButIncluded which we tell the hyphen included in names. Another property called DropSym and this is hyphen again. So in scanning process the hyphen dropped. Without this setting the END in get different color in END-OF-EXECUTION because END exist as command. So we want this: END-OF-EXECUTION full colored multi-word which included in KAREL_COMMAND$, which we insert in ColorCollection1 property.

There is another set of identifiers, the flags, which we have to place after specific statements, or for simplicity after any statement. So if we place one of them "alone" in a line we get different color. For Karel language there is no problem with  new lines because these included in the whitespace set. Also to make a new set with special color we have to use ColorCollection4 property with EXTRAFRONT property. So if a word has one of character in EXTRAFRONT then the system check the ColorCollection4 for a match. We have to provide upper and lower case for first letter, but no for the match (by default upper/lower case aren't significant).

In the image below we see the code in a form using indentation (spaces). Also with F9 we get a message (in the default language, which in my M2000 is Greek), about the number of words. The hyphen for this process is word boundary, so BEGINNING-OF-PROGRAM counts for three words. That is not bad, because anyway we can "measure" a program easy, using F9 to have an idea if something missed. 

For indentation use ctrl+tab and ctrl+shift+tab. You can mark many lines and apply to them the preferred indentation.

For the example the code has no indentation and the known identifiers are in a random case, so a bEGIN exist with another BEGIN.

This is the code as we copy to clipboard through control.  The color change to adapt the standard color scheme. See that FRONT-IS-BLOCKED are red like 4. Because these are "parameters". There is no -4, all numbers are from 1 positive. FRONT-IS-BLOCKED is a flag which reflect the situation in front of Karel Robot, but for the code is a parameter in a While or in an If structure, which have a meaning like to classifying the structure, as a kind of While or kind of If, as an abstraction.

With black color, in this colored scheme, we have instructions, the 5 standard of them (including move, turnleft, turnoff) plus any defined by us. So the color system not know for the 5 standard, so leave them at the same color as the new identifiers. If we place FRONT-IS-BLOCKED as first thing in a row, we get it in black color. The same for TIMES, AS, DO, THEΝ, if we place them as first thing in a row.  This isn't fault for syntax analyzer. This is for the student to place in a form which is good to him/her,. For example the statement Iterate 3 Times turnleft can be split in four lines, but this isn't nice. We can split it in two lines, keeping Iterate 3 Timers in the first line. Also there is another syntactic issue: After times can exist a final statement (black words), or a Begin End block, with any number of statements. So when we use two lines for Iterate 3 Times turnleft, we have to add indentation to turnleft maybe more, because there is no END. See the picture (where the black words here are magenta there).

So this is the first appear of Karel the Language, with a syntax color convention.

BEGINNING-OF-PROGRAM


DEFINE turnright AS
BEGIN
Iterate 4 times turnleft FINDPASS END
DEFINE FINDPASS AS
BEGIN WHILE FRONT-IS-BLOCKED DO turnleft
END
BEGINNING-OF-EXECUTION
IF RIGHT-IS-BLOCKED THEN bEGIN
ITERATE 3 TIMES BEGIN Begin begin turnright TURNLEFT End
begin MOVE end end WHILE FRONT-IS-BLOCKED DO FINDPASS
END END else BEGIN turnoff end
END-OF-EXECUTION


END-OF-PROGRAM


The form has an empty space at the bottom for a Status Line (I am thinking about it, nothing is finally). My intention will be to show in a list box all the identifiers to help the student to pick the right one, as a cheat sheet. Also a control box for compiling and running the program. The run as I think can be done without showing the Karel robot, and terminate with something like "Approved", or "Karel hit the wall in line 12 pos 1, program aborted". Another option will be to show Karel and to control each move, manually or selecting the speed of execution.

Also I have to load a Karel World (the Karel 's playground) as I defined it in a separate application.(this application exist). So in status line (bottom) I have to write the name of the world, and the starting conditions. Also a form which open above as toolbox, giving the Karel World as a drawing, for reference by the student.

Last will be the load/Save program, load Karel World, and a guide about Karel Language.




KAREL_COMMAND$="|BEGINNING-OF-PROGRAM|END-OF-PROGRAM|BEGINNING-OF-EXECUTION|END-OF-EXECUTION|BEGIN|END|DEFINE|IF|ELSE|ITERATE|WHILE|"
FLAGS$="|FRONT-IS-CLEAR|FRONT-IS-BLOCKED|LEFT-IS-CLEAR|LEFT-IS-BLOCKED|RIGHT-IS-CLEAR|RIGHT-IS-BLOCKED|BACK-IS-CLEAR|BACK-IS-BLOCKED|NEXT-TO-A-BEEPER|NOT-NEXT-TO-A-BEEPER|ANY-BEEPERS-IN-BEEPER-BAG|NO-BEEPERS-IN-BEEPER-BAG|FACING-NORTH|NOT-FACING-NORTH|FACING-SOUTH|NOT-FACING-SOUTH|FACING-EAST|NOT-FACING-EAST|FACING-WEST|NOT-FACING-WEST"


Declare IDE_Karel Form
Declare Karel_Pad EditBox Form IDE_Karel
With IDE_Karel,"UseIcon", True, "UseReverse", True, "Quit" as Quit
With IDE_Karel, "Title" as Caption$, "Visible" as Visible, "TitleHeight" as tHeight, "Sizable", True
Method IDE_Karel,"MakeStandardInfo", 1
Method Karel_Pad, "FontAttr", "Verdana", 12, true '  size=12, bold=true
Method Karel_Pad, "SetBarStyle", color(50, 100, 200), 5, 15
With Karel_Pad, "NoWrap", True, "SelLength" as SelLength, "HighlightParagraph", True
With Karel_Pad, "Text" as Pad.Text$, "NoColor", False, "ShowAlways", True, "SpaceIndent", 3
With Karel_Pad, "ColorCollection1", KAREL_COMMAND$
With Karel_Pad, "ColorCollection3","|TIMES|AS|DO|THEN|"
With Karel_Pad, "ColorCollection4", FLAGS$
With Karel_Pad, "WordCharRightButIncluded", "-", "StartSymbols", "123456789"
With Karel_Pad, "WordCharLeft"," ", "WordCharRight", " ", "DropSym", "-", "EXTRAFRONT","FLRBNAEflrbnae"
Method  Karel_Pad, "UserColorSet",0,,,0,-color(5), -#005588
With Karel_Pad, "ColorSet", -1
Pad.Text$={BEGINNING-OF-PROGRAM


DEFINE turnright AS
BEGIN
Iterate 4 times turnleft  FINDPASS END
DEFINE FINDPASS AS
BEGIN WHILE FRONT-IS-BLOCKED DO  turnleft
END
BEGINNING-OF-EXECUTION
IF RIGHT-IS-BLOCKED THEN bEGIN
ITERATE 3 TIMES BEGIN Begin begin turnright TURNLEFT End
begin MOVE end end WHILE FRONT-IS-BLOCKED DO FINDPASS
END END else BEGIN turnoff end
END-OF-EXECUTION


END-OF-PROGRAM


}
Function IDE_Karel.Resize {
      Layer IDE_Karel { Cls Color(255, 160, 0) ,0}
      With IDE_Karel, "Width" as NP.Width, "Height" as NP.Height, "TitleHeight" as tHeight
      tHeight1=theight*1
      If NP.height>tHeight1+theight Then {
            Method Karel_Pad,"move", twipsX*3, tHeight1, NP.Width-twipsX*6, NP.Height-tHeight1-tHeight
            With Karel_Pad, "NoWrap" as NoWrap
            Method Karel_Pad,"Resize"
      }
}
Call Local IDE_Karel.Resize()
Method IDE_Karel,"Show", 1
Declare Karel_Pad Nothing
Declare IDE_Karel Nothing


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.