Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Arithmetic-evaluation/Jq/arithmetic-evaluation-1.jq
Normal file
5
Task/Arithmetic-evaluation/Jq/arithmetic-evaluation-1.jq
Normal 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] == [] );
|
||||
22
Task/Arithmetic-evaluation/Jq/arithmetic-evaluation-2.jq
Normal file
22
Task/Arithmetic-evaluation/Jq/arithmetic-evaluation-2.jq
Normal 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] ;
|
||||
13
Task/Arithmetic-evaluation/Jq/arithmetic-evaluation-3.jq
Normal file
13
Task/Arithmetic-evaluation/Jq/arithmetic-evaluation-3.jq
Normal 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;
|
||||
22
Task/Arithmetic-evaluation/Jq/arithmetic-evaluation-4.jq
Normal file
22
Task/Arithmetic-evaluation/Jq/arithmetic-evaluation-4.jq
Normal 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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue