forked from MightyPork/crsn
parent
afd412f22a
commit
e580a2b679
@ -0,0 +1,47 @@ |
||||
use sexp::Sexp; |
||||
use crate::asm::parse::{ParserContext, parse_instructions}; |
||||
use crate::asm::instr::{Flatten, Routine}; |
||||
use crate::asm::error::CrsnError; |
||||
use crate::asm::parse::sexp_expect::expect_string_atom; |
||||
use crate::asm::parse::arg_parser::TokenParser; |
||||
use crate::asm::patches::SexpIsA; |
||||
use crate::asm::parse::parse_data::parse_reg_alias; |
||||
use crate::asm::data::Register; |
||||
use crate::asm::data::literal::RoutineName; |
||||
|
||||
pub fn parse_routine(mut toki: impl Iterator<Item=Sexp> + Clone, pcx: &ParserContext) -> Result<Box<dyn Flatten>, CrsnError> { |
||||
let name = expect_string_atom(toki.next())?; |
||||
|
||||
let arg_tokens = TokenParser::new(toki.clone().take_while(|e| e.is_atom()).collect(), pcx); |
||||
let toki = toki.skip_while(|e| e.is_atom()); |
||||
|
||||
{ |
||||
let mut pstate = pcx.state.borrow_mut(); |
||||
|
||||
let old = std::mem::replace(&mut pstate.reg_aliases, Default::default()); |
||||
pstate.reg_alias_stack.push(old); |
||||
|
||||
for (n, tok) in arg_tokens.into_iter().enumerate() { |
||||
let alias = parse_reg_alias(Some(tok))?; |
||||
|
||||
if pstate.constants.contains_key(&alias) { |
||||
return Err(CrsnError::Parse(format!("Symbol \"{}\" already used for a constant.", alias).into())); |
||||
} |
||||
|
||||
pstate.reg_aliases.insert(alias, Register::Arg(n as u8)); |
||||
} |
||||
} |
||||
|
||||
let body = parse_instructions(toki, pcx)?; |
||||
|
||||
{ |
||||
let mut pstate = pcx.state.borrow_mut(); |
||||
let old = pstate.reg_alias_stack.pop().unwrap(); |
||||
pstate.reg_aliases = old; |
||||
} |
||||
|
||||
return Ok(Box::new(Routine { |
||||
name: RoutineName(name), |
||||
body, |
||||
})); |
||||
} |
@ -0,0 +1,13 @@ |
||||
( |
||||
(def FOO 123) ; define a constant |
||||
(call my-add 7 8) |
||||
(cmp res0 138 (ne? (fault "assert failed"))) |
||||
(halt) |
||||
|
||||
(proc my-add a b ; give arguments names |
||||
(sym q r0) ; give register a temporary alias |
||||
(add q a b) |
||||
(add q FOO) |
||||
(ret q) |
||||
) |
||||
) |
Loading…
Reference in new issue