I am using python 2.3.5 and I just haven't seemed to understand why I get an error that says "SyntaxError: unqualified exec is not allowed in function 'parse' it contains a nested function with free variables". I am trying to develop a scripting language for my operating system that is built in on top of mono, so I will eventually be porting this to IronPython. I do not want people suggesting I just use program XYZ instead. If someone could fix my code I would greatly appreciate it. Also if anybody would like to improve this I would be happy. ;)

import sys
from optparse import OptionParser
__version__="0.1-Alpha"
__year__="2007"
__author__="Ozipto"
class ValsNVars:
	def __init__(self):
		self.odd=0
		self.var=0
		self.val=0
		self.getnext=0
		self.protected=0
		self.map = []
		self.var_list = []
	def get(self, variable):
		self.var=0
		self.val=0
		self.getnext=0
		self.protected=0
		for this in self.map:
			if self.protected == 1:
				self.protected = 0
			if self.val != 1 and self.var != 1:
				self.var = 1
				if variable == this:
					self.getnext=1
					self.protected=1
					self.var=0
					self.val=1
			if self.var == 1 and self.protected != 1:
				if variable == this:
					self.getnext=1
					self.protected=1
					self.var=0
					self.val=1
			if self.val == 1 and self.getnext == 1 and self.protected != 1:
				return this
	def where(self, variable):
		i = 0
		location = None
		for this in self.map:
			i = i + 1
			if self.protected == 1:
				self.protected = 0
			if self.val != 1 and self.var != 1:
				self.var = 1
				if variable == this:
					self.getnext=1
					self.protected=1
					self.var=0
					self.val=1
			if self.var == 1 and self.protected != 1:
				if variable == this:
					self.getnext=1
					self.protected=1
					self.var=0
					self.val=1
			if self.val == 1 and self.getnext == 1 and self.protected != 1:
				location=i
		return location
	def set(self, variable, value):
		if self.get(variable) != None:
			location = self.where(variable)
			self.map.remove(self.get(variable))
			self.map.insert(location, value)
		else:
			self.map.append(variable)
			self.map.append(value)
	def list(self):
		i = 0
		location = None
		for this in self.map:
			i = i + 1
			if self.protected == 1:
				self.protected = 0
			if self.val != 1 and self.var != 1:
				self.var = 1
				if variable == this:
					self.getnext=1
					self.protected=1
					self.var=0
					self.val=1
			if self.var == 1 and self.protected != 1:
				self.var_list.append(this)
		result = self.var_list
		self.var_list = []
		return result

