Add the init offset statement.
This provides a way of giving a consistent offset to every statement in a file.
This commit is contained in:
+29
-12
@@ -491,11 +491,14 @@ class Lexer(object):
|
||||
sub-lexers to lex sub-blocks.
|
||||
"""
|
||||
|
||||
def __init__(self, block, init=False):
|
||||
def __init__(self, block, init=False, init_offset=0):
|
||||
|
||||
# Are we underneath an init block?
|
||||
self.init = init
|
||||
|
||||
# The priority of auto-defined init statements.
|
||||
self.init_offset = init_offset
|
||||
|
||||
self.block = block
|
||||
self.eob = False
|
||||
|
||||
@@ -512,6 +515,7 @@ class Lexer(object):
|
||||
self.word_cache = ""
|
||||
|
||||
|
||||
|
||||
def advance(self):
|
||||
"""
|
||||
Advances this lexer to the next line in the block. The lexer
|
||||
@@ -649,7 +653,7 @@ class Lexer(object):
|
||||
|
||||
init = self.init or init
|
||||
|
||||
return Lexer(self.subblock, init=init)
|
||||
return Lexer(self.subblock, init=init, init_offset=self.init_offset)
|
||||
|
||||
def string(self):
|
||||
"""
|
||||
@@ -1797,7 +1801,7 @@ def image_statement(l, loc):
|
||||
rv = ast.Image(loc, name, expr, atl)
|
||||
|
||||
if not l.init:
|
||||
rv = ast.Init(loc, [ rv ], 990)
|
||||
rv = ast.Init(loc, [ rv ], 500 + l.init_offset)
|
||||
|
||||
l.advance()
|
||||
|
||||
@@ -1831,7 +1835,7 @@ def define_statement(l, loc):
|
||||
rv = ast.Define(loc, store, name, expr)
|
||||
|
||||
if not l.init:
|
||||
rv = ast.Init(loc, [ rv ], priority)
|
||||
rv = ast.Init(loc, [ rv ], priority + l.init_offset)
|
||||
|
||||
l.advance()
|
||||
|
||||
@@ -1865,7 +1869,7 @@ def default_statement(l, loc):
|
||||
rv = ast.Default(loc, store, name, expr)
|
||||
|
||||
if not l.init:
|
||||
rv = ast.Init(loc, [ rv ], priority)
|
||||
rv = ast.Init(loc, [ rv ], priority + l.init_offset)
|
||||
|
||||
l.advance()
|
||||
|
||||
@@ -1895,7 +1899,7 @@ def transform_statement(l, loc):
|
||||
rv = ast.Transform(loc, name, atl, parameters)
|
||||
|
||||
if not l.init:
|
||||
rv = ast.Init(loc, [ rv ], priority)
|
||||
rv = ast.Init(loc, [ rv ], priority + l.init_offset)
|
||||
|
||||
l.advance()
|
||||
|
||||
@@ -1964,6 +1968,19 @@ def label_statement(l, loc, init=False):
|
||||
l.advance()
|
||||
return ast.Label(loc, name, block, parameters, hide=hide)
|
||||
|
||||
@statement("init offset")
|
||||
def init_offset_statement(l, loc):
|
||||
|
||||
l.require('=')
|
||||
offset = l.require(l.integer)
|
||||
|
||||
l.expect_eol()
|
||||
l.expect_noblock('init offset statement')
|
||||
l.advance()
|
||||
|
||||
l.init_offset = int(offset)
|
||||
return [ ]
|
||||
|
||||
@statement("init label")
|
||||
def init_label_statement(l, loc):
|
||||
return label_statement(l, loc, init=True)
|
||||
@@ -1998,7 +2015,7 @@ def init_statement(l, loc):
|
||||
finally:
|
||||
l.init = old_init
|
||||
|
||||
return ast.Init(loc, block, priority)
|
||||
return ast.Init(loc, block, priority + l.init_offset)
|
||||
|
||||
|
||||
def screen1_statement(l, loc):
|
||||
@@ -2015,7 +2032,7 @@ def screen1_statement(l, loc):
|
||||
rv = ast.Screen(loc, screen)
|
||||
|
||||
if not l.init:
|
||||
rv = ast.Init(loc, [ rv ], -500)
|
||||
rv = ast.Init(loc, [ rv ], -500 + l.init_offset)
|
||||
|
||||
return rv
|
||||
|
||||
@@ -2031,7 +2048,7 @@ def screen2_statement(l, loc):
|
||||
rv = ast.Screen(loc, screen)
|
||||
|
||||
if not l.init:
|
||||
rv = ast.Init(loc, [ rv ], -500)
|
||||
rv = ast.Init(loc, [ rv ], -500 + l.init_offset)
|
||||
|
||||
return rv
|
||||
|
||||
@@ -2069,7 +2086,7 @@ def testcase_statement(l, loc):
|
||||
rv = ast.Testcase(loc, name, test)
|
||||
|
||||
if not l.init:
|
||||
rv = ast.Init(loc, [ rv ], 500)
|
||||
rv = ast.Init(loc, [ rv ], 500 + l.init_offset)
|
||||
|
||||
return rv
|
||||
|
||||
@@ -2129,7 +2146,7 @@ def translate_strings(init_loc, language, l):
|
||||
if l.init:
|
||||
return block
|
||||
|
||||
return ast.Init(init_loc, block, 0)
|
||||
return ast.Init(init_loc, block, l.init_offset)
|
||||
|
||||
@statement("translate")
|
||||
def translate_statement(l, loc):
|
||||
@@ -2260,7 +2277,7 @@ def style_statement(l, loc):
|
||||
ll.expect_eol()
|
||||
|
||||
if not l.init:
|
||||
rv = ast.Init(loc, [ rv ], 0)
|
||||
rv = ast.Init(loc, [ rv ], l.init_offset)
|
||||
|
||||
l.advance()
|
||||
|
||||
|
||||
@@ -5,6 +5,11 @@ Full Changelog
|
||||
Ren'Py 6.99.10
|
||||
==============
|
||||
|
||||
The new :ref:`init offset <init-offset-statement>` statement makes it
|
||||
possible to apply a priority offset to statements that run at init
|
||||
time, including ``init``, ``init python``, ``define``, ``default``,
|
||||
``style``, and ``transform``.
|
||||
|
||||
The `style_group` ui property has been renamed to `style_prefix`, to make
|
||||
its function more apparent. (The old name still works, for compatibility with
|
||||
older code.) A new `style_suffix` ui property has been added, allowing
|
||||
|
||||
@@ -117,7 +117,6 @@ Variables that have their value set in an init python block are not
|
||||
saved, loaded, and do not participate in rollback, unless the object
|
||||
the variable refers to is changed.
|
||||
|
||||
|
||||
.. _define-statement:
|
||||
|
||||
Define Statement
|
||||
@@ -171,6 +170,33 @@ prepending it to the variable name with a dot. For example::
|
||||
default schedule.day = 0
|
||||
|
||||
|
||||
.. init-offset-statement
|
||||
|
||||
Init Offset Statement
|
||||
---------------------
|
||||
|
||||
The init offset statement sets a priority offset for all statements
|
||||
that run at init time. (init, init python, define, default, screen,
|
||||
transform, style, and more.) The offset applies to all following
|
||||
statements in the current block and chold blocks, up to the next
|
||||
init priority statement. The statement::
|
||||
|
||||
init offset = 42
|
||||
|
||||
sets the priority offset to 42. In the code::
|
||||
|
||||
init offset = 2
|
||||
define foo = 2
|
||||
|
||||
init offset = 1
|
||||
define foo = 1
|
||||
|
||||
init offset = 0
|
||||
|
||||
The first define statement is run at priority 2, which means it runs
|
||||
after the second define statement, and hence ``foo`` winds up with
|
||||
a value of 2.
|
||||
|
||||
Names in the Store
|
||||
------------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user