Literals in ASTx¶
Introduction¶
Literals are fixed values embedded directly in source code, representing specific values. They are the raw data given in variables or constants and are not variables themselves but the data assigned to them. Common types of literals found in many programming languages include:
- Integer Literals: Represent whole numbers without any fractional component, e.g.,
42
,-1
,0
. - Floating-Point Literals: Represent real numbers with fractional parts, e.g.,
3.14
,-0.01
. - String Literals: Enclose sequences of characters, typically in quotes, e.g.,
"hello"
,'world'
. - Boolean Literals: Represent truth values, usually denoted by
true
andfalse
. - Null/None Literals: Represent the absence of a value or a null reference, e.g.,
null
in Java,None
in Python. - Character Literals: Represent single characters, e.g.,
'a'
,'Z'
. - Array or List Literals: Define arrays or lists directly, e.g.,
[1, 2, 3]
in Python. - Object Literals: Used in languages like JavaScript to define objects directly, e.g.,
{name: "Alice", age: 25}
.
Currently, ASTx just support integer literals (8, 16, 32, and 64 bits) and boolean literals, but the support for new ones should be done soon.
Example¶
Let's check a very some examples of the usage of literals with ASTx:
In [1]:
Copied!
import astx
import astx
In [2]:
Copied!
astx.LiteralInt8(value=1)
astx.LiteralInt8(value=1)
Out[2]:
In [3]:
Copied!
astx.LiteralInt16(value=1)
astx.LiteralInt16(value=1)
Out[3]:
In [4]:
Copied!
astx.LiteralInt32(value=1)
astx.LiteralInt32(value=1)
Out[4]:
In [5]:
Copied!
astx.LiteralInt64(value=1)
astx.LiteralInt64(value=1)
Out[5]:
In [6]:
Copied!
astx.LiteralBoolean(value=True)
astx.LiteralBoolean(value=True)
Out[6]:
Now, let's check how a math operation over literals would look like:
In [7]:
Copied!
lit_1_a = astx.LiteralInt32(value=1)
lit_1_b = astx.LiteralInt32(value=1)
lit_2 = astx.LiteralInt32(value=2)
(lit_1_a + lit_1_b + lit_2)
lit_1_a = astx.LiteralInt32(value=1)
lit_1_b = astx.LiteralInt32(value=1)
lit_2 = astx.LiteralInt32(value=2)
(lit_1_a + lit_1_b + lit_2)
Out[7]:
Conclusion¶
Literals are features really fundamental in any programming language. Currently, ASTx supports supports Integers (8, 16, 32, and 64 bits) and Boolean, and much more should be implemented soon!