com.github.gchudnov.bscript.lang.symbols

Type members

Classlikes

final case class DeclType(expr: Expr) extends Type

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"))))
trait Named
final case class SBlock(name: String) extends Symbol with Scope
final case class SBuiltInType(name: String) extends Symbol with Type
final case class SMethod(name: String) extends Symbol with Scope
final case class SStruct(name: String) extends Symbol with Scope with Type
final case class SVar(name: String) extends Symbol
trait Scope extends Named

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:

  1. 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.

  2. Named scopes: Many scopes, like classes and methods have names, but global and local scopes don’t.

  3. Nesting: Languages usually allow some form of scope nesting.

  4. Contents: Some scopes allow declarations, some allow statements, and some allow both. C structs only allow declarations.

  5. 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
object Scope
Companion:
class
final class ScopeStateException(message: String) extends LangException
trait Symbol extends Named

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:

  1. 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.

  2. Category - what kind of thing the symbol is. Is it a class, method, variable, label, and so on.

  3. 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
object Symbol
Companion:
class
final case class SymbolRef(name: String) extends Symbol

Reference to a Symbol

Reference to a Symbol

Value parameters:
name

name of the symbol

trait Type extends Named

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
object Type
Companion:
class
final case class TypeRef(name: String) extends Type

Reference to a type

Reference to a type

Value parameters:
name

Name of the type that is defined in one of the scopes

final case class VectorType(elementType: Type) extends Type

Vector Type.

Vector Type.

The element of the vector is inside of the type