/// Convert a chunk of AST words into a vec of `NamedStdRuntimeWord`s
Convert a chunk of AST words into a vec of `NamedStdRuntimeWord`s
Convert a chunk of AST words into a vec of ` NamedStdRuntimeWord ` s
fn to_named_rt_words(self, dict: &mut Dict) -> Vec<NamedStdRuntimeWord> {
        let mut ret = vec![];
        match self {
            Chunk::IfThen { if_body } => {
                // First, convert the body into a sequence
                let mut conv: VecDeque<NamedStdRuntimeWord> = if_body
                    .into_iter()
                    .map(|m| m.to_named_rt_words(dict))
                    .flatten()
                    .collect();
                conv.push_front(NamedStdRuntimeWord {
                    name: "CRJ".into(),
                    word: RuntimeWord::CondRelativeJump {
                        offset: conv.len() as i32,
                        jump_on: false,
                    },
                });
                let conv: Vec<NamedStdRuntimeWord> = conv.into_iter().collect();
                ret.extend(conv);
            }
            Chunk::IfElseThen { if_body, else_body } => {
                let mut if_conv: VecDeque<NamedStdRuntimeWord> = if_body
                    .into_iter()
                    .map(|m| m.to_named_rt_words(dict))
                    .flatten()
                    .collect();
                let else_conv: Vec<NamedStdRuntimeWord> = else_body
                    .into_iter()
                    .map(|m| m.to_named_rt_words(dict))
                    .flatten()
                    .collect();
                if_conv.push_back(NamedStdRuntimeWord {
                    name: "UCRJ".into(),
                    word: RuntimeWord::UncondRelativeJump {
                        offset: else_conv.len() as i32,
                    },
                });
                if_conv.push_front(NamedStdRuntimeWord {
                    name: "CRJ".into(),
                    word: RuntimeWord::CondRelativeJump {
                        offset: if_conv.len() as i32,
                        jump_on: false,
                    },
                });
                let conv: Vec<NamedStdRuntimeWord> =
                    if_conv.into_iter().chain(else_conv.into_iter()).collect();
                ret.extend(conv);
            }
            Chunk::DoLoop { do_body } => {
                // First, convert the body into a sequence
                let mut conv: VecDeque<NamedStdRuntimeWord> = do_body
                    .into_iter()
                    .map(|m| m.to_named_rt_words(dict))
                    .flatten()
                    .collect();
                conv.push_back(NamedStdRuntimeWord {
                    word: RuntimeWord::Verb(BuiltinToken::new(crate::builtins::bi_priv_loop)),
                    name: "PRIV_LOOP".into(),
                });
                let len = conv.len();
                conv.push_front(NamedStdRuntimeWord {
                    word: RuntimeWord::Verb(BuiltinToken::new(crate::builtins::bi_retstk_push)),
                    name: ">r".into(),
                });
                conv.push_front(NamedStdRuntimeWord {
                    word: RuntimeWord::Verb(BuiltinToken::new(crate::builtins::bi_retstk_push)),
                    name: ">r".into(),
                });
                // The Minus One here accounts for the addition of the CRJ. We should not loop back to
                // the double `>r`s, as those only happen once at the top of the loop.
                conv.push_back(NamedStdRuntimeWord {
                    word: RuntimeWord::CondRelativeJump {
                        offset: -1 * len as i32 - 1,
                        jump_on: false,
                    },
                    name: "CRJ".into(),
                });
                let conv: Vec<NamedStdRuntimeWord> = conv.into_iter().collect();
                ret.extend(conv);
            }
            Chunk::Token(tok) => {
                ret.push(if let Some(bi) = dict.bis.get(&tok).cloned() {
                    NamedStdRuntimeWord {
                        name: tok,
                        word: RuntimeWord::Verb(bi.clone()),
                    }
                } else if dict.data.contains_key(&tok) {
                    NamedStdRuntimeWord {
                        word: RuntimeWord::VerbSeq(VerbSeqInner::from_word(tok.clone())),
                        name: tok,
                    }
                } else if let Some(num) = parse_num(&tok) {
                    NamedStdRuntimeWord {
                        word: RuntimeWord::LiteralVal(num),
                        name: format!("LIT({})", num),
                    }
                } else {
                    panic!("{:?}", tok);
                    // return Err(Error::InternalError);
                });
            }
            Chunk::Comment { .. } => {
                // Nothing to do for comments
            }
        }
        ret
    }
