Ticket #1190 (new RFC)

Opened 5 years ago

Last modified 3 years ago

Replace .globalconst/.const directives by .const/.localconst (respectively)

Reported by: kjs Owned by:
Priority: normal Milestone:
Component: core Version: trunk
Severity: medium Keywords: PIR, .globalconst, .localconst, .const
Cc: jkeenan Language:
Patch status: Platform:


This ticket replaces RT57634.


in PIR you can use the .globalconst directive in a sub to define a constant that is globally accessible. Likewise, you can use the .const directive in a sub that is local to that sub.

.sub foo .globalconst int answer = 42 .const num PI = 3.14


answer in this case is globally accessible (in any other sub, that is parsed AFTER the foo subroutine, I should note) PI in this case is only accessible in this subroutine foo.

However, I question the need for .globalconst, as the .const directive can also be used /outside/ of a subroutine, like so:

.const int answer = 42

Therefore, the .globalconst directive seems to be superfluous; why have 2 directives that do the same thing; if a .globalconst is accessible globally anyway, there's no need to define it WITHIN a sub.

Therefore, my proposal is to remove the .globalconst directive; whenever you need to have a global const, use .const outside of a subroutine. whenever you need to have a local const (in a sub), use .const inside a subroutine.

comments welcome, kjs

Change History

Changed 5 years ago by kjs


--Andrew Whitworth

Changed 5 years ago by kjs

If we're going to remove one, let's deprecate .const outside of sub blocks, and make .globalconst mean a global constant everywhere. Consistency is a good thing, but so are clear distinctions between similar-but-different things.


Changed 5 years ago by kjs

I thought a bit more on this, and global .consts are more common than sub-local consts (I think). Following the Huffman principle, I think it would be nicer then to use '.localconst' for a local const, only available in a sub, and .const for global constants. (and thus removing .globalconst).

(I'm not sure where sub-local constants are stored; are they stored in the constants table of a PBC file? In that case the only thing preventing code from using a local constant in a global context would be the pir compiler.)

comments welcome, kjs

Changed 5 years ago by kjs



Changed 3 years ago by jkeenan

  • cc jkeenan added
  • component changed from none to core

I don't see any location where globalconst is implemented.

$ ack  globalconst *
78:=head3 .globalconst
80:X<.globalconst directive>
82:  .globalconst R<TYPE>R<NAME> = R<VALUE>

293:=item .globalconst <type> <identifier> = <const>
296:C<.globalconst> may only be used within a C<.sub>.

27:my @imcc_spdirec = qw(.call .result .return .local .const .globalconst

230:    ".endnamespace" ".eom" ".get_results" ".global" ".globalconst"

66:    .globalconst int KEY_DOWN = 258
67:    .globalconst int KEY_UP   = 259
68:    .globalconst int KEY_LEFT = 260
69:    .globalconst int KEY_RIGHT= 261
70:    .globalconst int KEY_HOME = 262

45:    .globalconst int MODE = 0o666
46:    .globalconst string FILE = '0'
47:    .globalconst string MAGIC = 'ustar'
48:    .globalconst string TAR_VERSION = '00'
49:    .globalconst int BLOCK = 512

36:    .globalconst int AZ_OK = 0
37:    .globalconst int AZ_STREAM_END = 1
38:    .globalconst int AZ_ERROR = 2
39:    .globalconst int AZ_FORMAT_ERROR = 3
40:    .globalconst int AZ_IO_ERROR = 4
41:    .globalconst int FA_UNIX = 3
42:    .globalconst int GPBF_HAS_DATA_DESCRIPTOR_MASK = 8
43:    .globalconst int COMPRESSION_STORED = 0
44:    .globalconst int COMPRESSION_DEFLATED = 8
45:    .globalconst int SIGNATURE_LENGTH = 4
46:    .globalconst int LOCAL_FILE_HEADER_LENGTH = 26
47:    .globalconst int DATA_DESCRIPTOR_LENGTH = 12
48:    .globalconst int CENTRAL_DIRECTORY_FILE_HEADER_LENGTH = 42

93:    .globalconst int RC_OK = 200
94:    .globalconst int RC_BAD_REQUEST = 400
95:    .globalconst int RC_NOT_FOUND = 404
96:    .globalconst int RC_INTERNAL_SERVER_ERROR = 500

33:    .globalconst int RC_MOVED_PERMANENTLY = 301
34:    .globalconst int RC_FOUND = 302
35:    .globalconst int RC_SEE_OTHER = 303
36:    .globalconst int RC_TEMPORARY_REDIRECT = 307
37:    .globalconst int RC_UNAUTHORIZED = 401
38:    .globalconst int RC_PROXY_AUTHENTICATION_REQUIRED = 407
39:    .globalconst int RC_NOT_IMPLEMENTED = 501

13:pir_output_is( <<'CODE', <<'OUT', "globalconst 1" );
16:    .globalconst int N = 5
32:pir_output_is( <<'CODE', <<'OUT', "globalconst 2" );
34:    .globalconst int N = 5
52:pir_output_is( <<'CODE', <<'OUT', "globalconst 3" );
59:    .globalconst int N = 5

12:     .globalconst string ok1 = "ok\n"

Am I missing something?


Changed 3 years ago by ligne

ack doesn't seem to know about lex files.

[chemnitz@10:12 ~/parrot]$ aack  globalconst
342:".globalconst"                return GLOBAL_CONST;

the GLOBAL_CONST token is in turn used in the following places:

[chemnitz@10:15 ~/parrot]$ aack  GLOBAL_CONST
342:".globalconst"                return GLOBAL_CONST;

1045:%token <t> GLOBAL_CONST
1911:   | GLOBAL_CONST { imcc->is_def = 1; } type IDENTIFIER '=' const

3027:return GLOBAL_CONST;

81:     GLOBAL_CONST = 287,
198:#define GLOBAL_CONST 287

1164:     GLOBAL_CONST = 287,
1281:#define GLOBAL_CONST 287

Changed 3 years ago by ligne

uh, aack being an alias i've got set up for "ack -a"...

Note: See TracTickets for help on using tickets.