Lexer Tokens#
Code Example
Runnable Example in Jac and JacLib
# Lexer tokens - Builtin type keywords
with entry {
# These lexer tokens are used as type annotations
# They are keywords that represent builtin types
x: str = "string";
y: int = 42;
z: float = 3.14;
lst: list = [1, 2, 3];
tup: tuple = (1, 2);
s: set = {1, 2};
d: dict = {"key": "value"};
b: bool = True;
print(x, y, z, lst, tup, s, d, b);
# Note: These are tokenized specially so they can be used as types
# See builtin_types.jac for more comprehensive type usage examples
}
Jac Grammar Snippet
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
|
Description
Builtin type keywords are special tokens in Jac that represent fundamental data types. These keywords are recognized by the lexer and can be used both as type annotations and as runtime type objects.
What are Builtin Type Keywords?
When you write code in Jac, the lexer (the part of the compiler that reads your code) recognizes certain words as special type keywords. These keywords represent the basic building blocks of data in your programs.
The Eight Builtin Type Keywords
Lines 7-14 demonstrate all eight builtin type keywords used as type annotations:
Keyword | Type | Example Value | Line |
---|---|---|---|
str |
String (text) | "string" |
7 |
int |
Integer (whole number) | 42 |
8 |
float |
Floating-point (decimal) | 3.14 |
9 |
list |
List (ordered collection) | [1, 2, 3] |
10 |
tuple |
Tuple (immutable sequence) | (1, 2) |
11 |
set |
Set (unique values) | {1, 2} |
12 |
dict |
Dictionary (key-value pairs) | {"key": "value"} |
13 |
bool |
Boolean (true/false) | True |
14 |
Type Annotation Syntax
The pattern for declaring a variable with a type annotation is:
For example, line 7 shows x: str = "string"
, which means:
- x
is the variable name
- str
is the type annotation (telling Jac this should be a string)
- "string"
is the value being assigned
How the Lexer Treats These Keywords
Lines 17-18 explain an important detail: these keywords are "tokenized specially" by the lexer. This means the lexer gives them special treatment so they can serve two purposes:
graph TD
A[Builtin Type Keyword] --> B[Used as Type Annotation]
A --> C[Used as Runtime Type Object]
B --> D["Example: x: int = 5"]
C --> E["Example: type(x) == int"]
Purpose 1: Type Annotations
Type annotations provide compile-time type information. They tell Jac (and developers reading the code) what type of data a variable should hold:
Declaration | What It Means |
---|---|
x: str |
x should hold string values |
y: int |
y should hold integer values |
z: float |
z should hold floating-point values |
Purpose 2: Runtime Type Objects
The same keywords can also be used at runtime as type objects. For example, you can use them with type()
checks, type conversions, or as values in your code.
Where These Keywords Appear
These builtin type keywords can be used in several contexts:
Context | Example | Lines |
---|---|---|
Variable declarations | x: str = "hello" |
7-14 |
Function parameters | def greet(name: str) {...} |
- |
Return type annotations | def get_age() -> int {...} |
- |
Class attributes | has name: str |
- |
Complete Example Breakdown
Line 16 prints all the variables, demonstrating that: - Each variable holds a value of its declared type - The type annotations don't prevent the code from running - All the builtin types work together in a single program
Related Information
Line 18 points to builtin_types.jac
for more comprehensive examples of how to use these types. This file (lexer_tokens.jac
) focuses specifically on showing that these are special keywords recognized by the lexer, not just regular identifiers.
Why Special Tokenization Matters
By tokenizing these keywords specially, Jac can: 1. Provide better error messages when types are misused 2. Enable type checking and inference 3. Allow these words to be used both as types and values 4. Reserve these words so they can't be used as variable names in most contexts