Page 5 of 6 FirstFirst ... 3 4 5 6 LastLast
Results 41 to 50 of 58

Thread: Map literals in ASH (potential feature)

  1. #41
    Developer Veracity's Avatar
    Join Date
    Mar 2006
    Location
    The Unseelie Court
    Posts
    10,944

    Default

    Actually needs two little patches:

    - One when you have just defined an aggregate
    - One when you have a multiple layer map and are parsing an aggregate literal.

    With revision 17658:

    Code:
    record rec1 { int a; string b; };
    
    rec1 [int,int] rmap {4: {4: new rec1( 10, "abc" ) }, 10: {40: new rec1( 100, "xyz" ) } };
    
    foreach key1, key2, val in rmap {
        print( "[ " + key1 + ", " + key2 + " ] -> ( " + val.a + ", " + val.b + " )" );
    }
    yields

    Code:
    > test-map2
    
    [ 4, 4 ] -> ( 10, abc )
    [ 10, 40 ] -> ( 100, xyz )
    I like it.
    Ph'nglui mglw'nafh Cthulhu
    R'lyeh wgah-nagl fhtagn.

  2. #42
    Developer Veracity's Avatar
    Join Date
    Mar 2006
    Location
    The Unseelie Court
    Posts
    10,944

    Default

    For Reference, I tried something I imagined would work, but didn't. I have this code:

    Code:
    // Far Future rewards
    static item RIKER = $item[ Riker's Search History ];		// combat item: 900-1000 sleaze damage
    static item KARDASHIAN = $item[ Shot of Kardashian Gin ];	// EPIC 1 drunk booze, +3 PVP fights
    static item POINTY_EARS = $item[ Unstable Pointy Ears ];	// accessory disappears at rollover, +3 stats
    static item MEMORY_DISK = $item[ Memory Disk, Alpha ];		// usable, allows access to The Far Future
    static item EARL_GREY = $item[ Tea, Earl Grey, Hot ];		// EPIC 1 full food, 1000 MP and 1000 HP
    
    // Keyword for Exandora's FarFuture script to replicate each item
    static string [item] FAR_FUTURE_OPTION;
    static
    {
        FAR_FUTURE_OPTION[ RIKER ] = "riker";
        FAR_FUTURE_OPTION[ KARDASHIAN ] = "booze";
        FAR_FUTURE_OPTION[ POINTY_EARS ] = "ears";
        FAR_FUTURE_OPTION[ MEMORY_DISK ] = "memory";
        FAR_FUTURE_OPTION[ EARL_GREY ] = "food";
    }
    I tried to initialize using a map literal: { RIKER: "riker", ...} but it didn't work.
    That's because although RIKER is (correctly) of type "item", it is not a constant. It is a Variable Reference. And, unfortunately, since static initialization happens at run time, not compile time, we can't even evaluate it at compile time.

    The map literal however does happen at compile time. If we generated code to initialize the map at runtime, we'd presumably evaluate the lhs and rhs.

    That is not how this works, though, so we should presumably require them to actually be constants and disallow variable references.
    Ph'nglui mglw'nafh Cthulhu
    R'lyeh wgah-nagl fhtagn.

  3. #43
    Developer
    Join Date
    Aug 2009
    Posts
    2,625

    Default

    It doesn't work even if you remove static, if I'm not mistaken.

    I agree that the two reasonable approaches are either to make the literal resolve during runtime, or to require keys to be const. I personally would prefer the former because it's less surprising to me, especially since values resolve as expected.

  4. #44
    Developer
    Join Date
    Aug 2009
    Posts
    2,625

    Default

    Looking back at r17654 -- we clearly evaluate the value.
    Code:
     entry.setValue( entry.getValue().execute( interpreter ) );
    I wonder if this would be as simple as just evaluating the key, as well.

  5. #45
    Developer
    Join Date
    Aug 2009
    Posts
    2,625

    Default

    Apparently setKey doesn't exist, so you'd have to construct a whole separate map. I guess you could cache that with an "isEvaluated" boolean private property.

  6. #46
    Developer
    Join Date
    Aug 2009
    Posts
    2,625

    Default

    On the other hand, if you wanted to require keys to be const, you could just throw an exception in Parser.parseAggregateLiteral if the lhs is a VariableReference.

  7. #47
    Developer Veracity's Avatar
    Join Date
    Mar 2006
    Location
    The Unseelie Court
    Posts
    10,944

    Default

    Test program:

    Code:
    // Far Future rewards
    static item RIKER = $item[ Riker's Search History ];		// combat item: 900-1000 sleaze damage
    static item KARDASHIAN = $item[ Shot of Kardashian Gin ];	// EPIC 1 drunk booze, +3 PVP fights
    static item POINTY_EARS = $item[ Unstable Pointy Ears ];	// accessory disappears at rollover, +3 stats
    static item MEMORY_DISK = $item[ Memory Disk, Alpha ];		// usable, allows access to The Far Future
    static item EARL_GREY = $item[ Tea, Earl Grey, Hot ];		// EPIC 1 full food, 1000 MP and 1000 HP
    
    // Keyword to ask Exandora's FarFuture script to replicate each item
    static string [item] FAR_FUTURE_OPTION;
    static
    {
        FAR_FUTURE_OPTION[ RIKER ] = "riker";
        FAR_FUTURE_OPTION[ KARDASHIAN ] = "booze";
        FAR_FUTURE_OPTION[ POINTY_EARS ] = "ears";
        FAR_FUTURE_OPTION[ MEMORY_DISK ] = "memory";
        FAR_FUTURE_OPTION[ EARL_GREY ] = "food";
    }
    
    foreach key, value in FAR_FUTURE_OPTION {
        print( key + " (" + key.to_int() + ") -> " + value );
    }
    yields:

    Code:
    Riker's Search History (9118) -> riker
    Shot of Kardashian Gin (9119) -> booze
    Unstable Pointy Ears (9120) -> ears
    Memory Disk, Alpha (9121) -> memory
    Tea, Earl Grey, Hot (9122) -> food
    With literals, test program:

    Code:
    // Far Future rewards
    static item RIKER = $item[ Riker's Search History ];		// combat item: 900-1000 sleaze damage
    static item KARDASHIAN = $item[ Shot of Kardashian Gin ];	// EPIC 1 drunk booze, +3 PVP fights
    static item POINTY_EARS = $item[ Unstable Pointy Ears ];	// accessory disappears at rollover, +3 stats
    static item MEMORY_DISK = $item[ Memory Disk, Alpha ];		// usable, allows access to The Far Future
    static item EARL_GREY = $item[ Tea, Earl Grey, Hot ];		// EPIC 1 full food, 1000 MP and 1000 HP
    
    // Keyword to ask Exandora's FarFuture script to replicate each item
    static string [item] FAR_FUTURE_OPTION {
         RIKER : "riker",
         KARDASHIAN : "booze",
         POINTY_EARS : "ears",
         MEMORY_DISK : "memory",
         EARL_GREY : "food"
    };
    
    foreach key, value in FAR_FUTURE_OPTION {
        print( key + " (" + key.to_int() + ") -> " + value );
    }
    yields:

    Code:
    EARL_GREY (0) -> food
    KARDASHIAN (0) -> booze
    MEMORY_DISK (0) -> memory
    POINTY_EARS (0) -> ears
    RIKER (0) -> riker
    We cannot evaluate the key at compile time since, although it looks like a constant, it doesn't actually get initialized until run time.

    I'll make the aggregate literal into something that evaluates at runtime. How hard can it be?
    Ph'nglui mglw'nafh Cthulhu
    R'lyeh wgah-nagl fhtagn.

  8. #48
    Developer Veracity's Avatar
    Join Date
    Mar 2006
    Location
    The Unseelie Court
    Posts
    10,944

    Default

    With revision 17878,

    Code:
    // Far Future rewards
    static item RIKER = $item[ Riker's Search History ];		// combat item: 900-1000 sleaze damage
    static item KARDASHIAN = $item[ Shot of Kardashian Gin ];	// EPIC 1 drunk booze, +3 PVP fights
    static item POINTY_EARS = $item[ Unstable Pointy Ears ];	// accessory disappears at rollover, +3 stats
    static item MEMORY_DISK = $item[ Memory Disk, Alpha ];		// usable, allows access to The Far Future
    static item EARL_GREY = $item[ Tea, Earl Grey, Hot ];		// EPIC 1 full food, 1000 MP and 1000 HP
    
    // Keyword to ask Exandora's FarFuture script to replicate each item
    static string [item] FAR_FUTURE_OPTION {
         RIKER : "riker",
         KARDASHIAN : "booze",
         POINTY_EARS : "ears",
         MEMORY_DISK : "memory",
         EARL_GREY : "food"
    };
    
    foreach key, value in FAR_FUTURE_OPTION {
        print( key + " (" + key.to_int() + ") -> " + value );
    }
    yields:

    Code:
    Riker's Search History (9118) -> riker
    Shot of Kardashian Gin (9119) -> booze
    Unstable Pointy Ears (9120) -> ears
    Memory Disk, Alpha (9121) -> memory
    Tea, Earl Grey, Hot (9122) -> food
    Ph'nglui mglw'nafh Cthulhu
    R'lyeh wgah-nagl fhtagn.

  9. #49
    Developer
    Join Date
    Aug 2009
    Posts
    2,625

    Default

    Certainly a different approach than I would have taken, but hey, works just as well (and probably a bit more efficient, too).

  10. #50
    Developer
    Join Date
    Aug 2009
    Posts
    2,625

    Default

    We probably no longer need the MapValue.execute override, correct?

Similar Threads

  1. flyeredML - potential rounding problem
    By Magus_Prime in forum Scripting Discussion
    Replies: 40
    Last Post: 02-17-2015, 12:38 PM
  2. Map literals
    By shademaster00 in forum Scripting Discussion
    Replies: 4
    Last Post: 02-12-2012, 04:43 PM
  3. 10018: Tweak ScriptMRUList for potential reuse.
    By RSS Bot in forum Latest SVN Changes
    Replies: 0
    Last Post: 11-21-2011, 04:40 PM
  4. set_auto_attack() Potential bug?
    By Banana Lord in forum Community Support
    Replies: 4
    Last Post: 06-14-2011, 05:51 AM
  5. Feature Potential Earnings (actual)
    By NardoLoopa in forum Bug Reports
    Replies: 9
    Last Post: 09-07-2010, 07:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •