コード例 #1
0
def p_complex169(p):
	'''complex169 : empty '''
	for args in p[-2] :
		print args
		sym_tab.addID(args['name'],args['type'])
		displayValue, offset = sym_tab.getAttri(args['name'], 'level'), sym_tab.getAttri(argument['name'], 'offset')
		place = sym_tab.new_temp((displayValue, offset), variable=argument['name'], loadFromMemory=True)
		sym_tab.addAttri(args['name'],  sym_tab.getCurrentScope(), place)
コード例 #2
0
def p_ARG_relational(p):
    ''' ARG : Identifier EQUALS ARG '''

    p[0] = {}

    place = None

    if sym_tab.exists(p[1]):

        sym_tab.add_attri(p[1])

        if p[1] in sym_tab.scope[len(sym_tab.scope) - 1]:
            place = sym_tab.getAttri(
                p[1], sym_tab.scope[len(sym_tab.scope) - 1]['__scope__'])
        else:
            disp_val = sym_tab.getAttri(p[1], 'scopelevel')
            offset = sym_tab.getAttri(p[1], 'offset')
            place = sym_tab.new_temp((disp_val, offset), variable=p[1])
            sym_tab.add_attri(p[1],
                              sym_tab.scope[len(self.scope) - 1]['__scope__'],
                              p[1])
            TAC.emit('=', p[1], str(p[3]['place']), '')
            p[0]['code'] = p[3]['code'] + [
                '=' + ',' + str(p[1]) + ',' + str(p[3]['place']) + ',' + ''
            ]
    else:

        sym_tab.addID(p[1], p[3]['type'])

        # Create a temporary for the current scope
        displayValue, offset = sym_tab.getAttri(
            p[1], 'scopeLevel'), sym_tab.getAttri(p[1], 'offset')
        place = sym_tab.new_temp((displayValue, offset), variable=p[1])
        sym_tab.add_attri(p[1],
                          sym_tab.scope[len(sym_tab.scope) - 1]['__scope__'],
                          p[1])
        TAC.emit('=', p[1], str(p[3]['place']), '')

        p[0]['code'] = p[3]['code'] + [
            '=' + ',' + str(p[1]) + ',' + str(p[3]['place']) + ',' + ''
        ]
コード例 #3
0
def p_complex145(p):
	'''complex145 : empty'''
	p[0] = {}
	p[0]['name_fun'] = sym_tab.get_name()
	
	sym_tab.addID(p[-1], "function")
	displayValue, offset = sym_tab.getAttri(p[-1], 'level'), sym_tab.getAttri(p[-1], 'offset')
	place = sym_tab.new_temp((displayValue, offset), variable=p[-1])
	sym_tab.add_attri(p[-1], sym_tab.getCurrentScope(), place)
	sym_tab.add_attri(p[-1], 'name_fun', p[0]['name_fun'])
	sym_tab.add_Sc(p[0]['name_fun'])
	TAC.code[p[0]['name_fun']] = []
	TAC.quad[p[0]['name_fun']] = 0
	TAC.nq[p[0]['name_fun']] = -1
コード例 #4
0
def p_ASGN_basic(p):
    '''ASGN : Identifier EQUALS EXPR '''
    p[0] = {}
    place = None
    if sym_tab.exists(p[1]):

        sym_tab.add_attri(p[1], 'type', p[3]['type'])

        if p[1] in sym_tab.scope[len(sym_tab.scope) - 1]:
            place = sym_tab.getAttri(
                p[1], sym_tab.scope[len(sym_tab.scope) - 1]['__scope__'])
        else:
            disp_val = sym_tab.getAttri(p[1], 'level')
            offset = sym_tab.getAttri(p[1], 'offset')
            place = sym_tab.new_temp((disp_val, offset), variable=p[1])
            sym_tab.add_attri(p[1],
                              sym_tab.scope[len(self.scope) - 1]['__scope__'],
                              p[1])
        TAC.emit('=', place, str(p[3]['place']), '')
        # if p[1] == 'a' and p[3]['place'] == '3' :
        #   print "HOLLO"
        #print p[3], "OKY" , p[1]
        p[0]['code'] = p[3]['code'] + [
            '=' + ',' + str(place) + ',' + str(p[3]['place']) + ',' + ''
        ]
    else:
        sym_tab.addID(p[1], p[3]['type'])

        # Create a temporary for the current scope
        displayValue, offset = sym_tab.getAttri(
            p[1], 'scopeLevel'), sym_tab.getAttri(p[1], 'offset')
        place = sym_tab.new_temp((displayValue, offset), variable=p[1])
        sym_tab.add_attri(p[1],
                          sym_tab.scope[len(sym_tab.scope) - 1]['__scope__'],
                          p[1])
        TAC.emit('=', p[1], str(p[3]['place']), '')
コード例 #5
0
def p_PRIMARY_LITERAL(p):
    ''' PRIMARY : LITERAL '''
    p[0] = {}
    p[0] = {'type': p[1]['type']}
    p[0]['place'] = sym_tab.new_temp()
    #sym_tab.count = sym_tab.count + 1

    if p[1]['type'] == 'STRING':
        p[0]['reference'] = p[1]['reference']
        TAC.emit('=', p[0]['place'], p[1]['reference'], '')
        p[0]['code'] = p[1]['code'] + [('=' + ',' + str(p[0]['place']) + ',' +
                                        str(p[1]['reference']) + ',' + '')]
    else:
        TAC.emit('=', p[0]['place'], p[1]['value'], '')
        p[0]['code'] = p[1]['code'] + [('=' + ',' + str(p[0]['place']) + ',' +
                                        str(p[1]['value']) + ',' + '')]