class Console:
	def __init__(self):
		self.type=None
		self.variable=None
		self.value=None
		self.protected=0
		self.ValsNVars=ValsNVars()
		self.sandbox=0
		self.sand_variable=None
		self.sand_value=None
		self.if_variable=None
		self.if_value=None
		self.if_do=0
		self.iffing=0
		self.setting=0
		self.DoNothing=0
		self.functioning=0
		self.function=None
		self.functionDo=None
		self.function_params = []
		self.paranthesis=0
		self.createdCode = []
		self.creating=0
		self.specialCreating=0
		self.createdDo=None
		self.functions = []
		self.create_function=0
		self.define=None
		self.created_params = []
	def clean(self):
		self.type=None
		self.variable=None
		self.value=None
		self.protected=0
		self.sandbox=0
		self.sand_variable=None
		self.sand_value=None
		self.setting=0
		self.DoNothing=0
		self.functioning=0
		self.function=None
		self.functionDo=None
		self.function_params = []
		self.parenthesis=0
	def if_clean(self):
		self.if_variable=None
		self.if_value=None
		self.if_do=0
		self.iffing=0
	def function_clean(self):
		self.createdCode = []
		self.creating=0
		self.specialCreating=0
		self.createdDo=None
		self.create_function=0
		self.created_params = []
		self.define=None
		self.params=None
	def process(self):
		self.ValsNVars.set(self.variable, self.value)
	def checker(self):
		print(self.ValsNVars.get(self.variable))
		self.clean()
		if self.iffing == 0:
			self.if_clean()
	def update(self):
		self.process()
		self.clean()
		if self.iffing == 0:
			self.if_clean()
	def include(self, what):
		return None
	def VarReturn(self, var):
		Var=self.ValsNVars
		if Var.get(var) != None:
			return Var.get(var)
		else:
			return var
	def parse(self, code):
		def increment(self, variable):
			self.ValsNVars.set(variable, int(self.ValsNVars.get(variable))+1)
		def decrement(self, variable):
			self.ValsNVars.set(variable, int(self.ValsNVars.get(variable))-1)
		def load(self, code):
			self.parse(code)
		def load_file(self, file):
			try:
				f = open(file, 'rU')
				code = f.read()
				f.close()
				self.load(code)
			except:
				print("The file, %s, could not be loaded" % file)
		for word in code.split():
			if self.protected == 1:
				self.protected = 0
			#if word != "=" and self.type == "variable" and self.protected !=1:
			#	print("#Variable and the previous word was a space")
			#	self.variable=self.variable+""+word
			#	self.protected=1
			if word != "=" and self.type==None and word != "if" and word != "func" and word != "(" and word != ")" and word != ";;" and word != "fi" and self.protected != 1 and self.DoNothing == 0:
				print("#Variable")
				self.type="variable"
				self.protected=1
				self.variable = word
				if self.creating == 1:
					self.createdCode.append(word)
			if word == "=" and self.protected != 1 and self.DoNothing == 0:
				print("#Hash")
				self.type = "hash"
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word != "=" and self.type == "hash" and self.protected != 1 and self.DoNothing == 0:
				print("#Value")
				self.type = "value"
				self.protected=1
				self.value = word
				if self.creating == 1:
					self.createdCode.append(word)
			if word == ";;" and self.type == "value" and self.protected != 1 and self.DoNothing == 0:
				print("#Updater")
				self.type = "updater"
				self.protected=1
				self.update()
				if self.creating == 1:
					self.createdCode.append(word)
			if word == ";;" and self.type == "variable" and self.protected != 1 and self.DoNothing == 0:
				print("#Checker")
				self.type = "checker"
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
				self.checker()
			if word == ";;" and self.type == "function" and self.protected != 1 and self.DoNothing == 0:
				print("#Execution")
				if self.creating == 1:
					self.createdCode.append(word)
				self.type = "execution"
				self.protected=1
				exec self.functionDo
				self.clean()
			if word == ";;" and self.type == "create_func" and self.protected != 1 and self.DoNothing == 0:
				print("#Function Creation")
				if self.specialCreating == 1:
					self.createdCode.append(word)
				self.protected=1
				self.creating=0
				self.specialCreating=0
				CodeCreation=self.createdDo+"load(self.createdCode)"
				exec CodeCreation
				self.functions.append(self.define)
				self.clean()
				self.function_clean()
			if word == "if" and self.protected != 1:
				print("#If")
				if self.type == "variable":
					print("I'm sorry but the \"if\" is a reserved keyword.")
					return None
				self.type = "if"
				if self.type == "value":
					self.sandbox=1
				self.iffing=1
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word != "=" and self.iffing == 1 and self.type == "if" and self.protected != 1:
				print("#If Variable")
				self.type = "if_variable"
				self.if_variable = word
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == "==" and self.iffing == 1 and self.type == "if_variable" and self.protected != 1:
				print("#If Hash")
				self.type = "if_hash"
				self.if_operator = "EqualTo"
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == ">" and self.iffing == 1 and self.type == "if_variable" and self.protected != 1:
				print("#If Hash")
				self.type = "if_hash"
				self.if_operator = "GreaterThan"
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == "<" and self.iffing == 1 and self.type == "if_variable" and self.protected != 1:
				print("#If Hash")
				self.type = "if_hash"
				self.if_operator = "LessThan"
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == ">=" and self.iffing == 1 and self.type == "if_variable" and self.protected != 1:
				print("#If Hash")
				self.type = "if_hash"
				self.if_operator = "GreaterThanOrEqualTo"
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == ">=" and self.iffing == 1 and self.type == "if_variable" and self.protected != 1:
				print("#If Hash")
				self.type = "if_hash"
				self.if_operator = "LessThanOrEqualTo"
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == "!=" and self.iffing == 1 and self.type == "if_variable" and self.protected != 1:
				print("#If Hash")
				self.type = "if_hash"
				self.if_operator = "NotEqualTo"
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word != "=" and self.iffing == 1 and self.type == "if_hash" and self.protected != 1:
				print("#If Value")
				self.type = "if_variable"
				self.if_value = word
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == ":" and self.iffing == 1 and self.type == "if_variable" and self.protected != 1:
				print("#If Operator")
				self.type = None
				self.protected=1
				if self.if_operator == "EqualTo":
					if self.VarReturn(self.if_variable) == self.VarReturn(self.if_value):
						self.DoNothing=0
					else:
						self.DoNothing=1
				if self.if_operator == "GreaterThan":
					if int(self.VarReturn(self.if_variable)) > int(self.VarReturn(self.if_value)):
						self.DoNothing=0
					else:
						self.DoNothing=1
				if self.if_operator == "LessThan":
					if int(self.VarReturn(self.if_variable)) < int(self.VarReturn(self.if_value)):
						self.if_do=1
					else:
						self.DoNothing=1
				if self.if_operator == "GreaterThanOrEqualTo":
					if int(self.VarReturn(self.if_variable)) > int(self.VarReturn(self.if_value)) or int(self.VarReturn(self.if_variable)) == int(self.VarReturn(self.if_value)):
						self.DoNothing=0
					else:
						self.DoNothing=1
				if self.if_operator == "LessThanOrEqualTo":
					if int(self.VarReturn(self.if_variable)) < int(self.VarReturn(self.if_value)) or int(self.VarReturn(self.if_variable)) == int(self.VarReturn(self.if_value)):
						self.DoNothing=0
					else:
						self.DoNothing=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == "fi" and self.iffing == 1 and self.protected !=1 :
				print("#End If")
				if self.DoNothing==1:
					self.DoNothing=0
				self.protected=1
				self.iffing=0
				if self.creating == 1:
					self.createdCode.append(word)
			if word == "(" and self.type == "variable" and self.protected != 1 and self.DoNothing == 0:
				print("#Begin Parenthesis")
				self.parenthesis+=1
				if self.parenthesis == 1:
					self.function = self.variable
					self.functioning = 1
				else:
					self.function_params.append(word)
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word != "(" and word != ")" and self.functioning == 1 and self.protected != 1 and self.DoNothing == 0:
				print("#Parameters")
				self.function_params.append(self.VarReturn(word))
				self.protected = 1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == ")" and self.functioning == 1 and self.protected != 1 and self.DoNothing == 0:
				print("#End Parenthesis")
				self.parenthesis-=1
				if self.parenthesis==1:
					params = ""
					for parameter in self.function_params:
						if params == "":
							params = parameter
						else:
							params=params+" "+parameter
					self.functionDo=self.function+"("+params+")"
					self.type="function"
				else:
					self.function_params.append(word)
				if self.creating == 1:
					self.createdCode.append(word)
			if word == "func" and self.type == "variable" and self.protected != 1 and self.DoNothing == 0:
				print("#Begin Function")
				self.create_function=1
				self.define = self.variable
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == "(" and self.create_function == 1 and self.protected != 1 and self.DoNothing == 0:
				print("#Function Begin Parenthesis")
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word != "(" and word != ")" and self.create_function == 1 and self.protected != 1 and self.DoNothing == 0:
				print("#Function Parameters")
				self.created_params.append(word)
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == ")" and self.create_function == 1 and self.protected != 1 and self.DoNothing == 0:
				print("#Function End Parenthesis")
				params = ""
				for parameter in self.created_params:
					if params == "":
						params = parameter
					else:
						params=params+" "+parameter
				self.createdDo="def %s(self, %s):" % (self.define, params)
				self.protected=1
				if self.creating == 1:
					self.createdCode.append(word)
			if word == ":" and self.create_function == 1 and self.protected != 1 and self.DoNothing == 0:
				print("#Function Operator")
				if self.creating == 1:
					self.createdCode.append(word)
					self.specialCreating=1
				self.creating=1
				self.protected=1
	def start(self):
		Run=1
		while Run:
			data = raw_input("OziptoScript>")
			if data == "Refresh" or data == "refresh" or data == "REFRESH":
				self.ValsNVars=ValsNVars()
				data=""
			if data == "Exit" or data == "exit" or data == "EXIT":
				Run=0
				data=""
			self.parse(data)
