Skip to content

Commit

Permalink
feat(runtime): Add "default" tables
Browse files Browse the repository at this point in the history
  • Loading branch information
watcol committed Jul 26, 2022
1 parent 7ced7cc commit 202aade
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions drake-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ impl<L: Clone> Environment<L> {
&mut self,
kind: TableHeaderKind,
pattern: Pattern<L>,
default: Table<L>,
mut default: Table<L>,
errors: &mut Vec<Error<L>>,
) {
default.make_default();
if let Some(mut cur) = core::mem::take(&mut self.current) {
if cur.is_movable(kind, &pattern) {
cur.next_array(default, errors);
Expand Down Expand Up @@ -260,7 +261,7 @@ fn table_insert<L: Clone>(
&mut table.local
};

if table.contains_key(&name) {
if table.contains_key(&name) && !table[&name].default {
errors.push(Error::DuplicateKey {
existing: table[&name].defined.clone(),
found: span,
Expand All @@ -272,6 +273,7 @@ fn table_insert<L: Clone>(
value,
defined: span,
used: global,
default: false,
},
);
}
Expand Down
10 changes: 10 additions & 0 deletions drake-types/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub struct Element<L> {
pub value: Value<L>,
/// A position where the element is defined
pub defined: Range<L>,
/// A flag marks as the element is overridable.
pub default: bool,
/// A flag checks if the element is used, or not
pub used: bool,
}
Expand Down Expand Up @@ -55,4 +57,12 @@ impl<L> Table<L> {
pub fn new() -> Self {
Self::default()
}

/// Marks the table as a default table.
pub fn make_default(&mut self) {
self.global
.values_mut()
.for_each(|elem| elem.default = true);
self.local.values_mut().for_each(|elem| elem.default = true);
}
}

0 comments on commit 202aade

Please sign in to comment.