コード例 #1
0
ファイル: bind.py プロジェクト: seanfrischmann/Repl
def bindFunc(user_input, env, active_env):
    local = ""
    not_local = ""
    if (env["stack"][1])[0].isalpha():
        local = env["stack"][1]
    elif (env["stack"][1])[0] == "<":
        local = primitives.get_local(env["stack"][1])
        if not local[0].isalpha():
            return False
    else:
        return False
    if (env["stack"][0])[0] == "<":
        not_local = primitives.get_not_local(env["stack"][0])
    else:
        not_local = env["stack"][0]
    if active_env == 0:
        if (local) in env["global"]["bindings"]:
            return False
        else:
            if not_local == ":closure:":
                (env["global"]["binded closures"])[local] = env["global"]["closures"][0]
                (env["global"]["bindings"])[local] = ":closure:"
            else:
                (env["global"]["bindings"])[local] = not_local
            env["stack"].pop(1)
            env["stack"].pop(0)
            env["stack"].insert(0, not_local)
            return True
    else:
        if (local) in env["active"][0]["bindings"]:
            return False
        else:
            if not_local == ":closure:":
                (env["active"][0]["binded closures"])[local] = env["global"]["closures"][0]
                (env["active"][0]["bindings"])[local] = ":closure:"
            else:
                (env["active"][0]["bindings"])[local] = not_local
            env["stack"].pop(1)
            env["stack"].pop(0)
            env["stack"].insert(0, not_local)
            return True
コード例 #2
0
ファイル: evaluater.py プロジェクト: seanfrischmann/Repl
def evaluate(user_input, env, active_env, isNested):
	position = 0
	isError = False
	isPrimitive = False
	isNegative = False
	isInsert = False
	if user_input == 'quit':
		sys.exit(0)
	try:
		temp = None
		if user_input[0] == '-':
			t = 1
			temp = ''
			while t < len(user_input):
				temp = temp + user_input[t]
				t = t+1
			if temp.isdigit():
				isNegative = True
		if user_input.isdigit() or isNegative:
			isInsert = True
			isNegative = False
		elif user_input[0] == '"':
			isInsert = True
		elif user_input == 'lessThan':
			if not primitives.less_func(env):
				isError = True
		elif user_input == 'if':
			if not primitives.if_func(env):
				isError = True
		elif user_input == 'bind':
			if not bind.bindFunc(user_input, env, active_env):
				isError = True
		elif user_input == 'concat':
			if not primitives.cat_func(env):
				isError = True
		elif user_input == 'length':
			if not primitives.len_func(env):
				isError = True
		elif user_input == 'equal':
			if primitives.equal_func(env):
				user_input = ':true:'
			else:
				user_input = ':false:'
			isInsert = True
		elif user_input == ':true:' or user_input == ':false:':
			isInsert = True
		elif user_input == ':error:':
			isError = True
		elif user_input == 'add':
			temp = primitives.add_func(env)
			isPrimitive = True
		elif user_input == 'sub':
			temp = primitives.sub_func(env)
			isPrimitive = True
		elif user_input == 'div':
			temp = primitives.div_func(env)
			isPrimitive = True
		elif user_input == 'rem':
			temp = primitives.rem_func(env)
			isPrimitive = True
		elif user_input == 'mul':
			temp = primitives.mul_func(env)
			isPrimitive = True
		elif user_input == 'neg':
			temp = primitives.neg_func(env)
			env['stack'].pop(0)
			temp = str(temp)
			env['stack'].insert(0, temp)
			user_input = ''
		elif user_input == 'pop':
			topValue = env['stack'][0]
			if topValue[0] == '<':
				topValue = primitives.get_not_local(topValue)
			if topValue == ':closure:':
				env['global']['closures'].pop(0)
			env['stack'].pop(0)
			user_input = ''
		elif user_input == 'exc':
			temp = env['stack'][1]
			env['stack'].pop(1)
			env['stack'].insert(0 , temp)
			user_input = ''
		elif user_input[0].isalpha():
			user_input = bind.evalName(user_input, env, isNested)
			isInsert = True
		else:
			isError = True
	except:
		isError = True
	if isPrimitive:
		env['stack'].pop(0)
		env['stack'].pop(0)
		temp = str(temp)
		env['stack'].insert(0, temp)
		user_input = ''
	if isInsert:
		env['stack'].insert(0, user_input)
		user_input = ''
	if isError:
		env['stack'].insert(0, ':error:')
