Added option to use english words as variable names
Took 15 minutes
This commit is contained in:
parent
d6f9a8684a
commit
1e22f5a968
|
@ -2,7 +2,7 @@ import random
|
||||||
import string
|
import string
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
# from english_words import get_english_words_set
|
from english_words import get_english_words_set
|
||||||
|
|
||||||
from constants import *
|
from constants import *
|
||||||
|
|
||||||
|
@ -38,13 +38,11 @@ class AstGenerator:
|
||||||
self.symbol_table.append(global_scope) # NOTE for debug
|
self.symbol_table.append(global_scope) # NOTE for debug
|
||||||
self.current_scope = global_scope
|
self.current_scope = global_scope
|
||||||
|
|
||||||
# names = get_english_words_set(['web2'], lower=True)
|
names = get_english_words_set(['web2'], alpha=True)
|
||||||
names = random.choices(string.ascii_letters, k=self.settings['properties']['id-length']['max'])
|
possible_names = filter(lambda x: self.settings['properties']['id-length']['max'] <= len(x) <= self.settings['properties']['id-length']['max'], names)
|
||||||
possible_names = filter(lambda x: self.settings['properties']['id-length']['min'] < len(x)
|
|
||||||
< self.settings['properties']['id-length']['max'], names)
|
|
||||||
|
|
||||||
var_name_len = len(list(possible_names))
|
|
||||||
var_name_list = list(possible_names)
|
var_name_list = list(possible_names)
|
||||||
|
var_name_len = len(var_name_list)
|
||||||
self.variable_names = var_name_list[0:var_name_len // 2]
|
self.variable_names = var_name_list[0:var_name_len // 2]
|
||||||
self.routine_names = var_name_list[var_name_len // 2:var_name_len]
|
self.routine_names = var_name_list[var_name_len // 2:var_name_len]
|
||||||
|
|
||||||
|
@ -529,10 +527,13 @@ class AstGenerator:
|
||||||
@param name_type:
|
@param name_type:
|
||||||
@return:
|
@return:
|
||||||
"""
|
"""
|
||||||
length = random.randint(self.settings['properties']['id-length']['min'],
|
if not self.settings['properties']['use-english-words']:
|
||||||
self.settings['properties']['id-length']['max'])
|
length = random.randint(self.settings['properties']['id-length']['min'],
|
||||||
name = ''.join(random.choices(string.ascii_letters, k=length))
|
self.settings['properties']['id-length']['max'])
|
||||||
return name
|
name = ''.join(random.choices(string.ascii_letters, k=length))
|
||||||
|
return name
|
||||||
|
else:
|
||||||
|
return random.choice(self.variable_names)
|
||||||
|
|
||||||
def get_op(self, type):
|
def get_op(self, type):
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ properties:
|
||||||
use-english-words: True # use english words instead of random names (this may limit the maximum number of names)
|
use-english-words: True # use english words instead of random names (this may limit the maximum number of names)
|
||||||
id-length: # length of identifiers
|
id-length: # length of identifiers
|
||||||
min: 1
|
min: 1
|
||||||
max: 10
|
max: 5
|
||||||
function-name-length: # length of function names
|
function-name-length: # length of function names
|
||||||
min: 1
|
min: 1
|
||||||
max: 10
|
max: 10
|
||||||
|
|
Loading…
Reference in a new issue