Jump to content

LOGO Interpreter


James
 Share

Recommended Posts

Hey,

This is just a start with writing a LOGO interpreter. Commands, FORWARD, CLEAR, COLOR, SIZE, TURN, WAIT, PENDOWN and PENUP.

Posted Image

So code:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $arrDic[9] = ["FORWARD", "COLOR", "CLEAR", "WAIT", "DO", "SIZE", "TURN", "PENUP", "PENDOWN"]
Global $arrCols[4][2] = [["BLACK", "0x000000"],["RED", "0xFF0000"],["BLUE", "0x0000FF"],["GREEN", "0x00FF00"]]
Global $turtle = @ScriptDir & '\turtle.gif', $Count = 0, $picDraw
Global Const $Pi = 3.14159265358979323846264338328
Dim $oldX = 228, $oldY = 300, $oldA = 90, $penColor = 0x000000, $penSize = 1, $penDown = 0 ; 0 is down and 1 is up
Global Const $degToRad = $Pi / 180

$guiMain = GUICreate("A3 LOGO Interpreter", 474, 445)
GUICtrlCreateGroup("", 8, 336, 457, 105)
$strCmd = GUICtrlCreateEdit("", 16, 352, 377, 81, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL))
$btnRun = GUICtrlCreateButton("&Run", 400, 352, 57, 81, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)

$picDraw = GUICtrlCreateGraphic(8, 8, 457, 330)
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlSetGraphic(-1, $GUI_GR_MOVE, $oldX, $oldY)
$picTurt = GUICtrlCreatePic($turtle, 228, 300, 20, 24)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnRun
            Command()
    EndSwitch
WEnd

Func Turtle($x, $y, $color = $penColor, $size = $penSize, $isDown = $penDown)
    If $isDown = 0 Then
        GUICtrlSetPos($picTurt, $x, $y)
        GUICtrlSetGraphic($picDraw, $GUI_GR_MOVE, $oldX, $oldY)
        GUICtrlSetGraphic($picDraw, $GUI_GR_PENSIZE, $size)
        GUICtrlSetGraphic($picDraw, $GUI_GR_COLOR, $color)
        GUICtrlSetGraphic($picDraw, $GUI_GR_LINE, $x, $y)
        GUICtrlSetGraphic($picDraw, $GUI_GR_REFRESH)
        $oldY = $y
        $oldX = $x
    Else
        GUICtrlSetPos($picTurt, $x, $y)
        $oldY = $y
        $oldX = $x
    EndIf
EndFunc   ;==>Turtle

Func Command()
    $Line = StringSplit(StringStripCR(GUICtrlRead($strCmd)), @LF)
    For $i = 1 To $Line[0]
        If $Line[$i] = "" Then
            ContinueLoop
        EndIf

        If StringInStr($Line[$i], " ") Then
            $ThisThing = StringLeft($Line[$i], StringInStr($Line[$i], " ") - 1)
        Else
            $ThisThing = $Line[$i]
        EndIf

        Switch StringUpper($ThisThing)
            Case $arrDic[0] ; FORWARD
                $ThisY = StringSplit($Line[$i], " ")
                If IsArray($ThisY) Then Turtle($oldX + ( Cos($oldA * $degToRad) * $ThisY[2]) , $oldY - ( Sin($oldA * $degToRad) * $ThisY[2]))
            Case $arrDic[1] ; COLOR
                $getPen = StringSplit($Line[$i], " ")
                If IsArray($getPen) Then $penColor = $getPen[2]
            Case $arrDic[2] ; CLEAR
                ClearGraphic()
            Case $arrDic[3] ; WAIT
                $Time = StringSplit($Line[$i], " ")
                If IsArray($Time) Then Sleep($Time[2])
            Case $arrDic[4] ; DO (Loop)
                $doCount = 0
                $ThisLoop = StringSplit($Line[$i], " ")
                If Not IsNumber($ThisLoop[2]) Then
                    MsgBox(0, "LOGO", "Error: DO loop requires a number.")
                Else
                    $RegExp = StringRegExp($Line[$i], "\[.*?\]", 3)
                    If @error Then
                        MsgBox(0, "LOGO", "Error: Malformed DO loop.")
                    Else
                        For $j = 0 To $RegExp[0]
                            ConsoleWrite("~> DEBUG: " & $RegExp[$j] & @CRLF)
                        Next
                        Do
                            ConsoleWrite("Count: #" & $doCount & @CRLF) ; DEBUG
                            $doCount = $doCount + 1 ; Increment the DD count
                        Until $doCount = $ThisLoop[2]
                    EndIf
                EndIf
            Case $arrDic[5] ; SIZE
                $getSize = StringSplit($Line[$i], " ")
                If IsArray($getSize) Then $penSize = $getSize[2]
            Case $arrDic[6] ; TURN
                $ThisTurn = StringSplit($Line[$i], " ")
                If Not IsArray($ThisTurn) Then
                    MsgBox(0, "LOGO", "Error: TURN requires a degree.")
                Else
                    
                    $oldA += Number($ThisTurn[2])
                    While ($oldA > 360)
                        $oldA -= 360
                    WEnd
                    While ($oldA < 0)
                        $oldA += 360
                    WEnd
                    
                    ;Turtle($oldX - $ThisTurn[2], $oldY)
                EndIf
            Case $arrDic[7] ; PENUP
                $penDown = 1
            Case $arrDic[8] ; PENDOWN
                $penDown = 0
            Case Else
                MsgBox(0, "LOGO", "Error: Unexpected command: " & $ThisThing)
        EndSwitch
    Next
