Skip to content

Commit

Permalink
Merge pull request #407 from EvgSkv/ti2023
Browse files Browse the repository at this point in the history
Disallow user variables to collide with internal ones.
  • Loading branch information
EvgSkv authored Nov 19, 2024
2 parents 3ec980b + 105948d commit 24e2fb8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
9 changes: 7 additions & 2 deletions compiler/rule_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def __init__(self, names_allocator=None, external_vocabulary=None,
self.tables = collections.OrderedDict()
# Table variable to clause variable map.
self.vars_map = {}
self.vars_heritage_map = {}
# Clause variable to one table variable.
self.inv_vars_map = {}
self.vars_unification = []
Expand Down Expand Up @@ -597,10 +598,12 @@ def ExtractPredicateStructure(c, s):
expr = field_value['value']['expression']
var_name = s.allocator.AllocateVar('%s_%s' % (table_name, table_var))
s.vars_map[table_name, table_var] = var_name
s.vars_heritage_map[table_name, table_var] = expr.get('expression_heritage')
s.inv_vars_map[var_name] = (table_name, table_var)
s.vars_unification.append(
{
'left': {'variable': {'var_name': var_name}},
'left': {'variable': {'var_name': var_name},
'expression_heritage': expr.get('expression_heritage')},
'right': expr
})

Expand Down Expand Up @@ -754,7 +757,9 @@ def InlinePredicateValuesRecursively(r, names_allocator, conjuncts):
r_predicate['predicate'] = copy.deepcopy(r['call'])
r_predicate['predicate']['record']['field_value'].append({
'field': 'logica_value',
'value': {'expression': {'variable': {'var_name': aux_var}}}
'value': {'expression': {
'variable': {'var_name': aux_var},
'expression_heritage': r['expression_heritage']}}
})
del r['call']
r['variable'] = {'var_name': aux_var}
Expand Down
4 changes: 4 additions & 0 deletions parser_py/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@ def ParseRecordInternals(s,
def ParseVariable(s: HeritageAwareString):
if (s and s[0] in set(string.ascii_lowercase) | set('_') and
set(s) <= VARIABLE_CHARS_SET):
if s.startswith('x_'):
raise ParsingException('Variables starting with >>x_<< are reserved '
'to be Logica compiler internal. Please use '
'a different name.', s)
return {'var_name': s}


Expand Down
8 changes: 6 additions & 2 deletions type_inference/research/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,13 @@ def BuildQuazyBody(self):
# This is from unnestings. I don't recall why are those even here :-(
# Need to clarify.
continue
# TODO: Location can be unknown due to injections. We should fix it.
heritage = self.structure.vars_heritage_map.get((table_id, field),
'UNKNOWN_LOCATION')
calls[table_id]['predicate']['record']['field_value'].append(
{'field': field,
'value': {'expression': {'variable': {'var_name': variable}}}
'value': {'expression': {'variable': {'var_name': variable},
'expression_heritage': heritage}}
}
)

Expand Down Expand Up @@ -583,7 +587,7 @@ class TypeErrorChecker:
def __init__(self, typed_rules):
self.typed_rules = typed_rules

def CheckForError(self, mode='print'):
def CheckForError(self, mode='print'):
self.found_error = self.SearchTypeErrors()
if self.found_error.type_error:
if mode == 'print':
Expand Down

0 comments on commit 24e2fb8

Please sign in to comment.