数据交换格式

发布: (2026年2月5日 GMT+8 09:03)
4 分钟阅读
原文: Dev.to

I’m happy to translate the article for you, but I’ll need the full text you’d like translated. Could you please paste the content (excluding the source link you already provided) here? Once I have the text, I’ll translate it into Simplified Chinese while preserving the original formatting and markdown.

动态类型语法

文件信息

// Data Interchange Format
// File Extension: .data, .schema
// Schema Definition Header File: .schema
// Data Interchange Source File: .data
// .data Files May be Dynamically Typed
// .data Files May be Statically Typed
// .data Files are Required
// .schema Files Must be Statistically Typed
// .schema Files Cannot be Dynamically Typed
// .schema Files are Optional

数据结构初始化

// Syntax – Name = {fields}
DataStructure = {
    integer        = 1;    
    float          = 1.0;
    boolean_type   = true;
    character      = 'd';
    string         = "string";
    array          = [];
    hash_map       = [()];
    DataStructure  = {}; 
}

原始类型初始化

类型变量示例
布尔boolean_typeboolean_type = true;
字符charactercharacter = 's';
浮点数floatfloat = 1.0;
整数integerinteger = 1;
字符串stringstring = "string";

数组初始化

array = [
    1,
    2,
    3,
    4,
    5
]

哈希映射初始化

hash_map = [
    (1|"string"),
    (2|"string"),
    (3|"string"),
    (4|"string"),
    (5|"string")
]

文件信息重述

// Data Interchange Format
// File Extension: .data, .schema
// Schema Definition Header File: .schema
// Data Interchange Source File: .data
// .data Files May be Dynamically Typed
// .data Files May be Statically Typed
// .data Files are Required
// .schema Files Must be Statistically Typed
// .schema Files Cannot be Dynamically Typed
// .schema Files are Optional

选项类型语法

// option name = value;

数据结构声明

// May be Declared with Initialized Fields
// Syntax – type Name = {fields}
// Optional Field Syntax – optional name;
struct DataStructure = {
    option   unsigned_integer_8bit;
    uint16               unsigned_integer_16bit;
    uint32               unsigned_integer_32bit;
    uint64               unsigned_integer_64bit;
    optional    unsigned_integer_128bit;
    option    signed_integer_8bit;
    int16                signed_integer_16bit;
    int32                signed_integer_32bit;
    int64                signed_integer_64bit;
    optional     signed_integer_128bit;
    optionfloat_32bit;
    float64              float_64bit;
    optional   float_128bit;
    bool                 boolean_type;
    option   character_8bit;
    char16               character_16bit;
    optional    character_32bit;
    option    string_8bit;
    str16                string_16bit;
    optional      string_32bit;
    array(5)      one_dimensional_array;
    array(5:2)    two_dimensional_array;
    map      hash_map;
    DataStructure        data_structure;
    EnumatorType         enumerator_type;
}

枚举声明

// 可以使用已初始化的字段声明
// 语法 – 类型 名称 = {字段}
enum EnumeratorType = {
    FIRST_VALUE,   // 值 = 0
    SECOND_VALUE,  // 值 = 1
    THIRD_VALUE,   // 值 = 2
    FOURTH_VALUE,  // 值 = 3
    FIFTH_VALUE    // 值 = 4
}

数据结构初始化

// Syntax – type name = {fields}
DataStructure data_structure = {
    uint8   unsigned_integer_8bit   = null;
    uint16  unsigned_integer_16bit  = 2;
    uint32  unsigned_integer_32bit  = 3;
    uint64  unsigned_integer_64bit  = 4;
    uint128 unsigned_integer_128bit = 5;
    int8    signed_integer_8bit     = null;
    int16   signed_integer_16bit    = -2;
    int32   signed_integer_32bit    = -3;
    int64   signed_integer_64bit    = -4;
    int128  signed_integer_128bit   = -5;
    float32 float_32bit             = null;
    float64 float_64bit             = 2.0;
    float128 float_128bit           = 3.0;
    bool    boolean_type           = true;
    char8   character_8bit         = null;
    char16  character_16bit        = 'c';
    char32  character_32bit        = 'd';
    str8    string_8bit            = null;
    str16   string_16bit           = "string";
    str32   string_32bit           = "string";
    array(5) one_dimensional_array = [];
    array(5:2) two_dimensional_array = [];
    map hash_map = [()];
    DataStructure data_structure = {};
    EnumratorType enumerator_type = FIRST_VALUE;
}

枚举器初始化

// Syntax – type name = value;
// OR Null Syntax – type name = null;
EnumeratorType enumerator_type = FIRST_VALUE;

原始类型初始化(带类型)

bool    boolean_type      = true;
char8   character_8bit    = 's';
float32 float_32bit       = 1.0;
uint8   unsigned_integer_8bit = 1;
str8    string_8bit       = "string";

一维数组初始化

// Syntax – type(length) name = [values]
// OR Unknown Size Syntax – type(?) name = [values]
// OR Null Syntax – type(length) name = [null]
array(5) one_dimensional_array = [
    1,
    2,
    3,
    4,
    5
]

二维数组初始化

// Syntax – type(length:width) name = [values]
// OR Unknown Size Syntax – type(?:?) name = [values]
// OR Null Syntax – type(length:width) name = [null]
array(3:2) two_dimensional_array = [
    1,
    2,
    3,
    4,
    5,
    6
]

哈希映射初始化

// 语法 – type name = [(key|value)]
// 或 空语法 – type name = [(null|null)]
map hash_map = [
    (1|"string"),
    (2|"string"),
    (3|"string"),
    (4|"string"),
    (5|"string")
]
Back to Blog

相关文章

阅读更多 »

其他数据结构

元组 tuple - 用圆括号把多个值括起来表示 - 与列表相同但不可更改,不能添加、删除、修改 字典 dict - 键和值成对出现的数据结构 - 当键比顺序更重要时使用 - 用大括号 {} 表示 使用 python 的 .get 方法可以在键不存在时返回默认值。

什么是类

类是一种用户自定义的结构,用于将数据和函数组合在一起。通过创建实例对象,可以访问和使用这些成员。它表示……