KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > filter > etcl > StaticTypeChecker


1 package org.jacorb.notification.filter.etcl;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 /**
25  * @author Alphonse Bendt
26  * @version $Id: StaticTypeChecker.java,v 1.5 2005/02/14 00:07:08 alphonse.bendt Exp $
27  */

28
29 public class StaticTypeChecker extends AbstractTCLVisitor
30 {
31     public void check(AbstractTCLNode rootNode) throws StaticTypeException
32     {
33         try
34         {
35             rootNode.acceptPostOrder(this);
36         } catch (VisitorException e)
37         {
38             throw new StaticTypeException(e.getMessage());
39         }
40     }
41
42     private void checkBinaryNumaryOperatorNode(AbstractTCLNode node) throws StaticTypeException
43     {
44         if (node.isStatic())
45         {
46             if (node.left().isNumber() && node.right().isNumber())
47             {
48                 return;
49             }
50             throw new StaticTypeException("num or float excepted): " + node.toStringTree());
51         }
52     }
53
54     public void visitGt(GtOperator n) throws VisitorException
55     {
56     }
57
58     public void visitPlus(PlusOperator n) throws VisitorException
59     {
60         checkBinaryNumaryOperatorNode(n);
61     }
62
63     public void visitMinus(MinusOperator node) throws VisitorException
64     {
65         checkBinaryNumaryOperatorNode(node);
66     }
67
68     public void visitDiv(DivOperator node) throws VisitorException
69     {
70         checkBinaryNumaryOperatorNode(node);
71     }
72
73     public void visitMult(MultOperator node) throws VisitorException
74     {
75         checkBinaryNumaryOperatorNode(node);
76     }
77
78     public void visitSubstr(SubstrOperator node) throws VisitorException
79     {
80         if (node.isStatic())
81         {
82             if (node.left().isString() && node.right().isString())
83             {
84                 return;
85             }
86             throw new StaticTypeException("~ Operator expects 2 Strings");
87         }
88     }
89
90     public void visitAnd(AndOperator and) throws VisitorException
91     {
92         if (and.isStatic())
93         {
94             if (and.left().isBoolean() && and.right().isBoolean())
95             {
96                 return;
97             }
98             throw new StaticTypeException("bool value expected");
99         }
100     }
101
102 }
Popular Tags