add support for character literals using single quotes

pull/21/head
Ondřej Hruška 4 years ago
parent ce5bf513c0
commit 562fc48151
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 31
      crsn/src/asm/parse/parse_data.rs
  2. 4
      examples/stacks.csn

@ -83,22 +83,20 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnE
return Ok(DataDisp::Discard); return Ok(DataDisp::Discard);
} }
// check if we have an alias defined if s.starts_with('\'') && s.ends_with('\'') {
{ if s.chars().count() == 3 {
let pstate = pcx.state.borrow(); return Ok(DataDisp::Immediate(s.chars().nth(1).unwrap() as u64));
if let Some(val) = pstate.constants.get(&s) { } else if s.chars().count() == 4 && s.chars().nth(1).unwrap() == '\\' {
return Ok(DataDisp::Immediate(*val)); return Ok(DataDisp::Immediate(s.chars().nth(2).unwrap() as u64));
} } else {
return Err(CrsnError::Parse(format!("Invalid character literal synax {}", s).into(), pos));
if let Some(val) = pstate.reg_aliases.get(&s) {
return Ok(DataDisp::Register(*val));
} }
} }
if let Some(reference) = s.strip_prefix('@') { if let Some(reference) = s.strip_prefix('@') {
let pstate = pcx.state.borrow(); let pstate = pcx.state.borrow();
if let Some(val) = pstate.reg_aliases.get(reference) { if let Some(reg) = pstate.reg_aliases.get(reference) {
Ok(DataDisp::ObjectPtr(*val)) Ok(DataDisp::ObjectPtr(*reg))
} else { } else {
let reg = reg::parse_reg(reference, &pos)?; let reg = reg::parse_reg(reference, &pos)?;
@ -108,9 +106,18 @@ pub fn parse_data_disp(tok: Sexp, pcx: &ParserContext) -> Result<DataDisp, CrsnE
Ok(DataDisp::ObjectPtr(reg)) Ok(DataDisp::ObjectPtr(reg))
} }
} }
} else if s.starts_with(|c: char| c.is_ascii_digit()) { } else if s.starts_with(|c: char| c.is_ascii_digit() || c == '-') {
Ok(DataDisp::Immediate(unsafe { std::mem::transmute(parse_i64(&s, &pos)?) })) Ok(DataDisp::Immediate(unsafe { std::mem::transmute(parse_i64(&s, &pos)?) }))
} else { } else {
let pstate = pcx.state.borrow();
if let Some(val) = pstate.constants.get(&s) {
return Ok(DataDisp::Immediate(*val));
}
if let Some(val) = pstate.reg_aliases.get(&s) {
return Ok(DataDisp::Register(*val));
}
let reg = reg::parse_reg(&s, &pos)?; let reg = reg::parse_reg(&s, &pos)?;
let pstate = pcx.state.borrow(); let pstate = pcx.state.borrow();

@ -11,7 +11,7 @@
(cmp r1 1 (ne? (fault))) (cmp r1 1 (ne? (fault)))
; test reverse push ; test reverse push
(push @r0 1) (push @r0 '🐈')
(push @r0 7) (push @r0 7)
(rpush @r0 8) (rpush @r0 8)
@ -19,7 +19,7 @@
(cmp r1 7 (ne? (fault))) (cmp r1 7 (ne? (fault)))
(pop r1 @r0) (pop r1 @r0)
(cmp r1 1 (ne? (fault))) (cmp r1 '🐈' (ne? (fault)))
(pop r1 @r0) (pop r1 @r0)
(cmp r1 8 (ne? (fault))) (cmp r1 8 (ne? (fault)))

Loading…
Cancel
Save