Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def is_variable(e):
return isinstance(e,Variable)
rpn_stack.append((tok,val,0))
elif tok == 'IDENTIFIER':
paramOrVariable = self.symtable.get(val, None)
if not paramOrVariable: raise SyntaxError("\"%s\"\n\tUnknown identifier \"%s\"." % (s, val))
# keep track of identifiers we've used (this is in the
# original problem). since we introduce new variables, i
# don't want them "shown" at the end. this is just a separate
# data structure to keep track of variables referened in the
# original problem.
self.used_syms.add(val)
# keep track of minimal variable and param references (in the rewritten problem)
if isinstance(paramOrVariable,Variable):
self.codegen.variables[val] = paramOrVariable
else:
self.codegen.parameters[val] = paramOrVariable
rpn_stack.append( (tok,paramOrVariable,0) )
elif tok == 'MACRO':
# functions have highest precedence
is_function_call = True
op_stack.append((tok,val,0))
argcount_stack.append(0)
elif tok == 'COMMA':
# same as semicolon. TODO: merge the two
op = ""
while op != 'LPAREN':
if op_stack:
op,sym,arg = op_stack[-1] # peek at top
def expand(self, x):
"""Expands parameters and constants"""
if isinstance(x,Variable):
# add to varlist
self.varlist[x.name] = x
return x
elif isinstance(x,Expression):
return x
else:
if x.shape is MATRIX:
raise Exception("Cannot expand %s since it is a matrix." % x.name)
v = self.new_var(x.shape)
m = v.name
if isinstance(x,Constant):
row = [(Eye(1),v), (Ones(1,x.value),)]
elif isinstance(x,Parameter):
row = [(Eye(m),v), (Vector(x),)]
def new_var(self,shape):
"""Creates a new, temporary variable"""
name = 't' + str(self.varcount)
self.varcount += 1
v = Variable(name, shape)
self.tmplist[name] = v
return v
elif v in self.symtable:
raise Exception("\"%(s)s\"\n\tThe name %(v)s is already in use for a parameter / variable." % locals())
shape = 'SCALAR'
# optionally parse shape
if toks:
shape, tmp = toks.popleft()
if shape != 'VECTOR' and shape != 'SCALAR':
raise SyntaxError("\"%(s)s\"\n\tExpected a VECTOR or SCALAR shape, but got %(tmp)s instead." % locals())
# if any remaining
if toks:
t, tmp = toks.popleft()
raise SyntaxError("\"%(s)s\"\n\tUnexpected ending for variables with %(t)s token %(tmp)s." % locals())
self.symtable[v] = Variable(v,build_shape[shape](v))
else:
raise SyntaxError("\"%(s)s\"\n\tNo variable name provided." % locals())
t, v = toks.popleft()
if t is not "IDENTIFIER":
raise SyntaxError("\"%(s)s\"\n\tExpected an identifier, but got %(t)s with value %(v)s instead." % locals())
elif v in self.symtable:
raise Exception("\"%(s)s\"\n\tThe name %(v)s is already in use for a parameter / variable." % locals())
shape, tmp = toks.popleft()
if shape is not "VECTOR" and shape is not "SCALAR":
raise SyntaxError("\"%(s)s\"\n\tExpected a VECTOR or SCALAR shape, but got %(tmp)s instead." % locals())
# if any remaining
if toks:
t, tmp = toks.popleft()
raise SyntaxError("\"%(s)s\"\n\tUnexpected ending for variables with %(t)s token %(tmp)s." % locals())
self.symtable[v] = Variable(shape)