OperatorExprAST.py revision 6999
1# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
2# Copyright (c) 2009 The Hewlett-Packard Development Company
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met: redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer;
9# redistributions in binary form must reproduce the above copyright
10# notice, this list of conditions and the following disclaimer in the
11# documentation and/or other materials provided with the distribution;
12# neither the name of the copyright holders nor the names of its
13# contributors may be used to endorse or promote products derived from
14# this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28from slicc.ast.ExprAST import ExprAST
29from slicc.symbols import Type
30
31class InfixOperatorExprAST(ExprAST):
32    def __init__(self, slicc, left, op, right):
33        super(InfixOperatorExprAST, self).__init__(slicc)
34
35        self.left = left
36        self.op = op
37        self.right = right
38
39    def __repr__(self):
40        return "[InfixExpr: %r %s %r]" % (self.left, self.op, self.right)
41
42    def generate(self, code):
43        lcode = self.slicc.codeFormatter()
44        rcode = self.slicc.codeFormatter()
45
46        ltype = self.left.generate(lcode)
47        rtype = self.right.generate(rcode)
48
49        # Figure out what the input and output types should be
50        if self.op in ("==", "!="):
51            output = "bool"
52            if (ltype != rtype):
53                self.error("Type mismatch: left and right operands of " +
54                           "operator '%s' must be the same type. " +
55                           "left: '%s', right: '%s'",
56                           self.op, ltype, rtype)
57        else:
58            if self.op in ("&&", "||"):
59                # boolean inputs and output
60                inputs = "bool"
61                output = "bool"
62            elif self.op in ("==", "!=", ">=", "<=", ">", "<"):
63                # Integer inputs, boolean output
64                inputs = "int"
65                output = "bool"
66            else:
67                # integer inputs and output
68                inputs = "int"
69                output = "int"
70
71            inputs_type = self.symtab.find(inputs, Type)
72
73            if inputs_type != ltype:
74                self.left.error("Type mismatch: left operand of operator " +
75                                "'%s' expects type '%s', actual was '%s'",
76                                self.op, inputs, ltype)
77
78            if inputs_type != rtype:
79                self.right.error("Type mismatch: right operand of operator " +
80                                 "'%s' expects type '%s', actual was '%s'",
81                                 self.op, inputs, rtype)
82
83        # All is well
84        fix = code.nofix()
85        code("($lcode ${{self.op}} $rcode)")
86        code.fix(fix)
87        return self.symtab.find(output, Type)
88