}
}
+static jv f_mod(jv input, jv a, jv b) {
+ jv_free(input);
+ if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
+ return jv_number((intmax_t)jv_number_value(a) % (intmax_t)jv_number_value(b));
+ } else {
+ return type_error2(a, b, "cannot be divided");
+ }
+}
+
static jv f_equal(jv input, jv a, jv b) {
jv_free(input);
return jv_bool(jv_equal(a, b));
{(cfunction_ptr)f_minus, "_minus", 3},
{(cfunction_ptr)f_multiply, "_multiply", 3},
{(cfunction_ptr)f_divide, "_divide", 3},
+ {(cfunction_ptr)f_mod, "_mod", 3},
{(cfunction_ptr)f_tonumber, "tonumber", 1},
{(cfunction_ptr)f_tostring, "tostring", 1},
{(cfunction_ptr)f_keys, "keys", 1},
"-=" { return SETMINUS; }
"*=" { return SETMULT; }
"/=" { return SETDIV; }
+"%=" { return SETMOD; }
"//=" { return SETDEFINEDOR; }
"<=" { return LESSEQ; }
">=" { return GREATEREQ; }
-"."|"="|";"|","|":"|"|"|"+"|"-"|"*"|"/"|"\$"|"<"|">" { return yytext[0];}
+"."|"="|";"|","|":"|"|"|"+"|"-"|"*"|"/"|"%"|"\$"|"<"|">" { return yytext[0];}
"["|"{"|"(" {
return enter(yytext[0], YY_START, yyscanner);
%token <literal> FIELD
%token <literal> LITERAL
%token <literal> FORMAT
+%token SETMOD "%="
%token EQ "=="
%token NEQ "!="
%token DEFINEDOR "//"
%right '|'
%left ','
%right "//"
-%nonassoc '=' SETPIPE SETPLUS SETMINUS SETMULT SETDIV SETDEFINEDOR
+%nonassoc '=' SETPIPE SETPLUS SETMINUS SETMULT SETDIV SETMOD SETDEFINEDOR
%left OR
%left AND
%nonassoc NEQ EQ '<' '>' LESSEQ GREATEREQ
%left '+' '-'
-%left '*' '/'
+%left '*' '/' '%'
%type <blk> Exp Term MkDict MkDictPair ExpD ElseBody QQString FuncDef FuncDefs String
case '-': funcname = "_minus"; break;
case '*': funcname = "_multiply"; break;
case '/': funcname = "_divide"; break;
+ case '%': funcname = "_mod"; break;
case EQ: funcname = "_equal"; break;
case NEQ: funcname = "_notequal"; break;
case '<': funcname = "_less"; break;
$$ = gen_binop($1, $3, '/');
} |
+Exp '%' Exp {
+ $$ = gen_binop($1, $3, '%');
+} |
+
Exp "/=" Exp {
$$ = gen_update($1, $3, '/');
} |
+Exp SETMOD Exp {
+ $$ = gen_update($1, $3, '%');
+} |
+
Exp "==" Exp {
$$ = gen_binop($1, $3, EQ);
} |
null
[2, 8, 10, 14]
+25 % 7
+null
+4
+
+49732 % 472
+null
+172
+
1 + tonumber + ("10" | tonumber)
4
15
{"foo": 42}
{"foo": 43}
-.[] += 2, .[] *= 2, .[] -= 2, .[] /= 2
+.[] += 2, .[] *= 2, .[] -= 2, .[] /= 2, .[] %=2
[1,3,5]
[3,5,7]
[2,6,10]
[-1,1,3]
[0.5, 1.5, 2.5]
+[1,1,1]
+
+[.[] % 7]
+[-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7]
+[0,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,0]
.foo += .foo
{"foo":2}