2019-12-25 21:47:56 +01:00
|
|
|
|
#!/usr/bin/env python3
|
2019-12-23 21:09:13 +01:00
|
|
|
|
# coding=utf8
|
|
|
|
|
|
2019-12-25 19:07:48 +01:00
|
|
|
|
import sys
|
2019-12-23 21:09:13 +01:00
|
|
|
|
from pprint import pprint
|
|
|
|
|
|
2019-12-25 19:07:48 +01:00
|
|
|
|
mappa_caratteri = [
|
|
|
|
|
"0", "1", "~", "2", "4", "+", "#", "3", "5", "6", "/", "7", "9", "-", "÷", "8",
|
|
|
|
|
"Φ", "A", "δ", "B", "D", "=", "α", "C", "E", "F", "%", "G", "I", ",", "γ", "H",
|
|
|
|
|
".", "S", "θ", "T", "V", "!", ")", "U", "W", "X", "?", "Y", "\'", "&", "⊗", "Z",
|
|
|
|
|
"ε", "J", "*", "K", "M", "(", "β", "L", "N", "O", "$", "η", "R", "P", "π", "Q"
|
2019-12-23 21:09:13 +01:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
caratteri_python_a_elea = {
|
2019-12-25 19:42:52 +01:00
|
|
|
|
char : idx
|
2019-12-25 19:07:48 +01:00
|
|
|
|
for idx, char in enumerate(mappa_caratteri) }
|
2019-12-23 21:09:13 +01:00
|
|
|
|
|
|
|
|
|
def stringa_python_a_elea(string):
|
|
|
|
|
s = []
|
|
|
|
|
|
2019-12-25 19:42:52 +01:00
|
|
|
|
for i in string:
|
2019-12-23 21:09:13 +01:00
|
|
|
|
if i in (' ', '\n'):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
xx = caratteri_python_a_elea.get(i, None)
|
|
|
|
|
if xx is None:
|
2019-12-25 19:07:48 +01:00
|
|
|
|
raise Exception("carattere %s non mappato" % i)
|
2019-12-23 21:09:13 +01:00
|
|
|
|
|
|
|
|
|
s.append(xx)
|
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
2019-12-25 19:07:48 +01:00
|
|
|
|
def elea_a_c(l):
|
|
|
|
|
l2 = ('%3d,' % i for i in l)
|
|
|
|
|
return ' ' + ' '.join(l2)
|
|
|
|
|
|
|
|
|
|
def stringa_elea_a_c(l):
|
|
|
|
|
e = stringa_python_a_elea(l)
|
|
|
|
|
e2 = elea_a_c(e)
|
|
|
|
|
return e2 + " /* " + l + " */"
|
|
|
|
|
|
2019-12-25 19:56:36 +01:00
|
|
|
|
PROLOGO = """/* Questo file e` generato automaticamente a partire
|
|
|
|
|
* dai sorgenti testuali (file .elea-txt).
|
|
|
|
|
* Non modificare!
|
|
|
|
|
*/
|
|
|
|
|
#include "elea_tipi.h"
|
2019-12-25 19:07:48 +01:00
|
|
|
|
|
|
|
|
|
const carattere %s[] = {
|
2019-12-23 21:09:13 +01:00
|
|
|
|
"""
|
|
|
|
|
|
2019-12-25 19:07:48 +01:00
|
|
|
|
EPILOGO = """};
|
|
|
|
|
|
|
|
|
|
const int %s_lunghezza = sizeof(%s) / sizeof(carattere);
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def txt_to_c(stringa_txt, nome):
|
|
|
|
|
x = []
|
|
|
|
|
x += (PROLOGO % nome).splitlines()
|
|
|
|
|
|
|
|
|
|
for line in stringa_txt.splitlines():
|
|
|
|
|
l2 = line.strip()
|
|
|
|
|
if len(l2) == 0: continue
|
|
|
|
|
x.append(stringa_elea_a_c(l2))
|
|
|
|
|
|
|
|
|
|
x += (EPILOGO % (nome, nome)).splitlines()
|
|
|
|
|
|
|
|
|
|
return '\n'.join(x)
|
|
|
|
|
|
|
|
|
|
def converti(nomefile):
|
|
|
|
|
nome = nomefile.split('.', 1)[0]
|
|
|
|
|
x = open(nomefile).read()
|
|
|
|
|
c = txt_to_c(x, nome)
|
|
|
|
|
open('%s.c' % nome, 'w').write(c)
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
try:
|
|
|
|
|
nomefile = sys.argv[1]
|
|
|
|
|
except:
|
2019-12-25 19:42:52 +01:00
|
|
|
|
print ("converte da txt in elea -> file c\n")
|
|
|
|
|
print ("usaggio: %s nomefile.txt")
|
2019-12-25 19:07:48 +01:00
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
converti(nomefile)
|
2019-12-23 21:09:13 +01:00
|
|
|
|
|