Skip to content

Commit

Permalink
Update code for checking whether the symbol is defined or not
Browse files Browse the repository at this point in the history
  • Loading branch information
larionvn committed Jun 6, 2017
1 parent 783f53b commit 745a815
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions mathics/builtin/scoping.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,10 @@ class Unique(Predefined):
Create a unique symbol whose name begins with x:
>> Unique["x"]
= x2
#> $3 = 3;
#> Unique[]
= $3
= $4
#> Unique[{}]
= {}
Expand Down Expand Up @@ -262,19 +263,19 @@ class Unique(Predefined):
= Unique[1]
#> Unique[{m, "s", n}, {Flat, Listable, Orderless}]
= {m$7, s4, n$8}
= {m$7, s5, n$8}
#> Attributes[{m$7, s4, n$8}]
#> Attributes[{m$7, s5, n$8}]
= {{Flat, Listable, Orderless}, {Flat, Listable, Orderless}, {Flat, Listable, Orderless}}
#> Unique[{x, "s", 1}, {Flat ^ Listable ^ Orderless}]
: 1 is not a symbol or a valid symbol name.
= Unique[{x, s, 1}, {Flat ^ Listable ^ Orderless}]
#> Unique[{"s"}, Flat]
= {s5}
= {s6}
#> Attributes[s5]
#> Attributes[s6]
= {Flat}
"""

Expand All @@ -297,6 +298,10 @@ def apply(self, evaluation):

new_name = '$%d' % (self.seq_number)
self.seq_number += 1
#Next symbol in case of new name is defined before
while evaluation.definitions.get_definition(new_name, True) is not None:
new_name = '$%d' % (self.seq_number)
self.seq_number += 1
return Symbol(new_name)

def apply_symbol(self, vars, attributes, evaluation):
Expand Down Expand Up @@ -332,8 +337,11 @@ def apply_symbol(self, vars, attributes, evaluation):
else:
new_name = '%s%d' % (symbol.get_string_value(), self.seq_number)
self.seq_number += 1
list.append(Symbol(new_name))

# Next symbol in case of new name is defined before
while evaluation.definitions.get_definition(new_name, True) is not None:
new_name = '%s%d' % (symbol.get_string_value(), self.seq_number)
self.seq_number += 1
list.append(Symbol(new_name))
for symbol in list:
for att in attrs:
evaluation.definitions.set_attribute(symbol.get_name(), att)
Expand Down

0 comments on commit 745a815

Please sign in to comment.