Handle new parser nodes

master
Salvo 'LtWorf' Tomaselli 2020-08-26 17:16:14 +07:00
parent 1181042ee0
commit a727c51e75
No known key found for this signature in database
GPG Key ID: B3A7CF0C801886CF
1 changed files with 15 additions and 16 deletions

@ -1,5 +1,5 @@
# Relational # Relational
# Copyright (C) 2016 Salvo "LtWorf" Tomaselli # Copyright (C) 2016-2020 Salvo "LtWorf" Tomaselli
# #
# Relational is free software: you can redistribute it and/or modify # Relational is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -18,14 +18,15 @@
# #
# This module splits a query into a program. # This module splits a query into a program.
from typing import List, Dict, Tuple
from relational import parser from relational.parser import Node, Binary, Unary, Variable
class Program: class Program:
def __init__(self, rels): def __init__(self, rels) -> None:
self.queries = [] self.queries: List[Tuple[str, Node]] = []
self.dictionary = {} # Key is the query, value is the relation self.dictionary: Dict[str, Node] = {} # Key is the query, value is the relation
self.vgen = vargen(rels, 'optm_') self.vgen = vargen(rels, 'optm_')
def __str__(self): def __str__(self):
@ -34,7 +35,7 @@ class Program:
r += '%s = %s' % (q[0], q[1]) + '\n' r += '%s = %s' % (q[0], q[1]) + '\n'
return r.rstrip() return r.rstrip()
def append_query(self, node): def append_query(self, node: Node) -> Node:
strnode = str(node) strnode = str(node)
rel = self.dictionary.get(strnode) rel = self.dictionary.get(strnode)
@ -43,29 +44,27 @@ class Program:
qname = next(self.vgen) qname = next(self.vgen)
self.queries.append((qname, node)) self.queries.append((qname, node))
n = parser.Node() n = Variable(qname)
n.kind = parser.RELATION
n.name = qname
self.dictionary[strnode] = n self.dictionary[strnode] = n
return n return n
def _separate(node, program): def _separate(node: Node, program: Program) -> None:
if node.kind == parser.UNARY and node.child.kind != parser.RELATION: if isinstance(node, Unary) and isinstance(node.child, Variable):
_separate(node.child, program) _separate(node.child, program)
rel = program.append_query(node.child) rel = program.append_query(node.child)
node.child = rel node.child = rel
elif node.kind == parser.BINARY: elif isinstance(node, Binary):
if node.left.kind != parser.RELATION: if not isinstance(node.left, Variable):
_separate(node.left, program) _separate(node.left, program)
rel = program.append_query(node.left) rel = program.append_query(node.left)
node.left = rel node.left = rel
if node.right.kind != parser.RELATION: if not isinstance(node.right, Variable):
_separate(node.right, program) _separate(node.right, program)
rel = program.append_query(node.right) rel = program.append_query(node.right)
node.right = rel node.right = rel
program.append_query(node) program.append_query(node)
def vargen(avoid, prefix=''): def vargen(avoid: str, prefix: str=''):
''' '''
Generates temp variables. Generates temp variables.
@ -87,7 +86,7 @@ def vargen(avoid, prefix=''):
yield r yield r
count += 1 count += 1
def split(node, rels): def split(node, rels) -> str:
''' '''
Split a query into a program. Split a query into a program.