MC-Basic Structures

From SoftMC-Wiki
Revision as of 12:40, 20 May 2014 by Lisa (talk | contribs) (Definition of Structure Types)
Jump to: navigation, search

Introduction

A structure is a data type used for storing a list of variables of different types into one variable.

Definition of Structure Types

A structure is not a predefined system data type. It must therefore be defined by the user before any declaration of structure variables.

The structure data type can be defined in the declarations section of the configuration file (before the PROGRAM block) and library files (before the first function block), using the following syntax:  

TYPE <structure_type_name>
      <variable_name>{[<index>]…} as <data-type> {<of> <robot_type>}
      …
END TYPE
  • This block defines names, types and array sizes of structure elements.
  • The following data types are supported for the structure: long, double, string, joint and location, generic axis, generic group, semaphore, generic compensation table, user error and user note.
  • User error and note structure elements are generic. They should be assigned to acquire an error number and a message.
  • Nesting of structures is not allowed.
  • Array elements are multidimensional, and can have up to 10 dimensions.

Example:

In config file →

Type X
     Name as String
     Length as Long
End Type

In application file →
Dim shared s1 as X
Program
     ?s1→Name
End program

Declaration of Structure Variables

Structure variables can be declared in all declaration scopes.

Arrays of structures can have up to ten dimensions.

Declaration Syntax

(COMMON SHARED|DIM {SHARED}) <variable_name> AS   < structure_type_name>
(COMMON SHARED|DIM {SHARED}) <variable_name>[<index>] AS <structure_type_name>

Assignment

Assignment of a single element of a structure

Syntax for assignment of a single element of a structure:

<structure_variable_name>→<structure_scalar_ element>  = <expression>
<structure_variable_name>→<structure_array_element>[index] = <expression>

Assignment of a whole structure

The entire structure can be assigned through a single assignment command. However, both assignor and assignee must have the same structure type.

Syntax for assignment of a structure:

<structure_variable_name1> = <structure_variable_name2> 
<structure_array_name>[<index>] = <structure_variable_name>

Query

Query of a single element of a structure

Syntax for querying a single element of a structure:

?<structure_variable_name>→<structure_scalar_element>
PRINTU “##.##” ; <structure_array_name>[<index>]→<structure_scalar_element>
<variable> =  <structure_variable_name>→<structure_array_element>[<index>]

Query of a whole structure

Query of a whole structure is forbidden, and will result in a translation error. ?<structure_variable_name> - will give a translation error

Example:

Common shared S1as X		declaration of an X-type structure variable
S1→Length = 2
? S1   					Translation Error
? S1→ Length 				2				

Operators

Operator cannot be used with whole structures. Operaters can be used with single elements of the structure, as long as they are not system elements (e.g., user errors and notes, semaphores).

Passing by Value and by Reference

A structure can be passed by value and by reference like any other data type.

An example for passing by reference:

Program
    Dim S1 as X
    Call Sub1(S1)
    ?S1→Length			0
End program
Sub Sub1(ByVal X1 as X)
    X1→Length = 1
End Sub
An example for passing by value:
Program
    Dim s1 as X
    Call Sub1(S1)
    ?S1→Length			1
End program
Sub Sub1(X as X1)
    X →Length = 1
End Sub

Returning Structure from Function

A function can return a structure variable like any other data type.

Example:

Dim Shared X1 as X
Program
   Dim S1 as X
   S1 = MyFunc(1)
End Program

Function MyFunc(i1 as long) as X
    MyFunc→Type = 1 
End Function