This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
huia/huia-compiler/src/block.rs

66 lines
1.2 KiB
Rust
Raw Normal View History

2019-03-17 18:52:21 +13:00
use crate::ir::IR;
use crate::stable::StringIdx;
use crate::ty::TyIdx;
2019-03-18 18:54:21 +13:00
use std::slice::IterMut;
2019-03-17 18:52:21 +13:00
2019-03-18 18:54:21 +13:00
#[derive(Debug, Default, PartialEq, Clone)]
2019-03-17 18:52:21 +13:00
pub struct Block {
arguments: Vec<(StringIdx, TyIdx)>,
2019-03-17 18:52:21 +13:00
ir: Vec<IR>,
}
impl Block {
2019-03-18 18:54:21 +13:00
/// Consumes the block and returns the IR data.
pub fn ir(self) -> Vec<IR> {
self.ir
2019-03-17 18:52:21 +13:00
}
pub fn is_empty(&self) -> bool {
self.ir.is_empty()
}
2019-03-18 18:54:21 +13:00
pub fn iter_mut(&mut self) -> IterMut<IR> {
self.ir.iter_mut()
}
pub fn new(arguments: Vec<(StringIdx, TyIdx)>) -> Block {
Block {
arguments,
ir: Vec::default(),
}
}
2019-03-18 18:54:21 +13:00
pub fn pop(&mut self) -> Option<IR> {
self.ir.pop()
}
pub fn push(&mut self, ir: IR) {
self.ir.push(ir);
2019-03-17 18:52:21 +13:00
}
pub fn set_arguments(&mut self, arguments: Vec<(StringIdx, TyIdx)>) {
self.arguments = arguments;
}
2019-03-17 18:52:21 +13:00
}
#[derive(Debug, Clone, PartialEq)]
pub struct BlockIdx(usize);
impl From<usize> for BlockIdx {
fn from(i: usize) -> BlockIdx {
BlockIdx(i)
}
}
impl From<BlockIdx> for usize {
fn from(i: BlockIdx) -> usize {
i.0
}
}
impl From<&BlockIdx> for usize {
fn from(i: &BlockIdx) -> usize {
i.0
}
}