Modified:
/trunk/include/ast.h
/trunk/include/astvisitor.h
/trunk/src/parser.y
=======================================
--- /trunk/include/ast.h Tue May 17 07:45:31 2011
+++ /trunk/include/ast.h Tue May 17 17:14:59 2011
@@ -287,7 +287,7 @@
m_initial_value->addRef();
}
- ~VariableDecl() {
+ virtual ~VariableDecl() {
m_type->delRef();
m_variable->delRef();
@@ -322,6 +322,21 @@
DISALLOW_COPY_AND_ASSIGN(VariableDecl);
};
+class AttributeDeclaration : public VariableDecl {
+public:
+ AttributeDeclaration(ASTNode* modifier, ASTNode* type, ASTNode* variable)
+ : VariableDecl(type, variable), m_modifier(modifier) {
+ m_modifier->addRef();
+ }
+
+ virtual ~AttributeDeclaration() {
+ m_modifier->delRef();
+ }
+private:
+ ASTNode* m_modifier;
+ DISALLOW_COPY_AND_ASSIGN(AttributeDeclaration);
+};
+
class Identifier : public ASTNode {
public:
explicit Identifier(const CString* name) {
@@ -652,7 +667,7 @@
}
}
- ~FuncDeclaration() {
+ virtual ~FuncDeclaration() {
m_name->delRef();
if (m_return) {
m_return->delRef();
@@ -687,14 +702,15 @@
MethodDeclaration(ASTNode* modifier, ASTNode* rtype, ASTNode* name,
ASTNode* args, ASTNode* block)
: FuncDeclaration(name, rtype, args, block), m_modifier(modifier) {
- modifier->addRef();
+ m_modifier->addRef();
}
- ~MethodDeclaration() {
+ virtual ~MethodDeclaration() {
+ m_modifier->delRef();
}
ASTNode* get_modifier() { return m_modifier; }
-private:
+protected:
ASTNode* m_modifier;
};
@@ -881,12 +897,24 @@
ClassStmtList() { }
~ClassStmtList() {
- std::list<MethodDeclaration*>::iterator
- it = m_methods_decl.begin(), end =
m_methods_decl.end();
-
- while (it != end) {
- (*it)->delRef();
- ++it;
+ {
+ std::list<MethodDeclaration*>::iterator
+ it = m_methods_decl.begin(), end =
m_methods_decl.end();
+
+ while (it != end) {
+ (*it)->delRef();
+ ++it;
+ }
+ }
+
+ {
+ std::list<AttributeDeclaration*>::iterator
+ it = m_attrib_decl.begin(), end = m_attrib_decl.end();
+
+ while (it != end) {
+ (*it)->delRef();
+ ++it;
+ }
}
}
@@ -894,15 +922,30 @@
MethodDeclaration* m =
static_cast<MethodDeclaration*>(method);
m_methods_decl.push_back(m);
m->addRef();
-
- std::cout << m->get_name()->get_value()->get_name()->str()
<< std::endl;
+
+ std::cout << "Method: " <<
+ m->get_name()->get_value()->get_name()->str() << std::endl;
+ }
+
+ void addAttribute(ASTNode* attribute) throw() {
+ AttributeDeclaration* attr =
static_cast<AttributeDeclaration*>(attribute);
+ m_attrib_decl.push_back(attr);
+ attr->addRef();
+
+ std::cout << "Attribute: " <<
+ attr->get_variable()->get_value()->get_name()->str() << std::endl;
}
std::list<MethodDeclaration*>& get_methods_decl() throw() {
return m_methods_decl;
}
+
+ std::list<AttributeDeclaration*>& get_attrib_decl() throw() {
+ return m_attrib_decl;
+ }
private:
std::list<MethodDeclaration*> m_methods_decl;
+ std::list<AttributeDeclaration*> m_attrib_decl;
DISALLOW_COPY_AND_ASSIGN(ClassStmtList);
};
=======================================
--- /trunk/include/astvisitor.h Tue May 17 16:46:31 2011
+++ /trunk/include/astvisitor.h Tue May 17 17:14:59 2011
@@ -53,6 +53,7 @@
class ArgumentList;
class FuncDeclaration;
class MethodDeclaration;
+class AttributeDeclaration;
class ReturnStmt;
#define AST_VISITOR(type, exprtype) void type::visit(exprtype* expr)
throw()
@@ -87,6 +88,7 @@
AST_VISITOR_DECL_VIRTUAL(ImportStmt);
AST_VISITOR_DECL_VIRTUAL(FuncDeclaration);
AST_VISITOR_DECL_VIRTUAL(MethodDeclaration);
+ AST_VISITOR_DECL_VIRTUAL(AttributeDeclaration);
AST_VISITOR_DECL_VIRTUAL(ReturnStmt);
private:
DISALLOW_COPY_AND_ASSIGN(ASTVisitor);
@@ -145,6 +147,7 @@
AST_VISITOR_DECL(ImportStmt);
AST_VISITOR_DECL(FuncDeclaration);
AST_VISITOR_DECL(MethodDeclaration);
+ AST_VISITOR_DECL(AttributeDeclaration);
AST_VISITOR_DECL(ReturnStmt);
private:
=======================================
--- /trunk/src/parser.y Tue May 17 07:45:31 2011
+++ /trunk/src/parser.y Tue May 17 17:14:59 2011
@@ -205,7 +205,7 @@
;
class_stmt_no_empty:
- class_stmt attribute_declaration
+ class_stmt attribute_declaration {
static_cast<ast::ClassStmtList*>($1)->addAttribute($2); $$ = $1; }
| class_stmt method_declaration {
static_cast<ast::ClassStmtList*>($1)->addMethod($2); $$ = $1; }
;
@@ -214,7 +214,7 @@
;
attribute_declaration:
- access_modifier TYPE IDENT ';'
+ access_modifier TYPE IDENT ';' { $$ = new
ast::AttributeDeclaration($1, $2, $3); }
;
arg_list: