示例#1
0
文件: struct.py 项目: C8E/nylo
 def transpile(self, mesh, path):
     self.path = path
     mesh[path + (Keyword('self'), )] = Value(repr(self))
     for key, value in self.value.items():
         if key == Keyword('atoms'):
             for i, el in enumerate(value):
                 if isinstance(el, TypeDef):
                     el = el.value[-1]
                 if isinstance(el, Keyword):
                     arg = path + (el.value, )
                     mesh[arg] = Keyword('placeholder')
                     mesh['arguments'][path].append(arg)
                 else:
                     mesh[path + (Value(i), )] = el
         if isinstance(key, TypeDef):
             key = key.value[-1]
         if isinstance(key, Keyword):
             mesh[path + (key.value, )] = Keyword('placeholder')
     for key, value in self.value.items():
         if isinstance(key, TypeDef):
             key = key.value[-1]
         newpath = path + (key, )
         if len(value) == 1:
             value[0].transpile(mesh, newpath)
             mesh[newpath] = value[0]
         else:
             for i, vl in enumerate(value):
                 vl.transpile(mesh, newpath)
                 mesh[newpath] = vl
示例#2
0
文件: struct.py 项目: C8E/nylo
 def transpile_call(self, mesh, path, called):
     for key, value in (
         [*self.value.items()] +
         [*zip(mesh['arguments'][called], self.value[Keyword('atoms')])]):
         if isinstance(key, TypeDef):
             key = key.value[-1]
         if key == Keyword('atoms'):
             continue
         if isinstance(key, Keyword):
             if key == Keyword('self'):
                 continue
             key.transpile(mesh, called)
             key = key.ref
             value = value[0]
         key = path + key[len(called):]
         value.transpile(mesh, path)
         mesh[key] = value
     mesh['arguments'][path] = \
         mesh['arguments'][called][len(self.value[Keyword('atoms')]):]
示例#3
0
文件: struct.py 项目: C8E/nylo
 def parse(self, parser):
     if parser.starts_with(':'):
         self.key = parser.getarg()
     elif not parser.starts_with('('):
         self.value[self.key].append(parser.getarg())
         if self.value[self.key][-1] is None:
             self.value[self.key].pop()
         self.key = Keyword('atoms')
     if parser.any_starts_with(('(', ':', ',')):
         parser.move()
         parser.parse(self, Value())
     elif parser.starts_with(')'):
         parser.move()
         if (len(self.value) == 1
                 and len(self.value[Keyword('atoms')]) == 1):
             return parser.hasparsed(self.value[Keyword('atoms')][0])
         parser.hasparsed(self)
     elif parser.starts_with('->'):
         parser.move(2)
         parser.parse(self, Value())
         self.key = Keyword('self')
     else:
         parser.parse(self, Value())
示例#4
0
class Call(Token):
    def __init__(self, called=None, caller=None):
        self.called, self.caller = called, caller

    def parse(self, parser):
        from nylo.tokens.struct import Struct
        if self.called:
            self.caller = parser.getarg()
            parser.hasparsed(self)
            parser.parse(Call())
        else:
            if not parser.starts_with('('):
                return
            self.called = parser.getarg()
            parser.parse(self, Struct())

    def __repr__(self):
        return f'{self.called}{self.caller}'

    def transpile(self, mesh, path):
        from nylo.tokens.struct import Struct
        if isinstance(self.called, TypeDef):
            self.called = self.called.value[-1]
        if isinstance(self.called, Keyword):
            self.called.transpile(mesh, path)
            called_path = self.called.ref
        else:
            self.called.transpile(mesh, path + ('temp', ))
            mesh[path + ('temp', )] = self.called
            called_path = path + ('temp', )
        if not isinstance(self.caller, Struct):
            self.caller = Struct(
                defaultdict(list, {Keyword('atoms'): [self.caller]}))
        self.caller.transpile_call(mesh, path, called_path)
        mesh['classes'][path] = called_path
        if Keyword('self') in self.caller.value:
            self.caller.value[Keyword('self')][0].transpile(mesh, called_path)
            mesh[path+(Keyword('self'),)] = \
                self.caller.value[Keyword('self')][0].chroot(path, called_path)
        self.toev = Keyword('self', path + (Keyword('self'), ))

    def interprete(self, mesh, interpreting, interpreted):
        interpreting.append(self.toev)

    def evaluate(self, mesh, interpreting, interpreted):
        interpreted.append(self)

    def chroot(self, oldroot, newroot):
        return self.toev.chroot(oldroot, newroot)
示例#5
0
 def transpile(self, mesh, path):
     from nylo.tokens.struct import Struct
     if isinstance(self.called, TypeDef):
         self.called = self.called.value[-1]
     if isinstance(self.called, Keyword):
         self.called.transpile(mesh, path)
         called_path = self.called.ref
     else:
         self.called.transpile(mesh, path + ('temp', ))
         mesh[path + ('temp', )] = self.called
         called_path = path + ('temp', )
     if not isinstance(self.caller, Struct):
         self.caller = Struct(
             defaultdict(list, {Keyword('atoms'): [self.caller]}))
     self.caller.transpile_call(mesh, path, called_path)
     mesh['classes'][path] = called_path
     if Keyword('self') in self.caller.value:
         self.caller.value[Keyword('self')][0].transpile(mesh, called_path)
         mesh[path+(Keyword('self'),)] = \
             self.caller.value[Keyword('self')][0].chroot(path, called_path)
     self.toev = Keyword('self', path + (Keyword('self'), ))
示例#6
0
文件: struct.py 项目: C8E/nylo
 def interprete(self, mesh, interpreting, interpreted):
     interpreting.append(Keyword('self', self.path + (Keyword('self'), )))
示例#7
0
文件: struct.py 项目: C8E/nylo
 def __init__(self, value=None):
     self.value = value if value else defaultdict(list)
     self.key = Keyword('atoms')
示例#8
0
 def evaluate(self, mesh, interpreting, interpreted):
     for i, el in enumerate(self.gets):
         if el in self.toev:
             self.gets[i] = interpreted.pop()
     interpreting.append(Keyword('get',
                                 self.keyword.ref + tuple(self.gets)))
示例#9
0
 def interprete(self, mesh, interpreting, interpreted):
     if self.path + (Keyword('self'), ) in mesh:
         return interpreting.append(
             Keyword('self', self.path + (Keyword('self'), )))
     interpreting.append(self)