1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

Add namespaces to Fift (#641)

* Add fift-based disassembler

* Fift improvements: namespaces, hashmaps, flow controls

* Fift: add lib with better block structuring and more

* Minor changes in fift HashMap + tests (#643)

* Minor changes in fift HashMap

* Add tests for extended fift

---------

Co-authored-by: OmicronTau <omicron@ton.org>
Co-authored-by: Tolya <1449561+tolya-yanot@users.noreply.github.com>
Co-authored-by: SpyCheese <mikle98@yandex.ru>
This commit is contained in:
EmelyanenkoK 2023-03-10 14:16:29 +03:00 committed by GitHub
parent 4590ed381b
commit 865ebfce8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 2323 additions and 699 deletions

View file

@ -110,3 +110,19 @@ TEST(Fift, test_sort) {
TEST(Fift, test_sort2) {
run_fift("sort2.fif");
}
TEST(Fift, test_hmap) {
run_fift("hmap.fif");
}
TEST(Fift, test_disasm) {
run_fift("disasm.fif");
}
TEST(Fift, test_fiftext) {
run_fift("fift-ext.fif");
}
TEST(Fift, test_namespaces) {
run_fift("namespaces.fif");
}

View file

@ -0,0 +1,70 @@
"Disasm.fif" include
"Asm.fif" include
<{
IF:<{
123456789 PUSHINT
}>ELSE<{
x{12345} PUSHSLICE
}>
WHILE:<{ ADD }>DO<{
10 PUSHINT REPEAT:<{
CONT:<{ NOP }>
CONT:<{ }>
}>
}>
}>s
disasm cr
x{007A7} disasm cr // Cannot disassemble: x{7}
<{
SWAP
s0 s10 XCHG s0 100 s() XCHG
s5 PUSH s6 POP
s4 s10 PUSH2
5 10 BLKSWAP
c4 PUSH c5 POP
}>s dup dup
disasm cr
std-disasm disasm cr
stack-disasm show-vm-code disasm cr
hide-vm-code
<{
1 INT 123456789 INT 123456789123456789123456789 INT
<b x{1234} s, b> PUSHREF
x{567} PUSHSLICE
10 ADDCONST
DIV DIVR DIVC
RSHIFTC 10 RSHIFTC#
20 MODPOW2#
30 MULRSHIFTR#
LSHIFTDIVC 40 LSHIFT#DIVR
}>s
disasm cr
PROGRAM{
11 DECLMETHOD foo1
12 DECLMETHOD foo2
13 DECLMETHOD foo3
DECLPROC main
foo1 PROC:<{
123 ADDCONST
}>
foo2 PROC:<{
OVER
5 EQINT
IFJMP:<{
NIP
}>
MUL
}>
foo3 PROC:<{
ADD
foo2 CALLDICT
}>
main PROC:<{
}>
}END>s
disasm

View file

@ -0,0 +1,107 @@
"FiftExt.fif" include
// if then
{
if{ 0> }then{ "Yes" }
} : run
5 run type cr // Yes
-5 run .s // nothing
// if then else
{
if{ 0> }then{ "Yes" }else{ "No" }
} : run
5 run type ." " // Yes
-5 run type cr // No
// if then elseif else
{
dup dup if{ 20 > }then{ 2drop "AAA" }elseif{ 10 > }then{ drop "BBB" }elseif{ 0> }then{ "CCC" }else{ "DDD" }
} : run
25 run type ." " // AAA
15 run type ." " // BBB
5 run type ." " // CCC
-5 run type cr // DDD
// while do
1
while{ dup 100 < }do{
dup .
2 *
}
cr // 1 2 4 8 16 32 64
drop
// repeat until
1
repeat{
dup .
3 *
}until{ dup 500 > }
cr // 1 3 9 27 81 243
drop
// def
def foo1 { * + }
5 10 20 foo1 . // 205
6 100 100 foo1 . cr // 10006
// defrec
defrec fib {
if{ dup 2 < }then{
drop 1
}else{
dup 1- fib swap 2- fib +
}
}
8 fib . // 34
9 fib . // 55
20 fib . cr // 10946
// [[ ... ]]
def foo2 { [[ ."Exec" cr 5 5 * ]] + }
."Calling foo2: "
100 foo2 . 200 foo2 . cr
// Nested blocks
def sqr { dup * }
def power {
1 -rot
while{ dup 0> }do{
if{ dup 1 and }then{
-rot tuck * swap rot
}
2/
if{ dup 0> }then{
swap sqr swap
}
}
2drop
}
3 0 power . // 1
3 1 power . // 3
3 4 power . // 81
3 12 power . // 531441
3 50 power . // 717897987691852588770249
cr
0 while{ dup 2 < }do{
."A"
0 while{ dup 2 < }do{
."B"
0 while{ dup 2 < }do{
."C"
0 while{ dup 2 < }do{
."D"
0 while{ dup 2 < }do{
."E"
0 while{ dup 2 < }do{
."F"
1+ } drop
1+ } drop
1+ } drop
1+ } drop
1+ } drop
1+ } drop
cr // ABCDEFFEFFDEFFEFFCDEFFEFFDEFFEFFBCDEFFEFFDEFFEFFCDEFFEFFDEFFEFFABCDEFFEFFDEFFEFFCDEFFEFFDEFFEFFBCDEFFEFFDEFFEFFCDEFFEFFDEFFEFF

69
crypto/test/fift/hmap.fif Normal file
View file

@ -0,0 +1,69 @@
hmapnew .s // (null)
// Get from empty hmap
dup 10 swap hmap@ null? not abort"expected null"
dup "abc" swap hmap@ null? not abort"expected null"
dup 10 swap hmap@? abort"expected 0"
dup "abc" swap hmap@? abort"expected 0"
dup hmapempty? not abort"expected -1"
.s cr // (null)
// Insert values
"value-1" 123 rot hmap!+
"value-2" "Abc" rot hmap!+
"value-3" "xyz" rot hmap!+
"value-4" B{78797A} rot hmap!+
"value-5" `xyz rot hmap!+
"abcdefg" null rot hmap!+ // No effect!
dup hmapempty? abort"expected 0"
// Get values
dup 123 swap hmap@ type cr // value-1
dup "Abc" swap hmap@ type cr // value-2
dup "xyz" swap hmap@ type cr // value-3
dup B{78797A} swap hmap@ type cr // value-4
dup `xyz swap hmap@ type cr // value-5
dup 123 swap hmap@? . type cr // -1 value-1
dup "Abc" swap hmap@? . type cr // -1 value-2
dup "xyz" swap hmap@? . type cr // -1 value-3
dup B{78797A} swap hmap@? . type cr // -1 value-4
dup `xyz swap hmap@? . type cr // -1 value-5
// Get null
dup 1234 swap hmap@ null? not abort"expected null"
dup null swap hmap@ null? not abort"expected null"
dup 1234 swap hmap@? abort"expected 0"
dup null swap hmap@? abort"expected 0"
cr
// hmapforeach
constant hmap
hmap { .s drop drop -1 } hmapforeach
.s drop
3 hmap { .s drop drop 1- dup } hmapforeach
.s cr drop drop hmap
// hmap! (insert) and hmap!+ (replace)
"value-11" 123 rot hmap!+
"value-66" 567 rot hmap!+
"value-33" "xyz" rot hmap!
"value-77" "qwe" rot hmap! // No effect!
dup 123 swap hmap@ type cr // value-11
dup 567 swap hmap@ type cr // value-66
dup "xyz" swap hmap@ type cr // value-33
dup "qwe" swap hmap@ null? not abort"null expected"
cr
// Delete
567 swap hmap-
789 swap hmap-
"qwe" swap hmap-? . // -1
"rty" swap hmap-? . // 0
`xyz swap hmap@- type // value-5
`zyx swap hmap@- null? not abort"null expected"
null 123 rot hmap! // same as hmap-
null 321 rot hmap!
null `xyz rot hmap!+
cr
constant hmap
hmap { .s drop drop -1 } hmapforeach
drop

View file

@ -0,0 +1,29 @@
namespace My
"a" constant a
"b" constant b
"c" constant c
a b c .s { drop } 3 times // "a" "b" "c"
My definitions
"b-my" constant b
"c-my" constant c
"d-my" constant d
a b c d .s { drop } 4 times // "a" "b-my" "c-my" "d-my"
Fift definitions
a b c .s { drop } 3 times // "a" "b-my" "c-my" "d-my"
My b My c My d .s { drop } 3 times // "b-my" "c-my" "d-my"
a b c .s { drop } 3 times // "a" "b" "c" "d"
My definitions
a b c d .s { drop } 4 times // "a" "b-my" "c-my" "d-my"
Fift a Fift b Fift c d .s { drop } 4 times // "a" "b" "c" "d-my"
Fift definitions
cr
My-wordlist @
{ drop type -1 } hmapforeach drop cr // "b " "d " "c "