diff options
author | Adrian Thurston <thurston@complang.org> | 2010-11-23 19:34:00 +0000 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2010-11-23 19:34:00 +0000 |
commit | 3c6cb4eb461e2ba1976f81df206055ceb85aa2eb (patch) | |
tree | 143eb08b9bff8308912541e1e8ecaade7b58260c /colm | |
parent | cbdb77e6b8f1b21de37564c9c455e39e472b558a (diff) | |
download | colm-3c6cb4eb461e2ba1976f81df206055ceb85aa2eb.tar.gz |
Added a node type for else blocks. This will simplify scopes for if statements.
Diffstat (limited to 'colm')
-rw-r--r-- | colm/analysis.cpp | 9 | ||||
-rw-r--r-- | colm/compile.cpp | 9 | ||||
-rw-r--r-- | colm/lmparse.kl | 36 | ||||
-rw-r--r-- | colm/parsetree.h | 9 |
4 files changed, 42 insertions, 21 deletions
diff --git a/colm/analysis.cpp b/colm/analysis.cpp index 908a59fc..53f1da61 100644 --- a/colm/analysis.cpp +++ b/colm/analysis.cpp @@ -159,13 +159,14 @@ void LangStmt::analyze( ParseData *pd ) const for ( StmtList::Iter stmt = *stmtList; stmt.lte(); stmt++ ) stmt->analyze( pd ); - if ( elsePart != 0 ) { - for ( StmtList::Iter stmt = *elsePart; stmt.lte(); stmt++ ) - stmt->analyze( pd ); - } + if ( elsePart != 0 ) + elsePart->analyze( pd ); break; } + case ElseType: { + break; + } case RejectType: break; case WhileType: { diff --git a/colm/compile.cpp b/colm/compile.cpp index 857a5e91..49c6872b 100644 --- a/colm/compile.cpp +++ b/colm/compile.cpp @@ -2157,8 +2157,7 @@ void LangStmt::compile( ParseData *pd, CodeVect &code ) const if ( elsePart != 0 ) { /* Compile the else branch. */ - for ( StmtList::Iter stmt = *elsePart; stmt.lte(); stmt++ ) - stmt->compile( pd, code ); + elsePart->compile( pd, code ); /* Set the distance for jump over the else part. */ distance = code.length() - jumpPastElse - 3; @@ -2167,6 +2166,12 @@ void LangStmt::compile( ParseData *pd, CodeVect &code ) const break; } + case ElseType: { + /* Compile the else branch. */ + for ( StmtList::Iter stmt = *stmtList; stmt.lte(); stmt++ ) + stmt->compile( pd, code ); + break; + } case RejectType: { code.append( IN_REJECT ); break; diff --git a/colm/lmparse.kl b/colm/lmparse.kl index b0187244..9b19ac7a 100644 --- a/colm/lmparse.kl +++ b/colm/lmparse.kl @@ -1268,9 +1268,9 @@ statement: code_expr final { $$->stmt = new LangStmt( InputLoc(), LangStmt::ExprType, $1->expr ); }; -statement: KW_If code_expr block_or_single elsif_list +statement: if_stmt final { - $$->stmt = new LangStmt( LangStmt::IfType, $2->expr, $3->stmtList, $4->stmtList ); + $$->stmt = $1->stmt; }; statement: KW_Reject final { @@ -1378,26 +1378,36 @@ iter_call: TK_Word new LangVarRef( $1->loc, new QualItemVect, $1->data ) ); }; +# +# If Statements +# +# IF +# IF +# Else + +nonterm if_stmt uses statement; + +if_stmt: KW_If code_expr block_or_single elsif_list + final { + $$->stmt = new LangStmt( LangStmt::IfType, $2->expr, $3->stmtList, $4->stmt ); + }; + nonterm elsif_list { - StmtList *stmtList; + LangStmt *stmt; }; elsif_list: elsif_clause elsif_list final { /* Put any of the followng elseif part, an else, or null into the elsePart. */ - $1->stmt->elsePart = $2->stmtList; - - /* Make a statement list with jsut the elseif clause in it. It will go into - * some else part. */ - $$->stmtList = new StmtList; - $$->stmtList->append( $1->stmt ); + $$->stmt = $1->stmt; + $$->stmt->elsePart = $2->stmt; }; elsif_list: optional_else final { - $$->stmtList = $1->stmtList; + $$->stmt = $1->stmt; }; nonterm elsif_clause @@ -1413,18 +1423,18 @@ elsif_clause: nonterm optional_else { - StmtList *stmtList; + LangStmt *stmt; }; optional_else: KW_Else block_or_single final { - $$->stmtList = $2->stmtList; + $$->stmt = new LangStmt( LangStmt::ElseType, $2->stmtList ); }; optional_else: final { - $$->stmtList = 0; + $$->stmt = 0; }; nonterm code_expr_list diff --git a/colm/parsetree.h b/colm/parsetree.h index 07edaa96..fc1255eb 100644 --- a/colm/parsetree.h +++ b/colm/parsetree.h @@ -1693,6 +1693,7 @@ struct LangStmt PrintStreamType, ExprType, IfType, + ElseType, RejectType, WhileType, ReturnType, @@ -1724,7 +1725,10 @@ struct LangStmt LangStmt( Type type, LangExpr *expr, StmtList *stmtList ) : type(type), expr(expr), stmtList(stmtList), next(0) {} - LangStmt( Type type, LangExpr *expr, StmtList *stmtList, StmtList *elsePart ) : + LangStmt( Type type, StmtList *stmtList ) : + type(type), stmtList(stmtList), next(0) {} + + LangStmt( Type type, LangExpr *expr, StmtList *stmtList, LangStmt *elsePart ) : type(type), expr(expr), stmtList(stmtList), elsePart(elsePart), next(0) {} LangStmt( const InputLoc &loc, Type type ) : @@ -1769,7 +1773,8 @@ struct LangStmt ExprVect *exprPtrVect; FieldInitVect *fieldInitVect; StmtList *stmtList; - StmtList *elsePart; + /* Either another if, or an else. */ + LangStmt *elsePart; String name; /* Normally you don't need to initialize double list pointers, however, we |