From 07db75bca9ef3cfc9fff889955d8d307ad9a585f Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Mon, 21 Oct 2024 22:52:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E5=AF=B9=E6=AF=94=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E6=94=B9=E4=B8=BA=E7=9C=8B=E8=B5=B7=E6=9D=A5?= =?UTF-8?q?=E6=9B=B4=E7=AE=80=E6=B4=81=E7=9A=84=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/pascals_triangle.mdtlbl | 105 +++++++++++++++++-------------- 1 file changed, 59 insertions(+), 46 deletions(-) diff --git a/examples/pascals_triangle.mdtlbl b/examples/pascals_triangle.mdtlbl index 629cb0b..852c9fd 100644 --- a/examples/pascals_triangle.mdtlbl +++ b/examples/pascals_triangle.mdtlbl @@ -39,30 +39,43 @@ const ForD = (const match @ { } } }); -const MakeMemMethods = (inline@ *Mem { - const Mem.Read = (match @ { - Addr { - read $ .. Addr; - } - Result Addr { - setres Result; - read Result .. Addr; - } - }); - const Mem.Write = (setres ..; match @ { - @ Value { - inline@ Addr { - write Value .. Addr; +const QuickMemory = (match @ { + Mem { + setres Mem; + const $.F = (match @ { + #** + * extend methods + *# + { + const $.Read = ([H:..](match @ { + Result Addr { + setres Result; + read Result H Addr; + } + })); } - } - }); + #** + * quick read + *# + Addr { + read $ .. Addr; + } + #** + * write + *# + @ Value { + inline@ Addr { + write Value .. Addr; + } + } + }); + } }); # codes const TriangleSize = 10; -const CurLine = cell1; -const PrevLine = cell2; -take MakeMemMethods[CurLine PrevLine]; +const CurLine = QuickMemory[cell1]->F; +const PrevLine = QuickMemory[cell2]->F; print"\ 1 @@ -71,20 +84,20 @@ print"\ lastSize = 2; -take PrevLine.Write[0 1 1]; # like `PrevLine[0, 1] = 1` +take PrevLine[0 1 1]; # like `PrevLine[0, 1] = 1` take ForD[3 (%(*TriangleSize+1))->$ const(match @ { I { - take CurLine.Write[0 1]; + take CurLine[0 1]; take ForD[1 lastSize const(match @ { J { - take CurLine.Write[J (*PrevLine.Read[(*J- 1)] + PrevLine.Read[J])]; + take CurLine[J (*PrevLine[(*J- 1)] + PrevLine[J])]; }})]; - take CurLine.Write[lastSize 1]; + take CurLine[lastSize 1]; lastSize++; take ForD[lastSize const(match @ { C { - print CurLine.Read[cur_c C]; - take PrevLine.Write[C cur_c]; + print CurLine[].Read[cur_c C]; + take PrevLine[C cur_c]; if C < (*lastSize- 1) { print " "; } }})]; print "\n"; @@ -97,32 +110,32 @@ print "1\n1 1\n" set lastSize 2 write 1 cell2 0 write 1 cell2 1 -set __26 3 -jump 31 greaterThanEq __26 11 +set __23 3 +jump 31 greaterThanEq __23 11 write 1 cell1 0 -set __43 1 -jump 16 greaterThanEq __43 lastSize -op sub __52 __43 1 -read __51 cell2 __52 -read __54 cell2 __43 -op add __49 __51 __54 -write __49 cell1 __43 -op add __43 __43 1 -jump 9 lessThan __43 lastSize +set __40 1 +jump 16 greaterThanEq __40 lastSize +op sub __49 __40 1 +read __48 cell2 __49 +read __51 cell2 __40 +op add __46 __48 __51 +write __46 cell1 __40 +op add __40 __40 1 +jump 9 lessThan __40 lastSize write 1 cell1 lastSize op add lastSize lastSize 1 -set __74 0 -jump 28 greaterThanEq __74 lastSize -read cur_c cell1 __74 +set __71 0 +jump 28 greaterThanEq __71 lastSize +read cur_c cell1 __71 print cur_c -write cur_c cell2 __74 -op sub __85 lastSize 1 -jump 26 greaterThanEq __74 __85 +write cur_c cell2 __71 +op sub __87 lastSize 1 +jump 26 greaterThanEq __71 __87 print " " -op add __74 __74 1 -jump 20 lessThan __74 lastSize +op add __71 __71 1 +jump 20 lessThan __71 lastSize print "\n" -op add __26 __26 1 -jump 6 lessThan __26 11 +op add __23 __23 1 +jump 6 lessThan __23 11 printflush message1 *#