Wiki

Ticket #38 (closed enhancement: fixed)

Opened 16 years ago

Last modified 16 years ago

Support multiple arg assignment

Reported by: hopscc Owned by: Chuck
Priority: major Milestone:
Component: Cobra Compiler Version: 0.8.0
Keywords: Cc:

Description

(also called tuple unpacking and arg unbinding)

Allow assignment to multiple args from a list (enumeration) in a single statement.
e.g

 a,b = fnReturns2Args()
 x,y = 1,2
 for x,y in ..whatever_list_array_string
 for key, value in dict ...

Attachments

multi-arg-assign.patch Download (12.7 KB) - added by hopscc 16 years ago.
multi-arg-for.patch Download (16.0 KB) - added by hopscc 16 years ago.
multiArg-assign-cleanup.patch Download (16.8 KB) - added by hopscc 16 years ago.
multiArg-comma-sep.patch Download (11.4 KB) - added by hopscc 16 years ago.

Change History

Changed 16 years ago by hopscc

  • status changed from new to assigned
  • owner set to hopscc

working on this...
Got the assignment bit working.

Changed 16 years ago by hopscc

Changed 16 years ago by hopscc

  • owner changed from hopscc to Chuck

Stage1: multiple variable assignment
code, tests and Note.

key,val for dicts in 'for' loop and
multi variable in 'for' on Enumeration yet to come

Changed 16 years ago by Chuck

  • owner changed from Chuck to hopscc

The above patch is applied. I made minor tweaks--nothing that should cause a conflict, but please updated soon.

Feedback in no particular order, but lettered for convenience:

(a) At some point "a, b = .stuff" should work where .stuff returns an IEnumerable instead of an IList.

(b) In the implementation, the parser is essentially doing the code generation by constructing a tree of AST nodes. What we need instead is a high level AST node called MultiTargetAssignStmt? that closely represents the source and then does that extra AST node creation in its _bindImp (after checking for various errors). ASTs have other potential purposes down the road such us:

  • syntax highlight code
  • check style against rules
  • look for code duplication
  • any kind of code analysis
  • code weaving, or AOP
  • people's pet research projects, which could be anything

In fact, they already support a .toCobraSource which will just come out wrong because of the parser creating a TryStmt? instead.

In general: The parser should be producing ASTs that closely match the source and then letting the ASTs do the error checking and code gen.

Sorry I never explained that before.

(c) I'm not sure why .idExpression[2] is needed. Python allows, for example:
t[0], t[1] = 3, 4

I get the impression just from reading the parsing code, that this won't work in Cobra. I suggest just taking a comma sep list of expressions and then checking them for assignability in the _bindImp of the MultiTargetAssignStmt? class.

(d) We'll still need "a, b = c, d" without brackets at some point.

Changed 16 years ago by hopscc

Changed 16 years ago by hopscc

  • owner changed from hopscc to Chuck

heres patch for stage2 and stage3, multiargs in for-statement for both (generic) dicts and lists.(plus tests and relnotes).
multiargs from non generic dict (hashtable) doesnt work pending fixup for enumerator type inference for hash tables.

Theres also a mod to multi-arg assign code to allow {Index,Argument} exception errors when target list longer than assign-from list (this was just removal of some previous exception trap code) and consequent mods to test files.

I'll probably work on redoing multiarg assign to do its rewrite in _bindImp and other cleanup as discussed next ( this will be internal code change only - no added functionality)
and then look at supporting a,b = b,a form without brackets

Changed 16 years ago by Chuck

Done in changeset:1704 and changeset:1705

Note that I consolidated the test cases under Tests/320-misc-two/810-multi-assignment

This keeps them all in one related place and puts them after the basic generics test cases. This "topical directory approach", which includes the error checking, is what I'm leaning towards these days, but it's not obvious from the existing test cases which are up to ~3 years old.

Regarding "a, b = c, d", note that you may get insights on how to process this by studying the Python grammar which you can search for and read online.

There was the usual code tweaking, so please update.

Also, please no more "top comments":

# blah blah
def foo
    pass

"def foo" should be the first thing about a method. Then args, return type, doc string, comments, etc.

Changed 16 years ago by hopscc

  • owner changed from Chuck to hopscc

reworking multiarg assign statement implementation.

Changed 16 years ago by hopscc

Changed 16 years ago by hopscc

rework multiarg assignment code so handling done via new multiTargetAssignment statement. Redo parsing to be simpler,cleaner and smaller.
More error checking and additional tests.
Now support multiarg assignment to indexers

Changed 16 years ago by Chuck

Applied in changeset:1716 with some minor tweaks.

Also, test files are all under Tests\320-misc-two\810-multi-assignment and have somewhat different names or numbers.

Changed 16 years ago by hopscc

Good stuff.
The patch had a testfile 120-multi-assign-fail.cobra that tested some
non lvalue assignments generated compilation error messages - was that dropped intentionally?

New patch for a,b = c,d multiarg assign form on its way soon as I get my tree cleaned up and patch generated.

Changed 16 years ago by hopscc

Changed 16 years ago by hopscc

  • owner changed from hopscc to Chuck

heres the patch for the multiArg assign from comma sep list of expressions.
2 more test files and relNotes.
Think I got all your tweaks fm the last patch ( more collisions that I'd expected)
but you may want to recheck them...

Changed 16 years ago by Chuck

  • status changed from assigned to accepted

You should see the tests you mentioned in:

Tests\320-misc-two\810-multi-assignment\122-multi-assign-error.cobra

I'm applying the latest patch now.

Changed 16 years ago by Chuck

  • status changed from accepted to assigned

Latest patch applied in changeset:1728

Thanks.

Is this a done deal or is there more to come?

Changed 16 years ago by Chuck

  • status changed from assigned to closed
  • resolution set to fixed

Per an email from hops, this is done.

Note: See TracTickets for help on using tickets.