I usually seperate the functions into 3 sections. Input, lexical analyzer, and parser. The input just sends the next character to the lexical analyzer, the lexical analyzer strings the input into tokens, and the parser actually acts upon the whole thing.
I know that seems like a lot for just changing colors, but it'll make it easier when you add functions, as well, as changing input. Like you could change to reading from a file or over a network connection just by changing the GetCharacter routine.
Const TOKEN_EOL = 0 Const TOKEN_TAG = 1 Const TOKEN_STRING = 2 Global push = False Global pushed$ Global message$ Global messagepointer = 1 Global token$ Global TextX = 0 Global TextY = 0 ;character input Function GetCharacter$() If push = True ;if a character has been pushed onto the buffer, send it first DebugLog pushed$ push = False Return pushed$ End If If messagepointer > Len(message$) Then Return "eol" c$ = Mid$(message$,messagepointer,1) messagepointer = messagepointer + 1 Return c$ End Function Function PushCharacter(c$) Pushed = c$ Push = True End Function Function Lex() c$ = GetCharacter() Select c$ Case "[" c$ = GetCharacter() Token = "" While c$ <> "]" And c$<> "eol" Token = Token + c$ c$ = GetCharacter() Wend Return TOKEN_TAG Case "eol" Token$ = "" Return TOKEN_EOL Default Token$ = "" While c$ <> "["And c$<>"eol" Token = Token + c$ c$ = GetCharacter() Wend PushCharacter(c$) Return TOKEN_STRING End Select End Function Function DisplayText(TextString$) Message$ = TextString$ Repeat TokenValue = Lex() DebugLog TokenValue +" " + Token$ Select TokenValue Case TOKEN_EOL Return Case TOKEN_TAG Select Lower(Token$) Case "r" Color 255,0,0 Case "g" Color 0,255,0 Case "b" Color 0,0,255 Case "w" Color 255,255,255 Case "p" Color 255,0,255 Case "n" Color 212,212,212 Case "return" TextX = 0 TextY = TextY + 16 Default RuntimeError "Don't recognize tag " + Token End Select Default Text TextX,TextY,Token$ TextX = TextX + 8*Len(Token) End Select Forever End Function Graphics 800,600 m$ = "[R]Please [G]Change [B]My [P]colors[Return]" m$ = m$ + "[N]And this is default / normal text and this should be [B]blue Text" DisplayText(m$) WaitKey()
I know that seems like a lot for just changing colors, but it'll make it easier when you add functions, as well, as changing input. Like you could change to reading from a file or over a network connection just by changing the GetCharacter routine.