1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 /* Generated By:JJTree: Do not edit this line. ASTIdent.java */ 19 /* JJT: 0.3pre1 */ 20 21 package Mini; 22 import org.apache.bcel.generic.ConstantPoolGen; 23 import org.apache.bcel.generic.ILOAD; 24 import org.apache.bcel.generic.InstructionList; 25 import org.apache.bcel.generic.LocalVariableGen; 26 import org.apache.bcel.generic.MethodGen; 27 import org.apache.bcel.generic.PUSH; 28 29 /** 30 * 31 * @version $Id$ 32 */ 33 public class ASTIdent extends ASTExpr implements org.apache.bcel.Constants { 34 private String name; 35 private Variable reference; // Reference in environment to decl of this ident 36 37 // Generated methods ASTIdent(int id)38 ASTIdent(int id) { 39 super(id); 40 } 41 ASTIdent(MiniParser p, int id)42 ASTIdent(MiniParser p, int id) { 43 super(p, id); 44 } 45 jjtCreate(MiniParser p, int id)46 public static Node jjtCreate(MiniParser p, int id) { 47 return new ASTIdent(p, id); 48 } 49 ASTIdent(String name, int type, int line, int column)50 public ASTIdent(String name, int type, int line, int column) { 51 super(line, column, JJTIDENT); 52 53 this.name = name; 54 this.type = type; 55 } 56 57 // closeNode, dump inherited 58 59 /** 60 * @return identifier and line/column number of appearance 61 */ 62 @Override toString()63 public String toString() { 64 return super.toString() + " = " + name; 65 } 66 67 /** 68 * Overrides ASTExpr.traverse() 69 */ 70 @Override traverse(Environment env)71 public ASTExpr traverse(Environment env) { 72 EnvEntry entry = env.get(name); 73 74 if(entry == null) { 75 MiniC.addError(line, column, "Undeclared identifier " + name); 76 } else if(entry instanceof Function) { 77 MiniC.addError(line, column, 78 "Function " + name + " used as an identifier."); 79 } else { 80 reference = (Variable)entry; 81 } 82 83 return this; // Nothing to reduce/traverse further here 84 } 85 86 /** 87 * Overrides AstExpr.eval() 88 */ 89 @Override eval(int expected)90 public int eval(int expected) { 91 ASTIdent ident = reference.getName(); 92 int t = ident.getType(); 93 94 is_simple = true; // (Very) simple expression, always true 95 96 if((t == T_UNKNOWN) && (expected == T_UNKNOWN)) { 97 type = T_UNKNOWN; 98 } else if((t == T_UNKNOWN) && (expected != T_UNKNOWN)) { 99 ident.setType(expected); 100 type = expected; 101 } 102 else if((t != T_UNKNOWN) && (expected == T_UNKNOWN)) { 103 ident.setType(t); 104 type = t; 105 } else { 106 type = t; // Caller has to check for an error, i.e. t != expected 107 } 108 109 return type; 110 } 111 112 /** 113 * Fourth pass, produce Java code. 114 */ 115 @Override code(StringBuffer buf)116 public void code(StringBuffer buf) { 117 if(name.equals("TRUE")) { 118 ASTFunDecl.push(buf, "1"); 119 } else if(name.equals("FALSE")) { 120 ASTFunDecl.push(buf, "0"); 121 } else { 122 ASTFunDecl.push(buf, name); 123 } 124 } 125 126 /** 127 * Fifth pass, produce Java byte code. 128 */ 129 @Override byte_code(InstructionList il, MethodGen method, ConstantPoolGen cp)130 public void byte_code(InstructionList il, MethodGen method, ConstantPoolGen cp) { 131 if(name.equals("TRUE")) { 132 il.append(new PUSH(cp, 1)); 133 } else if(name.equals("FALSE")) { 134 il.append(new PUSH(cp, 0)); 135 } else { 136 LocalVariableGen local_var = reference.getLocalVariable(); 137 il.append(new ILOAD(local_var.getIndex())); 138 } 139 ASTFunDecl.push(); 140 } 141 142 setName(String name)143 public void setName(String name) { this.name = name; } getName()144 public String getName() { return name; } 145 } 146