Type System Base Class: Module type_system
¶
These classes provide the type system mechanism.
-
class
pyrser.type_system.
TypeName
(value)[source]¶ Bases:
str
-
is_polymorphic
¶ Check if one of composed type name is poly
-
-
class
pyrser.type_system.
Symbol
(name, parent=None)[source]¶ Bases:
object
Symbol are semantic name used in a language. Could be map to binary symbol but conceptually are more related to language symbol table.
-
class
pyrser.type_system.
Signature
(name: str)[source]¶ Bases:
pyrser.type_system.symbol.Symbol
Describe a function or variable signature for the language
-
class
pyrser.type_system.
EvalCtx
(sig: pyrser.type_system.signature.Signature)[source]¶ Bases:
object
Store environment for mono/poly call.
-
compute_tret
¶
-
get_compute_sig
(self) → Signature[source]¶ Compute a signature Using resolution!!!
TODO: discuss of relevance of a final generation for a signature
-
get_resolved_names
(self, type_name: TypeName) → list[source]¶ Use self.resolution to subsitute type_name. Allow to instanciate polymorphic type ?1, ?toto
-
is_polymorphic
¶
-
name
¶
-
set_parent
(self, parent) → object[source]¶ When we add a parent (from Symbol), don’t forget to resolve.
-
set_resolved_name
(self, ref: dict, type_name2solve: TypeName, type_name_ref: TypeName)[source]¶ Warning!!! Need to rethink it when global poly type
-
tparams
¶
-
tret
¶
-
use_translator
(self, translator)[source]¶ Attach a translator to an EvalCtx to convert output of the function into another type
-
use_variadic_types
(self, list_type)[source]¶ Attach a list of types for extra variadic argument of a call
-
variadic
¶
-
-
class
pyrser.type_system.
Scope
(name: str=None, sig: [<class 'pyrser.type_system.signature.Signature'>]=None, state=0, is_namespace=True)[source]¶ Bases:
pyrser.type_system.symbol.Symbol
Scope of Signature for a Scope/namespace/type etc... Basic abstraction of type checking. Scope is not a ‘pure’ python set but something between a set and a dict...
-
_Scope__update_count
(self)¶ Update internal counters
-
__contains__
(self, s: Signature) → bool[source]¶ check if a Signature or a Type is declared in a Scope
-
__init__
(self, name: str= None, sig= None, state= 0, is_namespace= True)[source]¶ Unnamed scope for global scope
A Scope have basically 3 possibles states:
FREE: it’s a standalone Scope, generally the global Scope LINKED: the Scope is related to another Scope for type resolution,
like compound statement- EMBEDDED: the Scope was added into another Scope,
- it forwards all ‘in’ calls...like namespacing
A Scope could or couldn’t act like a namespace. All Signature added into a ‘namespaced’ Scope was prefixed by the Scope name.
-
callInjector
(self, old: Node, trans: Translator) → Node[source]¶ If don’t have injector call from parent
-
difference
(self, sig: Scope) → Scope[source]¶ Create a new Set produce by a Set subtracted by another Set
-
findTranslationTo
(self, t2: str)[source]¶ Find an arrow (->) aka a function able to translate something to t2
-
get
(self, key: str, default= None) → Signature[source]¶ Get a signature instance by its internal_name
-
get_all_polymorphic_return
(self) → bool[source]¶ For now, polymorphic return type are handle by symbol artefact.
–> possible multi-polymorphic but with different constraint attached!
-
get_by_params
(self, params)[source]¶ Retrieve a Set of all signature that match the parameter list. Return a pair:
pair[0] the overloads for the functions pair[1] the overloads for the parameters
(a list of candidate list of parameters)
-
get_by_return_type
(self, tname: str) → Scope[source]¶ Retrieve a Set of all signature by (return) type
-
getsig_by_symbol_name
(self, name: str) → Signature[source]¶ Retrieve the unique Signature of a symbol. Fail if the Signature is not unique
-
intersection
(self, sig: Scope) → Scope[source]¶ Create a new Set produce by the intersection of 2 Set
-
intersection_update
(self, oset: Scope) → Scope[source]¶ Update Set with common values of another Set
-
symmetric_difference
(self, sig: Scope) → Scope[source]¶ Create a new Set with values present in only one Set
-
-
pyrser.type_system.
StateScope
¶ alias of
Enum
-
class
pyrser.type_system.
Tuple
(sig: [<class 'pyrser.type_system.signature.Signature'>]=None)[source]¶ Bases:
list
A tuple is just a list of signature.
-
class
pyrser.type_system.
Type
(name: str, sig: [<class 'pyrser.type_system.signature.Signature'>]=None)[source]¶ Bases:
pyrser.type_system.scope.Scope
A Type is a named scope similar to ADT (Abstract Data Type). So we have member variables (ordered) that represent internal state. Member functions (work on an instance of type, depend of the language ref/or not). Non member variables/functions/values as in a scope.
-
type_name
¶
-
-
class
pyrser.type_system.
Var
(name: str, tret: str)[source]¶ Bases:
pyrser.type_system.signature.Signature
Describe a variable signature for the language
-
is_polymorphic
¶
-
-
class
pyrser.type_system.
Val
(value, tret: str)[source]¶ Bases:
pyrser.type_system.signature.Signature
Describe a value signature for the language
-
nvalues
= 0¶
-
valuniq
= {}¶
-
-
class
pyrser.type_system.
Fun
(name: str, tret: str, tparams: list=None, variadic=None)[source]¶ Bases:
pyrser.type_system.signature.Signature
Describe a function signature for the language
-
arity
¶
-
is_polymorphic
¶
-
return_type
¶
-
this_type
¶
-
-
class
pyrser.type_system.
Translator
(fun: pyrser.type_system.fun.Fun, notify: pyrser.error.Notification=None)[source]¶ Bases:
object
Handle conversion from type_source to type_target thru a function. Do error.Notification if applyiable.
-
fun
¶
-
notify
¶
-
source
¶
-
target
¶
-
-
class
pyrser.type_system.
MapSourceTranslate
[source]¶ Bases:
dict
Handle all conversions functions provide as Translator.
-
_MapSourceTranslate__reversed
(self) → reversed¶
-
-
class
pyrser.type_system.
MapTargetTranslate
[source]¶ Bases:
dict
Handle all translation to one type
-
_MapTargetTranslate__reversed
(self) → reversed¶
-
key
¶
-
-
class
pyrser.type_system.
Inference
[source]¶ Bases:
object
-
feedback
(self, diagnostic= None)[source]¶ Do feedback. Write infos into diagnostic object, if this parameter is not provide and self is a AST (has is own diagnostic object), use the diagnostic of self.
-
infer_block
(self, body, diagnostic= None)[source]¶ Infer type on block is to type each of is sub-element
-
infer_id
(self, ident, diagnostic= None)[source]¶ Infer type from an ID! - check if ID is declarated in the scope - if no ID is polymorphic type
-
infer_literal
(self, args, diagnostic= None)[source]¶ Infer type from an LITERAL! Type of literal depend of language. We adopt a basic convention
-
infer_type
(self, init_scope: Scope= None, diagnostic= None)[source]¶ Do inference. Write infos into diagnostic object, if this parameter is not provide and self is a AST (has is own diagnostic object), use the diagnostic of self.
-
type_algos
(self)[source]¶ - Sub class must return a Tuple of 3 elements:
- the method to use to infer type.
- the list of params to used when infer a type.
- the method to use when feedback a type.
This is useful to connect AST members to generic algo or to overload some specific semantic for your language.
-
-
class
pyrser.type_system.
InferNode
(init_scope: pyrser.type_system.scope.Scope=None, parent: pyrser.type_system.scope.Scope=None)[source]¶ Bases:
object
An instance of this class is automatically attach on each AST Nodes by the inference algorithm during the inference process into the ‘.infer_node’ attribute. The aim is to keep track of the correct Scope and the final EvalCtx for each AST Nodes.