Skip to content

Commit

Permalink
Fix runtime concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed Oct 14, 2023
1 parent 61e9497 commit efe3a98
Show file tree
Hide file tree
Showing 91 changed files with 10,842 additions and 4,221 deletions.
1,810 changes: 1,241 additions & 569 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ edition = "2021"

[features]
default = ["extensions", "crypto-functions", "encoding-functions"]
extensions = ["js-sandbox"]
extensions = ["js_playground"]
crypto-functions = ["md-5", "sha2"]
encoding-functions = ["base64", "urlencoding"]

[dependencies]
once_cell = "1.18.0"
thiserror = "1.0.40"
regex = "1.9.4"
pest = "2.5.6"
pest_derive = "2.7.3"
Expand All @@ -27,7 +29,7 @@ chrono = "0.4.23"
rand = "0.8.5"

# Feature deps
js-sandbox = { version = "0.2.0-rc.2", optional = true }
js_playground = { git = "https://github.com/rscarson/js-playground.git", optional = true }
md-5 = { version = "0.10.5", optional = true }
sha2 = { version = "0.10.6", optional = true }
base64 = { version = "0.21.0", optional = true }
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ This project is the engine behind [Lavendeux](https://rscarson.github.io/lavende
### Getting Started
To use it, create a `ParserState` object, and use it to tokenize input with `Token::new`:
```rust
use lavendeux_parser::{ParserState, ParserError, Token, Value};
use lavendeux_parser::{ParserState, Error, Token, Value};

fn main() -> Result<(), ParserError> {
fn main() -> Result<(), Error> {
// Create a new parser, and tokenize 2 lines
let mut state : ParserState = ParserState::new();
let lines = Token::new("x=9\nsqrt(x) @bin", &mut state)?;
Expand All @@ -32,9 +32,9 @@ fn main() -> Result<(), ParserError> {
```
The result will be a `Token` object:
```rust
use lavendeux_parser::{ParserState, ParserError, Token, Value};
use lavendeux_parser::{ParserState, Error, Token, Value};

fn main() -> Result<(), ParserError> {
fn main() -> Result<(), Error> {
let mut state : ParserState = ParserState::new();
let lines = Token::new("x=9\nsqrt(x) @bin", &mut state)?;

Expand All @@ -56,8 +56,8 @@ fn main() -> Result<(), ParserError> {

A number of functions and @decorators are available for expressions to use - add more using the state:
```rust
use lavendeux_parser::{ParserState, ParserError, DecoratorDefinition, FunctionDefinition, FunctionArgument, Value};
use lavendeux_parser::errors::*;
use lavendeux_parser::{ParserState, Error, DecoratorDefinition, FunctionDefinition, FunctionArgument, Value};
use lavendeux_parser::Error;

let mut state : ParserState = ParserState::new();
state.decorators.register(DecoratorDefinition {
Expand Down Expand Up @@ -151,9 +151,9 @@ Extensions are enabled by default, and can be excluded by disabling the crate's

Extensions can be loaded as follows:
```rust
use lavendeux_parser::{ParserState, ParserError, Value, Token};
use lavendeux_parser::{ParserState, Error, Value, Token};

fn main() -> Result<(), ParserError> {
fn main() -> Result<(), Error> {
let mut state : ParserState = ParserState::new();

// Load one extension
Expand Down
10 changes: 5 additions & 5 deletions example_extensions/colour_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* It must return an object similar to the one below.
* @returns Object
*/
function extension() {
export function extension() {
return {
name: "HTML Colour Utilities",
author: "@rscarson",
Expand Down Expand Up @@ -31,7 +31,7 @@ function extension() {
* @param {Value} args
* @returns {Value} result
*/
function function_colour(args) {
export function function_colour(args) {
if (args.length != 1) {
throw "color(s): expected 1 argument";
} else if (!args[0].String) {
Expand Down Expand Up @@ -60,7 +60,7 @@ function function_colour(args) {
* @param {Value} args
* @returns {Value} result
*/
function function_complement(args) {
export function function_complement(args) {
if (args.length != 1) {
throw "complement(n): expected 1 argument";
} else if (!args[0].Integer) {
Expand Down Expand Up @@ -89,15 +89,15 @@ function function_complement(args) {
* @param {Value} args
* @returns {String} result
*/
function decorator_colour(value) {
export function decorator_colour(value) {
if (value.Integer) {
return '#'+value.Integer.toString(16).padEnd(6, '0');
}

throw "@color: expected an integer value";
}

function extract_rgb(int_val) {
export function extract_rgb(int_val) {
return {
"b": int_val & 0xFF,
"g": (int_val >> 8) & 0xFF,
Expand Down
37 changes: 37 additions & 0 deletions example_extensions/example.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions example_extensions/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let extension = lavendeux.extend({
'name': 'simple_extension',
'author': '@rscarson',
'version': '1.0.0'
});

extension.addFunction('add', (left, right) => left + right, 'Float')
.requireArgument('Integer').requireArgument('Integer');

extension.addDecorator('usd', (input) => `${input}`, 'Float')

lavendeux.register(extension);
49 changes: 16 additions & 33 deletions example_extensions/stateful_functions.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
/**
* This function tells Lavendeux about this extension.
* It must return an object similar to the one below.
* @returns Object
*/
function extension() {
return {
name: "Stateful function demo",
author: "@rscarson",
version: "0.2.0",
let extension = lavendeux.extend({
'name': 'stateful_extension',
'author': '@rscarson',
'version': '1.0.0'
});

functions: {
"set": "functionSet"
},
};
}

/**
* Functions can also be stateful, gaining access to the parser's variables
* @param {Value} args
* @returns {Value} result
*/
function functionSet(args) {
if (args.length != 2) {
throw new Error("set(<string>, <any>): expected 2 arguments");
} else if (!args[0].String) {
throw "set(<string>, <any>): expected a string value";
}

let name = args[0].String, value = args[1];
const state = getState();
extension.addFunction('put', (name, value, state) => {
state[name] = value;
setState(state);

return value;
}
})
.requireArgument('String')
.requireArgument();

extension.addFunction('get', (name, state) => {
return state[name];
})
.requireArgument('String');

lavendeux.register(extension);
Loading

0 comments on commit efe3a98

Please sign in to comment.