Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,5 @@
def star(E): (E | star(E)) // .;
def plus(E): E | (plus(E) // . );
def optional(E): E // .;
def amp(E): . as $in | E | $in;
def neg(E): select( [E] == [] );

View file

@ -0,0 +1,22 @@
def literal($s):
select(.remainder | startswith($s))
| .result += [$s]
| .remainder |= .[$s | length :] ;
def box(E):
((.result = null) | E) as $e
| .remainder = $e.remainder
| .result += [$e.result] # the magic sauce
;
# Consume a regular expression rooted at the start of .remainder, or emit empty;
# on success, update .remainder and set .match but do NOT update .result
def consume($re):
# on failure, match yields empty
(.remainder | match("^" + $re)) as $match
| .remainder |= .[$match.length :]
| .match = $match.string ;
def parseNumber($re):
consume($re)
| .result = .result + [.match|tonumber] ;

View file

@ -0,0 +1,13 @@
def Expr:
def ws: consume(" *");
def Number: ws | parseNumber( "-?[0-9]+([.][0-9]*)?" );
def Sum:
def Parenthesized: ws | consume("[(]") | ws | box(Sum) | ws | consume("[)]");
def Factor: Parenthesized // Number;
def Product: box(Factor | star( ws | (literal("*") // literal("/")) | Factor));
Product | ws | star( (literal("+") // literal("-")) | Product);
Sum;

View file

@ -0,0 +1,22 @@
# Left-to-right evaluation
def eval:
if type == "array" then
if length == 0 then null
else .[-1] |= eval
| if length == 1 then .[0]
else (.[:-2] | eval) as $v
| if .[-2] == "*" then $v * .[-1]
elif .[-2] == "/" then $v / .[-1]
elif .[-2] == "+" then $v + .[-1]
elif .[-2] == "-" then $v - .[-1]
else tostring|error
end
end
end
else .
end;
def eval(String):
{remainder: String}
| Expr.result
| eval;