if __name__ == "__main__" or __name__ == sys.executable:
	Interpreter = Console()
	option_list = [
		make_option("-f", "--filename",
        	action="store", type="string", dest="filename"),
	make_option("-q", "--quiet",
        	action="store_false", dest="verbose"),
	]
	parser = OptionParser(option_list=option_list)
	if parser.has_option("-f") or parser.has_option("--filename"):
		f = open(parser.filename)
		codeline = f.readlines()
		for line in codelines:
			Interpreter.parse(line)
	else:
		print("OziptoScript Version %s" % __version__)
		print("Copyright %s " % __year__ + "by %s" % __author__)
		Interpreter.start()

Hi,

I suggest that if you don't have any luck here, try the following addresses as some specialize in certain areas that may not be covered here. The first address is the general discussion list for the Python programming language. The second address is for all the addresses that mail.Python.org handles.

1. http://mail.python.org/mailman/listinfo/python-list (Python-list)

2. http://mail.python.org/mailman/listinfo (all the public mailing lists on mail.python.org)

Best of luck!:)

fredzik.

I would try incrementally removing some of the functions from the scope of parse(). Sooner or later Python will stop complaining and you'll have a better idea where your problem lies.

At that point even if you can't figure out why you're getting the error, you'll have a much better chance someone will look at a 50-line program instead of a 450-line program.

Thanks for the suggestions, actually this is about the sixth time I tried to rewrite my language. I have given up the syntax used here and I have created a new simpler syntax. The language I am currently creating generates code using an intermediate code that translates to python on the fly. If any of you are interested I will be releasing my code at my website as open source. If you are wondering, I don't work on my website all the time and I host it myself, here.

This question has already been answered. Start a new discussion instead.