@covopen/covscript
v1.3.0
Published
Converts simple logic expressions to JSON logic and back
Readme
covscript
This repo contains a parser and a generator for Covscript, a minimal language used for formulating Covquestions questionaires.
The language is compiled to a subset of JSON-logic. JSON-logic expressions can be converted back to Covscript.
Syntax and supported ops
A program in Covscript is always a single expression. The result of the expression is the result of the program.
The following literals are supported:
- Numeric constants (
1,-2,3.5,1e+3) - Boolean constants (
true,false) - Quote-Delimeted String constants (
"Hello") - Vaiables (
question3.value)
The following binary operations are supported:
- Arithmetic operands (
+,-,*,/) - Logical operands (
and,or) - Comparisons (
>,<,<=,>=,==,!=) - In-Operator for lists (
in) - Unary logical negation (
!)
Also, there is an inline If, parantheses are supported.
The operator precedence follows JavaScript precedence, or in other words, it does what you would expect.
Examples
If work.value == "medical" Then
"ShowMedicalAdvisory"
Else
If has_fever.value Then
"ShowHighRisk"
Else
"ShowLowRisk"
EndIf
EndIfIf contact_date.value > symptoms_date.value Then "StayHome" Else "AlsoStayHome" EndIfFor more examples, please have a look at the unit tests.
Using the Parser
import { CovscriptToJsonLogicConverter } from "../src";
const parser = new CovscriptToJsonLogicConverter();
const res = parser.parse("1 + 2");
console.log(res); // { '+': [1, 2] }Converting back to Covscript
import { CovscriptGenerator } from "../src/generator";
const generator = new CovscriptGenerator();
const rendered = generator.generate({ "+": [1, 2] });
console.log(res); // '1 + 2'