-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #937 from andrew-johnson-4/smart-string-1
Smart string 1
- Loading branch information
Showing
10 changed files
with
19,878 additions
and
19,576 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
|
||
dev: install-production | ||
lm EXAMPLES/control-flow.lsts | ||
lm tests/regress/smart-string.lm | ||
cc tmp.c | ||
./a.out | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
|
||
type SmartString (SmartString( data:U8[] , start:U8[] , end:U8[] )); | ||
|
||
intern := λ(: s String). (: ( | ||
(SmartString( (as s U8[]) (as s U8[]) (+( (as s U8[]) (.length s) )) )) | ||
) SmartString); | ||
|
||
intern := λ(: s SmartString). (: ( | ||
s | ||
) SmartString); | ||
|
||
untern := λ(: s SmartString). (: ( | ||
(let r SNil) | ||
(let s-start (.start s)) | ||
(while (<( s-start (.end s) )) ( | ||
(set r (+( | ||
r (SAtom(clone-rope([]( s-start 0_u64 )))) | ||
))) | ||
(set s-start (+( s-start 1_u64 ))) | ||
)) | ||
(clone-rope r) | ||
) String); | ||
|
||
.length := λ(: x SmartString). (: ( | ||
(-( (.end x) (.start x) )) | ||
) U64); | ||
|
||
== := λ(: x SmartString)(: y String). (: ( | ||
(==( x (intern y) )) | ||
) U64); | ||
|
||
== := λ(: x SmartString)(: y SmartString). (: ( | ||
(let r 0_u64) | ||
(if (is( x y )) (set r 1_u64) ( | ||
(let x-start (.start x)) | ||
(let y-start (.start y)) | ||
(set r 1_u64) | ||
(while (&&( | ||
(<( x-start (.end x) )) | ||
(<( y-start (.end y) )) | ||
)) ( | ||
(if (==( ([]( x-start 0_u64 )) ([]( y-start 0_u64 )) )) ( | ||
(set x-start (+( x-start 1_u64 ))) | ||
(set y-start (+( y-start 1_u64 ))) | ||
) ( | ||
(set r 0_u64) | ||
(set x-start (.end x)) | ||
(set y-start (.end y)) | ||
)) | ||
)) | ||
(if (||( | ||
(<( x-start (.end x) )) | ||
(<( y-start (.end y) )) | ||
)) (set r 0_u64) ()) | ||
)) | ||
r | ||
) U64); | ||
|
||
print := λ(: x SmartString). (: ( | ||
(let start (.start x)) | ||
(while (!=( start (.end x) )) ( | ||
(putchar(as ([]( start 0_u64 )) U32)) | ||
(set start (+( start 1_u64 ))) | ||
)) | ||
) Nil); | ||
|
||
[:] := λ(: x SmartString)(: low I64)(: hi I64). (: ( | ||
(if (<( low 0_i64 )) ( | ||
(set low (+( (as (.length x) I64) low ))) | ||
) ()) | ||
(if (<( hi 0_i64 )) ( | ||
(set hi (+( (as (.length x) I64) hi ))) | ||
) ()) | ||
(let lowp (+( (.start x) low ))) | ||
(let hip (+( (.start x) hi ))) | ||
(if (<( hip lowp )) ( | ||
(fail 'Index\sOut\sOf\sBounds:\sSmartString.[:]_s) | ||
) ()) | ||
(if (<( lowp (.start x) )) ( | ||
(fail 'Index\sOut\sOf\sBounds:\sSmartString.[:]_s) | ||
) ()) | ||
(if (>( hip (.end x) )) ( | ||
(fail 'Index\sOut\sOf\sBounds:\sSmartString.[:]_s) | ||
) ()) | ||
(SmartString( (.data x) lowp hip )) | ||
) SmartString); | ||
|
||
tail-string := λ(: x SmartString). (: ( | ||
([:]( x 1_i64 (as (.length x) I64) )) | ||
) SmartString); | ||
|
||
[] := λ(: x SmartString)(: low I64). (: ( | ||
(if (<( low 0_i64 )) ( | ||
(set low (+( (as (.length x) I64) low ))) | ||
) ()) | ||
(let lowp (+( (.start x) low ))) | ||
(if (<( lowp (.start x) )) ( | ||
(fail 'Index\sOut\sOf\sBounds:\sSmartString.[]_s) | ||
) ()) | ||
(if (>=( lowp (.end x) )) ( | ||
(fail 'Index\sOut\sOf\sBounds:\sSmartString.[]_s) | ||
) ()) | ||
([]( lowp 0_u64 )) | ||
) U8); | ||
|
||
head-string := λ(: x SmartString). (: ( | ||
([]( x 0_i64 )) | ||
) U8); | ||
|
||
has-suffix := λ(: base SmartString)(: sfx SmartString). (: ( | ||
(==( | ||
([:]( base (as (-( (.length base) (.length sfx) )) I64) (as (.length base) I64) )) | ||
sfx )) | ||
) U64); | ||
|
||
remove-suffix := λ(: base SmartString)(: sfx SmartString). (: ( | ||
([:]( base 0_i64 (as (-( (.length base) (.length sfx) )) I64) )) | ||
) SmartString); | ||
|
||
has-prefix := λ(: base SmartString)(: pfx SmartString). (: ( | ||
(==( | ||
([:]( base 0_i64 (as (.length pfx) I64) )) | ||
pfx )) | ||
) U64); | ||
|
||
remove-prefix := λ(: base SmartString)(: sfx SmartString). (: ( | ||
([:]( base (as (.length sfx) I64) (as (.length base) I64) )) | ||
) SmartString); | ||
|
||
.replace := λ(: base SmartString)(: pat SmartString)(: n SmartString). (: ( | ||
(let r SNil) | ||
(while (>( (.length base) 0_u64 )) ( | ||
(if (has-prefix( base pat )) ( | ||
(set base (remove-prefix( base pat ))) | ||
(set r (+( r (SAtom(untern n)) ))) | ||
) ( | ||
(set r (+( r (SAtom(clone-rope(head-string base))) ))) | ||
(set base (tail-string base)) | ||
)) | ||
)) | ||
(clone-rope r) | ||
) String); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
import LIB/default.lm; | ||
|
||
main := λ. (: ( | ||
(print (intern 'ABC_s)) | ||
(print ([:]( (intern 'ABC_s) 1_i64 -1_i64 ))) | ||
) Nil); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ABCB |