Skip to content

Commit

Permalink
将对比中的示例改为看起来更简洁的写法
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Oct 21, 2024
1 parent dc1773d commit 07db75b
Showing 1 changed file with 59 additions and 46 deletions.
105 changes: 59 additions & 46 deletions examples/pascals_triangle.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Expand All @@ -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
*#

0 comments on commit 07db75b

Please sign in to comment.