1.4.6 • Published 5 years ago

red-juice v1.4.6

Weekly downloads
6
License
ISC
Repository
github
Last release
5 years ago

Red Juice logo Red Juice v1.4.6

npm version Build Status Coverage Status

Introduction

Red Juice is a home-made language with the purpose of procedurally generate mathematical problems.

You can output lines and equations while executing formal expressions and storing them in variables.

Since v1.4, it is now a Turing Complete language.

Installation

First, you need to build giac (see this page for more info) :

Warning : Giac needs a node version before 12

# build with max capacity (might be long anyway)
JOBS=4 npm install giac@latest

Then you can install with the following :

npm install red-juice --production

Usage

With node

then

const interpreter = require('red-juice');

...

const errors = interpreter.verify(code);
if(errors.length){
   //print/handle errors
} else {
   const output = interpreter.eval(code);
   // see example for output format
}

Commande line

node red-juice.js inputFile [-v][-h][-q][-d][-o outputFile][-j][-f][-s seed][-l]

-v / --verbose : more info
-h / --help : show this message
-q / --quiet : do not log output
-d / --debug : show debug log
-o / --output : indicate output file (activate json format)
-j / --json : json format
-f / --final : show as final exercise
-s / --seed : indicate numeric seed
-l / --latex : enable latex output of vars

Example

The code :

VAR A = 2;
VAR B = (A + 1)/2;
$\frac{{A}}{{B}}$ #LaTeX expression
START ex1;
    TIME 60;
    IF A < 5;
        ${A}\times {B}$
        this is a test
        START partA;
            this is partA
        END partA;
    ELSE;
        START partA;
            this is partA
        END partA;
    ENDIF;
END ex1;