コード例 #6
0
ファイル: parser.py プロジェクト: vakhil/Compilers_Gowtham
def p_relation_operations(p) :
  ''' ARG : ARG less_than ARG
          | ARG greater_than ARG
          | ARG lessthan_equals ARG
          | ARG greater_than_equals ARG
          | ARG double_equals ARG
          | ARG Exclaim1 ARG '''
  types = 'UNDEFINED'

  if p[1]['type'] == p[3]['type'] == 'NUMBER':
        types = 'BOOLEAN'
  else:
        expType = 'error'
        debug.printError('Operands to relational expressions must be numbers')
        raise SyntaxError

  p[0] = { 'type' : type }
  p[0]['place'] = sym_tab.new_temp()

    # Emit code
  TAC.emit(p[2],p[0]['place'], p[1]['place'], p[3]['place'])
  p[0]['code'] = p[2]+","+str(p[0]['place'])+","+str(p[1]['place'])+","+ str(p[3]['place'])
コード例 #7
0
def p_relation_operations(p) :
	''' ARG : ARG less_than ARG
					| ARG greater_than ARG
					| ARG lessthan_equals ARG
					| ARG greater_than_equals ARG
					| ARG double_equals ARG
					| ARG Exclaim1 ARG '''
	types = 'UNDEFINED'
	
	if p[1]['type'] == 'NUMBER' and p[3]['type'] == 'NUMBER':
				types = 'BOOLEAN'
	else:
				expType = 'error'
				debug.printError('Operands to relational expressions must be numbers')
				raise SyntaxError

	p[0] = { 'type' : types }
	p[0]['place'] = sym_tab.new_temp()

		# Emit code
	var = ''
	if var == '<' :
		var = 'jl'
	if var == '>' :
		var = 'jg'
	if var == '>=':
		var = 'jge'
	if var == '<=' :
		var = 'jle' 
	if var == '==' :
		var = 'je'

	p[0]['truelist'] = []
	p[0]['truelist'].append(TAC.getNQ())
	p[0]['falseList'] = []
	p[0]['truelist'].append(TAC.getNQ()+1)
	TAC.emit('IFgoto',var,p[1]['place'],p[3]['place'],'__')
	TAC.emit('goto','__')
コード例 #8
0
def p_ARG_operations(p):
    ''' ARG :  ARG Add ARG 
          | ARG MINUS ARG 
          | ARG Multiply ARG 
          | ARG DIVIDE ARG '''

    p[0] = {}
    p[0]['place'] = sym_tab.new_temp()

    types = ''
    if p[2] == '*':
        p[2] = 'X'
    if p[1]['type'] == 'NUMBER' and p[3]['type'] == 'NUMBER':
        types = 'NUMBER'
        TAC.emit(p[2], p[0]['place'], p[1]['place'], p[3]['place'])
        p[0]['code'] = []
        p[0]['code'] = [(p[2] + ',' + str(p[0]['place']) + ',' +
                         str(p[1]['place']) + ',' + str(p[3]['place']))]

    else:
        debug.printError('Type Mismatch')

    p[0]['type'] = types
コード例 #9
0
def p_ARG_operations(p):
    ''' EXPR :  EXPR Add EXPR 
		  | EXPR MINUS EXPR 
		  | EXPR Multiply EXPR
		  | EXPR DIVIDE EXPR 
		  | EXPR Percentage EXPR'''

    p[0] = {}
    p[0]['place'] = sym_tab.new_temp()

    types = ''
    if p[2] == '*':
        p[2] = 'X'
    if p[1]['type'] == 'NUMBER' and p[3]['type'] == 'NUMBER':
        types = 'NUMBER'
        TAC.emit(p[2], p[0]['place'], p[1]['place'], p[3]['place'])
        p[0]['code'] = []
        p[0]['code'] = [(p[2] + ',' + str(p[0]['place']) + ',' +
                         str(p[1]['place']) + ',' + str(p[3]['place']))]

    else:
        debug.printError('Type Mismatch')

    p[0]['type'] = types
コード例 #10
0
def p_PRIMARY_LITERAL(p):
    ''' PRIMARY : LITERAL '''
    p[0] = {}
    var = ''
    if 'reference' in p[1].keys():
        var = p[1]['reference']
    else:
        var = p[1]['value']
    p[0] = {'type': p[1]['type']}
    p[0]['place'] = sym_tab.new_temp()
    #sym_tab.count = sym_tab.count + 1

    if p[1]['type'] == 'STRING':
        p[0]['reference'] = p[1]['reference']
        TAC.emit('=', p[0]['place'], p[1]['reference'], '')

        p[0]['code'] = p[1]['code'] + [('=' + ',' + str(p[0]['place']) + ',' +
                                        str(p[1]['reference']) + ',' + '')]
    else:
        TAC.emit('=', p[0]['place'], p[1]['value'], '')
        print "MON", TAC.getNQ(), p[1]['value'], (TAC.code)
        p[0]['code'] = p[1]['code'] + [('=' + ',' + str(p[0]['place']) + ',' +
                                        str(p[1]['value']) + ',' + '')]
        print TAC.getNQ(), "JOKE"