Bdanilko / EdPy

A compiler for a subset of python2 to be used on the Microbric Edison robot
GNU General Public License v2.0
18 stars 12 forks source link

Ed constants cannot be used in list initialization #3

Open PSLLSP opened 5 years ago

PSLLSP commented 5 years ago

I cannot use constants from Ed module to initialize a list. I think that Ed.NOTE_C_7 is just a constant, is't it? There is a commented line that generates syntax error and more workaround follows.

list in EdPy is just a fixed size array of integers. Is it allocated during compile time? If it is allocated by compiler, why I can do buffer overflow (opera[5] = Ed.NOTE_C_8). This line is real troublemaker...

#-------------Setup----------------

import Ed

Ed.EdisonVersion = Ed.V2

Ed.DistanceUnits = Ed.CM
Ed.Tempo = Ed.TEMPO_MEDIUM

#--------Your code below-----------

# SYNTAX ERROR: LIST INIT code too complex for EdPy :-(
# opera = Ed.List(5, [Ed.NOTE_C_7, Ed.NOTE_D_7, Ed.NOTE_E_7, Ed.NOTE_G_7, Ed.NOTE_A_7])

opera = Ed.List(5)
opera[0] = Ed.NOTE_C_7
opera[1] = Ed.NOTE_D_7
opera[2] = Ed.NOTE_E_7
opera[3] = Ed.NOTE_G_7
opera[4] = Ed.NOTE_A_7
opera[5] = Ed.NOTE_C_8  # This is a buffer overflow, not found by compiler!

# this part doesn't work well when opera[5] is defined. Bug that is difficult to find... :-(
for note in opera:
    Ed.PlayTone(note, Ed.NOTE_HALF)
    while Ed.ReadMusicEnd() == Ed.MUSIC_NOT_FINISHED: pass
    Ed.TimeWait(100, Ed.TIME_MILLISECONDS)

# this works ok
Ed.PlayBeep()
waitMusicEnd()
Ed.TimeWait(100, Ed.TIME_MILLISECONDS)

playNote(Ed.NOTE_C_7)
playNote(Ed.NOTE_D_7)
playNote(Ed.NOTE_E_7)
playNote(Ed.NOTE_G_7)
playNote(Ed.NOTE_A_7)

def playNote(note):
    Ed.PlayTone(note, Ed.NOTE_HALF)
    #while Ed.ReadMusicEnd() == Ed.MUSIC_NOT_FINISHED: pass
    waitMusicEnd()
    Ed.TimeWait(100, Ed.TIME_MILLISECONDS)

def waitMusicEnd():
    while Ed.ReadMusicEnd() == Ed.MUSIC_NOT_FINISHED: pass
Bdanilko commented 5 years ago

Hi, I haven't been in the code much for the last couple of years but here is what I think from memory:

I'll look at the compiler check on constants, but it is lower on my list because of other commitments. If you have a pull request I'm happy to review that for you.

Glad that you are taking Edision and EdPy out for a run!