erde-lang / erde

A programming language that compiles to Lua.
https://erde-lang.github.io
MIT License
39 stars 4 forks source link

Destructuring to globals compiles incorrectly #14

Closed tp86 closed 1 year ago

tp86 commented 1 year ago

I couldn't figure out how to make destructuring work in REPL:

$ erde
Erde 0.5-1 on Lua 5.4 -- Copyright (C) 2021-2023 bsuth
> t = { a = 1 }
> t.a -- sanity check
1
> { a } = t -- this doesn't work and it's fine
stdin:1: unexpected token '{'
> global { a } = t -- maybe explicitly mark as global?
> a -- nope
nil
> module { a } = t -- module doesn't work either
> a
nil
> local { a } = t -- of course, this won't work in REPL
> a
nil
> do { local {a} = t print(a) } -- this will work, but is useless
1
> 

Looking at the playground I noticed that destructuring with global keyword doesn't seem to be compiled correctly:

global { a } = t

compiles to

__ERDE_TMP_2__
=
t
local a = __ERDE_TMP_2__.a
-- Compiled with Erde 0.5-1
-- __ERDE_COMPILED__

I want a to be global variable, not some temporary variable. Would be much better to compile to something like

a  -- declare global (local, module) variable, but it's really unnecessary for globals
do
  local __ERDE_TMP_2__ = t
  a = __ERDE_TMP_2__.a
end
bsuth commented 1 year ago

Nice catch, looks like the compilation for global destructuring was completely wrong. Should be fixed by https://github.com/erde-lang/erde/commit/7f499b6171791a1e3d48a07e0c85c5bd2a787b97 but lmk if there are still any problems

tp86 commented 1 year ago

Thanks for such a quick fix!