Ticket #1190 (new RFC)

Opened 12 years ago

Last modified 11 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:

Description

This ticket replaces RT57634.

hi,

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

.end

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 12 years ago by kjs

+1

--Andrew Whitworth

Changed 12 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.

Allison

Changed 12 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 12 years ago by kjs

+1

Pm

Changed 11 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 *
docs/book/draft/ch11_directive_reference.pod
78:=head3 .globalconst
80:X<.globalconst directive>
82:  .globalconst R<TYPE>R<NAME> = R<VALUE>

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

editor/kate_syntax.pl
27:my @imcc_spdirec = qw(.call .result .return .local .const .globalconst

editor/pir-mode.el
230:    ".endnamespace" ".eom" ".get_results" ".global" ".globalconst"

examples/library/ncurses_life.pir
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

runtime/parrot/library/Archive/Tar.pir
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

runtime/parrot/library/Archive/Zip.pir
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

runtime/parrot/library/LWP/Protocol.pir
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

runtime/parrot/library/LWP/UserAgent.pir
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

t/compilers/imcc/syn/const.t
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

t/compilers/imcc/syn/scope.t
12:     .globalconst string ok1 = "ok\n"

Am I missing something?

kid51

Changed 11 years ago by ligne

ack doesn't seem to know about lex files.

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

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

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

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

compilers/imcc/imclexer.c
3027:return GLOBAL_CONST;

compilers/imcc/imcparser.h
81:     GLOBAL_CONST = 287,
198:#define GLOBAL_CONST 287

compilers/imcc/imcparser.c
1164:     GLOBAL_CONST = 287,
1281:#define GLOBAL_CONST 287
1863:  "ANNOTATE", "GLOBAL_CONST", "PLUS_ASSIGN", "MINUS_ASSIGN", "MUL_ASSIGN",

Changed 11 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.