Suponha
proc
getClassName() : String
begin
return
“A”; // esta é a
end
class Store
private:
var
n : integer;
public:
proc
get() : integer
begin
return
self.n;
end
proc
begin
self.n = n;
end
end
class SubStore subclassOf Store
end //
class Program
public:
proc
run()
var
s : Store;
begin
s = Store.new();
write(
s.getClassName() ); // imprime "Store"
s = SubStore.new();
write(
s.getClassName() ); // imprime "SubStore"
end
end
private void classDec() {
if ( lexer.token != Symbol.CLASS )
error.
lexer.nextToken();
if ( lexer.token != Symbol.IDENT )
error.
String
className = lexer.getStringValue();
lexer.nextToken();
currentClass = new ClassDec(className);
// note
//
//
MethodDec
m = new MethodDec("getClassName");
//
getClassName
m.setParameterList(null);
//
getClassName
m.setReturnType(Type.stringType);
StatementList sl = new StatementList();
//
st.addStatement( new ReturnStatement( new
LiteralStringExp(className) ) );
m.setStatementList(st);
// getClassName
m.setLocalVarList(null);
//
adiciona getClassName na
currentClass.addPublicMethod( m );
//
continua a
...
}
Veja
//
// acabou de
instVar =
new InstanceVariable(lexer.getStringValue());
instVarList.addElement(instVar);
localList.addElement(instVar);
lexer.nextToken();
if ( encontrou o
"g"
// a
//
//
instVar.you_should_create_a_get_method();
...
//
Enumeration e = localList.elements();
while (
e.hasMoreElements() ) {
p = (InstanceVariable ) e.nextElement();
p.setType(t);
if ( p.should_I_create_a_get_method() ) {
//
String
instanceVariableName = p.getName();
MethodDec
m = new MethodDec( "get" +
Character.toUpperCase(instanceVariableName.charAt(0)) +
instanceVariableName.substring(1)
);
m.setParameterList(null);
m.setReturnType(t);
StatementList sl = new StatementList();
st.addStatement( new ReturnStatement(new VariableExpr(p)) );
m.setStatementList(st);
m.setLocalVarList(null);
currentClass.addPublicMethod( m );
}
}
A