fix(users/tazjin): rustfmt code with non-default settings
rustfmt only sometimes detects path-based nested config files (probably some kind of race?), so my users folder uses a separate formatting check for rustfmt to avoid flaky CI. Enough flakes around already ... Change-Id: Ifd862f9974f071b3a256643dd8e56c019116156a Reviewed-on: https://cl.tvl.fyi/c/depot/+/5242 Reviewed-by: tazjin <tazjin@tvl.su> Autosubmit: tazjin <tazjin@tvl.su> Tested-by: BuildkiteCI
This commit is contained in:
parent
8b8c98380e
commit
0d0b43ed88
16 changed files with 348 additions and 421 deletions
16
users/tazjin/default.nix
Normal file
16
users/tazjin/default.nix
Normal file
|
@ -0,0 +1,16 @@
|
|||
# //users/tazjin-specific CI configuration.
|
||||
{ pkgs, ... }:
|
||||
|
||||
let
|
||||
rustfmt = pkgs.writeShellScript "rustfmt-tazjin" ''
|
||||
${pkgs.fd}/bin/fd -e rs | \
|
||||
${pkgs.ripgrep}/bin/rg 'users/tazjin' | \
|
||||
xargs ${pkgs.rustfmt}/bin/rustfmt --check --config-path users/tazjin
|
||||
'';
|
||||
|
||||
in
|
||||
rustfmt.overrideAttrs (_: {
|
||||
meta.ci.extraSteps.rustfmt = {
|
||||
command = rustfmt;
|
||||
};
|
||||
})
|
|
@ -38,8 +38,8 @@
|
|||
//!
|
||||
//! * an event type representing all possible events in the machine
|
||||
//!
|
||||
//! * an action type representing a description of all possible
|
||||
//! side-effects of the machine
|
||||
//! * an action type representing a description of all possible side-effects
|
||||
//! of the machine
|
||||
//!
|
||||
//! Using the definition above we can now say that a transition in a
|
||||
//! state-machine, involving these three types, takes an initial state
|
||||
|
@ -92,14 +92,13 @@
|
|||
//!
|
||||
//! * `finito`: Core components and classes of Finito
|
||||
//!
|
||||
//! * `finito-in-mem`: In-memory implementation of state machines
|
||||
//! that do not need to live longer than an application using
|
||||
//! standard library concurrency primitives.
|
||||
//! * `finito-in-mem`: In-memory implementation of state machines that do not
|
||||
//! need to live longer than an application using standard library
|
||||
//! concurrency primitives.
|
||||
//!
|
||||
//! * `finito-postgres`: Postgres-backed, persistent implementation
|
||||
//! of state machines that, well, do need to live longer. Uses
|
||||
//! Postgres for concurrency synchronisation, so keep that in
|
||||
//! mind.
|
||||
//! * `finito-postgres`: Postgres-backed, persistent implementation of state
|
||||
//! machines that, well, do need to live longer. Uses Postgres for
|
||||
//! concurrency synchronisation, so keep that in mind.
|
||||
//!
|
||||
//! Which should cover most use-cases. Okay, enough prose, lets dive
|
||||
//! in.
|
||||
|
@ -110,8 +109,8 @@
|
|||
|
||||
extern crate serde;
|
||||
|
||||
use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::fmt::Debug;
|
||||
use std::mem;
|
||||
|
||||
|
@ -120,7 +119,10 @@ use std::mem;
|
|||
///
|
||||
/// This trait is used to implement transition logic and to "tie the
|
||||
/// room together", with the room being our triplet of types.
|
||||
pub trait FSM where Self: Sized {
|
||||
pub trait FSM
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
/// A human-readable string uniquely describing what this FSM
|
||||
/// models. This is used in log messages, database tables and
|
||||
/// various other things throughout Finito.
|
||||
|
@ -166,7 +168,7 @@ pub trait FSM where Self: Sized {
|
|||
|
||||
/// `act` interprets and executes FSM actions. This is the only
|
||||
/// part of an FSM in which side-effects are allowed.
|
||||
fn act(Self::Action, &Self::State) -> Result<Vec<Self::Event>, Self::Error>;
|
||||
fn act(action: Self::Action, state: &Self::State) -> Result<Vec<Self::Event>, Self::Error>;
|
||||
}
|
||||
|
||||
/// This function is the primary function used to advance a state
|
||||
|
@ -223,11 +225,13 @@ pub trait FSMBackend<S: 'static> {
|
|||
/// Insert a new state-machine into the backend's storage and
|
||||
/// return its newly allocated key.
|
||||
fn insert_machine<F>(&self, initial: F) -> Result<Self::Key, Self::Error>
|
||||
where F: FSM + Serialize + DeserializeOwned;
|
||||
where
|
||||
F: FSM + Serialize + DeserializeOwned;
|
||||
|
||||
/// Retrieve the current state of an FSM by its key.
|
||||
fn get_machine<F: FSM>(&self, key: Self::Key) -> Result<F, Self::Error>
|
||||
where F: FSM + Serialize + DeserializeOwned;
|
||||
where
|
||||
F: FSM + Serialize + DeserializeOwned;
|
||||
|
||||
/// Advance a state machine by applying an event and persisting it
|
||||
/// as well as any resulting actions.
|
||||
|
@ -236,8 +240,9 @@ pub trait FSMBackend<S: 'static> {
|
|||
/// on the backend used. Please consult the backend's
|
||||
/// documentation for details.
|
||||
fn advance<'a, F: FSM>(&'a self, key: Self::Key, event: F::Event) -> Result<F, Self::Error>
|
||||
where F: FSM + Serialize + DeserializeOwned,
|
||||
F::State: From<&'a S>,
|
||||
F::Event: Serialize + DeserializeOwned,
|
||||
F::Action: Serialize + DeserializeOwned;
|
||||
where
|
||||
F: FSM + Serialize + DeserializeOwned,
|
||||
F::State: From<&'a S>,
|
||||
F::Event: Serialize + DeserializeOwned,
|
||||
F::Action: Serialize + DeserializeOwned;
|
||||
}
|
||||
|
|
|
@ -27,15 +27,15 @@
|
|||
//! The door can only be locked if it is closed. Oh, and it has a few
|
||||
//! extra features:
|
||||
//!
|
||||
//! * whenever the door's state changes, an IRC channel receives a
|
||||
//! message about that
|
||||
//! * whenever the door's state changes, an IRC channel receives a message about
|
||||
//! that
|
||||
//!
|
||||
//! * the door calls the police if the code is intered incorrectly more
|
||||
//! than a specified number of times (mhm, lets say, three)
|
||||
//! * the door calls the police if the code is intered incorrectly more than a
|
||||
//! specified number of times (mhm, lets say, three)
|
||||
//!
|
||||
//! * if the police is called the door can not be interacted with
|
||||
//! anymore (and honestly, for the sake of this example, we don't
|
||||
//! care how its functionality is restored)
|
||||
//! * if the police is called the door can not be interacted with anymore (and
|
||||
//! honestly, for the sake of this example, we don't care how its
|
||||
//! functionality is restored)
|
||||
//!
|
||||
//! ## The Door - Visualized
|
||||
//!
|
||||
|
@ -71,7 +71,8 @@
|
|||
//!
|
||||
//! Alright, enough foreplay, lets dive in!
|
||||
|
||||
#[macro_use] extern crate serde_derive;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
extern crate failure;
|
||||
extern crate finito;
|
||||
|
@ -292,11 +293,13 @@ mod tests {
|
|||
use finito::advance;
|
||||
|
||||
fn test_fsm<S: FSM>(initial: S, events: Vec<S::Event>) -> (S, Vec<S::Action>) {
|
||||
events.into_iter().fold((initial, vec![]), |(state, mut actions), event| {
|
||||
let (new_state, mut new_actions) = advance(state, event);
|
||||
actions.append(&mut new_actions);
|
||||
(new_state, actions)
|
||||
})
|
||||
events
|
||||
.into_iter()
|
||||
.fold((initial, vec![]), |(state, mut actions), event| {
|
||||
let (new_state, mut new_actions) = advance(state, event);
|
||||
actions.append(&mut new_actions);
|
||||
(new_state, actions)
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -313,7 +316,10 @@ mod tests {
|
|||
];
|
||||
let (final_state, actions) = test_fsm(initial, events);
|
||||
|
||||
assert_eq!(final_state, DoorState::Locked { code: 4567, attempts: 2 });
|
||||
assert_eq!(final_state, DoorState::Locked {
|
||||
code: 4567,
|
||||
attempts: 2
|
||||
});
|
||||
assert_eq!(actions, vec![
|
||||
DoorAction::NotifyIRC("door was closed".into()),
|
||||
DoorAction::NotifyIRC("door was opened".into()),
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
//! This module defines error types and conversions for issue that can
|
||||
//! occur while dealing with persisted state machines.
|
||||
|
||||
use std::result;
|
||||
use std::fmt;
|
||||
use uuid::Uuid;
|
||||
use std::error::Error as StdError;
|
||||
use std::{fmt, result};
|
||||
use uuid::Uuid;
|
||||
|
||||
// errors to chain:
|
||||
use postgres::Error as PgError;
|
||||
|
@ -41,20 +40,15 @@ impl fmt::Display for Error {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use ErrorKind::*;
|
||||
let msg = match &self.kind {
|
||||
Serialization(err) =>
|
||||
format!("JSON serialization error: {}", err),
|
||||
Serialization(err) => format!("JSON serialization error: {}", err),
|
||||
|
||||
Database(err) =>
|
||||
format!("PostgreSQL error: {}", err),
|
||||
Database(err) => format!("PostgreSQL error: {}", err),
|
||||
|
||||
DBPool(err) =>
|
||||
format!("Database connection pool error: {}", err),
|
||||
DBPool(err) => format!("Database connection pool error: {}", err),
|
||||
|
||||
FSMNotFound(id) =>
|
||||
format!("FSM with ID {} not found", id),
|
||||
FSMNotFound(id) => format!("FSM with ID {} not found", id),
|
||||
|
||||
ActionNotFound(id) =>
|
||||
format!("Action with ID {} not found", id),
|
||||
ActionNotFound(id) => format!("Action with ID {} not found", id),
|
||||
};
|
||||
|
||||
match &self.context {
|
||||
|
@ -66,7 +60,7 @@ impl fmt::Display for Error {
|
|||
|
||||
impl StdError for Error {}
|
||||
|
||||
impl <E: Into<ErrorKind>> From<E> for Error {
|
||||
impl<E: Into<ErrorKind>> From<E> for Error {
|
||||
fn from(err: E) -> Error {
|
||||
Error {
|
||||
kind: err.into(),
|
||||
|
@ -99,11 +93,11 @@ pub trait ResultExt<T> {
|
|||
fn context<C: fmt::Display>(self, ctx: C) -> Result<T>;
|
||||
}
|
||||
|
||||
impl <T, E: Into<Error>> ResultExt<T> for result::Result<T, E> {
|
||||
impl<T, E: Into<Error>> ResultExt<T> for result::Result<T, E> {
|
||||
fn context<C: fmt::Display>(self, ctx: C) -> Result<T> {
|
||||
self.map_err(|err| Error {
|
||||
context: Some(format!("{}", ctx)),
|
||||
.. err.into()
|
||||
..err.into()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,10 @@
|
|||
//!
|
||||
//! TODO: events & actions should have `SERIAL` keys
|
||||
|
||||
#[macro_use] extern crate postgres;
|
||||
#[macro_use] extern crate postgres_derive;
|
||||
#[macro_use]
|
||||
extern crate postgres;
|
||||
#[macro_use]
|
||||
extern crate postgres_derive;
|
||||
|
||||
extern crate chrono;
|
||||
extern crate finito;
|
||||
|
@ -14,23 +16,25 @@ extern crate serde;
|
|||
extern crate serde_json;
|
||||
extern crate uuid;
|
||||
|
||||
#[cfg(test)] mod tests;
|
||||
#[cfg(test)] extern crate finito_door;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
#[cfg(test)]
|
||||
extern crate finito_door;
|
||||
|
||||
mod error;
|
||||
pub use error::{Result, Error, ErrorKind};
|
||||
pub use error::{Error, ErrorKind, Result};
|
||||
|
||||
use chrono::prelude::{DateTime, Utc};
|
||||
use error::ResultExt;
|
||||
use finito::{FSM, FSMBackend};
|
||||
use finito::{FSMBackend, FSM};
|
||||
use postgres::transaction::Transaction;
|
||||
use postgres::GenericConnection;
|
||||
use serde::Serialize;
|
||||
use r2d2_postgres::{r2d2, PostgresConnectionManager};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use std::marker::PhantomData;
|
||||
use uuid::Uuid;
|
||||
use r2d2_postgres::{r2d2, PostgresConnectionManager};
|
||||
|
||||
type DBPool = r2d2::Pool<PostgresConnectionManager>;
|
||||
type DBConn = r2d2::PooledConnection<PostgresConnectionManager>;
|
||||
|
@ -112,15 +116,13 @@ pub struct FinitoPostgres<S> {
|
|||
db_pool: DBPool,
|
||||
}
|
||||
|
||||
impl <S> FinitoPostgres<S> {
|
||||
impl<S> FinitoPostgres<S> {
|
||||
pub fn new(state: S, db_pool: DBPool, _pool_size: usize) -> Self {
|
||||
FinitoPostgres {
|
||||
state, db_pool,
|
||||
}
|
||||
FinitoPostgres { state, db_pool }
|
||||
}
|
||||
}
|
||||
|
||||
impl <State: 'static> FSMBackend<State> for FinitoPostgres<State> {
|
||||
impl<State: 'static> FSMBackend<State> for FinitoPostgres<State> {
|
||||
type Key = Uuid;
|
||||
type Error = Error;
|
||||
|
||||
|
@ -134,10 +136,11 @@ impl <State: 'static> FSMBackend<State> for FinitoPostgres<State> {
|
|||
let fsm = S::FSM_NAME.to_string();
|
||||
let state = serde_json::to_value(initial).context("failed to serialise FSM")?;
|
||||
|
||||
self.conn()?.execute(query, &[&id, &fsm, &state]).context("failed to insert FSM")?;
|
||||
self.conn()?
|
||||
.execute(query, &[&id, &fsm, &state])
|
||||
.context("failed to insert FSM")?;
|
||||
|
||||
return Ok(id);
|
||||
|
||||
}
|
||||
|
||||
fn get_machine<S: FSM + DeserializeOwned>(&self, key: Uuid) -> Result<S> {
|
||||
|
@ -156,10 +159,12 @@ impl <State: 'static> FSMBackend<State> for FinitoPostgres<State> {
|
|||
/// processing is finished as running actions may result in additional
|
||||
/// transitions.
|
||||
fn advance<'a, S>(&'a self, key: Uuid, event: S::Event) -> Result<S>
|
||||
where S: FSM + Serialize + DeserializeOwned,
|
||||
S::State: From<&'a State>,
|
||||
S::Event: Serialize + DeserializeOwned,
|
||||
S::Action: Serialize + DeserializeOwned {
|
||||
where
|
||||
S: FSM + Serialize + DeserializeOwned,
|
||||
S::State: From<&'a State>,
|
||||
S::Event: Serialize + DeserializeOwned,
|
||||
S::Action: Serialize + DeserializeOwned,
|
||||
{
|
||||
let conn = self.conn()?;
|
||||
let tx = conn.transaction().context("could not begin transaction")?;
|
||||
let state = get_machine_internal(&tx, key, true)?;
|
||||
|
@ -187,16 +192,18 @@ impl <State: 'static> FSMBackend<State> for FinitoPostgres<State> {
|
|||
}
|
||||
}
|
||||
|
||||
impl <State: 'static> FinitoPostgres<State> {
|
||||
impl<State: 'static> FinitoPostgres<State> {
|
||||
/// Execute several actions at the same time, each in a separate
|
||||
/// thread. Note that actions returning further events, causing
|
||||
/// further transitions, returning further actions and so on will
|
||||
/// potentially cause multiple threads to get created.
|
||||
fn run_actions<'a, S>(&'a self, fsm_id: Uuid, action_ids: Vec<Uuid>) where
|
||||
fn run_actions<'a, S>(&'a self, fsm_id: Uuid, action_ids: Vec<Uuid>)
|
||||
where
|
||||
S: FSM + Serialize + DeserializeOwned,
|
||||
S::Event: Serialize + DeserializeOwned,
|
||||
S::Action: Serialize + DeserializeOwned,
|
||||
S::State: From<&'a State> {
|
||||
S::State: From<&'a State>,
|
||||
{
|
||||
let state: S::State = (&self.state).into();
|
||||
let conn = self.conn().expect("TODO");
|
||||
|
||||
|
@ -214,17 +221,19 @@ impl <State: 'static> FinitoPostgres<State> {
|
|||
|
||||
/// Retrieve a single connection from the database connection pool.
|
||||
fn conn(&self) -> Result<DBConn> {
|
||||
self.db_pool.get().context("failed to retrieve connection from pool")
|
||||
self.db_pool
|
||||
.get()
|
||||
.context("failed to retrieve connection from pool")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Insert a single state-machine into the database and return its
|
||||
/// newly allocated, random UUID.
|
||||
pub fn insert_machine<C, S>(conn: &C, initial: S) -> Result<Uuid> where
|
||||
pub fn insert_machine<C, S>(conn: &C, initial: S) -> Result<Uuid>
|
||||
where
|
||||
C: GenericConnection,
|
||||
S: FSM + Serialize {
|
||||
S: FSM + Serialize,
|
||||
{
|
||||
let query = r#"
|
||||
INSERT INTO machines (id, fsm, state)
|
||||
VALUES ($1, $2, $3)
|
||||
|
@ -240,13 +249,12 @@ pub fn insert_machine<C, S>(conn: &C, initial: S) -> Result<Uuid> where
|
|||
}
|
||||
|
||||
/// Insert a single event into the database and return its UUID.
|
||||
fn insert_event<C, S>(conn: &C,
|
||||
fsm_id: Uuid,
|
||||
event: &S::Event) -> Result<Uuid>
|
||||
fn insert_event<C, S>(conn: &C, fsm_id: Uuid, event: &S::Event) -> Result<Uuid>
|
||||
where
|
||||
C: GenericConnection,
|
||||
S: FSM,
|
||||
S::Event: Serialize {
|
||||
S::Event: Serialize,
|
||||
{
|
||||
let query = r#"
|
||||
INSERT INTO events (id, fsm, fsm_id, event)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
|
@ -254,21 +262,19 @@ where
|
|||
|
||||
let id = Uuid::new_v4();
|
||||
let fsm = S::FSM_NAME.to_string();
|
||||
let event_value = serde_json::to_value(event)
|
||||
.context("failed to serialize event")?;
|
||||
let event_value = serde_json::to_value(event).context("failed to serialize event")?;
|
||||
|
||||
conn.execute(query, &[&id, &fsm, &fsm_id, &event_value])?;
|
||||
return Ok(id)
|
||||
return Ok(id);
|
||||
}
|
||||
|
||||
/// Insert a single action into the database and return its UUID.
|
||||
fn insert_action<C, S>(conn: &C,
|
||||
fsm_id: Uuid,
|
||||
event_id: Uuid,
|
||||
action: &S::Action) -> Result<Uuid> where
|
||||
fn insert_action<C, S>(conn: &C, fsm_id: Uuid, event_id: Uuid, action: &S::Action) -> Result<Uuid>
|
||||
where
|
||||
C: GenericConnection,
|
||||
S: FSM,
|
||||
S::Action: Serialize {
|
||||
S::Action: Serialize,
|
||||
{
|
||||
let query = r#"
|
||||
INSERT INTO actions (id, fsm, fsm_id, event_id, content, status)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
|
@ -276,23 +282,26 @@ fn insert_action<C, S>(conn: &C,
|
|||
|
||||
let id = Uuid::new_v4();
|
||||
let fsm = S::FSM_NAME.to_string();
|
||||
let action_value = serde_json::to_value(action)
|
||||
.context("failed to serialize action")?;
|
||||
let action_value = serde_json::to_value(action).context("failed to serialize action")?;
|
||||
|
||||
conn.execute(
|
||||
query,
|
||||
&[&id, &fsm, &fsm_id, &event_id, &action_value, &ActionStatus::Pending]
|
||||
)?;
|
||||
conn.execute(query, &[
|
||||
&id,
|
||||
&fsm,
|
||||
&fsm_id,
|
||||
&event_id,
|
||||
&action_value,
|
||||
&ActionStatus::Pending,
|
||||
])?;
|
||||
|
||||
return Ok(id)
|
||||
return Ok(id);
|
||||
}
|
||||
|
||||
/// Update the state of a specified machine.
|
||||
fn update_state<C, S>(conn: &C,
|
||||
fsm_id: Uuid,
|
||||
state: &S) -> Result<()> where
|
||||
fn update_state<C, S>(conn: &C, fsm_id: Uuid, state: &S) -> Result<()>
|
||||
where
|
||||
C: GenericConnection,
|
||||
S: FSM + Serialize {
|
||||
S: FSM + Serialize,
|
||||
{
|
||||
let query = r#"
|
||||
UPDATE machines SET state = $1 WHERE id = $2
|
||||
"#;
|
||||
|
@ -312,23 +321,28 @@ fn update_state<C, S>(conn: &C,
|
|||
fn alter_for_update(alter: bool, query: &str) -> String {
|
||||
match alter {
|
||||
false => query.to_string(),
|
||||
true => format!("{} FOR UPDATE", query),
|
||||
true => format!("{} FOR UPDATE", query),
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieve the current state of a state machine from the database,
|
||||
/// optionally locking the machine state for the duration of some
|
||||
/// enclosing transaction.
|
||||
fn get_machine_internal<C, S>(conn: &C,
|
||||
id: Uuid,
|
||||
for_update: bool) -> Result<S> where
|
||||
fn get_machine_internal<C, S>(conn: &C, id: Uuid, for_update: bool) -> Result<S>
|
||||
where
|
||||
C: GenericConnection,
|
||||
S: FSM + DeserializeOwned {
|
||||
let query = alter_for_update(for_update, r#"
|
||||
S: FSM + DeserializeOwned,
|
||||
{
|
||||
let query = alter_for_update(
|
||||
for_update,
|
||||
r#"
|
||||
SELECT state FROM machines WHERE id = $1
|
||||
"#);
|
||||
"#,
|
||||
);
|
||||
|
||||
let rows = conn.query(&query, &[&id]).context("failed to retrieve FSM")?;
|
||||
let rows = conn
|
||||
.query(&query, &[&id])
|
||||
.context("failed to retrieve FSM")?;
|
||||
|
||||
if let Some(row) = rows.into_iter().next() {
|
||||
Ok(serde_json::from_value(row.get(0)).context("failed to deserialize FSM")?)
|
||||
|
@ -339,20 +353,25 @@ fn get_machine_internal<C, S>(conn: &C,
|
|||
|
||||
/// Retrieve an action from the database, optionally locking it for
|
||||
/// the duration of some enclosing transaction.
|
||||
fn get_action<C, S>(conn: &C, id: Uuid) -> Result<(ActionStatus, S::Action)> where
|
||||
fn get_action<C, S>(conn: &C, id: Uuid) -> Result<(ActionStatus, S::Action)>
|
||||
where
|
||||
C: GenericConnection,
|
||||
S: FSM,
|
||||
S::Action: DeserializeOwned {
|
||||
let query = alter_for_update(true, r#"
|
||||
S::Action: DeserializeOwned,
|
||||
{
|
||||
let query = alter_for_update(
|
||||
true,
|
||||
r#"
|
||||
SELECT status, content FROM actions
|
||||
WHERE id = $1 AND fsm = $2
|
||||
"#);
|
||||
"#,
|
||||
);
|
||||
|
||||
let rows = conn.query(&query, &[&id, &S::FSM_NAME])?;
|
||||
|
||||
if let Some(row) = rows.into_iter().next() {
|
||||
let action = serde_json::from_value(row.get(1))
|
||||
.context("failed to deserialize FSM action")?;
|
||||
let action =
|
||||
serde_json::from_value(row.get(1)).context("failed to deserialize FSM action")?;
|
||||
Ok((row.get(0), action))
|
||||
} else {
|
||||
Err(ErrorKind::ActionNotFound(id).into())
|
||||
|
@ -360,13 +379,17 @@ fn get_action<C, S>(conn: &C, id: Uuid) -> Result<(ActionStatus, S::Action)> whe
|
|||
}
|
||||
|
||||
/// Update the status of an action after an attempt to run it.
|
||||
fn update_action_status<C, S>(conn: &C,
|
||||
id: Uuid,
|
||||
status: ActionStatus,
|
||||
error: Option<String>,
|
||||
_fsm: PhantomData<S>) -> Result<()> where
|
||||
fn update_action_status<C, S>(
|
||||
conn: &C,
|
||||
id: Uuid,
|
||||
status: ActionStatus,
|
||||
error: Option<String>,
|
||||
_fsm: PhantomData<S>,
|
||||
) -> Result<()>
|
||||
where
|
||||
C: GenericConnection,
|
||||
S: FSM {
|
||||
S: FSM,
|
||||
{
|
||||
let query = r#"
|
||||
UPDATE actions SET status = $1, error = $2
|
||||
WHERE id = $3 AND fsm = $4
|
||||
|
@ -389,10 +412,16 @@ fn update_action_status<C, S>(conn: &C,
|
|||
/// panic), the error will be persisted. Should it fail by panicking
|
||||
/// (which developers should never do explicitly in action
|
||||
/// interpreters) its status will not be changed.
|
||||
fn run_action<S>(tx: Transaction, id: Uuid, state: &S::State, _fsm: PhantomData<S>)
|
||||
-> Result<Vec<S::Event>> where
|
||||
fn run_action<S>(
|
||||
tx: Transaction,
|
||||
id: Uuid,
|
||||
state: &S::State,
|
||||
_fsm: PhantomData<S>,
|
||||
) -> Result<Vec<S::Event>>
|
||||
where
|
||||
S: FSM,
|
||||
S::Action: DeserializeOwned {
|
||||
S::Action: DeserializeOwned,
|
||||
{
|
||||
let (status, action) = get_action::<Transaction, S>(&tx, id)?;
|
||||
|
||||
let result = match status {
|
||||
|
@ -401,29 +430,25 @@ fn run_action<S>(tx: Transaction, id: Uuid, state: &S::State, _fsm: PhantomData<
|
|||
// If the action succeeded, update its status to
|
||||
// completed and return the created events.
|
||||
Ok(events) => {
|
||||
update_action_status(
|
||||
&tx, id, ActionStatus::Completed, None, PhantomData::<S>
|
||||
)?;
|
||||
update_action_status(&tx, id, ActionStatus::Completed, None, PhantomData::<S>)?;
|
||||
events
|
||||
},
|
||||
}
|
||||
|
||||
// If the action failed, persist the debug message and
|
||||
// return nothing.
|
||||
Err(err) => {
|
||||
let msg = Some(format!("{:?}", err));
|
||||
update_action_status(
|
||||
&tx, id, ActionStatus::Failed, msg, PhantomData::<S>
|
||||
)?;
|
||||
update_action_status(&tx, id, ActionStatus::Failed, msg, PhantomData::<S>)?;
|
||||
vec![]
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
_ => {
|
||||
// TODO: Currently only pending actions are run because
|
||||
// retryable actions are not yet implemented.
|
||||
vec![]
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
tx.commit().context("failed to commit transaction")?;
|
||||
|
|
|
@ -16,7 +16,11 @@ fn test_insert_machine() {
|
|||
let door = insert_machine(&conn, initial).expect("Failed to insert door");
|
||||
let result = get_machine(&conn, &door, false).expect("Failed to fetch door");
|
||||
|
||||
assert_eq!(result, DoorState::Opened, "Inserted door state should match");
|
||||
assert_eq!(
|
||||
result,
|
||||
DoorState::Opened,
|
||||
"Inserted door state should match"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -41,7 +45,10 @@ fn test_advance() {
|
|||
}
|
||||
|
||||
let result = get_machine(&conn, &door, false).expect("Failed to fetch door");
|
||||
let expected = DoorState::Locked { code: 4567, attempts: 2 };
|
||||
let expected = DoorState::Locked {
|
||||
code: 4567,
|
||||
attempts: 2,
|
||||
};
|
||||
|
||||
assert_eq!(result, expected, "Advanced door state should match");
|
||||
}
|
||||
|
|
|
@ -63,9 +63,11 @@ enum Precedence {
|
|||
Equality, // == !=
|
||||
Comparison, // < > <= >=
|
||||
Term, // + -
|
||||
Factor, // * /
|
||||
Unary, // ! -
|
||||
Call, // . ()
|
||||
Factor, //
|
||||
//
|
||||
// * /
|
||||
Unary, // ! -
|
||||
Call, // . ()
|
||||
Primary,
|
||||
}
|
||||
|
||||
|
@ -78,11 +80,7 @@ struct ParseRule<T: Iterator<Item = Token>> {
|
|||
}
|
||||
|
||||
impl<T: Iterator<Item = Token>> ParseRule<T> {
|
||||
fn new(
|
||||
prefix: Option<ParseFn<T>>,
|
||||
infix: Option<ParseFn<T>>,
|
||||
precedence: Precedence,
|
||||
) -> Self {
|
||||
fn new(prefix: Option<ParseFn<T>>, infix: Option<ParseFn<T>>, precedence: Precedence) -> Self {
|
||||
ParseRule {
|
||||
prefix,
|
||||
infix,
|
||||
|
@ -105,18 +103,16 @@ impl Precedence {
|
|||
Precedence::Factor => Precedence::Unary,
|
||||
Precedence::Unary => Precedence::Call,
|
||||
Precedence::Call => Precedence::Primary,
|
||||
Precedence::Primary => panic!(
|
||||
"invalid parser state: no higher precedence than Primary"
|
||||
),
|
||||
Precedence::Primary => {
|
||||
panic!("invalid parser state: no higher precedence than Primary")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn rule_for<T: Iterator<Item = Token>>(token: &TokenKind) -> ParseRule<T> {
|
||||
match token {
|
||||
TokenKind::LeftParen => {
|
||||
ParseRule::new(Some(Compiler::grouping), None, Precedence::None)
|
||||
}
|
||||
TokenKind::LeftParen => ParseRule::new(Some(Compiler::grouping), None, Precedence::None),
|
||||
|
||||
TokenKind::Minus => ParseRule::new(
|
||||
Some(Compiler::unary),
|
||||
|
@ -124,57 +120,33 @@ fn rule_for<T: Iterator<Item = Token>>(token: &TokenKind) -> ParseRule<T> {
|
|||
Precedence::Term,
|
||||
),
|
||||
|
||||
TokenKind::Plus => {
|
||||
ParseRule::new(None, Some(Compiler::binary), Precedence::Term)
|
||||
}
|
||||
TokenKind::Plus => ParseRule::new(None, Some(Compiler::binary), Precedence::Term),
|
||||
|
||||
TokenKind::Slash => {
|
||||
ParseRule::new(None, Some(Compiler::binary), Precedence::Factor)
|
||||
}
|
||||
TokenKind::Slash => ParseRule::new(None, Some(Compiler::binary), Precedence::Factor),
|
||||
|
||||
TokenKind::Star => {
|
||||
ParseRule::new(None, Some(Compiler::binary), Precedence::Factor)
|
||||
}
|
||||
TokenKind::Star => ParseRule::new(None, Some(Compiler::binary), Precedence::Factor),
|
||||
|
||||
TokenKind::Number(_) => {
|
||||
ParseRule::new(Some(Compiler::number), None, Precedence::None)
|
||||
}
|
||||
TokenKind::Number(_) => ParseRule::new(Some(Compiler::number), None, Precedence::None),
|
||||
|
||||
TokenKind::True => {
|
||||
ParseRule::new(Some(Compiler::literal), None, Precedence::None)
|
||||
}
|
||||
TokenKind::True => ParseRule::new(Some(Compiler::literal), None, Precedence::None),
|
||||
|
||||
TokenKind::False => {
|
||||
ParseRule::new(Some(Compiler::literal), None, Precedence::None)
|
||||
}
|
||||
TokenKind::False => ParseRule::new(Some(Compiler::literal), None, Precedence::None),
|
||||
|
||||
TokenKind::Nil => {
|
||||
ParseRule::new(Some(Compiler::literal), None, Precedence::None)
|
||||
}
|
||||
TokenKind::Nil => ParseRule::new(Some(Compiler::literal), None, Precedence::None),
|
||||
|
||||
TokenKind::Bang => {
|
||||
ParseRule::new(Some(Compiler::unary), None, Precedence::None)
|
||||
}
|
||||
TokenKind::Bang => ParseRule::new(Some(Compiler::unary), None, Precedence::None),
|
||||
|
||||
TokenKind::BangEqual => {
|
||||
ParseRule::new(None, Some(Compiler::binary), Precedence::Equality)
|
||||
}
|
||||
TokenKind::BangEqual => ParseRule::new(None, Some(Compiler::binary), Precedence::Equality),
|
||||
|
||||
TokenKind::EqualEqual => {
|
||||
ParseRule::new(None, Some(Compiler::binary), Precedence::Equality)
|
||||
}
|
||||
TokenKind::EqualEqual => ParseRule::new(None, Some(Compiler::binary), Precedence::Equality),
|
||||
|
||||
TokenKind::Greater => {
|
||||
ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison)
|
||||
}
|
||||
TokenKind::Greater => ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison),
|
||||
|
||||
TokenKind::GreaterEqual => {
|
||||
ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison)
|
||||
}
|
||||
|
||||
TokenKind::Less => {
|
||||
ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison)
|
||||
}
|
||||
TokenKind::Less => ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison),
|
||||
|
||||
TokenKind::LessEqual => {
|
||||
ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison)
|
||||
|
@ -184,9 +156,7 @@ fn rule_for<T: Iterator<Item = Token>>(token: &TokenKind) -> ParseRule<T> {
|
|||
ParseRule::new(Some(Compiler::variable), None, Precedence::None)
|
||||
}
|
||||
|
||||
TokenKind::String(_) => {
|
||||
ParseRule::new(Some(Compiler::string), None, Precedence::None)
|
||||
}
|
||||
TokenKind::String(_) => ParseRule::new(Some(Compiler::string), None, Precedence::None),
|
||||
|
||||
_ => ParseRule::new(None, None, Precedence::None),
|
||||
}
|
||||
|
@ -236,9 +206,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
|
|||
|
||||
fn define_variable(&mut self, var: Option<ConstantIdx>) -> LoxResult<()> {
|
||||
if self.locals.scope_depth == 0 {
|
||||
self.emit_op(OpCode::OpDefineGlobal(
|
||||
var.expect("should be global"),
|
||||
));
|
||||
self.emit_op(OpCode::OpDefineGlobal(var.expect("should be global")));
|
||||
} else {
|
||||
self.locals
|
||||
.locals
|
||||
|
@ -305,9 +273,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
|
|||
}
|
||||
|
||||
fn block(&mut self) -> LoxResult<()> {
|
||||
while !self.check(&TokenKind::RightBrace)
|
||||
&& !self.check(&TokenKind::Eof)
|
||||
{
|
||||
while !self.check(&TokenKind::RightBrace) && !self.check(&TokenKind::Eof) {
|
||||
self.declaration()?;
|
||||
}
|
||||
|
||||
|
@ -712,9 +678,8 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
|
|||
|
||||
pub fn compile(code: &str) -> Result<(Interner, Chunk), Vec<Error>> {
|
||||
let chars = code.chars().collect::<Vec<char>>();
|
||||
let tokens = scanner::scan(&chars).map_err(|errors| {
|
||||
errors.into_iter().map(Into::into).collect::<Vec<Error>>()
|
||||
})?;
|
||||
let tokens = scanner::scan(&chars)
|
||||
.map_err(|errors| errors.into_iter().map(Into::into).collect::<Vec<Error>>())?;
|
||||
|
||||
let mut compiler = Compiler {
|
||||
tokens: tokens.into_iter().peekable(),
|
||||
|
|
|
@ -23,10 +23,7 @@ impl crate::Lox for Interpreter {
|
|||
Interpreter {}
|
||||
}
|
||||
|
||||
fn interpret(
|
||||
&mut self,
|
||||
code: String,
|
||||
) -> Result<Self::Value, Vec<Self::Error>> {
|
||||
fn interpret(&mut self, code: String) -> Result<Self::Value, Vec<Self::Error>> {
|
||||
let (strings, chunk) = compiler::compile(&code)?;
|
||||
vm::interpret(strings, chunk).map_err(|e| vec![e])
|
||||
}
|
||||
|
|
|
@ -118,12 +118,7 @@ impl VM {
|
|||
|
||||
OpCode::OpNegate => {
|
||||
let v = self.pop();
|
||||
with_type!(
|
||||
self,
|
||||
v,
|
||||
Value::Number(num),
|
||||
self.push(Value::Number(-num))
|
||||
);
|
||||
with_type!(self, v, Value::Number(num), self.push(Value::Number(-num)));
|
||||
}
|
||||
|
||||
OpCode::OpSubtract => binary_op!(self, Number, -),
|
||||
|
@ -141,15 +136,18 @@ impl VM {
|
|||
self.push(Value::String(new_s.into()));
|
||||
}
|
||||
|
||||
(Value::Number(n_a), Value::Number(n_b)) =>
|
||||
self.push(Value::Number(n_a + n_b)),
|
||||
(Value::Number(n_a), Value::Number(n_b)) => {
|
||||
self.push(Value::Number(n_a + n_b))
|
||||
}
|
||||
|
||||
_ => return Err(Error {
|
||||
line: self.chunk.get_line(self.ip - 1),
|
||||
kind: ErrorKind::TypeError(
|
||||
"'+' operator only works on strings and numbers".into()
|
||||
),
|
||||
})
|
||||
_ => {
|
||||
return Err(Error {
|
||||
line: self.chunk.get_line(self.ip - 1),
|
||||
kind: ErrorKind::TypeError(
|
||||
"'+' operator only works on strings and numbers".into(),
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -205,8 +203,7 @@ impl VM {
|
|||
self.stack.len() > local_idx.0,
|
||||
"stack is not currently large enough for local"
|
||||
);
|
||||
self.stack[local_idx.0] =
|
||||
self.stack.last().unwrap().clone();
|
||||
self.stack[local_idx.0] = self.stack.last().unwrap().clone();
|
||||
}
|
||||
|
||||
OpCode::OpJumpPlaceholder(_) => {
|
||||
|
@ -255,9 +252,7 @@ impl VM {
|
|||
fn print_value(&self, val: Value) -> String {
|
||||
match val {
|
||||
Value::String(LoxString::Heap(s)) => s,
|
||||
Value::String(LoxString::Interned(id)) => {
|
||||
self.strings.lookup(id).into()
|
||||
}
|
||||
Value::String(LoxString::Interned(id)) => self.strings.lookup(id).into(),
|
||||
_ => format!("{:?}", val),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
use std::env;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::process;
|
||||
use std::{env, fs, io, process};
|
||||
|
||||
mod bytecode;
|
||||
mod scanner;
|
||||
|
@ -15,10 +12,7 @@ pub trait Lox {
|
|||
type Error: std::fmt::Display;
|
||||
|
||||
fn create() -> Self;
|
||||
fn interpret(
|
||||
&mut self,
|
||||
source: String,
|
||||
) -> Result<Self::Value, Vec<Self::Error>>;
|
||||
fn interpret(&mut self, source: String) -> Result<Self::Value, Vec<Self::Error>>;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -29,9 +23,7 @@ fn main() {
|
|||
}
|
||||
|
||||
match env::var("LOX_INTERPRETER").as_ref().map(String::as_str) {
|
||||
Ok("treewalk") => {
|
||||
pick::<treewalk::interpreter::Interpreter>(args.nth(1))
|
||||
}
|
||||
Ok("treewalk") => pick::<treewalk::interpreter::Interpreter>(args.nth(1)),
|
||||
_ => pick::<bytecode::Interpreter>(args.nth(1)),
|
||||
}
|
||||
}
|
||||
|
@ -46,8 +38,7 @@ fn pick<I: Lox>(file_arg: Option<String>) {
|
|||
|
||||
// Run Lox code from a file and print results to stdout
|
||||
fn run_file<I: Lox>(file: &str) {
|
||||
let contents =
|
||||
fs::read_to_string(file).expect("failed to read the input file");
|
||||
let contents = fs::read_to_string(file).expect("failed to read the input file");
|
||||
let mut lox = I::create();
|
||||
run(&mut lox, contents);
|
||||
}
|
||||
|
|
|
@ -106,15 +106,9 @@ impl<'a> Scanner<'a> {
|
|||
|
||||
// possible multi-character tokens
|
||||
'!' => self.add_if_next('=', TokenKind::BangEqual, TokenKind::Bang),
|
||||
'=' => {
|
||||
self.add_if_next('=', TokenKind::EqualEqual, TokenKind::Equal)
|
||||
}
|
||||
'=' => self.add_if_next('=', TokenKind::EqualEqual, TokenKind::Equal),
|
||||
'<' => self.add_if_next('=', TokenKind::LessEqual, TokenKind::Less),
|
||||
'>' => self.add_if_next(
|
||||
'=',
|
||||
TokenKind::GreaterEqual,
|
||||
TokenKind::Greater,
|
||||
),
|
||||
'>' => self.add_if_next('=', TokenKind::GreaterEqual, TokenKind::Greater),
|
||||
|
||||
'/' => {
|
||||
// support comments until EOL by discarding characters
|
||||
|
@ -234,8 +228,7 @@ impl<'a> Scanner<'a> {
|
|||
self.advance();
|
||||
}
|
||||
|
||||
let ident: String =
|
||||
self.source[self.start..self.current].iter().collect();
|
||||
let ident: String = self.source[self.start..self.current].iter().collect();
|
||||
|
||||
// Determine whether this is an identifier, or a keyword:
|
||||
let token_kind = match ident.as_str() {
|
||||
|
|
|
@ -34,11 +34,7 @@ impl Callable {
|
|||
}
|
||||
}
|
||||
|
||||
fn call(
|
||||
&self,
|
||||
lox: &mut Interpreter,
|
||||
args: Vec<Value>,
|
||||
) -> Result<Value, Error> {
|
||||
fn call(&self, lox: &mut Interpreter, args: Vec<Value>) -> Result<Value, Error> {
|
||||
match self {
|
||||
Callable::Builtin(builtin) => builtin.call(args),
|
||||
|
||||
|
@ -50,10 +46,8 @@ impl Callable {
|
|||
fn_env.define(param, value)?;
|
||||
}
|
||||
|
||||
let result = lox.interpret_block_with_env(
|
||||
Some(Rc::new(RwLock::new(fn_env))),
|
||||
&func.body,
|
||||
);
|
||||
let result =
|
||||
lox.interpret_block_with_env(Some(Rc::new(RwLock::new(fn_env))), &func.body);
|
||||
|
||||
match result {
|
||||
// extract returned values if applicable
|
||||
|
@ -109,22 +103,13 @@ pub struct Environment {
|
|||
}
|
||||
|
||||
impl Environment {
|
||||
fn define(
|
||||
&mut self,
|
||||
name: &scanner::Token,
|
||||
value: Value,
|
||||
) -> Result<(), Error> {
|
||||
fn define(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> {
|
||||
let ident = identifier_str(name)?;
|
||||
self.values.insert(ident.into(), value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get(
|
||||
&self,
|
||||
ident: &str,
|
||||
line: usize,
|
||||
depth: usize,
|
||||
) -> Result<Value, Error> {
|
||||
fn get(&self, ident: &str, line: usize, depth: usize) -> Result<Value, Error> {
|
||||
if depth > 0 {
|
||||
match &self.enclosing {
|
||||
None => {
|
||||
|
@ -137,9 +122,7 @@ impl Environment {
|
|||
})
|
||||
}
|
||||
Some(parent) => {
|
||||
let env = parent
|
||||
.read()
|
||||
.expect("fatal: environment lock poisoned");
|
||||
let env = parent.read().expect("fatal: environment lock poisoned");
|
||||
return env.get(ident, line, depth - 1);
|
||||
}
|
||||
}
|
||||
|
@ -154,11 +137,7 @@ impl Environment {
|
|||
})
|
||||
}
|
||||
|
||||
fn assign(
|
||||
&mut self,
|
||||
name: &scanner::Token,
|
||||
value: Value,
|
||||
) -> Result<(), Error> {
|
||||
fn assign(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> {
|
||||
let ident = identifier_str(name)?;
|
||||
|
||||
match self.values.get_mut(ident) {
|
||||
|
@ -242,22 +221,14 @@ impl Lox for Interpreter {
|
|||
|
||||
impl Interpreter {
|
||||
// Environment modification helpers
|
||||
fn define_var(
|
||||
&mut self,
|
||||
name: &scanner::Token,
|
||||
value: Value,
|
||||
) -> Result<(), Error> {
|
||||
fn define_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> {
|
||||
self.env
|
||||
.write()
|
||||
.expect("environment lock is poisoned")
|
||||
.define(name, value)
|
||||
}
|
||||
|
||||
fn assign_var(
|
||||
&mut self,
|
||||
name: &scanner::Token,
|
||||
value: Value,
|
||||
) -> Result<(), Error> {
|
||||
fn assign_var(&mut self, name: &scanner::Token, value: Value) -> Result<(), Error> {
|
||||
self.env
|
||||
.write()
|
||||
.expect("environment lock is poisoned")
|
||||
|
@ -271,11 +242,10 @@ impl Interpreter {
|
|||
kind: ErrorKind::UndefinedVariable(ident.into()),
|
||||
})?;
|
||||
|
||||
self.env.read().expect("environment lock is poisoned").get(
|
||||
ident,
|
||||
var.name.line,
|
||||
depth,
|
||||
)
|
||||
self.env
|
||||
.read()
|
||||
.expect("environment lock is poisoned")
|
||||
.get(ident, var.name.line, depth)
|
||||
}
|
||||
|
||||
/// Interpret the block in the supplied environment. If no
|
||||
|
@ -324,16 +294,10 @@ impl Interpreter {
|
|||
Value::Literal(Literal::String(output))
|
||||
}
|
||||
Statement::Var(var) => return self.interpret_var(var),
|
||||
Statement::Block(block) => {
|
||||
return self.interpret_block_with_env(None, block)
|
||||
}
|
||||
Statement::Block(block) => return self.interpret_block_with_env(None, block),
|
||||
Statement::If(if_stmt) => return self.interpret_if(if_stmt),
|
||||
Statement::While(while_stmt) => {
|
||||
return self.interpret_while(while_stmt)
|
||||
}
|
||||
Statement::Function(func) => {
|
||||
return self.interpret_function(func.clone())
|
||||
}
|
||||
Statement::While(while_stmt) => return self.interpret_while(while_stmt),
|
||||
Statement::Function(func) => return self.interpret_function(func.clone()),
|
||||
Statement::Return(ret) => {
|
||||
return Err(Error {
|
||||
line: 0,
|
||||
|
@ -348,9 +312,7 @@ impl Interpreter {
|
|||
fn interpret_var(&mut self, var: &parser::Var) -> Result<Value, Error> {
|
||||
let init = var.initialiser.as_ref().ok_or_else(|| Error {
|
||||
line: var.name.line,
|
||||
kind: ErrorKind::InternalError(
|
||||
"missing variable initialiser".into(),
|
||||
),
|
||||
kind: ErrorKind::InternalError("missing variable initialiser".into()),
|
||||
})?;
|
||||
let value = self.eval(init)?;
|
||||
self.define_var(&var.name, value.clone())?;
|
||||
|
@ -369,10 +331,7 @@ impl Interpreter {
|
|||
}
|
||||
}
|
||||
|
||||
fn interpret_while(
|
||||
&mut self,
|
||||
stmt: &parser::While,
|
||||
) -> Result<Value, Error> {
|
||||
fn interpret_while(&mut self, stmt: &parser::While) -> Result<Value, Error> {
|
||||
let mut value = Value::Literal(Literal::Nil);
|
||||
while eval_truthy(&self.eval(&stmt.condition)?) {
|
||||
value = self.interpret_stmt(&stmt.body)?;
|
||||
|
@ -381,10 +340,7 @@ impl Interpreter {
|
|||
Ok(value)
|
||||
}
|
||||
|
||||
fn interpret_function(
|
||||
&mut self,
|
||||
func: Rc<parser::Function>,
|
||||
) -> Result<Value, Error> {
|
||||
fn interpret_function(&mut self, func: Rc<parser::Function>) -> Result<Value, Error> {
|
||||
let name = func.name.clone();
|
||||
let value = Value::Callable(Callable::Function {
|
||||
func,
|
||||
|
@ -414,9 +370,7 @@ impl Interpreter {
|
|||
(TokenKind::Minus, Value::Literal(Literal::Number(num))) => {
|
||||
Ok(Literal::Number(-num).into())
|
||||
}
|
||||
(TokenKind::Bang, right) => {
|
||||
Ok(Literal::Boolean(!eval_truthy(&right)).into())
|
||||
}
|
||||
(TokenKind::Bang, right) => Ok(Literal::Boolean(!eval_truthy(&right)).into()),
|
||||
|
||||
(op, right) => Err(Error {
|
||||
line: expr.operator.line,
|
||||
|
@ -478,10 +432,7 @@ impl Interpreter {
|
|||
Ok(value)
|
||||
}
|
||||
|
||||
fn eval_logical(
|
||||
&mut self,
|
||||
logical: &parser::Logical,
|
||||
) -> Result<Value, Error> {
|
||||
fn eval_logical(&mut self, logical: &parser::Logical) -> Result<Value, Error> {
|
||||
let left = eval_truthy(&self.eval(&logical.left)?);
|
||||
let right = eval_truthy(&self.eval(&logical.right)?);
|
||||
|
||||
|
@ -490,10 +441,7 @@ impl Interpreter {
|
|||
TokenKind::Or => Ok(Literal::Boolean(left || right).into()),
|
||||
kind => Err(Error {
|
||||
line: logical.operator.line,
|
||||
kind: ErrorKind::InternalError(format!(
|
||||
"Invalid logical operator: {:?}",
|
||||
kind
|
||||
)),
|
||||
kind: ErrorKind::InternalError(format!("Invalid logical operator: {:?}", kind)),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
@ -504,10 +452,7 @@ impl Interpreter {
|
|||
Value::Literal(v) => {
|
||||
return Err(Error {
|
||||
line: call.paren.line,
|
||||
kind: ErrorKind::RuntimeError(format!(
|
||||
"not callable: {:?}",
|
||||
v
|
||||
)),
|
||||
kind: ErrorKind::RuntimeError(format!("not callable: {:?}", v)),
|
||||
})
|
||||
}
|
||||
};
|
||||
|
@ -546,10 +491,7 @@ fn eval_truthy(lit: &Value) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
fn set_enclosing_env(
|
||||
this: &RwLock<Environment>,
|
||||
parent: Rc<RwLock<Environment>>,
|
||||
) {
|
||||
fn set_enclosing_env(this: &RwLock<Environment>, parent: Rc<RwLock<Environment>>) {
|
||||
this.write()
|
||||
.expect("environment lock is poisoned")
|
||||
.enclosing = Some(parent);
|
||||
|
|
|
@ -124,56 +124,54 @@ pub enum Statement {
|
|||
|
||||
// Parser
|
||||
|
||||
/*
|
||||
program → declaration* EOF ;
|
||||
|
||||
declaration → funDecl
|
||||
| varDecl
|
||||
| statement ;
|
||||
|
||||
funDecl → "fun" function ;
|
||||
function → IDENTIFIER "(" parameters? ")" block ;
|
||||
parameters → IDENTIFIER ( "," IDENTIFIER )* ;
|
||||
|
||||
|
||||
statement → exprStmt
|
||||
| forStmt
|
||||
| ifStmt
|
||||
| printStmt
|
||||
| returnStmt
|
||||
| whileStmt
|
||||
| block ;
|
||||
|
||||
forStmt → "for" "(" ( varDecl | exprStmt | ";" )
|
||||
expression? ";"
|
||||
expression? ")" statement ;
|
||||
|
||||
returnStmt → "return" expression? ";" ;
|
||||
|
||||
whileStmt → "while" "(" expression ")" statement ;
|
||||
|
||||
exprStmt → expression ";" ;
|
||||
|
||||
ifStmt → "if" "(" expression ")" statement
|
||||
( "else" statement )? ;
|
||||
|
||||
printStmt → "print" expression ";" ;
|
||||
|
||||
expression → assignment ;
|
||||
assignment → IDENTIFIER "=" assignment
|
||||
| logic_or ;
|
||||
logic_or → logic_and ( "or" logic_and )* ;
|
||||
logic_and → equality ( "and" equality )* ;
|
||||
equality → comparison ( ( "!=" | "==" ) comparison )* ;
|
||||
comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
|
||||
term → factor ( ( "-" | "+" ) factor )* ;
|
||||
factor → unary ( ( "/" | "*" ) unary )* ;
|
||||
unary → ( "!" | "-" ) unary | call ;
|
||||
call → primary ( "(" arguments? ")" )* ;
|
||||
arguments → expression ( "," expression )* ;
|
||||
primary → NUMBER | STRING | "true" | "false" | "nil"
|
||||
| "(" expression ")" ;
|
||||
*/
|
||||
// program → declaration* EOF ;
|
||||
//
|
||||
// declaration → funDecl
|
||||
// | varDecl
|
||||
// | statement ;
|
||||
//
|
||||
// funDecl → "fun" function ;
|
||||
// function → IDENTIFIER "(" parameters? ")" block ;
|
||||
// parameters → IDENTIFIER ( "," IDENTIFIER )* ;
|
||||
//
|
||||
//
|
||||
// statement → exprStmt
|
||||
// | forStmt
|
||||
// | ifStmt
|
||||
// | printStmt
|
||||
// | returnStmt
|
||||
// | whileStmt
|
||||
// | block ;
|
||||
//
|
||||
// forStmt → "for" "(" ( varDecl | exprStmt | ";" )
|
||||
// expression? ";"
|
||||
// expression? ")" statement ;
|
||||
//
|
||||
// returnStmt → "return" expression? ";" ;
|
||||
//
|
||||
// whileStmt → "while" "(" expression ")" statement ;
|
||||
//
|
||||
// exprStmt → expression ";" ;
|
||||
//
|
||||
// ifStmt → "if" "(" expression ")" statement
|
||||
// ( "else" statement )? ;
|
||||
//
|
||||
// printStmt → "print" expression ";" ;
|
||||
//
|
||||
// expression → assignment ;
|
||||
// assignment → IDENTIFIER "=" assignment
|
||||
// | logic_or ;
|
||||
// logic_or → logic_and ( "or" logic_and )* ;
|
||||
// logic_and → equality ( "and" equality )* ;
|
||||
// equality → comparison ( ( "!=" | "==" ) comparison )* ;
|
||||
// comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
|
||||
// term → factor ( ( "-" | "+" ) factor )* ;
|
||||
// factor → unary ( ( "/" | "*" ) unary )* ;
|
||||
// unary → ( "!" | "-" ) unary | call ;
|
||||
// call → primary ( "(" arguments? ")" )* ;
|
||||
// arguments → expression ( "," expression )* ;
|
||||
// primary → NUMBER | STRING | "true" | "false" | "nil"
|
||||
// | "(" expression ")" ;
|
||||
|
||||
struct Parser {
|
||||
tokens: Vec<Token>,
|
||||
|
@ -213,9 +211,7 @@ impl Parser {
|
|||
if params.len() >= 255 {
|
||||
return Err(Error {
|
||||
line: self.peek().line,
|
||||
kind: ErrorKind::InternalError(
|
||||
"255 parameter limit exceeded.".into(),
|
||||
),
|
||||
kind: ErrorKind::InternalError("255 parameter limit exceeded.".into()),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -429,10 +425,7 @@ impl Parser {
|
|||
|
||||
return Err(Error {
|
||||
line: equals.line,
|
||||
kind: ErrorKind::InvalidAssignmentTarget(format!(
|
||||
"{:?}",
|
||||
equals
|
||||
)),
|
||||
kind: ErrorKind::InvalidAssignmentTarget(format!("{:?}", equals)),
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -495,9 +488,7 @@ impl Parser {
|
|||
}
|
||||
|
||||
fn unary(&mut self) -> ExprResult {
|
||||
if self.match_token(&TokenKind::Bang)
|
||||
|| self.match_token(&TokenKind::Minus)
|
||||
{
|
||||
if self.match_token(&TokenKind::Bang) || self.match_token(&TokenKind::Minus) {
|
||||
return Ok(Expr::Unary(Unary {
|
||||
operator: self.previous().clone(),
|
||||
right: Box::new(self.unary()?),
|
||||
|
@ -557,10 +548,7 @@ impl Parser {
|
|||
|
||||
TokenKind::LeftParen => {
|
||||
let expr = self.expression()?;
|
||||
self.consume(
|
||||
&TokenKind::RightParen,
|
||||
ErrorKind::UnmatchedParens,
|
||||
)?;
|
||||
self.consume(&TokenKind::RightParen, ErrorKind::UnmatchedParens)?;
|
||||
return Ok(Expr::Grouping(Grouping(Box::new(expr))));
|
||||
}
|
||||
|
||||
|
@ -632,11 +620,7 @@ impl Parser {
|
|||
&self.tokens[self.current - 1]
|
||||
}
|
||||
|
||||
fn consume(
|
||||
&mut self,
|
||||
kind: &TokenKind,
|
||||
err: ErrorKind,
|
||||
) -> Result<Token, Error> {
|
||||
fn consume(&mut self, kind: &TokenKind, err: ErrorKind) -> Result<Token, Error> {
|
||||
if self.check_token(kind) {
|
||||
return Ok(self.advance());
|
||||
}
|
||||
|
|
|
@ -56,13 +56,14 @@ impl<'a> Resolver<'a> {
|
|||
// The resolver does not clone references, so unless
|
||||
// the interpreter is called before the resolver this
|
||||
// case should never happen.
|
||||
None => return Err(Error {
|
||||
line: 0,
|
||||
kind: ErrorKind::InternalError(
|
||||
"multiple function references before interpretation"
|
||||
.into(),
|
||||
),
|
||||
}),
|
||||
None => {
|
||||
return Err(Error {
|
||||
line: 0,
|
||||
kind: ErrorKind::InternalError(
|
||||
"multiple function references before interpretation".into(),
|
||||
),
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -79,10 +80,7 @@ impl<'a> Resolver<'a> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn resolve_function(
|
||||
&mut self,
|
||||
func: &'a mut parser::Function,
|
||||
) -> Result<(), Error> {
|
||||
fn resolve_function(&mut self, func: &'a mut parser::Function) -> Result<(), Error> {
|
||||
self.declare(&func.name.lexeme);
|
||||
self.define(&func.name.lexeme);
|
||||
|
||||
|
@ -123,17 +121,13 @@ impl<'a> Resolver<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn resolve_variable(
|
||||
&mut self,
|
||||
var: &'a mut parser::Variable,
|
||||
) -> Result<(), Error> {
|
||||
fn resolve_variable(&mut self, var: &'a mut parser::Variable) -> Result<(), Error> {
|
||||
if let Some(scope) = self.scopes.last_mut() {
|
||||
if let Some(false) = scope.get(var.name.lexeme.as_str()) {
|
||||
return Err(Error {
|
||||
line: var.name.line,
|
||||
kind: ErrorKind::StaticError(
|
||||
"can't read local variable in its own initialiser"
|
||||
.into(),
|
||||
"can't read local variable in its own initialiser".into(),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
@ -143,10 +137,7 @@ impl<'a> Resolver<'a> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn resolve_assign(
|
||||
&mut self,
|
||||
assign: &'a mut parser::Assign,
|
||||
) -> Result<(), Error> {
|
||||
fn resolve_assign(&mut self, assign: &'a mut parser::Assign) -> Result<(), Error> {
|
||||
self.resolve_expr(&mut assign.value)?;
|
||||
assign.depth = self.resolve_local(&assign.name);
|
||||
Ok(())
|
||||
|
@ -162,10 +153,7 @@ impl<'a> Resolver<'a> {
|
|||
None
|
||||
}
|
||||
|
||||
fn resolve_call(
|
||||
&mut self,
|
||||
call: &'a mut parser::Call,
|
||||
) -> Result<(), Error> {
|
||||
fn resolve_call(&mut self, call: &'a mut parser::Call) -> Result<(), Error> {
|
||||
self.resolve_expr(&mut call.callee)?;
|
||||
|
||||
for arg in call.args.iter_mut() {
|
||||
|
@ -198,10 +186,7 @@ impl<'a> Resolver<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn resolve(
|
||||
globals: &[String],
|
||||
block: &mut parser::Block,
|
||||
) -> Result<(), Error> {
|
||||
pub fn resolve(globals: &[String], block: &mut parser::Block) -> Result<(), Error> {
|
||||
let mut resolver: Resolver = Default::default();
|
||||
|
||||
// Scope for static globals only starts, never ends.
|
||||
|
|
22
users/tazjin/rustfmt.toml
Normal file
22
users/tazjin/rustfmt.toml
Normal file
|
@ -0,0 +1,22 @@
|
|||
edition = "2021"
|
||||
newline_style = "Unix"
|
||||
|
||||
# Default code with is 100 characters, comments should follow
|
||||
# suit.
|
||||
wrap_comments = true
|
||||
|
||||
# The default of this option creates hard-to-read nesting of
|
||||
# conditionals, turn it off.
|
||||
combine_control_expr = false
|
||||
|
||||
# Group imports by module, but no higher. This avoids hard-to-read
|
||||
# nested use statements.
|
||||
imports_granularity = "Module"
|
||||
|
||||
# Avoid vertical visual clutter by unnecessarily exploding
|
||||
# block-like arguments.
|
||||
overflow_delimited_expr = true
|
||||
|
||||
# Miscellaneous
|
||||
format_code_in_doc_comments = true
|
||||
normalize_comments = true
|
Loading…
Reference in a new issue