dictu-lang / Dictu

Dictu is a high-level dynamically typed, multi-paradigm, interpreted programming language.
https://dictu-lang.com
MIT License
267 stars 53 forks source link

[BUG] Unexpected behaviour with POP_REPL and functions called from the REPL #529

Closed RevengerWizard closed 2 years ago

RevengerWizard commented 2 years ago

Is there an existing issue for this?

Current Behavior

Currently, when entering the repl of Dictu, expressions are directly printed as expected on the console (repl), which is also way more easier than always having to call 'print' every time. However, this behaviour also works with every expression inside functions and functions inside modules, revealing and printing the contents inside.

Expected Behavior

The repl should pop only expressions inputted on the console of the repl, leaving up the contents of outside functions or modules. Kind of like in Python and Lua.

Steps To Reproduce

No response

Anything else?

First time reporting issues here! I'm following this project for quite a bit of time and by trying to implement this option in my small Lox inspired language I got the same problem, requiring to find another solution for directly printing expressions to the repl.

Jason2605 commented 2 years ago

This problem actually is something that isn't too tricky to solve (at least in Dictu, but assuming since you're following on from Lox it should be relatively straight forward for you also!) - when we decide whether the opcode is OP_POP or OP_POP_REPL we can just add another check to ensure we are at the top level within our compiler!

E.g here: https://github.com/dictu-lang/Dictu/blob/develop/src/vm/compiler.c#L1938

Becomes

if (compiler->parser->vm->repl && t != TOKEN_EQUAL && compiler->type == TYPE_TOP_LEVEL) {

Hope your project goes well, and I hope Dictu has been of some use for you!