mirror of
https://github.com/urbit/shrub.git
synced 2024-12-21 18:01:32 +03:00
jets: Add +slaw jet which parses %ud and %tas.
Falls back for other types.
This commit is contained in:
parent
55d2f555e9
commit
55e9bd7056
@ -116,6 +116,8 @@
|
||||
u3_noun u3we_rexp(u3_noun);
|
||||
u3_noun u3we_trip(u3_noun);
|
||||
|
||||
u3_noun u3we_slaw(u3_noun);
|
||||
|
||||
u3_noun u3we_pfix(u3_noun);
|
||||
u3_noun u3we_plug(u3_noun);
|
||||
u3_noun u3we_pose(u3_noun);
|
||||
|
105
pkg/urbit/jets/e/slaw.c
Normal file
105
pkg/urbit/jets/e/slaw.c
Normal file
@ -0,0 +1,105 @@
|
||||
/* j/3/slaw.c
|
||||
**
|
||||
*/
|
||||
#include "all.h"
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
/* functions
|
||||
*/
|
||||
|
||||
u3_noun
|
||||
_parse_ud(u3_noun txt) {
|
||||
c3_c* c = u3r_string(txt);
|
||||
|
||||
// First character must represent a digit
|
||||
c3_c* cur = c;
|
||||
if (cur[0] > '9' || cur[0] < '0') {
|
||||
c3_free(c);
|
||||
return 0;
|
||||
}
|
||||
c3_w total = cur[0] - '0';
|
||||
cur++;
|
||||
|
||||
int since_last_period = 0;
|
||||
while (cur[0] != 0) {
|
||||
since_last_period++;
|
||||
if (cur[0] == '.') {
|
||||
since_last_period = 0;
|
||||
cur++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cur[0] > '9' || cur[0] < '0') {
|
||||
c3_free(c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
total = u3qa_mul(total, 10);
|
||||
total = u3qa_add(total, cur[0] - '0');
|
||||
cur++;
|
||||
|
||||
if (since_last_period > 3) {
|
||||
c3_free(c);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
c3_free(c);
|
||||
return u3nc(0, total);
|
||||
}
|
||||
|
||||
u3_noun
|
||||
_parse_tas(u3_noun txt) {
|
||||
// For any symbol which matches, txt will return itself as a
|
||||
// value. Therefore, this is mostly checking validity.
|
||||
c3_c* c = u3r_string(txt);
|
||||
|
||||
// First character must represent a lowercase letter
|
||||
c3_c* cur = c;
|
||||
if (!islower(cur[0])) {
|
||||
c3_free(c);
|
||||
return 0;
|
||||
}
|
||||
cur++;
|
||||
|
||||
while (cur[0] != 0) {
|
||||
if (!(islower(cur[0]) || isdigit(cur[0]) || cur[0] == '-')) {
|
||||
c3_free(c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
cur++;
|
||||
}
|
||||
|
||||
c3_free(c);
|
||||
return u3nc(0, txt);
|
||||
}
|
||||
|
||||
u3_noun
|
||||
u3we_slaw(u3_noun cor)
|
||||
{
|
||||
u3_noun mod;
|
||||
u3_noun txt;
|
||||
|
||||
if (c3n == u3r_mean(cor, u3x_sam_2, &mod,
|
||||
u3x_sam_3, &txt, 0) ||
|
||||
!_(u3a_is_cat(mod))) {
|
||||
return u3m_bail(c3__fail);
|
||||
}
|
||||
|
||||
switch (mod) {
|
||||
/* TODO: case c3__p. Need background jets first. */
|
||||
|
||||
case c3__ud:
|
||||
return _parse_ud(txt);
|
||||
|
||||
// %ta is used once in link.hoon. don't bother.
|
||||
|
||||
case c3__tas:
|
||||
return _parse_tas(txt);
|
||||
|
||||
default:
|
||||
return u3_none;
|
||||
}
|
||||
}
|
@ -607,6 +607,9 @@ static c3_c* _141_qua_trip_ha[] = {
|
||||
0
|
||||
};
|
||||
|
||||
static u3j_harm _141_qua_slaw_a[] = {{".2", u3we_slaw}, {}};
|
||||
|
||||
|
||||
static u3j_harm _141_qua__po_ind_a[] = {{".2", u3wcp_ind}, {}};
|
||||
static c3_c* _141_qua__po_ind_ha[] = {
|
||||
"95bbe9867dbbd1b9ce12671d64cf7b1dee8d987c6770955a83c73291c4537a61",
|
||||
@ -883,6 +886,8 @@ static u3j_core _141_qua_d[] =
|
||||
|
||||
{ "mink", 7, _141_qua_mink_a, 0, _141_qua_mink_ha },
|
||||
{ "mule", 7, _141_qua_mule_a, 0, _141_qua_mule_ha },
|
||||
|
||||
{ "slaw", 7, _141_qua_slaw_a, 0, 0 },
|
||||
{}
|
||||
};
|
||||
static c3_c* _141_qua_ha[] = {
|
||||
|
Loading…
Reference in New Issue
Block a user