buzz-language / buzz

👨‍🚀 buzz, A small/lightweight statically typed scripting language
https://buzz-lang.dev
MIT License
1.15k stars 31 forks source link

Optimize list and map initialization #245

Closed giann closed 1 month ago

giann commented 6 months ago

Instead of:

000 041 OP_LIST 7 type: 0x101617c00 `[int]`
001 |   OP_CONSTANT     1 10
002 |   OP_LIST_APPEND
003 |   OP_CONSTANT     2 3
004 |   OP_LIST_APPEND
005 |   OP_CONSTANT     3 12
006 |   OP_LIST_APPEND
007 |   OP_CONSTANT     4 0

Do:

000 041 OP_LIST 7 type: 0x101617c00 `[int]`
001 |   OP_CONSTANT     1 10
002 |   OP_CONSTANT     2 3
003 |   OP_CONSTANT     3 12
004 |   OP_CONSTANT     4 0
005 |   OP_LIST_APPEND  4    // There's 4 elements on the stack to append to the list

This is also true with maps.