コード例 #3
0
ファイル: hw4.py プロジェクト: seanfrischmann/Repl
def repl():
	print (
		'\nInterpreter\n'
		'Author: Sean Frischmann\n'
		'Class: Cse 305\n'
	)
	env = dict()
	env['stack'] = list()
	env['global'] = dict()
	env['active'] = list()
	env['global']['bindings'] = dict()
	env['global']['closures'] = list()
	env['global']['binded closures'] = dict()
	env['inactive'] = list()
	mode = 'default'
	buf = ''
	isSpace = False
	isNested = False
	apply_count = 0
	temp_buf = []
	temp_position = []
	active_env = 0
	closure_count = 0
	user_input = ''
	while True:
		position=0
		if apply_count > 0:
			if isNested:
				isNested = False
				env['inactive'] = list()
			if len(temp_buf) != 0:
				buf = temp_buf[0]
				temp_buf.pop(0)
				position = temp_position[0]
				temp_position.pop(0)
				position += 1
			apply_count += -1
			active_env += -1
			if len(env['active']) != 0:
				env['active'].pop(0)
		elif mode == 'default':
			buf = input('repl> ')
		else:
			buf = input(' ')
		while position < len(buf):
			if buf[position] == '"':
				if mode == 'closure':
					mode = 'closure-string'
				elif mode == 'closure-string':
					mode = 'closure'
				elif mode == 'string':
					mode = 'default'
				else:
					mode = 'string'
			if buf[position] == '{':
				closure_count = closure_count + 1
				mode = 'closure'
			if (buf[position] == ' ') and (mode == 'default'):
				isSpace = True
			else:
				user_input = user_input + buf[position]
			if isSpace or position == len(buf)-1:
				if user_input == 'apply':
					topValue = env['stack'][0]
					if topValue[0] == '<':
						topValue = primitives.get_not_local(topValue)
					if topValue == ':closure:':
						temp_buf.insert(0, buf)
						temp_position.insert(0, position)
						closureValue = env['global']['closures'][0]
						env['active'].insert(0,{})
						env['active'][0]['bindings'] = dict()
						env['active'][0]['binded closures'] = dict()
						if isinstance(closureValue, dict):
							isNested = True
							linked_env = []
							for val in closureValue.values():
								linked_env = linked_env + val
							env['inactive'] = linked_env
							env['inactive'].insert(0,env['active'][0])
							for val in closureValue.keys():
								closureValue = val
						buf = closureValue
						env['global']['closures'].pop(0)
						env['stack'].pop(0)
						active_env += 1
					else:
						buf = ':error:' + buf[position+1:len(buf)]
					user_input = ''
					apply_count += 1
					isSpace = False
					position = 0
					continue
				elif user_input == 'load':
					if len(env['stack']) > 0 and (env['stack'][0])[0] == '"':
						try:
							inputFile = env['stack'][0]
							inputFile = inputFile.replace('"','')
							if position == len(buf)-1:
								buf = buf[position+1:len(buf)]
							else:
								buf = buf[position:len(buf)]
							buf = load(inputFile)+' '+':true:'+buf
							env['stack'].pop(0)
							user_input = ''
							position = 0
							continue
						except:
							user_input = ':false:'
							continue
				elif mode == 'string':
					user_input = user_input + '\n'
				elif mode == 'default':
					evaluater.evaluate(user_input,env, active_env, isNested)
					user_input = ''
					isSpace = False
			if buf[position] == '}' and mode == 'closure':
				closure_count = closure_count - 1
				if closure_count == 0:
					isFrontValid = True
					isEndValid = True
					isMore = True
					if user_input[1] != ' ':
						isFrontValid = False
						temp = ':error:'+user_input[2:len(user_input)]
						user_input = temp
					if user_input[len(user_input)-2] != ' ':
						isEndValid = False
						if isFrontValid:
							temp = ':error:'+user_input[1:len(user_input)]
							user_input = temp
					if (position+1) < len(buf):
						if buf[position+1] != ' ':
							if isFrontValid:
								isFrontValid = False
								temp = ':error:'+user_input[1:len(user_input)]
								user_input = temp
						else:
							isMore = True
					if isFrontValid and isEndValid:
						closure.createClosure(user_input, env, active_env)
						if isMore:
							position = position+1
					else:
						buf = user_input + buf[position+1:len(buf)]
						position = -1
					mode = 'default'
					user_input = ''
			position = position+1
		if mode == 'default' and (apply_count == 0):
			print_list(env['stack'])