partial combination of phase0 and phase1 implemented

This commit is contained in:
Carl Beekhuizen 2019-05-16 23:52:40 +02:00
parent 306a613c8a
commit 7c8f83d5e8
No known key found for this signature in database
GPG Key ID: D05CA176D0020646
2 changed files with 24 additions and 20 deletions

View File

@ -5,12 +5,14 @@ from argparse import ArgumentParser
from typing import Tuple, List from typing import Tuple, List
def split_retain_delimiter(regex_pattern: str, text: str) -> List[str]: def split_and_label(regex_pattern: str, text: str) -> List[str]:
''' '''
Splits a string based on regex, but down not remove the matched text Splits a string based on regex, but down not remove the matched text.
It subsequently labels the matches with their match
''' '''
find_pattern = r'%s.*?(?=%s|$)' % (regex_pattern, regex_pattern) find_pattern = r'''%s.*?(?=%s|$)''' % (regex_pattern, regex_pattern)
return re.findall(find_pattern, text, re.DOTALL) matches = re.findall(find_pattern, text, re.DOTALL)
return list(map(lambda x: [re.match(regex_pattern, x).group(0), x], matches))
def inserter(oldfile: str, newfile: str) -> Tuple[str, str]: def inserter(oldfile: str, newfile: str) -> Tuple[str, str]:
@ -21,10 +23,10 @@ def inserter(oldfile: str, newfile: str) -> Tuple[str, str]:
return bar return bar
# end insert @LabelName # end insert @LabelName
''' '''
new_insert_objects = re.split(r"(# begin insert |# end insert @[a-zA-Z0-9_]*\n)", newfile) new_insert_objects = re.split(r"(# begin insert |# end insert @[\w\d_]*\n)", newfile)
# Retrieve label from insert objects # Retrieve label from insert objects
def get_labeled_object(labeled_text): def get_labeled_object(labeled_text):
label = re.match(r"@[a-zA-Z0-9_]*\n", labeled_text) label = re.match(r"@[\w\d_]*\n", labeled_text)
if label is not None: if label is not None:
label = label.group(0) label = label.group(0)
labeled_text = re.sub(label, '', labeled_text) labeled_text = re.sub(label, '', labeled_text)
@ -47,20 +49,27 @@ def merger(oldfile:str, newfile:str) -> str:
Seeks out functions and objects in new and old files. Seeks out functions and objects in new and old files.
Replaces old objects with new ones if they exist. Replaces old objects with new ones if they exist.
''' '''
old_objects = split_retain_delimiter('\n[a-zA-Z]', oldfile) object_regex = r'''(?:\n[@\w]+[\s\w]*[='" "\.\w]*)|(?:\s{4}global_vars\["\w+"\])'''
new_objects = split_retain_delimiter('\n[a-zA-Z]', newfile) ssz_object_regex = r'''(?:\w+|\s{4}global_vars\["\w+"\]) = SSZType\(\{\n'''
object_regex = r"\n[#@a-zA-Z_0-9]+[\sa-zA-Z_0-9]*[(){}=:'" "]*" old_objects = split_and_label(object_regex, oldfile)
old_object_tuples = list(map(lambda x: [re.match(object_regex, x).group(0),x], old_objects)) new_objects = split_and_label(object_regex, newfile)
for new_item in new_objects: for new_item in new_objects:
found_old = False found_old = False
for old_item in old_object_tuples: for index, old_item in enumerate(old_objects):
if old_item[0] == re.match(object_regex, new_item).group(0): if old_item[0] == new_item[0]:
old_item[1] = new_item ssz_object_match = re.match(ssz_object_regex, new_item[1])
if ssz_object_match is not None:
new_item[1] = re.sub(ssz_object_regex, '', new_item[1])
old_item[1] = re.sub(r'\n\w*\}\)', '', old_item[1])
old_item[1] += new_item[1]
else:
old_item[1] = new_item[1]
found_old = True found_old = True
old_objects[index] = old_item
break break
if not found_old: if not found_old:
old_object_tuples += [[re.match(object_regex, new_item).group(0), new_item]] old_objects.append(new_item)
return ''.join(elem for elem in map(lambda x: x[1], old_object_tuples)) return ''.join(elem for elem in map(lambda x: x[1], old_objects))
def build_phase0_spec(sourcefile, outfile=None): def build_phase0_spec(sourcefile, outfile=None):

View File

@ -64,11 +64,6 @@ def get_spec(file_name: str) -> List[str]:
if len(type_line) > 0: if len(type_line) > 0:
code_lines.append(' ' + type_line) code_lines.append(' ' + type_line)
code_lines.append('\n') code_lines.append('\n')
code_lines.append('ssz_types = [\n')
for (ssz_type_name, _) in type_defs:
code_lines.append(f' {ssz_type_name},\n')
code_lines.append(']')
code_lines.append('\n')
code_lines.append('def get_ssz_type_by_name(name: str) -> SSZType:') code_lines.append('def get_ssz_type_by_name(name: str) -> SSZType:')
code_lines.append(' return globals()[name]') code_lines.append(' return globals()[name]')
code_lines.append('') code_lines.append('')