From 698ed9a70ce8a30168d35de61eeebb5ff226f4dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20H=C3=B6rnvall?= Date: Thu, 24 Sep 2020 14:32:13 +0200 Subject: [PATCH] added label_names and labels to LocalVariables struct --- src/scopes.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/scopes.rs b/src/scopes.rs index ffbc274..e109422 100644 --- a/src/scopes.rs +++ b/src/scopes.rs @@ -245,16 +245,31 @@ impl Scopes { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum LabelKind { + Block, + StackFrame, + Loop, +} + // TODO: We want to use this to figure out what to capture in a closure too. #[derive(Debug)] pub struct LocalVariables { variables: Vec<(Ustr, ScopeMemberId)>, block_stack: Vec, + + label_names: Vec<((LabelKind, Option), LabelId)>, + labels: IdVec<(), LabelId>, } impl LocalVariables { pub fn new() -> Self { - Self { variables: Vec::new(), block_stack: Vec::new() } + Self { + variables: Vec::new(), + block_stack: Vec::new(), + label_names: Vec::new(), + labels: IdVec::new(), + } } pub fn add_member(&mut self, scopes: &mut Scopes, loc: CodeLoc, name: Ustr) -> ScopeMemberId { @@ -279,10 +294,40 @@ impl LocalVariables { None } + pub fn get_label(&self, wanted_name: Ustr) -> Option { + for &((_, name), id) in self.label_names.iter().rev() { + if name == Some(wanted_name) { + return Some(id); + } + } + + None + } + + pub fn get_label_by_kind(&self, wanted_kind: LabelKind) -> Option { + for &((kind, _), id) in self.label_names.iter().rev() { + if kind == wanted_kind { + return Some(id); + } + } + + None + } + pub fn push(&mut self) { self.block_stack.push(self.variables.len()); } + pub fn push_label(&mut self, kind: LabelKind, name: Option) -> LabelId { + let id = self.labels.push(()); + self.label_names.push(((kind, name), id)); + id + } + + pub fn pop_label(&mut self) { + self.label_names.pop(); + } + pub fn pop(&mut self) { let len = self.block_stack.pop().unwrap(); self.variables.truncate(len);