Croissant Runtime
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crsn/crsn/src/asm/parse/mod.rs

50 lines
1.4 KiB

use std::cell::RefCell;
use std::collections::HashMap;
4 years ago
use std::sync::atomic::AtomicU32;
4 years ago
4 years ago
pub use parse_instr::parse_instructions;
4 years ago
use crate::asm::data::literal::{ConstantName, RegisterAlias, Value};
use crate::asm::data::Register;
4 years ago
use crate::asm::error::CrsnError;
4 years ago
use crate::asm::instr::Op;
use crate::asm::parse::sexp_expect::expect_list;
use crate::module::CrsnExtension;
4 years ago
pub mod parse_cond;
pub mod parse_instr;
pub mod parse_data;
pub mod sexp_expect;
pub mod parse_op;
pub mod arg_parser;
pub mod parse_routine;
pub struct ParserContext<'a> {
/// Extension modules
pub parsers: &'a [Box<dyn CrsnExtension>],
/// Mutable state
pub state: RefCell<ParserState>,
}
#[derive(Default)]
pub struct ParserState {
/// Register aliases within the routine
pub reg_aliases: HashMap<RegisterAlias, Register>,
/// The old reg aliases map is pushed here when entering a routine definition
pub reg_alias_stack: Vec<HashMap<RegisterAlias, Register>>,
/// Global constants
pub constants: HashMap<ConstantName, Value>,
}
pub fn parse(source: &str, parsers: &ParserContext) -> Result<Vec<Op>, CrsnError> {
let items = expect_list(Some(sexp::parse(source)?), true)?;
4 years ago
/* numbered labels start with a weird high number
to avoid conflicts with user-defined numbered labels */
let label_num = AtomicU32::new(0x7890_0000);
parse_instructions(items.into_iter(), parsers)?
.flatten(&label_num)
4 years ago
}