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) |
||||||
|
) |
||||||
|
) |
@ -1,26 +1,24 @@ |
|||||||
; Set log level to "info" or above for the best results! |
; Set log level to "info" or above for the best results! |
||||||
|
|
||||||
( |
( |
||||||
(proc main |
(sc-init 800 600) |
||||||
(sc-init 800 600) |
(sc-opt 1 1) ; auto blit |
||||||
(sc-opt 1 1) ; auto blit |
(sc-opt 2 25) ; frame rate |
||||||
(sc-opt 2 25) ; frame rate |
|
||||||
|
|
||||||
(ld r0 5) ; x |
(ld r0 5) ; x |
||||||
(ld r1 0) ; y |
(ld r1 0) ; y |
||||||
|
|
||||||
(ld r2 1) ; dx |
(ld r2 1) ; dx |
||||||
(ld r3 1) ; dy |
(ld r3 1) ; dy |
||||||
|
|
||||||
(ld r5 0x3300ff) |
(ld r5 0x3300ff) |
||||||
|
|
||||||
(:loop) |
(:loop) |
||||||
(add r5 0x000001) |
(add r5 0x000001) |
||||||
(sc-px r0 r1 r5) |
(sc-px r0 r1 r5) |
||||||
(add r0 r2) |
(add r0 r2) |
||||||
(add r1 r3) |
(add r1 r3) |
||||||
(cmp r0 799 (eq? (ld r2 -1)) (ne? (cmp r0 0 (eq? (ld r2 1))))) |
(cmp r0 799 (eq? (ld r2 -1)) (ne? (cmp r0 0 (eq? (ld r2 1))))) |
||||||
(cmp r1 599 (eq? (ld r3 -1)) (ne? (cmp r1 0 (eq? (ld r3 1))))) |
(cmp r1 599 (eq? (ld r3 -1)) (ne? (cmp r1 0 (eq? (ld r3 1))))) |
||||||
(j :loop) |
(j :loop) |
||||||
) |
(fault "unreachable") |
||||||
) |
) |
||||||
|
Loading…
Reference in new issue