From 3af6cc33ff246d31513b9a645403a9ece58983a0 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 6 Jan 2012 00:42:36 -0700 Subject: Two experimental SML converters. --- demo.stage | 4 +- src/options.cpp | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/smlnode.cpp | 4 ++ 3 files changed, 192 insertions(+), 2 deletions(-) diff --git a/demo.stage b/demo.stage index 5ba0c13..c644522 100644 --- a/demo.stage +++ b/demo.stage @@ -133,9 +133,9 @@ situation <> } enter { - display('''You're standing on a narrow stretch of beach. The ocean + display('''You're standing on a narrow stretch of beach.[break] The ocean sparkles to the north, there's a cliff face just to the south. The - beach extends westward. To the east are some jagged rocks.'''); + beach extends [green>westwardgetType() ) + { + case SmlNode::typeRoot: + sio << "

"; + for( SmlNode::SmlNodeList::const_iterator i = + pNode->getChildren().begin(); i; i++ ) + { + smlToHtml( *i ); + } + sio << "

"; + break; + + case SmlNode::typeText: + sio << pNode->getText(); + break; + + case SmlNode::typeTag: + if( pNode->getChildren().isEmpty() ) + { + if( pNode->getText() == "break" ) + sio << "

"; + } + else + { + if( pNode->getText() == "red" ) + { + sio << ""; + for( SmlNode::SmlNodeList::const_iterator i = + pNode->getChildren().begin(); i; i++ ) + { + smlToHtml( *i ); + } + sio << ""; + } + else if( pNode->getText() == "green" ) + { + sio << ""; + for( SmlNode::SmlNodeList::const_iterator i = + pNode->getChildren().begin(); i; i++ ) + { + smlToHtml( *i ); + } + sio << ""; + } + } + break; + } +} + +void appendToken( Bu::String &sCurLine, Bu::String &sNextToken, int &iLineLen ) +{ + if( iLineLen + sNextToken.getSize() + 1 >= 78 ) + { + sio << sCurLine << sio.nl; + iLineLen = 0; + sCurLine = sNextToken; + + } + else + { + sCurLine += sNextToken; + } + iLineLen += sNextToken.getSize() + 1; + sCurLine += " "; + sNextToken.clear(); +} + +void smlToConsole( const SmlNode *pNode, Bu::String &sCurLine, + Bu::String &sNextToken, int &iLineLen, int &iState ) +{ +// sio << "Begin: iState = " << iState << sio.nl; + switch( pNode->getType() ) + { + case SmlNode::typeRoot: + for( SmlNode::SmlNodeList::const_iterator i = + pNode->getChildren().begin(); i; i++ ) + { + smlToConsole( *i, sCurLine, sNextToken, iLineLen, iState ); + } + break; + + case SmlNode::typeText: + { + Bu::String::const_iterator iBgn = pNode->getText().begin(); + Bu::String::const_iterator iEnd = iBgn; + for(;iBgn;) + { +// sio << iState << ": [" << (iBgn?*iBgn:'-') << ":" +// << (iEnd?*iEnd:'-') << "]" +// << sio.nl; + int iTmpLen = 0; + switch( iState ) + { + case 0: // begining of paragraph + if( iBgn && ( *iBgn == ' ' || *iBgn == '\n' || + *iBgn == '\r' || *iBgn == '\t' ) ) + { + iBgn++; + } + else + { + // Here is where you would indent paragraphs + iEnd = iBgn; + iState = 1; + } + break; + + case 1: // non-whitespace + if( !iEnd ) + { + sNextToken.append( iBgn, iEnd ); + iBgn = iEnd; + } + else if( *iEnd == ' ' || *iEnd == '\n' || + *iEnd == '\r' || *iEnd == '\t' ) + { + sNextToken.append( iBgn, iEnd ); + iState = 2; + iBgn = iEnd; + } + else + { + iEnd++; + iTmpLen++; + } + break; + + case 2: // Whitespace + if( iBgn && (*iBgn == ' ' || *iBgn == '\n' || + *iBgn == '\r' || *iBgn == '\t') ) + { + iBgn++; + } + else + { + iEnd = iBgn; + iState = 1; + appendToken( sCurLine, sNextToken, iLineLen ); + } + break; + } + } + } + break; + + case SmlNode::typeTag: + if( pNode->getChildren().isEmpty() ) + { + if( pNode->getText() == "break" ) + { + appendToken( sCurLine, sNextToken, iLineLen ); + if( !sCurLine.isEmpty() ) + sio << sCurLine << sio.nl; + sCurLine.clear(); + iLineLen = 0; + iState = 0; + } + } + else + { + for( SmlNode::SmlNodeList::const_iterator i = + pNode->getChildren().begin(); i; i++ ) + { + smlToConsole( *i, sCurLine, sNextToken, iLineLen, iState ); + } + } + break; + } + +// sio << "Green? \x1b[1mhello\x1b[0m huh" << sio.nl; +} + +void smlToConsole( const SmlNode *pNode ) +{ + Bu::String sBuf, sBuf2; + int i1 = 0, i2 = 0; + smlToConsole( pNode, sBuf, sBuf2, i1, i2 ); + appendToken( sBuf, sBuf2, i1 ); + sio << sBuf << sBuf2 << sio.nl; +} + int Options::smlTest( Bu::Array aArgs ) { Bu::String sContent; @@ -80,6 +263,9 @@ int Options::smlTest( Bu::Array aArgs ) SmlNode *pRoot = SmlNode::parse( sContent ); sio << *pRoot << sio.nl; + + smlToConsole( pRoot ); + delete pRoot; exit( 0 ); diff --git a/src/smlnode.cpp b/src/smlnode.cpp index a9e533d..e2f8a74 100644 --- a/src/smlnode.cpp +++ b/src/smlnode.cpp @@ -13,6 +13,10 @@ SmlNode::SmlNode( Type eType, const Bu::String &sText, SmlNode *pParent ) : SmlNode::~SmlNode() { + for( SmlNodeList::iterator i = lChildren.begin(); i; i++ ) + { + delete *i; + } } SmlNode *SmlNode::parse( const Bu::String &sSrc ) -- cgit v1.2.3