EndFunc   ;==>Command

Func ClearGraphic()
    For $INDEX = 0 To $Count
        GUICtrlDelete($picDraw)
        GUICtrlCreateGraphic(8, 8, 457, 330)
        GUICtrlSetBkColor(-1, 0xFFFFFF)
        GUICtrlSetGraphic(-1, $GUI_GR_MOVE, $oldX, $oldY)
        $oldX = 228
        $oldY = 300
        $oldA = 90
        Turtle(228, 300)
    Next
    $Count = 0
EndFunc   ;==>ClearGraphic

And turtle:

Posted Image

This code also contains my start at the DO loop.

Have fun and improvements welcome :)

James

Edit: What the hell happened to

boxes?

Edit 2: Fixed the problem with not drawing after second command. Thanks Andreik.

Edit 3: Added COLOR command

Edit 4: Added WAIT command, Andreik.

Edited by JamesBrooks
Link to comment
Share on other sites

  • Replies 74
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

A suggestion: before clear the graphic maybe you want to wait a little time

Global $arrDic[5] = ["FORWARD", "RIGHT", "REPEAT", "CLEAR","WAIT"]

Case $arrDic[4]
                $TIME = StringSplit($Line[$i]," ")
                If IsArray($TIME) Then
                    Sleep($TIME[2])
                EndIf

When the words fail... music speaks.

Link to comment
Share on other sites

A suggestion: before clear the graphic maybe you want to wait a little time

Global $arrDic[5] = ["FORWARD", "RIGHT", "REPEAT", "CLEAR","WAIT"]

Case $arrDic[4]
                 $TIME = StringSplit($Line[$i]," ")
                 If IsArray($TIME) Then
                     Sleep($TIME[2])
                 EndIf
Updated, first post :0
Link to comment
Share on other sites

@JamesBrooks

When I try to enter anyvalid commands I have array error like this one :

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
Turtle($oldX, $oldY - $ThisY[2])
Turtle($oldX, $oldY - ^ ERROR
Link to comment
Share on other sites

$arrDic[4] :)

Oops my bad :lmao:

@JamesBrooks

When I try to enter anyvalid commands I have array error like this one :

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
 Turtle($oldX, $oldY - $ThisY[2])
 Turtle($oldX, $oldY - ^ ERROR
Are you using the latest?
Link to comment
Share on other sites

Should FORWARD allow X and Y so this Case would be

Case $arrDic[0]

                    $ThisXY = StringSplit($Line[$i], " ")

                    Turtle($oldX + $ThisXY[2], $oldY - $ThisXY[3])

and maybe you need a bit more checking for the correct number of parameters.

What is the problem with Code boxes?

Would be nice if you could have options like clear to start a new drawing because if you want to build up a complicated drawing you would want to try a bit then add a bit and try that, so you need to be able to clear everything. Also an option to save the instructions, and allow FORWARD x,y as well as FORWARD x y maybe?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Should FORWARD allow X and Y so this Case would be

Case $arrDic[0]
 
                     $ThisXY = StringSplit($Line[$i], " ")
 
                     Turtle($oldX + $ThisXY[2], $oldY - $ThisXY[3])

and maybe you need a bit more checking for the correct number of parameters.

What is the problem with Code boxes?

Would be nice if you could have options like clear to start a new drawing because if you want to build up a complicated drawing you would want to try a bit then add a bit and try that, so you need to be able to clear everything. Also an option to save the instructions, and allow FORWARD x,y as well as FORWARD x y maybe?

FORWARD only accepts a Y parameter as later TURN will be the X :)

Checking is not done yet, I want to make sure everything works first.

There is a CLEAR command which will redraw the "canvas". Save instructions... Like a history type thing? Sounds good, will come when commands are done.

Link to comment
Share on other sites

FORWARD only accepts a Y parameter as later TURN will be the X :)

Checking is not done yet, I want to make sure everything works first.

There is a CLEAR command which will redraw the "canvas". Save instructions... Like a history type thing? Sounds good, will come when commands are done.

Ah thanks I somehow missed the CLEAR.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...