@@ -56,71 +56,74 @@ namespace microcode {
5656 }
5757 }
5858 const addTile = ( tile : Tile , rule : RuleDefn ) => {
59+ control . assert ( rule != undefined )
5960 const tid = getTid ( tile )
61+ if ( isSensor ( tid ) ) rule . push ( tile , "sensors" , false )
6062 if ( isFilter ( tid ) ) rule . push ( tile , "filters" , false )
6163 else if ( isModifier ( tid ) ) rule . push ( tile , "modifiers" , false )
6264 else rule . push ( tile , "actuators" , false )
6365 }
66+ let cursor = 0
67+ const whiteSpace = ( s : string ) => {
68+ return s == " " || s == "\n" || s == "\t"
69+ }
70+ const getToken = ( ) => {
71+ let prev = cursor
72+ let gotToken = false
73+ while ( cursor < str . length ) {
74+ if ( whiteSpace ( str [ cursor ] ) ) {
75+ if ( gotToken ) return str . slice ( prev , cursor )
76+ cursor ++
77+ prev = cursor
78+ } else {
79+ gotToken = true
80+ cursor ++
81+ }
82+ }
83+ if ( gotToken ) return str . slice ( prev , cursor )
84+ return undefined
85+ }
6486 const prog = new ProgramDefn ( )
6587 prog . pages = [ ]
66- const lines = str . split ( "\n" )
88+
6789 let currPage : PageDefn = undefined
6890 let currRule : RuleDefn = undefined
6991 let currTile : Tile = undefined
70- for ( let i = 0 ; i < lines . length ; i ++ ) {
71- console . log ( `lines[${ i } ] = ${ lines [ i ] } ` )
72- if ( currTile && currTile instanceof IconEditor ) {
73- const all5 = lines . slice ( i , i + 5 ) . join ( "\n" )
74- console . log ( `get the image?\n${ all5 } ` )
75- currTile . field = currTile . fieldEditor . fromString ( all5 )
76- currTile = undefined
77- i = i + 4 // loop count adds one more
78- continue
79- }
80- const tokens = lines [ i ] . split ( " " )
81- if ( tokens . length == 0 ) continue
82- console . log ( `tokens = ${ tokens . join ( ":" ) } ` )
83- let tok = tokens . shift ( )
92+ let tok : string = undefined
93+ while ( ( tok = getToken ( ) ) ) {
8494 console . log ( `tok1 = ${ tok } ` )
85- if ( tok == "Page" ) {
95+ if ( currTile ) {
96+ if (
97+ currTile instanceof IconEditor ||
98+ currTile instanceof MelodyEditor
99+ ) {
100+ control . assert ( tok == "`" )
101+ let iconTokens = [ ]
102+ while ( ( tok = getToken ( ) ) != "`" ) iconTokens . push ( tok )
103+ control . assert ( tok == "`" )
104+ currTile . field = currTile . fieldEditor . fromTokens ( iconTokens )
105+ } else if ( currTile instanceof DigitEditor ) {
106+ currTile . field = currTile . fieldEditor . fromTokens ( [ tok ] )
107+ }
108+ currTile = undefined
109+ } else if ( tok == "Page" ) {
86110 if ( currPage ) {
87111 if ( currRule ) currPage . rules . push ( currRule )
88112 prog . pages . push ( currPage )
89113 currRule = undefined
90114 }
91115 currPage = new PageDefn ( )
116+ getToken ( ) // consume page #
92117 continue
93118 } else if ( tok == "When" ) {
94119 control . assert ( currPage != undefined )
95120 if ( currRule ) currPage . rules . push ( currRule )
96- currRule = undefined
97- tok = tokens . shift ( )
98- }
99- for ( ; tok !== undefined ; tok = tokens . shift ( ) ) {
100- console . log ( `tok2 = ${ tok } ` )
101- if ( ! tok ) continue
102- if ( ! currRule ) {
103- currRule = new RuleDefn ( )
104- // can we have When followed by Do?
105- currRule . sensors . push ( token2tile ( tok ) as number )
106- continue
107- }
108- if ( tok == "Do" ) continue
121+ currRule = new RuleDefn ( )
122+ } else if ( tok == "Do" ) {
123+ control . assert ( currRule != undefined )
124+ } else {
109125 currTile = token2tile ( tok )
110126 addTile ( currTile , currRule )
111- if ( currTile instanceof IconEditor ) {
112- console . log ( `got IconEditor` )
113- break
114- } else if ( currTile instanceof DigitEditor ) {
115- currTile . field = currTile . fieldEditor . fromString ( tok )
116- currTile = undefined
117- } else if ( currTile instanceof MelodyEditor ) {
118- currTile . field = currTile . fieldEditor . fromString (
119- tok + tokens . join ( " " )
120- )
121- currTile = undefined
122- break
123- }
124127 }
125128 }
126129 if ( currRule ) currPage . rules . push ( currRule )
0 commit comments