OILS / opy / _regtest / src / asdl / const.py View on Github | oilshell.org

36 lines, 2 significant
1#!/usr/bin/env python
2"""
3const.py
4"""
5
6DEFAULT_INT_WIDTH = 3 # 24 bits
7
8# 2^24 - 1 is used as an invalid/uninitialized value for ASDL integers.
9
10# Why? We have a few use cases for invalid/sentinel values:
11# - span_id, line_id. Sometimes we don't have a span ID.
12# - file descriptor: 'read x < f.txt' vs 'read x 0< f.txt'
13#
14# Other options for representation:
15#
16# 1. ADSL could use signed integers, then -1 is valid.
17# 2. Use a type like fd = None | Some(int fd)
18#
19# I don't like #1 because ASDL is lazily-decoded, and then we have to do sign
20# extension on demand. (24 bits to 32 or 64). As far as I can tell, sign
21# extension requires a branch, at least in portable C (on the sign bit).
22#
23# The second option is semantically cleaner. But it needlessly
24# inflates the size of both the source code and the data. Instead of having a
25# single "inline" integer, we would need a reference to another value.
26#
27# We could also try to do some fancy thing like fd = None |
28# Range<1..max_fd>(fd), with smart encoding. But that is overkill for these
29# use cases.
30#
31# Using InvalidInt instead of -1 seems like a good compromise.
32
33NO_INTEGER = (1 << (DEFAULT_INT_WIDTH * 8)) - 1
34
35# NOTE: In Python: 1 << (n * 8) - 1 is wrong! I thought that bit shift would
36# have higher precedence.