Skip to content

Commit

Permalink
doc(runtime): Documents for runtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
watcol committed Jul 18, 2022
1 parent fcbda3a commit 3df69d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
14 changes: 8 additions & 6 deletions drake-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use core::ops::Range;
use drake_types::ast::{
Expression, ExpressionKind, KeyKind, Literal, Pattern, PatternKind, Statement, StatementKind,
};
use drake_types::runtime::{Table, Value, Variable};
use drake_types::runtime::{Element, Table, Value};

/// Errors for runtimes
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error<L> {
DuplicateKey {
Expand All @@ -21,6 +22,7 @@ pub enum Error<L> {
},
}

/// Evaluates an AST to a value.
pub fn evaluate<L: Clone>(ast: Vec<Statement<L>>) -> Result<Value<L>, Error<L>> {
let mut root = Table::new();
let current_table = &mut root;
Expand Down Expand Up @@ -53,7 +55,7 @@ fn bind<L: Clone>(
table,
global,
key.name,
Variable {
Element {
value: expr_to_value(expr)?,
defined: key.span.clone(),
used: global,
Expand Down Expand Up @@ -96,7 +98,7 @@ fn expr_to_value<L: Clone>(expr: Expression<L>) -> Result<Value<L>, Error<L>> {
&mut table,
global,
key.name,
Variable {
Element {
value: expr_to_value(expr)?,
defined: key.span.clone(),
used: global,
Expand All @@ -117,8 +119,8 @@ fn table_insert<L>(
table: &mut Table<L>,
global: bool,
key: String,
var: Variable<L>,
) -> Option<&Variable<L>> {
elem: Element<L>,
) -> Option<&Element<L>> {
let table = if global {
&mut table.global
} else {
Expand All @@ -128,7 +130,7 @@ fn table_insert<L>(
if table.contains_key(&key) {
Some(&table[&key])
} else {
table.insert(key, var);
table.insert(key, elem);
None
}
}
Expand Down
20 changes: 17 additions & 3 deletions drake-types/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
//! Types for runtimes
use alloc::string::String;
use alloc::vec::Vec;
use core::ops::Range;
use hashbrown::HashMap;

/// Evaluated values
#[derive(Clone, Debug, PartialEq)]
pub enum Value<L> {
/// A literal character
Character(char),
/// A literal string
String(String),
/// A literal integer
Integer(u64),
/// A literal float
Float(f64),
/// An array
Array(Vec<Value<L>>),
/// A table
Table(Table<L>),
}

/// Table's elements
#[derive(Clone, Debug, PartialEq)]
pub struct Variable<L> {
pub struct Element<L> {
/// A value of the element
pub value: Value<L>,
/// A position where the element is defined
pub defined: Range<L>,
/// A flag checks if the element is used, or not
pub used: bool,
}

/// Evaluated tables
#[derive(Clone, Debug, PartialEq)]
pub struct Table<L> {
pub global: HashMap<String, Variable<L>>,
pub local: HashMap<String, Variable<L>>,
pub global: HashMap<String, Element<L>>,
pub local: HashMap<String, Element<L>>,
}

impl<L> Default for Table<L> {
Expand All @@ -37,6 +50,7 @@ impl<L> Default for Table<L> {
}

impl<L> Table<L> {
/// Creates a new instance.
#[inline]
pub fn new() -> Self {
Self::default()
Expand Down

0 comments on commit 3df69d9

Please sign in to comment.