IfStatementAST.py (6999:f226c098c393) IfStatementAST.py (7839:9e556fb25900)
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.StatementAST import StatementAST
29from slicc.symbols import Type
30
31class IfStatementAST(StatementAST):
32 def __init__(self, slicc, cond, then, else_):
33 super(IfStatementAST, self).__init__(slicc)
34
35 assert cond is not None
36 assert then is not None
37
38 self.cond = cond
39 self.then = then
40 self.else_ = else_
41
42 def __repr__(self):
43 return "[IfStatement: %r%r%r]" % (self.cond, self.then, self.else_)
44
45 def generate(self, code, return_type):
46 cond_code = self.slicc.codeFormatter()
47 cond_type = self.cond.generate(cond_code)
48
49 if cond_type != self.symtab.find("bool", Type):
50 self.cond.error("Condition of if stmt must be bool, type was '%s'",
51 ctype)
52
53 # Conditional
54 code.indent()
55 code('if ($cond_code) {')
56 # Then part
57 code.indent()
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.StatementAST import StatementAST
29from slicc.symbols import Type
30
31class IfStatementAST(StatementAST):
32 def __init__(self, slicc, cond, then, else_):
33 super(IfStatementAST, self).__init__(slicc)
34
35 assert cond is not None
36 assert then is not None
37
38 self.cond = cond
39 self.then = then
40 self.else_ = else_
41
42 def __repr__(self):
43 return "[IfStatement: %r%r%r]" % (self.cond, self.then, self.else_)
44
45 def generate(self, code, return_type):
46 cond_code = self.slicc.codeFormatter()
47 cond_type = self.cond.generate(cond_code)
48
49 if cond_type != self.symtab.find("bool", Type):
50 self.cond.error("Condition of if stmt must be bool, type was '%s'",
51 ctype)
52
53 # Conditional
54 code.indent()
55 code('if ($cond_code) {')
56 # Then part
57 code.indent()
58 self.symtab.pushFrame()
58 self.then.generate(code, return_type)
59 self.then.generate(code, return_type)
60 self.symtab.popFrame()
59 code.dedent()
60 # Else part
61 if self.else_:
62 code('} else {')
63 code.indent()
61 code.dedent()
62 # Else part
63 if self.else_:
64 code('} else {')
65 code.indent()
66 self.symtab.pushFrame()
64 self.else_.generate(code, return_type)
67 self.else_.generate(code, return_type)
68 self.symtab.popFrame()
65 code.dedent()
66 code('}') # End scope
67
68 def findResources(self, resources):
69 # Take a worse case look at both paths
70 self.then.findResources(resources)
71 if self.else_ is not None:
72 self.else_.findResources(resources)
69 code.dedent()
70 code('}') # End scope
71
72 def findResources(self, resources):
73 # Take a worse case look at both paths
74 self.then.findResources(resources)
75 if self.else_ is not None:
76 self.else_.findResources(resources)