kostacivos/kostacivos/visit_superpotente.py

122 lines
5.8 KiB
Python

import ast
import re
from Note import nota
from Armonia import armonia
import struttura
store = struttura()
# dictionary che raccoglie tutti i tipi che vengono parserati da ast e ne restituisce le funzioni che quel tipo deve eseguire
# tuple, contengono funzioni: cercare in prondita' o caricando valori foglia dell'albero, chiare le funzioni di traduttore musicale
typeOfNode = {
'ast.Num' : ('node.n',),
'ast.Str' : ('node.s',),
'ast.FormattedValues' : ('node.value',),
'ast.JoinedStr' : ('node.values',),
'ast.Bytes' : ('node.s',),
'ast.List' : ('node.elts',),
'ast.Tuple' : ('node.elts',),
'ast.Set' : ('node.elts',),
'ast.Dict' : ('node.keys',),
'ast.NameConstant' : ('node.value',),
'ast.Name' : ('node.id',),
'ast.Starred' : ('node.value',),
'ast.Expr' : ('node.value',),
'ast.UnaryOP' : ('node.operand',),
'ast.BinOp' : ('[node.left, node.right]',),
'ast.BoolOP' : ('[node.op, node.values]',),
'ast.Compare' : ('[node.left, node.ops, node.comparators]',),
'ast.Call' : ('[node.func, node.args, node.keywords]',),
'ast.keyword' : ('[node.arg, node.value]',),
'ast.ifExp' : ('[node.test, node.body, node.orelse]',),
'ast.Attribute' : ('[node.attr, node.value]',),
'ast.Subscript' : ('[node.slice, node.value]',),
'ast.Index' : ('node.value',),
'ast.Slice' : ('[node.lower, node.upper, node.step]',),
'ast.ExtSlice' : ('node.dims',),
'ast.ListCompe' : ('[node.generators, node.elt]',),
'ast.SetCompe' : ('[node.generators, node.elt]',),
'ast.GeneratorExp' : ('[node.generators, node.elt]',),
'ast.DictComp' : ('[node.key, node.value, node.generator]',),
'ast.comprehension' : ('[node.target, node.iter, node.ifs]',),
'ast.Assign' : ('[node.targets, node.value]',),
'ast.AnnAssign' : ('[node.targets, node.target, node.value, node.simple]',),
'ast.AugAssign' : ('[node.targets, node.op, node.value]',),
'ast.Print' : ('[node.dest, node.values]',),
'ast.Raise' : ('[node.exc, node.cause]',),
'ast.Assert' : ('[node.test, node.msg]',),
'ast.Delete' : ('node.targets',),
'ast.Import' : ('node.names',),
'ast.ImportFrom' : ('[node.module, node.names, node.level]',),
'ast.Alias' : ('node.name',),
'ast.If' : ('[node.test, node.body, node.orelse]',),
'ast.For' : ('[node.target, node.iter, node.body, node.orelse]',),
'ast.While' : ('[node.test, node.body, node.orelse]',),
'ast.Try' : ('[node.handlers, node.body, node.orelse, node.finalbody]',),
'ast.TryFinally' : ('[node.body, node.finalbody]',),
'ast.TryExcept' : ('[node.handlers, node.body, node.orelse]',),
'ast.ExceptHandler' : ('[node.type, node.name, node.body]',),
'ast.With' : ('[node.items, node.body]',),
'ast.withitem' : ('[node.context_expr]',),
'ast.FunctionDef' : ('[node.name, node.args, node.body, node.decorator_list]',),
'ast.Lambda' : ('[node.args, node.body]',),
'ast.arguments' : ('[node.args, node.default]',),
'ast.arg' : ('[node.arg, node.annotation]',),
'ast.Return' : ('node.value',),
'ast.Yield' : ('node.value',),
'ast.YieldFrom' : ('node.value',),
'ast.Global' : ('node.names',),
'ast.Nonlocal' : ('node.names',),
'ast.ClassDef' : ('[node.name, node.bases, node.keywords, node.starargs, node.body, , node.kwargs, node.decorator_list]','armonia(store.get_nota())'),
'ast.Await' : ('node.value',),
'ast.AsyncFunctionDef' : ('[node.name, node.args, node.body, node.decorator_list, node.returns]',),
'ast.AsyncFor' : ('[node.target, node.iter, node.body, node.orelse]',),
'ast.AsyncWith' : ('[node.items, node.body]',),
'ast.Module' : ('node.body', 'nota()'),
}
# visitatore padre che cerca funzioni di tipo nodo
class v(ast.NodeVisitor):
def generic_visit(self, node):
print (type(node).__name__)
ast.NodeVisitor.generic_visit(self, node)
# visitatore figlio, dato un nodo percorre tutto l'albero
# esegue anche le funzioni di traduzione assegnate ad ogni nodo
class z(v):
import ast
def visit(self, node):
a = 0
funzione = 0
for i in typeOfNode:
if re.match(i, str(type(node))[9:]):
print (type(node), typeOfNode.get(i))
locals()
a = eval(typeOfNode.get(i)[0], locals())
try:
funzione = eval(typeOfNode.get(i)[1], locals(), globals())
print (funzione)
except: pass
l = z()
print (typeOfNode.get(i)[0]+'\n')
try:
print (type(a))
for i in a:
if type(i) == list:
for j in i:
l.visit(j)
else :
l.visit(i)
except:
try:
if re.match('ast', str(type(a))[9:]):
l.visit(a)
else:
print(a)
except: print ('---------------------> Pass'),
return a
a = z()
g = a.visit(tree01)