Will output (

{
    "content" : {
        "" : "$\frac{2}{\frac{3}{2}}$\n",
        "#ex1" : "$2\times \frac{3}{2}$\nthis is a test\n",
        "#ex1#partA" : "this is partA\n"
    },
    "timers" : {
        "#ex1" : 60
    }
}

Writing rules

  • A line without a semicolon (;) is an output line.
  • Everything after an hashtag (#) is considered commented. (even outside code lines)
  • Indentation doesn't matter.
  • Variables in output surrounded by braces ({var}) will be replaced by its value in LaTeX.
  • Variables can be tested in output with {var:something}, it will be replaced with 'something' only if 'var' is not null.
  • Same thing, {!var:something}, it will be replaced with something only if 'var' is null.
  • Tests in output can be more complex like {var > 0 || var2 != 5:something}
  • In output you can use {simplify:something} and what's inside will be simplified and output in LaTeX
  • If the simplify output is red, it means it failed somehow (there is maybe some * missing between values)
  • Code line must start with a keyword (see below).
  • Expression will be computed and stored as formal. (1/3 will keep its formal value and be simplified if needed)
  • Keywords should be uppercase, but will work as any capitalization.
  • You can use predefined constants like PI.
  • Variable names must start with a letter.
  • Expression are written with basic mathematical symbols (-+*/^%) and functions. (see below)
  • Functions can be used with any capitalization.

Keywords

VAR - Assignation

Example :

VAR A = RAND(1, 10);
VAR A = e^SIN(A);

IF, ELIF, ELSE, ENDIF - Condition

Example :

IF A < 5;
    do something
ELIF A > 6 && A <= 10 || A >= 15;
    do something else
ELSE;
    do something else
ENDIF;

WHILE, ENDWHILE - Loops

Example :

WHILE A != B;
   do something
ENDWHILE;

START, END - Partition

Example :

will be outputed in part with blank name
START part1;
    will be outputed in part with name #part1
    START partA;
        will be outputed in part with name #part1#partA
    END partA;
    START partB;
        will be outputed in part with name #part1#partB
    END partB;
END part1;

TIME - Indication

Example :

START part1;
    TIME 60; #indicate for #part1
    START partA;
        TIME 65; #indicate for #part1#partA
    END partA;
    TIME 30; #overwrite for #part1
END part1;

Functions and operators list

Basic functions

NameAliasArgumentsMore info
rand2random value between argument 1 and argument 2 included
randnn2random value not null between argument 1 and argument 2 included
abs1absolute value or module of complex
sign1sign (-1,0,+1)
max2maximum
min2minimum
round1
floor1
frac1
ceil1
factorial1
sqrt1square root
expe^var1exponential
logln1natural logarithm
log101base 10 logarithm

Complexes

NameAliasArgumentsMore info
re1real part of complex
im1imaginary part of complex
arg1argument
conj1conjugate

Trigonometry

NameAliasArgumentsMore info
sin1sine
cos1cosine
tan1tangent
cot1cotangent
asin1arc sine
acos1arc cosine
atan1arc tangent
sinh1hyperbolic sine
cosh1hyperbolic cosine
tanh1hyperbolic tangent
asinh1argument of hyperbolic sine
acosh1argument of hyperbolic cosine
atanh1argument of hyperbolic tangent
asinh1argument of hyperbolic sine
acosh1argument of hyperbolic cosine
atanh1argument of hyperbolic tangent

Operators

NameAliasArgumentsMore info
neg-var1negative value
not!var1inverse value
plusvar1 + var22addition
minusvar1 - var22substraction
divvar1 / var22division
modvar1 % var22modulo
powvar1 ^ var22power
timesvar1 * var22multiplication
eqvar1 == var22equal
neqvar1 != var22not equal
gtevar1 >= var22greater than or equal
ltevar1 <= var22less than or equal
gtvar1 > var22greater than
ltvar1 < var22less than
andvar1 && var22
orvar1 || var22

Vectors and Matrices

NameAliasArgumentsMore info
lengthlen1size of var
randvec3random vector of size arg 1 and values between arg 2 and arg 3
randmat4random matrix of size arg 1 x arg 2 and values between arg 3 and arg 4
getvar1[var2]2get value of cell in var
cross2cross product between 2 vectors
vec{n}ncreates a vector with n given values (ex: vec3(1,2,3))
tran~mat11transpose of matrix
mtimesmat1.*mat22cell by cell multiplication
rank1rank of matrix
det1determinant of matrix
ker1kernel of matrix
image1image of matrix
idn1identity matrix of given size
zeros2create a matrix of size arg 1 x arg 2 filled with 0
ones2create a matrix of size arg 1 x arg 2 filled with 1

Constants

NameAliasExplanation
eexponential
piPIπ
infinityinf
icomplex i
euler_gammaEuler constant γ
trueTrueboolean value
falseFalseboolean value

Changelog

  • 1.4.6: fixed auto install of giac.node
  • 1.4.5: added max node version + more info to install
  • 1.4.4: multiple subscriptions in formula (ex : 1)
  • 1.4.3: zeros and ones matrix functions
  • 1.4.2:
    • VAR A[B] = ...; var subscription by other var
    • no LaTeX in output by default in CLI (need --latex)
    • loop bug fix
  • 1.4.1: command line interface
  • 1.4.0: loops with WHILE, ENDWHILE
  • 1.3.5: Predefined capitalization for constants
  • 1.3.4: len function for vectors and matrices
  • 1.3.3: better looking vectors in LaTeX
  • 1.3.2: variable cell attribution VAR A[0] = 2;
  • 1.3.1 :
    • More constants : e, i, infinity, inf, euler_gamma, true, false
    • vec{n} creates vectors size n
    • subscriptable vars in expressions A[0] * A[1]
  • 1.3.0 :
    • Matrices and vectors functions
    • Safe giac call (avoid ERROR: something in output)
  • 1.2.9 : TIME keyword and output format change
  • 1.2.8 : RANDNN function
  • 1.2.7 : Remove blank outputed lines
  • 1.2.6 : Word blacklist for output
  • 1.2.5 : {simplify:2x+2x} -> 4\\cdot x
  • 1.2.4 : Version number to be shown in editor
  • 1.2.3 : Added error 'Restricted variable name'
  • 1.2.2 : Fix when output had a lot of -- or +-
  • 1.2.1 : +- and -- handling (not replacing already there)
  • 1.2 : A lot of functions ported from giac (see above)
  • 1.1.1 :
    • Fixed mis-implementation of Shunting Yard
    • Operators into functions (! -> not)
  • 1.1 :
    • LaTeX conditions {v:{v > 1:new version}{v == 0:same version}}
    • Bug fix
  • 1.0 :
    • Versioning
    • Not operators (!,!=`)
    • Negative things (-func, -var)
    • Debug switch
    • Bug fix
1.4.6

5 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago