Skip to content

Commit

Permalink
refactor(types): non-exhaustive enums
Browse files Browse the repository at this point in the history
  • Loading branch information
watcol committed Jul 26, 2022
1 parent da24526 commit 19f0029
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
49 changes: 37 additions & 12 deletions drake-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ impl<L: Clone> Environment<L> {
},
key,
),
_ => {
self.errors.push(Error::NotSupported {
feature: "unknown pattern",
span: pattern.span,
});
return;
}
};

table_insert(table, key, value, &mut self.errors);
Expand Down Expand Up @@ -90,6 +97,7 @@ impl<L: Clone> Current<L> {
value: match kind {
TableHeaderKind::Normal => CurrentValue::Table(default),
TableHeaderKind::Array => CurrentValue::Array(vec![default]),
_ => unimplemented!(),
},
}
}
Expand All @@ -102,6 +110,7 @@ impl<L: Clone> Current<L> {
(PatternKind::Key(key1), PatternKind::Key(key2)) => {
key1.kind == key2.kind && key1.name == key2.name
}
_ => false,
}
}

Expand Down Expand Up @@ -141,7 +150,7 @@ pub fn evaluate<L: Clone>(ast: Vec<Statement<L>>) -> Snapshot<L> {
for stmt in ast {
match stmt.kind {
StatementKind::ValueBinding(pattern, expr) => {
let value = expr_to_value(expr.kind, &mut env.errors);
let value = expr_to_value(expr, &mut env.errors).0;
env.bind(pattern, value)
}
StatementKind::TableHeader(kind, pattern, default) => {
Expand All @@ -150,41 +159,57 @@ pub fn evaluate<L: Clone>(ast: Vec<Statement<L>>) -> Snapshot<L> {
.unwrap_or_default();
env.header(kind, pattern, default)
}
_ => env.errors.push(Error::NotSupported {
feature: "unknown statements",
span: stmt.span,
}),
}
}

env.close()
}

fn expr_to_value<L: Clone>(expr: ExpressionKind<L>, errors: &mut Vec<Error<L>>) -> Value<L> {
match expr {
fn expr_to_value<L: Clone>(
expr: Expression<L>,
errors: &mut Vec<Error<L>>,
) -> (Value<L>, Range<L>) {
let val = match expr.kind {
ExpressionKind::Literal(Literal::Character(c)) => Value::Character(c),
ExpressionKind::Literal(Literal::String(s)) => Value::String(s),
ExpressionKind::Literal(Literal::Integer(i)) => Value::Integer(i),
ExpressionKind::Literal(Literal::Float(f)) => Value::Float(f),
ExpressionKind::Array(arr) => Value::Array(
arr.into_iter()
.map(|elem| expr_to_value(elem.kind, errors))
.map(|elem| expr_to_value(elem, errors).0)
.collect(),
),
ExpressionKind::InlineTable(arr) => {
let mut table = Table::new();
for (key, expr) in arr {
table_insert(&mut table, key, expr_to_value(expr.kind, errors), errors);
table_insert(&mut table, key, expr_to_value(expr, errors).0, errors);
}
Value::Table(table)
}
}
_ => {
errors.push(Error::NotSupported {
feature: "unknown expressions",
span: expr.span.clone(),
});
Value::Table(Table::new())
}
};

(val, expr.span)
}

fn expr_to_table<L: Clone>(expr: Expression<L>, errors: &mut Vec<Error<L>>) -> Option<Table<L>> {
match expr_to_value(expr.kind, errors) {
Value::Table(table) => Some(table),
val => {
match expr_to_value(expr, errors) {
(Value::Table(table), _) => Some(table),
(val, span) => {
errors.push(Error::KindMismatch {
expect: vec![Kind::Table],
found: Kind::from_value(&val),
span: expr.span,
span,
});
None
}
Expand Down Expand Up @@ -230,9 +255,9 @@ fn key_destruct<L>(key: Key<L>, errors: &mut Vec<Error<L>>) -> Option<(bool, Str
match key.kind {
KeyKind::Normal => Some((true, key.name, key.span)),
KeyKind::Local => Some((false, key.name, key.span)),
KeyKind::Builtin => {
_ => {
errors.push(Error::NotSupported {
feature: "built-in keys",
feature: "unknown keys",
span: key.span,
});
None
Expand Down
6 changes: 6 additions & 0 deletions drake-types/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Statement<L> {

/// Kinds of statements
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum StatementKind<L> {
/// A value binding like `pat = "expr"`
ValueBinding(Pattern<L>, Expression<L>),
Expand All @@ -24,6 +25,7 @@ pub enum StatementKind<L> {

/// Kinds of table headers
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum TableHeaderKind {
/// A normal table header like `[table]`
Normal,
Expand All @@ -42,6 +44,7 @@ pub struct Pattern<L> {

/// Kinds of patterns
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PatternKind<L> {
/// A key pattern
Key(Key<L>),
Expand All @@ -60,6 +63,7 @@ pub struct Key<L> {

/// Kinds of keys
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum KeyKind {
/// A normal key like `key`
Normal,
Expand All @@ -80,6 +84,7 @@ pub struct Expression<L> {

/// Kinds of expressions
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum ExpressionKind<L> {
/// A literal
Literal(Literal),
Expand All @@ -91,6 +96,7 @@ pub enum ExpressionKind<L> {

/// Values of literals
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Literal {
Character(char),
String(String),
Expand Down
3 changes: 3 additions & 0 deletions drake-types/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use hashbrown::HashMap;

/// Evaluated values
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Value<L> {
/// A literal character
Character(char),
Expand Down Expand Up @@ -76,6 +77,7 @@ pub struct Snapshot<L> {

/// Errors for runtimes
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error<L> {
DuplicateKey {
existing: Range<L>,
Expand All @@ -98,6 +100,7 @@ pub enum Error<L> {

/// Name of value kinds for errors.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Kind {
Character,
String,
Expand Down
5 changes: 5 additions & 0 deletions drake-types/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use core::fmt;

/// Values of tokens
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Token {
/// A line break
Newline,
Expand All @@ -21,6 +22,7 @@ pub enum Token {

/// Kinds of symbols
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Symbol {
/// An assign sign (`=`, `U+003D`)
Assign,
Expand Down Expand Up @@ -55,6 +57,7 @@ pub struct Identifier {

/// Kinds of identifiers
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum IdentifierKind {
/// A bare key
Bare,
Expand All @@ -64,6 +67,7 @@ pub enum IdentifierKind {

/// Literal values
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Literal {
/// An integer
Integer(u64, Radix),
Expand All @@ -77,6 +81,7 @@ pub enum Literal {

/// Radixes of integers
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Radix {
/// A binary integer starts with `0b`
Binary,
Expand Down

0 comments on commit 19f0029

Please sign in to comment.