forked from zhangj111/astnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_data.py
79 lines (40 loc) · 1.54 KB
/
prepare_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from pycparser import c_parser, c_ast
import pandas as pd
import os
import re
import sys
from gensim.models.word2vec import Word2Vec
import pickle
from tree import ASTNode, SingleNode
import numpy as np
def get_sequences(node, sequence):
current = SingleNode(node)
sequence.append(current.get_token())
for _, child in node.children():
get_sequences(child, sequence)
if current.get_token().lower() == 'compound':
sequence.append('End')
def get_blocks(node, block_seq):
children = node.children()
name = node.__class__.__name__
if name in ['FuncDef', 'If', 'For', 'While', 'DoWhile']:
block_seq.append(ASTNode(node))
if name is not 'For':
skip = 1
else:
skip = len(children) - 1
for i in range(skip, len(children)):
child = children[i][1]
if child.__class__.__name__ not in ['FuncDef', 'If', 'For', 'While', 'DoWhile', 'Compound']:
block_seq.append(ASTNode(child))
get_blocks(child, block_seq)
elif name is 'Compound':
block_seq.append(ASTNode(name))
for _, child in node.children():
if child.__class__.__name__ not in ['If', 'For', 'While', 'DoWhile']:
block_seq.append(ASTNode(child))
get_blocks(child, block_seq)
block_seq.append(ASTNode('End'))
else:
for _, child in node.children():
get_blocks(child, block_seq)