com.github.gchudnov.bscript.lang.symbols
Type members
Classlikes
Allows to reference types of other variables and expressions.
Allows to reference types of other variables and expressions.
example #1:
int x = 10;
decltype(x) y = 20; // DeclType(Var(SymbolRef("x")))
example #2:
int x = 10;
long y = 20L;
decltype(x + y) z = 30; // DeclType(Add(Var(SymbolRef("x")), Var(SymbolRef("y"))))
Scope
Scope
A scope is a code region with a well-defined boundary that groups symbol definitions (in a dictionary associated with that code region).
Scope boundaries usually coincide with begin and end tokens such as curly braces. We call this lexical scoping because the extent of a scope is lexically delimited.
Here’s a list of scope characteristics:
-
Static vs. dynamic scoping: Most languages have static scoping, but some (like classic LISP and PostScript) have dynamic scoping. Think of dynamic scoping as allowing methods to see the local variables of invoking methods.
-
Named scopes: Many scopes, like classes and methods have names, but global and local scopes don’t.
-
Nesting: Languages usually allow some form of scope nesting.
-
Contents: Some scopes allow declarations, some allow statements, and some allow both. C structs only allow declarations.
-
Visibility: The symbols in a scope might or might not be visible to some other code section.
Scopes don’t need to track the code region from which we create them. Instead, the AST for the code regions point to their scopes. This makes sense because we’re going to look up symbols in scopes according to what we find in the AST nodes.
A reference’s scope stack is the set of scopes on the path to the root of the scope tree. We call this stack the semantic context.
So, to resolve a symbol reference, we look for it in its semantic context, starting with the current scope. If resolve() doesn’t find the symbol in the current scope, it asks the enclosing scope if it can find the symbol. resolve() recursively walks toward the root of the scope tree until it finds the symbol or runs out of scopes.
- Companion:
- object
Symbol - a generic programming language symbol - contains information about a language element.
Symbol - a generic programming language symbol - contains information about a language element.
Symbols have:
-
Name - Symbols are usually identifiers like x, f, and T, but they can be operators too. For example, in Ruby, we can use operators like + as method names.
-
Category - what kind of thing the symbol is. Is it a class, method, variable, label, and so on.
-
Type - To validate operation x+y, for example, we need to know the types of x and y. Dynamically typed languages like Python track type information at run-time. Statically typed languages like C++ and Java track type information at compile time.
- Companion:
- object
Reference to a Symbol
Reference to a Symbol
- Value parameters:
- name
name of the symbol
Type is used to distinguish between user-defined types and other program symbols.
Type is used to distinguish between user-defined types and other program symbols.
We’re using it only as a tag.
- Companion:
- object
Reference to a type
Reference to a type
- Value parameters:
- name
Name of the type that is defined in one of the scopes
Vector Type.
Vector Type.
The element of the vector is inside of the type