Skip to content

Commit b1c0bae

Browse files
committed
Implemented append.
1 parent 63b6709 commit b1c0bae

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ Feature | Type | Status | Notes
335335
`>` | function | ✅ |
336336
`>=` | function | ✅ |
337337
`and` | operator | ✅ | For two parameters.
338-
`append` | function | 🚧 |
338+
`append` | function | |
339339
`as-contract` | operator | 🚧 |
340340
`as-max-len?` | operator | 🚧 |
341341
`asserts!` | function | ✅ |

lib/Clar2EVM/compile.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ and compile_expression env = function
262262
end
263263

264264
| ListExpression xs ->
265-
List.rev (List.concat_map (compile_expression env) xs) @ [EVM.from_int (List.length xs)]
265+
List.concat_map (compile_expression env) xs @ [EVM.from_int (List.length xs)]
266266

267267
| Lt (a, b) ->
268268
let a = compile_expression env a in
@@ -386,6 +386,15 @@ and compile_expression env = function
386386
| Keyword "stx-liquid-supply" -> unsupported "stx-liquid-supply"
387387
| Keyword "tx-sender" -> EVM.origin
388388

389+
| FunctionCall ("append", [list; element]) ->
390+
begin match type_of_expression list, type_of_expression element with
391+
| List (n, e1), e2 when e1 = e2 ->
392+
let list = compile_expression env list in
393+
let element = compile_expression env element in
394+
list @ EVM.pop1 @ element @ [EVM.from_int (n + 1)]
395+
| t, e -> unsupported_function2 "append" t e
396+
end
397+
389398
| FunctionCall ("asserts!", [bool_expr; _]) -> (* TODO: thrown_value *)
390399
begin match type_of_expression bool_expr with
391400
| Bool ->

lib/Clar2EVM/typecheck.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ let rec type_of_expression = function
5656
| Keyword "tx-sender" -> Principal
5757
| Keyword id -> unimplemented (Printf.sprintf "type_of_expression for %s" id)
5858

59+
| FunctionCall ("append", [list; element]) ->
60+
begin match type_of_expression list, type_of_expression element with
61+
| List (n, e1), e2 when e1 = e2 -> List (n + 1, e1)
62+
| t, e -> unsupported_function2 "append" t e
63+
end
64+
5965
| FunctionCall ("get", [_; tuple]) -> type_of_expression tuple
6066
| FunctionCall ("hash160", _) -> Buff 20
6167
| FunctionCall ("keccak256", _) -> Buff 32

test/functions.t

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ https://docs.blockstack.org/references/language-functions
6060
6161
append:
6262
63+
$ clarc -t opcode -f only-function=test <<EOF
64+
> (define-read-only (test) (append (list 5 6 7) 8))
65+
> EOF
66+
PUSH1 0x05 PUSH1 0x06 PUSH1 0x07 PUSH1 0x03 POP PUSH1 0x08 PUSH1 0x04
67+
PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x00 RETURN STOP
68+
6369
asserts!:
6470
6571
$ clarc -t opcode -f only-function=test <<EOF
@@ -313,7 +319,7 @@ list:
313319
$ clarc -t opcode -f only-function=test <<EOF
314320
> (define-read-only (test) (list 1 2 3))
315321
> EOF
316-
PUSH1 0x03 PUSH1 0x02 PUSH1 0x01 PUSH1 0x03 PUSH1 0x00 MSTORE PUSH1 0x20
322+
PUSH1 0x01 PUSH1 0x02 PUSH1 0x03 PUSH1 0x03 PUSH1 0x00 MSTORE PUSH1 0x20
317323
PUSH1 0x00 RETURN STOP
318324
319325
map:

0 commit comments

Comments
 (0)