Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions examples/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{error::Error as StdError, fs::File, io::Read};

use clap::{crate_description, crate_name, crate_version, Arg, Command};

use piccolo::{
compiler::{self, interning::BasicInterner, string_utils::debug_utf8_lossy, CompiledPrototype},
io,
};
use piccolo::compiler::interning::BasicInterner;
use piccolo::compiler::string_utils::debug_utf8_lossy;
use piccolo::compiler::{self, CompiledPrototype};
use piccolo::io;
use std::error::Error as StdError;
use std::fs::File;
use std::io::Read;

fn print_function<S: AsRef<[u8]>>(function: &CompiledPrototype<S>, depth: usize) {
let indent = " ".repeat(depth);
Expand Down
4 changes: 2 additions & 2 deletions examples/execute.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use piccolo::io::buffered_read;
use piccolo::{Closure, Executor, Lua};
use std::fs::File;
use std::io::Read;

use piccolo::{io::buffered_read, Closure, Executor, Lua};

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load the Lua file
let mut file = buffered_read(File::open("./examples/execute.lua")?)?;
Expand Down
11 changes: 5 additions & 6 deletions examples/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::fs::File;
use std::{error::Error as StdError, io::Read};

use clap::{crate_description, crate_name, crate_version, Arg, ArgAction, Command};
use rustyline::DefaultEditor;

use piccolo::compiler::{ParseError, ParseErrorKind};
use piccolo::{
compiler::{ParseError, ParseErrorKind},
io, meta_ops, Callback, CallbackReturn, Closure, Executor, ExternError, Function, Lua,
StashedExecutor,
};
use rustyline::DefaultEditor;
use std::error::Error as StdError;
use std::fs::File;
use std::io::Read;

fn run_code(lua: &mut Lua, executor: &StashedExecutor, code: &str) -> Result<(), ExternError> {
lua.try_enter(|ctx| {
Expand Down
3 changes: 3 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
newline_style = "Unix"
imports_granularity = "Module"
group_imports = "One"
20 changes: 7 additions & 13 deletions src/any.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use std::{
any::TypeId,
fmt,
hash::{Hash, Hasher},
};

use gc_arena::{
arena::Root,
barrier::{self, Write},
Collect, Gc, Mutation, Rootable,
};
use core::any::TypeId;
use core::fmt;
use core::hash::{Hash, Hasher};
use gc_arena::arena::Root;
use gc_arena::barrier::{self, Write};
use gc_arena::{Collect, Gc, Mutation, Rootable};

/// A `Gc` pointer to any type `T: Collect + 'gc` which allows safe downcasting.
///
Expand Down Expand Up @@ -187,9 +182,8 @@ impl<'gc, M> Any<'gc, M> {

#[cfg(test)]
mod tests {
use gc_arena::arena::rootless_mutate;

use super::*;
use gc_arena::arena::rootless_mutate;

#[test]
fn test_any_value() {
Expand Down
27 changes: 11 additions & 16 deletions src/async_callback.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
use std::{
cell::Cell,
future::{poll_fn, Future},
marker::PhantomData,
mem,
pin::Pin,
ptr,
rc::Rc,
task::{self, Poll, RawWaker, RawWakerVTable, Waker},
};

use gc_arena::{Collect, DynamicRootSet, Mutation};

use crate::stash::{Fetchable, Stashable};
use crate::{
stash::{Fetchable, Stashable},
BoxSequence, Context, Error, Execution, Function, Sequence, SequencePoll, Stack, StashedError,
StashedFunction, StashedThread, Thread,
};
use alloc::rc::Rc;
use core::cell::Cell;
use core::future::{poll_fn, Future};
use core::marker::PhantomData;
use core::pin::Pin;
use core::task::{self, Poll, RawWaker, RawWakerVTable, Waker};
use core::{mem, ptr};
use gc_arena::{Collect, DynamicRootSet, Mutation};

/// Create a [`Sequence`] impl from a [`Future`] that can suspend, call Lua functions, yield to Lua,
/// and resume threads as async method calls via a held [`AsyncSequence`] proxy.
///
/// Can be used to implement `Sequence` in a way MUCH easier than manual state machines.
///
/// Currently uses `async` to do what in the future could be more directly accomplished with
/// coroutines (see the unstable [`std::ops::Coroutine`] trait). The [`std::task::Context`]
/// coroutines (see the unstable [`core::ops::Coroutine`] trait). The [`core::task::Context`]
/// available within the created future is **meaningless** and has a NOOP waker; we are only using
/// `async` as a stable way to express what would be better expressed as a simple coroutine.
///
/// It is possible to integrate proper async APIs with `piccolo`, and to even have a method to
/// "wake" Lua coroutines with a *real* [`std::task::Waker`], but simply calling an external async
/// "wake" Lua coroutines with a *real* [`core::task::Waker`], but simply calling an external async
/// function from the created future here is *not* the way to do it. It will not do what you want,
/// and probably will result in panics.
///
Expand Down
15 changes: 6 additions & 9 deletions src/callback.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::{
fmt,
hash::{Hash, Hasher},
pin::Pin,
};

use allocator_api2::boxed;
use gc_arena::{allocator_api::MetricsAlloc, Collect, Gc, Mutation};

use crate::{Context, Error, Execution, Function, Stack, Thread};
use allocator_api2::boxed;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::pin::Pin;
use gc_arena::allocator_api::MetricsAlloc;
use gc_arena::{Collect, Gc, Mutation};

/// Describes the next action for an [`Executor`](crate::Executor) to take after a callback has
/// returned.
Expand Down
20 changes: 9 additions & 11 deletions src/closure.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use std::hash::{Hash, Hasher};

use crate::compiler::{self, CompiledPrototype, FunctionRef, LineNumber};
use crate::opcode::OpCode;
use crate::thread::OpenUpValue;
use crate::types::UpValueDescriptor;
use crate::{Constant, Context, String, Table, Value};
use allocator_api2::{boxed, vec, SliceExt};
use gc_arena::{allocator_api::MetricsAlloc, lock::Lock, Collect, Gc, Mutation};
use core::hash::{Hash, Hasher};
use gc_arena::allocator_api::MetricsAlloc;
use gc_arena::lock::Lock;
use gc_arena::{Collect, Gc, Mutation};
use thiserror::Error;

use crate::{
compiler::{self, CompiledPrototype, FunctionRef, LineNumber},
opcode::OpCode,
thread::OpenUpValue,
types::UpValueDescriptor,
Constant, Context, String, Table, Value,
};

// Note: These errors must not have #[error(transparent)] so that
// anyhow::Error::root_cause and downcasting work as expected by the
// interpreter. (Even though that gives slightly cleaner error messages).
Expand Down
58 changes: 25 additions & 33 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
use std::{
collections::{hash_map, VecDeque},
fmt, iter, mem,
use super::lexer::LineNumber;
use super::operators::{
categorize_binop, comparison_binop_const_fold, comparison_binop_operation,
simple_binop_const_fold, simple_binop_operation, unop_const_fold, unop_operation,
BinOpCategory, ComparisonBinOp, ShortCircuitBinOp, SimpleBinOp,
};

use super::parser::{
AssignmentStatement, AssignmentTarget, BinaryOperator, Block, CallSuffix, Chunk,
ConstructorField, Expression, FieldSuffix, ForStatement, FunctionCallStatement,
FunctionDefinition, FunctionStatement, HeadExpression, IfStatement, LocalAttributes,
LocalFunctionStatement, LocalStatement, PrimaryExpression, RecordKey, RepeatStatement,
ReturnStatement, SimpleExpression, Statement, SuffixPart, SuffixedExpression, TableConstructor,
UnaryOperator, WhileStatement,
};
use super::register_allocator::RegisterAllocator;
use super::StringInterner;
use crate::constant::IdenticalConstant;
use crate::opcode::{OpCode, Operation, RCIndex};
use crate::types::{
ConstantIndex16, ConstantIndex8, Opt254, PrototypeIndex, RegisterIndex, UpValueDescriptor,
UpValueIndex, VarCount,
};
use crate::Constant;
use ahash::HashMap;
use alloc::collections::VecDeque;
use core::{fmt, iter, mem};
use gc_arena::Collect;
use std::collections::hash_map;
use thiserror::Error;

use crate::{
constant::IdenticalConstant,
opcode::{OpCode, Operation, RCIndex},
types::{
ConstantIndex16, ConstantIndex8, Opt254, PrototypeIndex, RegisterIndex, UpValueDescriptor,
UpValueIndex, VarCount,
},
Constant,
};

use super::{
lexer::LineNumber,
operators::{
categorize_binop, comparison_binop_const_fold, comparison_binop_operation,
simple_binop_const_fold, simple_binop_operation, unop_const_fold, unop_operation,
BinOpCategory, ComparisonBinOp, ShortCircuitBinOp, SimpleBinOp,
},
parser::{
AssignmentStatement, AssignmentTarget, BinaryOperator, Block, CallSuffix, Chunk,
ConstructorField, Expression, FieldSuffix, ForStatement, FunctionCallStatement,
FunctionDefinition, FunctionStatement, HeadExpression, IfStatement, LocalAttributes,
LocalFunctionStatement, LocalStatement, PrimaryExpression, RecordKey, RepeatStatement,
ReturnStatement, SimpleExpression, Statement, SuffixPart, SuffixedExpression,
TableConstructor, UnaryOperator, WhileStatement,
},
register_allocator::RegisterAllocator,
StringInterner,
};

#[derive(Debug, Copy, Clone, Error)]
pub enum CompileErrorKind {
#[error("insufficient available registers")]
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/interning.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::rc::Rc;

use ahash::HashSet;
use alloc::rc::Rc;

pub trait StringInterner {
type String: AsRef<[u8]> + Clone;
Expand Down
23 changes: 9 additions & 14 deletions src/compiler/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use std::{char, fmt};

use gc_arena::Collect;
use thiserror::Error;

use super::string_utils::{
debug_utf8_lossy, is_alpha, is_digit, is_newline, FORM_FEED, VERTICAL_TAB,
};
use super::StringInterner;
use crate::compiler::string_utils::{
from_digit, from_hex_digit, is_hex_digit, is_space, read_dec_float, read_dec_integer,
read_hex_float, read_hex_integer, ALERT_BEEP, BACKSPACE,
};

use super::{
string_utils::{debug_utf8_lossy, is_alpha, is_digit, is_newline, FORM_FEED, VERTICAL_TAB},
StringInterner,
};
use core::{char, fmt};
use gc_arena::Collect;
use thiserror::Error;

#[derive(Clone)]
pub enum Token<S> {
Expand Down Expand Up @@ -920,11 +917,9 @@ fn get_reserved_word_token<S>(word: &[u8]) -> Option<Token<S>> {

#[cfg(test)]
mod tests {
use std::rc::Rc;

use crate::compiler::interning::BasicInterner;

use super::*;
use crate::compiler::interning::BasicInterner;
use alloc::rc::Rc;

fn test_tokens(source: &str, tokens: &[Token<Rc<[u8]>>]) {
let mut lexer = Lexer::new(source.as_bytes(), BasicInterner::default());
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pub mod parser;
mod register_allocator;
pub mod string_utils;

pub use self::{
compiler::{compile_chunk, CompileError, CompileErrorKind, CompiledPrototype, FunctionRef},
interning::StringInterner,
lexer::LineNumber,
parser::{parse_chunk, ParseError, ParseErrorKind},
pub use self::compiler::{
compile_chunk, CompileError, CompileErrorKind, CompiledPrototype, FunctionRef,
};
pub use self::interning::StringInterner;
pub use self::lexer::LineNumber;
pub use self::parser::{parse_chunk, ParseError, ParseErrorKind};
9 changes: 3 additions & 6 deletions src/compiler/operators.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
opcode::{Operation, RCIndex},
types::RegisterIndex,
Constant,
};

use super::parser::{BinaryOperator, UnaryOperator};
use crate::opcode::{Operation, RCIndex};
use crate::types::RegisterIndex;
use crate::Constant;

// Binary operators which map directly to a single opcode
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
Expand Down
11 changes: 4 additions & 7 deletions src/compiler/parser.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::{fmt, ops, rc::Rc};

use super::lexer::{LexError, Lexer, LineNumber, Token};
use super::StringInterner;
use alloc::rc::Rc;
use core::{fmt, ops};
use thiserror::Error;

use super::{
lexer::{LexError, Lexer, LineNumber, Token},
StringInterner,
};

#[derive(Debug, Clone)]
pub struct LineAnnotated<T> {
pub inner: T,
Expand Down
6 changes: 2 additions & 4 deletions src/compiler/string_utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{
fmt::{self, Write as _},
str,
};
use core::fmt::{self, Write as _};
use core::str;

pub fn trim_whitespace(mut s: &[u8]) -> &[u8] {
s = &s[s.iter().position(|&c| !is_space(c)).unwrap_or(s.len())..];
Expand Down
6 changes: 2 additions & 4 deletions src/constant.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::hash::{Hash, Hasher};

use gc_arena::Collect;

use crate::compiler::string_utils::{read_float, read_integer, trim_whitespace};
use core::hash::{Hash, Hasher};
use gc_arena::Collect;

#[derive(Debug, Copy, Clone, Collect)]
#[collect(no_drop)]
Expand Down
4 changes: 2 additions & 2 deletions src/conversion.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{array, iter, ops, string::String as StdString};

use crate::{
Callback, Closure, Context, Function, String, Table, Thread, TypeError, UserData, Value,
};
use alloc::string::String as StdString;
use core::{array, iter, ops};

pub trait IntoValue<'gc> {
fn into_value(self, ctx: Context<'gc>) -> Value<'gc>;
Expand Down
11 changes: 6 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{error::Error as StdError, fmt, string::String as StdString, sync::Arc};

use gc_arena::{Collect, Gc, Rootable};
use thiserror::Error;

use crate::{
Callback, CallbackReturn, Context, FromValue, Function, IntoValue, MetaMethod, Singleton,
Table, UserData, Value,
};
use alloc::string::String as StdString;
use alloc::sync::Arc;
use core::error::Error as StdError;
use core::fmt;
use gc_arena::{Collect, Gc, Rootable};
use thiserror::Error;

#[derive(Debug, Clone, Copy, Error)]
#[error("type error, expected {expected}, found {found}")]
Expand Down
Loading