UniSet 2.45.1
Пространство имен st2js.type_system

Функции

IECType resolve_type (IRExpression expr, dict[str, IRVariable] variables, list[IRFBInstance]|None fb_instances=None)
IECType _resolve_field_access_type (IRFieldAccess expr, list[IRFBInstance]|None fb_instances=None, dict[str, IRVariable]|None variables=None)
IECType _resolve_arithmetic_type (IECType left, IECType right, str op)
IRProgram check_types (IRProgram program, *, bool ignore_undefined=False)
dict[str, IRVariable_build_variable_dict (IRProgram program)
IRStatement _check_statement (IRStatement stmt, dict[str, IRVariable] variables, list[IRFBInstance]|None fb_instances=None)
IRAssignment _check_assignment (IRAssignment assign, dict[str, IRVariable] variables, list[IRFBInstance]|None fb_instances=None)
IECType|None _resolve_target_type (IRExpression target, dict[str, IRVariable] variables)
IRExpression _coerce_assignment (IRExpression value, IECType value_type, IECType target_type)
IRExpression _check_expression (IRExpression expr, dict[str, IRVariable] variables, list[IRFBInstance]|None fb_instances=None)
IRBinaryOp _check_binary_op (IRBinaryOp expr, dict[str, IRVariable] variables, list[IRFBInstance]|None fb_instances=None)
IRIfElse _check_if_else (IRIfElse stmt, dict[str, IRVariable] variables, list[IRFBInstance]|None fb_instances=None)
IRCase _check_case (IRCase stmt, dict[str, IRVariable] variables, list[IRFBInstance]|None fb_instances=None)
IRForLoop _check_for_loop (IRForLoop stmt, dict[str, IRVariable] variables, list[IRFBInstance]|None fb_instances=None)

Переменные

bool _ignore_undefined = False
dict _ARITHMETIC_OPS = {"+", "-", "*", "/", "MOD"}
dict _COMPARISON_OPS = {"<", ">", "<=", ">=", "=", "<>"}
dict _LOGICAL_OPS = {"AND", "OR", "XOR"}
dict _NUMERIC_TYPES
dict _INTEGER_TYPES
dict _FB_TYPE_MAP

Подробное описание

Type system for the st2js converter.

Provides type resolution and type checking for IEC 61131-3 types.
Walks the IR tree and inserts IRTypeCoercion nodes where implicit
type conversions are needed for correct JavaScript code generation.

Supported coercions:
  - BOOL in arithmetic context -> IRTypeCoercion(BOOL, INT)
  - REAL assigned to INT/DINT variable -> IRTypeCoercion(REAL, INT/DINT)
  - INT assigned to REAL -> no coercion (JS handles automatically)

Type errors are raised for incompatible types (e.g., STRING in arithmetic).

Функции

◆ _build_variable_dict()

dict[str, IRVariable] st2js.type_system._build_variable_dict ( IRProgram program)
protected
Build a name->IRVariable dict from all program variable sections.

Also adds flat aliases for GVL fields so unqualified access
(e.g., DI_GRU_QG1_IsOn instead of IO.DI_GRU_QG1_IsOn) resolves correctly.

◆ _check_assignment()

IRAssignment st2js.type_system._check_assignment ( IRAssignment assign,
dict[str, IRVariable] variables,
list[IRFBInstance] | None fb_instances = None )
protected
Type-check an assignment, inserting coercions on the value if needed.

◆ _check_binary_op()

IRBinaryOp st2js.type_system._check_binary_op ( IRBinaryOp expr,
dict[str, IRVariable] variables,
list[IRFBInstance] | None fb_instances = None )
protected
Type-check a binary operation, inserting BOOL->INT coercions for arithmetic.

◆ _check_case()

IRCase st2js.type_system._check_case ( IRCase stmt,
dict[str, IRVariable] variables,
list[IRFBInstance] | None fb_instances = None )
protected
Type-check a CASE statement.

◆ _check_expression()

IRExpression st2js.type_system._check_expression ( IRExpression expr,
dict[str, IRVariable] variables,
list[IRFBInstance] | None fb_instances = None )
protected
Type-check an expression, inserting BOOL->INT coercions in arithmetic.

◆ _check_for_loop()

IRForLoop st2js.type_system._check_for_loop ( IRForLoop stmt,
dict[str, IRVariable] variables,
list[IRFBInstance] | None fb_instances = None )
protected
Type-check a FOR loop.

◆ _check_if_else()

IRIfElse st2js.type_system._check_if_else ( IRIfElse stmt,
dict[str, IRVariable] variables,
list[IRFBInstance] | None fb_instances = None )
protected
Type-check an IF/ELSIF/ELSE statement.

◆ _check_statement()

IRStatement st2js.type_system._check_statement ( IRStatement stmt,
dict[str, IRVariable] variables,
list[IRFBInstance] | None fb_instances = None )
protected
Type-check a single statement, returning a potentially modified copy.

◆ _coerce_assignment()

IRExpression st2js.type_system._coerce_assignment ( IRExpression value,
IECType value_type,
IECType target_type )
protected
Insert coercion node if value type doesn't match target type.

Rules:
  - REAL -> INT/DINT: insert coercion (Math.trunc in codegen)
  - INT -> REAL: no coercion (JS handles)
  - STRING -> numeric: type error
  - Same type: no coercion

◆ _resolve_arithmetic_type()

IECType st2js.type_system._resolve_arithmetic_type ( IECType left,
IECType right,
str op )
protected
Resolve the result type of an arithmetic operation.

Rules:
  - Any operand REAL -> result is REAL
  - DINT + INT -> DINT
  - INT + INT -> INT
  - BOOL is treated as INT in arithmetic (coercion handled separately)

Raises:
    STTypeError: If a non-numeric type is used in arithmetic.

◆ _resolve_field_access_type()

IECType st2js.type_system._resolve_field_access_type ( IRFieldAccess expr,
list[IRFBInstance] | None fb_instances = None,
dict[str, IRVariable] | None variables = None )
protected
Resolve the type of a field access expression.

For FB instances, looks up the output type from the fb_registry.
For STRUCT variables, looks up the field type from struct_fields.
Returns VOID if the instance/variable or field is not found.

◆ _resolve_target_type()

IECType | None st2js.type_system._resolve_target_type ( IRExpression target,
dict[str, IRVariable] variables )
protected
Resolve the type of an assignment target.

◆ check_types()

IRProgram st2js.type_system.check_types ( IRProgram program,
* ,
bool ignore_undefined = False )
Walk the IR tree, inserting type coercion nodes where needed.

Modifies a copy of the program; the original is not mutated.

Args:
    program: The IRProgram to type-check.
    ignore_undefined: If True, undefined variables produce warnings
        instead of errors (assumes INT type).

Returns:
    A new IRProgram with IRTypeCoercion nodes inserted.

Raises:
    STTypeError: If incompatible types are detected.

◆ resolve_type()

IECType st2js.type_system.resolve_type ( IRExpression expr,
dict[str, IRVariable] variables,
list[IRFBInstance] | None fb_instances = None )
Determine the IEC type of an expression.

Args:
    expr: The IR expression node to resolve.
    variables: A name->IRVariable dict for variable type lookup.
    fb_instances: Optional list of FB instances for field access resolution.

Returns:
    The resolved IECType for the expression.

Raises:
    STTypeError: If the expression contains incompatible types.

Переменные

◆ _FB_TYPE_MAP

dict st2js.type_system._FB_TYPE_MAP
protected
Инициализатор
1= {
2 "BOOL": IECType.BOOL,
3 "SINT": IECType.SINT,
4 "INT": IECType.INT,
5 "DINT": IECType.DINT,
6 "LINT": IECType.LINT,
7 "USINT": IECType.USINT,
8 "UINT": IECType.UINT,
9 "UDINT": IECType.UDINT,
10 "ULINT": IECType.ULINT,
11 "BYTE": IECType.BYTE,
12 "WORD": IECType.WORD,
13 "DWORD": IECType.DWORD,
14 "LWORD": IECType.LWORD,
15 "REAL": IECType.REAL,
16 "LREAL": IECType.LREAL,
17 "TIME": IECType.TIME,
18 "STRING": IECType.STRING,
19 "WSTRING": IECType.WSTRING,
20}

◆ _INTEGER_TYPES

dict st2js.type_system._INTEGER_TYPES
protected
Инициализатор
1= {
2 IECType.SINT, IECType.INT, IECType.DINT, IECType.LINT,
3 IECType.USINT, IECType.UINT, IECType.UDINT, IECType.ULINT,
4 IECType.BYTE, IECType.WORD, IECType.DWORD, IECType.LWORD,
5}

◆ _NUMERIC_TYPES

dict st2js.type_system._NUMERIC_TYPES
protected
Инициализатор
1= {
2 IECType.BOOL,
3 IECType.SINT, IECType.INT, IECType.DINT, IECType.LINT,
4 IECType.USINT, IECType.UINT, IECType.UDINT, IECType.ULINT,
5 IECType.BYTE, IECType.WORD, IECType.DWORD, IECType.LWORD,
6 IECType.REAL, IECType.LREAL,
7}