3.0.11 • Published 4 days ago

bare-script v3.0.11

Weekly downloads
-
License
MIT
Repository
-
Last release
4 days ago

bare-script

npm GitHub

BareScript is a simple, lightweight, and portable programming language. Its Pythonic syntax is influenced by JavaScript, C, and the Unix Shell. BareScript also has a library of built-in functions for common programming operations. BareScript can be embedded within applications or used as a stand-alone programming language using the command-line interface.

There are two implementations of BareScript: BareScript for JavaScript (this package) and BareScript for Python. Both implementations have 100% unit test coverage with identical unit test suites, so you can be confident that BareScript will execute the same regardless of the underlying runtime environment.

Links

Executing BareScript Scripts

To execute a BareScript script, parse the script using the parseScript function. Then execute the script using the executeScript function or the executeScriptAsync function. For example:

import {executeScript} from 'bare-script/lib/runtime.js';
import {parseScript} from 'bare-script/lib/parser.js';

// Parse the script
const script = parseScript(`\
# Double a number
function double(n):
    return n * 2
endfunction

return N + ' times 2 is ' + double(N)
`);

// Execute the script
const globals = {'N': 10};
console.log(executeScript(script, {globals}));

This outputs:

10 times 2 is 20

The BareScript Library

The BareScript Library includes a set of built-in functions for mathematical operations, object manipulation, array manipulation, regular expressions, HTTP fetch and more. The following example demonstrates the use of the systemFetch, objectGet, and arrayLength functions.

import {executeScriptAsync} from 'bare-script/lib/runtimeAsync.js';
import {parseScript} from 'bare-script/lib/parser.js';

// Parse the script
const script = parseScript(`\
# Fetch the BareScript library documentation JSON
docs = jsonParse(systemFetch('https://craigahobbs.github.io/bare-script/library/library.json'))

# Return the number of library functions
return 'The BareScript Library has ' + arrayLength(objectGet(docs, 'functions')) + ' functions'
`);

// Execute the script
console.log(await executeScriptAsync(script, {'fetchFn': fetch}));

This outputs:

The BareScript Library has 105 functions

Evaluating BareScript Expressions

To evaluate a BareScript expression, parse the expression using the parseExpression function. Then evaluate the expression using the evaluateExpression function or the evaluateExpressionAsync function.

Expression evaluation includes the BareScript Expression Library, a set of built-in, spreadsheet-like functions.

For example:

import {evaluateExpression} from 'bare-script/lib/runtime.js';
import {parseExpression} from 'bare-script/lib/parser.js';

// Parse the expression
const expr = parseExpression('2 * max(a, b, c)');

// Evaluate the expression
const variables = {'a': 1, 'b': 2, 'c': 3};
console.log(evaluateExpression(expr, null, variables))

This outputs:

6

The BareScript Command-Line Interface (CLI)

You can run BareScript from the command line using the BareScript CLI, "bare". BareScript script files use the ".bare" file extension.

bare script.bare

Note: In the BareScript CLI, import statements and the systemFetch function read non-URL paths from the local file system. systemFetch calls with a non-URL path and a request body write the body to the path.

MarkdownUp, a Markdown Viewer with BareScript

MarkdownUp is a Markdown Viewer that executes BareScript embedded within Markdown documents. MarkdownUp extends its standard library with functions for dynamically rendering Markdown text, drawing SVG images, etc.

For example:

# Markdown Application

This is a Markdown document with embedded BareScript:

~~~ markdown-script
markdownPrint('Hello, Markdown!')
~~~

Development

This package is developed using javascript-build. It was started using javascript-template as follows:

template-specialize javascript-template/template/ bare-script/ -k package bare-script -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs' -k noapp 1
3.0.11

4 days ago

3.0.9

9 days ago

3.0.10

8 days ago

3.0.8

1 month ago

3.0.7

1 month ago

3.0.6

1 month ago

3.0.5

2 months ago

3.0.4

2 months ago

3.0.3

2 months ago

3.0.2

2 months ago

3.0.1

2 months ago

3.0.0

2 months ago

2.3.2

3 months ago

2.3.0

3 months ago

2.3.1

3 months ago

2.2.18

5 months ago

2.2.17

5 months ago

2.2.16

6 months ago

2.2.15

6 months ago

2.2.14

6 months ago

2.2.13

6 months ago

2.2.12

6 months ago

2.2.11

6 months ago

2.2.10

7 months ago

2.2.9

7 months ago

2.2.8

7 months ago

2.2.7

7 months ago

2.2.6

7 months ago

2.2.5

7 months ago

2.2.4

7 months ago

2.2.3

8 months ago

2.2.2

8 months ago

2.2.1

8 months ago

2.2.0

8 months ago

2.1.1

8 months ago

2.1.0

8 months ago

2.0.3

9 months ago

2.0.2

9 months ago