transpiler.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import csv
  2. # read state table csv
  3. def parse_st_csv(file_path):
  4. with open(file_path, newline='') as csvfile:
  5. reader = csv.reader(csvfile)
  6. data = [row for row in reader]
  7. state_table = []
  8. events = []
  9. actions = []
  10. section = None
  11. for row in data:
  12. if row[0] == 'States/Events':
  13. section = 'state_table'
  14. continue
  15. elif row[0] == 'Events':
  16. section = 'events'
  17. continue
  18. elif row[0] == 'Actions':
  19. section = 'actions'
  20. continue
  21. if section == 'state_table':
  22. state_table.append(row)
  23. elif section == 'events':
  24. events.append(row)
  25. elif section == 'actions':
  26. actions.append(row)
  27. return state_table, events, actions
  28. # state table transpiler
  29. def transpile_st(state_table, events, actions):
  30. # actions 0-n (starting from 0)
  31. actions = [a[0] for a in actions]
  32. # conditions for events
  33. conditions = [e[0] for e in events]
  34. # events on conditions
  35. events = [e[2:] for e in events]
  36. # state names
  37. states = [s[0] for s in state_table]
  38. # columns of state table - event_states[0] is E1
  39. # this is used because action/nextstate is choosed based on event
  40. event_states = [[erows[i] for erows in state_table] for i in range(2, len(state_table[2]))]
  41. print(event_states)
  42. state_table, events, actions = parse_st_csv('TTExample.csv')
  43. transpile_st(state_table, events, actions)