@@ -243,7 +243,7 @@ def make_constexpr(lineno, expr):
243243 return sym .CONSTEXPR (expr , lineno = lineno )
244244
245245
246- def make_strslice (lineno , s , lower , upper ):
246+ def make_strslice (lineno : int , s , lower , upper ):
247247 """Wrapper: returns String Slice node"""
248248 return sym .STRSLICE .make_node (lineno , s , lower , upper )
249249
@@ -395,17 +395,21 @@ def make_call(id_: str, lineno: int, args: sym.ARGLIST):
395395 arr .append_child (offset )
396396 return arr
397397
398- if entry .class_ == CLASS .var : # An already declared/used string var
398+ if entry .class_ in ( CLASS .var , CLASS . const ) : # An already declared/used string var
399399 if len (args ) > 1 :
400400 errmsg .syntax_error_not_array_nor_func (lineno , id_ )
401401 return None
402402
403- entry = SYMBOL_TABLE .access_var (id_ , lineno )
404- if entry is None :
405- return None
403+ if entry .class_ == CLASS .var :
404+ entry = SYMBOL_TABLE .access_var (id_ , lineno )
405+ if entry is None :
406+ return None
406407
407408 if len (args ) == 1 :
408- return sym .STRSLICE .make_node (lineno , entry , args [0 ].value , args [0 ].value )
409+ if entry .class_ == CLASS .var :
410+ return make_strslice (lineno , entry , args [0 ].value , args [0 ].value )
411+ # it's a const
412+ return make_strslice (lineno , sym .STRING (entry .value , lineno ), args [0 ].value , args [0 ].value )
409413
410414 mark_entry_as_accessed (entry )
411415 return entry
@@ -2478,6 +2482,11 @@ def p_string_lp_expr_rp(p):
24782482
24792483def p_expr_id_substr (p ):
24802484 """string : ID substr"""
2485+ entry = SYMBOL_TABLE .get_entry (p [1 ])
2486+ if entry is not None and entry .type_ == TYPE .string and entry .token == "CONST" :
2487+ p [0 ] = make_strslice (p .lineno (1 ), entry , p [2 ][0 ], p [2 ][1 ])
2488+ return
2489+
24812490 entry = SYMBOL_TABLE .access_var (p [1 ], p .lineno (1 ), default_type = TYPE .string )
24822491 p [0 ] = None
24832492 if entry is None :
@@ -2595,7 +2604,7 @@ def p_idcall_expr(p):
25952604 if p [0 ] is None :
25962605 return
25972606
2598- if p [0 ].token in ("STRSLICE" , "ID" , "STRING" ):
2607+ if p [0 ].token in ("STRSLICE" , "ID" , "STRING" ) or p [ 0 ]. token == "CONST" and p [ 0 ]. type_ == TYPE . string :
25992608 entry = SYMBOL_TABLE .access_call (p [1 ], p .lineno (1 ))
26002609 mark_entry_as_accessed (entry )
26012610 return
0 commit comments