
# BABA Program
# Converts a string of input characters  
# to a string of A's and B's 
# using the following mapping
# 1 beeper = A, 2 beepers = B, 3 beepers means stop
# no gaps are required between the encoding.

# THIS EXAMPLE CAN BE RUN IN 
# PRIMITIVE, STANDARD or AUXILIARY MODE.

# EXERCISE: modify this code to
# print C's and D's as well as A's and B's.

# NOTE: the intent is NOT for you to 
# memorize this code, or learn it
# in detail. Rather concepts of 
# abstraction, representation etc.
# will be illustrated in class using this. 

define main {
    # move to the lower right corner of the screen
    # the input representation starts here

    Home

    # create a marker at the 
    # beginning to indicate where 
    # the next letter should go 
    # this marker will get moved as needed
    turn_north
    move
    create_beeper

    # go back to the corner and start translating
    Home
    Translate
}


# the next 4 procedures turn 
# BB to a particular direction
define turn_north {
    while (not_facing_north) {
        turn_left
    }
}

define turn_west {
    while (not_facing_west) {
        turn_left
    }
}

define turn_east {
    while (not_facing_east) {
        turn_left
    }
}

define turn_south {
    while (not_facing_south) {
        turn_left
    }
}

# the following simply moves 
# BB to the lower left corner
# of its world 
define Home {
    turn_west
    while(front_is_clear) {
        move
    }
    turn_south
    while(front_is_clear) {
        move
    }
}

# we use a marker to locate 
# where the next output goes.
define LocateNextOut {
    Home
    turn_north
    move
    turn_east
    # move east until marker found
    while (not_next_to_a_beeper) {
        move
    }
    # destroy the marker
    destroy_beeper
    turn_north
    move
}

# find each input, and display the corresponding
# character 
define Translate {
    # the while loop here 
    # is basically a do forever
    # as we use turn_off in the middle if 
    # end-of-input is reached
    while (next_to_a_beeper) {
        destroy_beeper
        if (not_next_to_a_beeper) {
            # if only one beeper 
            # then we want an A
            printA
        } else {
            destroy_beeper
            if (not_next_to_a_beeper) {
                # if two beepers then we want B
                printB
            # MODIFY HERE to print C, D, ...
            } else {
                #  end of input marker
                #   -- stop the program
                turn_off
            }
        }
        SetNextMark

        # find the next input character.
        Home
        turn_east
        while (not_next_to_a_beeper) {
            move
        }    
    }
}

define SetNextMark {
    turn_south
    while (front_is_clear) {
        move
    }
    turn_north
    move
    turn_east
    move
    move
    create_beeper
}


# each character has its own routine for layout

define printA {
    LocateNextOut
    # the setspeed is just so 
    # the characters can be drawn quickly
    set_speed(3)
    create_beeper
    move
    create_beeper
    move
    create_beeper
    move
    create_beeper
    move
    turn_east
    move
    create_beeper
    turn_south
    move
    move
    create_beeper
    move
    move
    turn_east
    move
    create_beeper
    turn_north
    move
    create_beeper
    move
    create_beeper
    move
    create_beeper
    restore_speed
}

define printB {
    LocateNextOut
    set_speed(3)
    create_beeper
    move
    create_beeper
    move
    create_beeper
    move
    create_beeper
    move
    create_beeper
    move
    turn_east
    move
    turn_south
    move
    create_beeper
    move
    move
    create_beeper
    move
    move
    create_beeper
    turn_east
    move
    turn_north
    move
    create_beeper
    move
    move
    create_beeper
    restore_speed
}

    
    
