mirror of
https://github.com/tweag/nickel.git
synced 2024-09-20 08:05:15 +03:00
Add string contracts and primops wrapper in stdlib
This commit is contained in:
parent
176ee43440
commit
d2f6d44238
@ -13,11 +13,12 @@ pub const CONTRACTS: (&str, &str) = (
|
|||||||
);
|
);
|
||||||
pub const LISTS: (&str, &str) = ("<stdlib/lists>", include_str!("../stdlib/lists.ncl"));
|
pub const LISTS: (&str, &str) = ("<stdlib/lists>", include_str!("../stdlib/lists.ncl"));
|
||||||
pub const RECORDS: (&str, &str) = ("<stdlib/records>", include_str!("../stdlib/records.ncl"));
|
pub const RECORDS: (&str, &str) = ("<stdlib/records>", include_str!("../stdlib/records.ncl"));
|
||||||
|
pub const STRINGS: (&str, &str) = ("<stdlib/strings>", include_str!("../stdlib/strings.ncl"));
|
||||||
pub const NUMS: (&str, &str) = ("<stdlib/nums>", include_str!("../stdlib/nums.ncl"));
|
pub const NUMS: (&str, &str) = ("<stdlib/nums>", include_str!("../stdlib/nums.ncl"));
|
||||||
|
|
||||||
/// Return the list `(name, source_code)` of all the stdlib modules.
|
/// Return the list `(name, source_code)` of all the stdlib modules.
|
||||||
pub fn modules() -> Vec<(&'static str, &'static str)> {
|
pub fn modules() -> Vec<(&'static str, &'static str)> {
|
||||||
vec![BUILTINS, CONTRACTS, LISTS, RECORDS, NUMS]
|
vec![BUILTINS, CONTRACTS, LISTS, RECORDS, STRINGS, NUMS]
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Accessors to the builtin contracts.
|
/// Accessors to the builtin contracts.
|
||||||
|
76
stdlib/strings.ncl
Normal file
76
stdlib/strings.ncl
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
{
|
||||||
|
strings =
|
||||||
|
{
|
||||||
|
BoolLiteral = fun s l =>
|
||||||
|
if %isStr% s then
|
||||||
|
if s == "true" || s == "True" then
|
||||||
|
"true"
|
||||||
|
else if s == "false" || s == "False" then
|
||||||
|
"false"
|
||||||
|
else
|
||||||
|
%blame% (%tag% "expected \"true\" or \"false\", got #{s} l)
|
||||||
|
else
|
||||||
|
%blame% (%tag% "expected string" l);
|
||||||
|
|
||||||
|
NumLiteral = fun s l =>
|
||||||
|
if %isStr% s then
|
||||||
|
// use regex here
|
||||||
|
if true then
|
||||||
|
s
|
||||||
|
else
|
||||||
|
%blame% (%tag% "expected \"true\" or \"false\", got #{s} l)
|
||||||
|
else
|
||||||
|
%blame% (%tag% "not a string" l);
|
||||||
|
|
||||||
|
CharLiteral = fun
|
||||||
|
|
||||||
|
Ident = fun s l =>
|
||||||
|
if %isStr% s then
|
||||||
|
//use regex here
|
||||||
|
if true then
|
||||||
|
else
|
||||||
|
%blame% (%tag% "not a string" l);
|
||||||
|
|
||||||
|
NonEmpty = fun s l =>
|
||||||
|
if %isStr% then
|
||||||
|
if %strLength% s != 0 then
|
||||||
|
s
|
||||||
|
else
|
||||||
|
%blame% (%tag% "empty string" l)
|
||||||
|
else
|
||||||
|
%blame% (%tag% "not a string" l);
|
||||||
|
|
||||||
|
join = fun l sep =>
|
||||||
|
if l == [] then
|
||||||
|
""
|
||||||
|
else
|
||||||
|
lists.foldl (fun acc s => acc ++ sep ++ s) (%head% l) (%tail% l);
|
||||||
|
|
||||||
|
split : Str -> Str -> List Str = fun s sep => %split% s sep;
|
||||||
|
|
||||||
|
trim : Str -> Str = fun s => %trim% s;
|
||||||
|
|
||||||
|
chars : Str -> List Str => fun s => %strChars% s;
|
||||||
|
code : #NonEmpty -> Num => fun s => %charCode% s;
|
||||||
|
|
||||||
|
uppercase : Str -> Str = fun s => %uppercase% s;
|
||||||
|
lowercase : Str -> Str = fun s => %lowercase% s;
|
||||||
|
contains: Str -> Str -> Bool = fun s needle => %contains% s needle;
|
||||||
|
replace: Str -> Str -> Str -> Str = fun s pattern replace =>
|
||||||
|
%replace% s pattern replace;
|
||||||
|
|
||||||
|
match : Str -> Str -> { | Dyn } = %blame% "fail";
|
||||||
|
|
||||||
|
length : Str -> Num = fun s => %strLength% s;
|
||||||
|
substring: Str -> Num -> Num -> Str = fun s start end =>
|
||||||
|
%substring% s start end;
|
||||||
|
|
||||||
|
fromNum : Num -> Str = fun n => %strFrom% n;
|
||||||
|
fromEnum : forall a. < | a> -> Str => fun enum => %strFrom% enum;
|
||||||
|
fromBool : Bool -> Str => fun bool => %strFrom% bool;
|
||||||
|
|
||||||
|
toNum | #NumLiteral -> Num = fun s => %numFrom% n;
|
||||||
|
toBool | #BoolLiteral -> Bool = fun s => s == "true";
|
||||||
|
toEnum | #Ident -> Bool = fun s => %enumFrom% s;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user