fn to_named_rt_words ( self , dict : & mut Dict ) -> Vec < NamedStdRuntimeWord > { let mut ret = vec ! [ ] ; match self { Chunk :: IfThen { if_body } => { // First, convert the body into a sequence let mut conv : VecDeque < NamedStdRuntimeWord > = if_body . into_iter ( ) . map ( | m | m . to_named_rt_words ( dict ) ) . flatten ( ) . collect ( ) ; conv . push_front ( NamedStdRuntimeWord { name : " " . into ( ) , word : RuntimeWord :: CondRelativeJump { offset : conv . len ( ) as i32 , jump_on : false , } , } ) ; let conv : Vec < NamedStdRuntimeWord > = conv . into_iter ( ) . collect ( ) ; ret . extend ( conv ) ; } Chunk :: IfElseThen { if_body , else_body } => { let mut if_conv : VecDeque < NamedStdRuntimeWord > = if_body . into_iter ( ) . map ( | m | m . to_named_rt_words ( dict ) ) . flatten ( ) . collect ( ) ; let else_conv : Vec < NamedStdRuntimeWord > = else_body . into_iter ( ) . map ( | m | m . to_named_rt_words ( dict ) ) . flatten ( ) . collect ( ) ; if_conv . push_back ( NamedStdRuntimeWord { name : " " . into ( ) , word : RuntimeWord :: UncondRelativeJump { offset : else_conv . len ( ) as i32 , } , } ) ; if_conv . push_front ( NamedStdRuntimeWord { name : " " . into ( ) , word : RuntimeWord :: CondRelativeJump { offset : if_conv . len ( ) as i32 , jump_on : false , } , } ) ; let conv : Vec < NamedStdRuntimeWord > = if_conv . into_iter ( ) . chain ( else_conv . into_iter ( ) ) . collect ( ) ; ret . extend ( conv ) ; } Chunk :: DoLoop { do_body } => { // First, convert the body into a sequence let mut conv : VecDeque < NamedStdRuntimeWord > = do_body . into_iter ( ) . map ( | m | m . to_named_rt_words ( dict ) ) . flatten ( ) . collect ( ) ; conv . push_back ( NamedStdRuntimeWord { word : RuntimeWord :: Verb ( BuiltinToken :: new ( crate :: builtins :: bi_priv_loop ) ) , name : " " . into ( ) , } ) ; let len = conv . len ( ) ; conv . push_front ( NamedStdRuntimeWord { word : RuntimeWord :: Verb ( BuiltinToken :: new ( crate :: builtins :: bi_retstk_push ) ) , name : " " . into ( ) , } ) ; conv . push_front ( NamedStdRuntimeWord { word : RuntimeWord :: Verb ( BuiltinToken :: new ( crate :: builtins :: bi_retstk_push ) ) , name : " " . into ( ) , } ) ; // The Minus One here accounts for the addition of the CRJ. We should not loop back to // the double `>r`s, as those only happen once at the top of the loop. conv . push_back ( NamedStdRuntimeWord { word : RuntimeWord :: CondRelativeJump { offset : - 1 * len as i32 - 1 , jump_on : false , } , name : " " . into ( ) , } ) ; let conv : Vec < NamedStdRuntimeWord > = conv . into_iter ( ) . collect ( ) ; ret . extend ( conv ) ; } Chunk :: Token ( tok ) => { ret . push ( if let Some ( bi ) = dict . bis . get ( & tok ) . cloned ( ) { NamedStdRuntimeWord { name : tok , word : RuntimeWord :: Verb ( bi . clone ( ) ) , } } else if dict . data . contains_key ( & tok ) { NamedStdRuntimeWord { word : RuntimeWord :: VerbSeq ( VerbSeqInner :: from_word ( tok . clone ( ) ) ) , name : tok , } } else if let Some ( num ) = parse_num ( & tok ) { NamedStdRuntimeWord { word : RuntimeWord :: LiteralVal ( num ) , name : format ! ( " " num ) , } } else { panic ! ( " " tok ) ; // return Err(Error::InternalError); } ) ; } Chunk :: Comment { .. } => { // Nothing to do for comments } } ret }
Convert a chunk of AST words into a vec of `NamedStdRuntimeWord`s
Convert a chunk of AST words into a vec of ` NamedStdRuntimeWord ` s
self
dict
&mut Dict