diff --git a/Makefile b/Makefile index abe1b11a2..282fe47f8 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC = clang CFLAGS = -w -O2 -march=native -mtune=native dev: install-production - time lm --v2 tests/promises/lm-ascript/ascript-integrated.lsts + time lm --v2 tests/promises/string/prefix.lsts gcc tmp.c ./a.out diff --git a/lib2/core/baremetal.lsts b/lib2/core/baremetal.lsts index a87b31ad0..9a17ba9d6 100644 --- a/lib2/core/baremetal.lsts +++ b/lib2/core/baremetal.lsts @@ -28,5 +28,6 @@ import lib2/core/cstring.lsts; import lib2/core/array.lsts; import lib2/core/io.lsts; import lib2/core/cmp.lsts; +import lib2/core/c_int.lsts; let config-v23 = true; diff --git a/lib2/core/c_int.lsts b/lib2/core/c_int.lsts new file mode 100644 index 000000000..d71a7d7f2 --- /dev/null +++ b/lib2/core/c_int.lsts @@ -0,0 +1,21 @@ + +declare-binop( $"!=", raw-type(C<"int">), raw-type(C<"int">), raw-type(Bool), ( l"("; x; l"!="; y; l")"; ) ); +declare-binop( $"==", raw-type(C<"int">), raw-type(C<"int">), raw-type(Bool), ( l"("; x; l"=="; y; l")"; ) ); +declare-binop( $"<", raw-type(C<"int">), raw-type(C<"int">), raw-type(Bool), ( l"("; x; l"<"; y; l")"; ) ); +declare-binop( $"<=", raw-type(C<"int">), raw-type(C<"int">), raw-type(Bool), ( l"("; x; l"<="; y; l")"; ) ); +declare-binop( $">", raw-type(C<"int">), raw-type(C<"int">), raw-type(Bool), ( l"("; x; l">"; y; l")"; ) ); +declare-binop( $">=", raw-type(C<"int">), raw-type(C<"int">), raw-type(Bool), ( l"("; x; l">="; y; l")"; ) ); + +declare-binop( $"&", raw-type(C<"int">), raw-type(C<"int">), raw-type(C<"int">), ( l"("; x; l"&"; y; l")"; ) ); +declare-binop( $"|", raw-type(C<"int">), raw-type(C<"int">), raw-type(C<"int">), ( l"("; x; l"|"; y; l")"; ) ); +declare-binop( $"^", raw-type(C<"int">), raw-type(C<"int">), raw-type(C<"int">), ( l"("; x; l"^"; y; l")"; ) ); +declare-binop( $"+", raw-type(C<"int">), raw-type(C<"int">), raw-type(C<"int">), ( l"("; x; l"+"; y; l")"; ) ); +declare-binop( $"-", raw-type(C<"int">), raw-type(C<"int">), raw-type(C<"int">), ( l"("; x; l"-"; y; l")"; ) ); +declare-binop( $"/", raw-type(C<"int">), raw-type(C<"int">), raw-type(C<"int">), ( l"("; x; l"/"; y; l")"; ) ); +declare-binop( $"%", raw-type(C<"int">), raw-type(C<"int">), raw-type(C<"int">), ( l"("; x; l"%"; y; l")"; ) ); +declare-binop( $"*", raw-type(C<"int">), raw-type(C<"int">), raw-type(C<"int">), ( l"("; x; l"*"; y; l")"; ) ); + +declare-binop( $"<<", raw-type(C<"int">), raw-type(C<"int">), raw-type(C<"int">), ( l"("; x; l"<<"; y; l")"; ) ); +declare-binop( $">>", raw-type(C<"int">), raw-type(C<"int">), raw-type(C<"int">), ( l"("; x; l">>"; y; l")"; ) ); + +declare-unop( $"~", raw-type(C<"int">), raw-type(C<"int">), ( l"((uint64_t)(~"; x; l"))"; ) ); diff --git a/lib2/core/platform-macros.lsts b/lib2/core/platform-macros.lsts index 646a267b1..00f02f87c 100644 --- a/lib2/core/platform-macros.lsts +++ b/lib2/core/platform-macros.lsts @@ -1,4 +1,10 @@ +deprecated macro (rl"declare-zop"(op-alias, r-type, op-expr)) ( + let :Blob op-alias(): r-type = ( + $":expression"(op-expr); + ); +); + deprecated macro (rl"declare-unop"(op-alias, x-type, r-type, op-expr)) ( let :Blob op-alias(x: x-type): r-type = ( $":expression"(op-expr); diff --git a/lib2/core/regex.lsts b/lib2/core/regex.lsts index 16fc02cb5..0fcd4a5d1 100644 --- a/lib2/core/regex.lsts +++ b/lib2/core/regex.lsts @@ -10,22 +10,33 @@ let :Blob .rm_eo(t: C<"regmatch_t">): U64 = ( $":expression"( l"("; $":expression"(t); l".rm_eo)"; ); ); +declare-binop( $"set.rm_so", raw-type(C<"regmatch_t">), raw-type(U64), raw-type(Nil), ( l"("; x; l".rm_so="; y; l")"; ) ); +declare-binop( $"set.rm_eo", raw-type(C<"regmatch_t">), raw-type(U64), raw-type(Nil), ( l"("; x; l".rm_eo="; y; l")"; ) ); + +declare-ternop( $"set.rm_so", raw-type(C<"regmatch_t">[]), raw-type(USize), raw-type(U64), raw-type(Nil), ( l"("; x; l"["; y; l"].rm_so="; z; l")"; ) ); +declare-ternop( $"set.rm_eo", raw-type(C<"regmatch_t">[]), raw-type(USize), raw-type(U64), raw-type(Nil), ( l"("; x; l"["; y; l"].rm_eo="; z; l")"; ) ); + +declare-zop( $"reg_startend", raw-type(C<"int">), l"REG_STARTEND" ); +declare-zop( $"reg_nomatch", raw-type(C<"int">), l"REG_NOMATCH" ); + let .has-prefix(text: String, rgx: Regex): Bool = ( # TODO: fix this mess. The preprocessor is unpacking these erroneously # macros 2.0 doesn't have this problem, but this code still needs to be updated after swapping let a1 = &rgx as C<"regex_t">[]; let a2 = (text.data.data + text.start-offset) as C<"char">[]; let a3 = 0 as C<"size_t">; - let a4 = 0 as C<"regmatch_t">[]; - let a5 = 0 as C<"int">; + let a4 = () : C<"regmatch_t">[1]; + a4[0].rm_so = 0; + a4[0].rm_eo = text.length; + let a5 = reg_startend(); let status = regexec( a1, a2, a3, - a4, + a4 as C<"regmatch_t">[], a5 ); - (status as U64) == 0 + status != reg_nomatch() and a4[0].rm_so==0 ); let $"=="(text: CString, rgx: Regex): Bool = ( @@ -42,16 +53,18 @@ let $"=="(text: CString, rgx: Regex): Bool = ( a4, a5 ); - (status as U64)==0 and matches[0].rm_so==0 and matches[0].rm_eo==text.length + status != reg_nomatch() and matches[0].rm_so==0 and matches[0].rm_eo==text.length ); let .remove-prefix(text: String, rgx: Regex): String? = ( let matches = () : C<"regmatch_t">[1]; + matches[0].rm_so = 0; + matches[0].rm_eo = text.length; let a1 = &rgx as C<"regex_t">[]; let a2 = (text.data.data + text.start-offset) as C<"char">[]; let a3 = 1 as C<"size_t">; let a4 = matches as C<"regmatch_t">[]; - let a5 = 0 as C<"int">; + let a5 = reg_startend(); let status = regexec( a1, a2, @@ -59,18 +72,21 @@ let .remove-prefix(text: String, rgx: Regex): String? = ( a4, a5 ); - if matches[0].rm_so != 0 then (None : String?) + if status == reg_nomatch() then (None : String?) + else if matches[0].rm_so != 0 then (None : String?) else if matches[0].rm_eo == 0 then (None : String?) - else Some(text[matches[0].rm_eo as I64 : ]) + else Some(text[matches[0].rm_eo as I64 : ]); ); let .get-prefix(text: String, rgx: Regex): String? = ( let matches = () : C<"regmatch_t">[1]; + matches[0].rm_so = 0; + matches[0].rm_eo = text.length; let a1 = &rgx as C<"regex_t">[]; let a2 = (text.data.data + text.start-offset) as C<"char">[]; let a3 = 1 as C<"size_t">; let a4 = matches as C<"regmatch_t">[]; - let a5 = 0 as C<"int">; + let a5 = reg_startend(); let status = regexec( a1, a2, @@ -78,7 +94,8 @@ let .get-prefix(text: String, rgx: Regex): String? = ( a4, a5 ); - if matches[0].rm_so != 0 then (None : String?) + if status == reg_nomatch() then (None : String?) + else if matches[0].rm_so != 0 then (None : String?) else if matches[0].rm_eo == 0 then (None : String?) else Some(text[0_i64 : matches[0].rm_eo as I64]) ); diff --git a/tests/promises/string/prefix.lsts b/tests/promises/string/prefix.lsts index 650ebf055..107ed4dcc 100644 --- a/tests/promises/string/prefix.lsts +++ b/tests/promises/string/prefix.lsts @@ -30,3 +30,6 @@ assert( " ".remove-prefix(r/^[a-zA-Z0-9_-]+/) == (None : String?) ); assert( " import "[1_u64:].remove-prefix(r/^[a-zA-Z0-9_-]+/) == Some(" ") ); assert( " "[1_u64:].remove-prefix(r/^[a-zA-Z0-9_-]+/) == (None : String?) ); + +assert( "ORANGEAPPLE"[:6_u64].remove-prefix(r/APPLE/) == (None : String?) ); +assert( not("ORANGEAPPLE"[:6_u64].has-prefix(r/APPLE/)) );