Trad 语言设计文档 - 文章教程

Trad 语言设计文档

发布于 2021-10-13 字数 6158 浏览 1006 评论 0

This document is not complete, we are welcome you to improve it.

ClassDeclaration

The translation rules are as follows:

  • The naming format of the output class method is {Class}_{Method}
  • The first letter of each word of the class method name is uppercase
  • Default output new() and delete() functions for wrapping constructor() and destructor() functions
  • If this class is not exported, it’s methods are always defined as static
  • constructor() and destructor() are always defined as static
  • the stdlib.h file is included by default, Because free() and malloc() are used.

Basic usage:

// input
class Foo {
    constructor() {
        this.text = "hello, world!"
    }

    bar() {
        printf(this.text)
    }
}
// output .c
#include <stdlib.h>

typedef struct FooRec_ FooRec;
typedef struct FooRec_* Foo;

struct {
    char *text;
} FooRec_;

static void Foo_Contructor(Foo _this)
{
    _this->text = "hello, world!";
}

static void Foo_Destructor(Foo _this)
{
}

static void Foo_Bar(Foo _this)
{
    printf(_this.text);
}

static Foo Foo_New()
{
    Foo _this = malloc(sizeof(FooRec));

    if (_this == NULL) {
        return NULL;
    }
    Foo_Contructor(_this);
    return _this;
}

static void Foo_Delete(Foo _this)
{
    Foo_Destructor(_this);
    free(_this);
}

Export A Class:

// input
class Foo {
    constructor() {
        this.text = "hello, world!"
    }

    bar() {
        printf(this.text)
    }
}

export Foo
// output .h
typedef struct FooRec_* Foo;

void Foo_Bar(Foo _this):

Foo Foo_New();

void Foo_Delete(Foo _this);
// output .c
#include <stdlib.h>
#include "foo.h"

typedef struct FooRec_ FooRec;

struct {
    char *text;
} FooRec_;

static void Foo_Contructor(Foo _this)
{
    _this->text = "hello, world!";
}

static void Foo_Destructor(Foo _this)
{
}

void Foo_Bar(Foo _this)
{
    printf(_this.text);
}

Foo Foo_New()
{
    Foo _this = malloc(sizeof(FooRec));

    if (_this == NULL) {
        return NULL;
    }
    Foo_Contructor(_this);
    return _this;
}

void Foo_Delete(Foo _this)
{
    Foo_Destructor(_this);
    free(_this);
}

VariableDeclaration

Basic usage:

// input
const a = 1
// output
LCUI_ObjectRec a;

Number_Init(&a, 1);
Object_Destroy(&a);

BinaryExpression

Number operation:

// input
const a = 1
const b = a * 200
// output
LCUI_ObjectRec a;
LCUI_ObjectRec b;
LCUI_ObjectRec _num_1;
LCUI_Object number_2;

Number_Init(&a, 1);
Number_Init(&b, 0);
Number_Init(&_num_1, 200);
_num_2 = Object_Operate(a, "*", &_num_1);
Object_Destroy(&a);
Object_Destroy(&b);
Object_Destroy(&_1);
Object_Delete(number_2);

String operation:

// input
const a = 'hello,'
const b = a + ' world!'
// output
LCUI_ObjectRec a;
LCUI_ObjectRec b;
LCUI_ObjectRec _str_1;
LCUI_Object _str_2;

String_Init(&a, "hello");
String_Init(&b, NULL);
String_Init(&_str_1, " world!");
_str_2 = Object_Operate(a, "*", &_str_1);
Object_Destroy(&a);
Object_Destroy(&b);
Object_Destroy(&_str_1);
Object_Delete(_str_2);

Implicit type conversion:

// input
const a = 1
const b = a + 2 + 'str' + 3
// output
LCUI_ObjectRec a;
LCUI_ObjectRec b;
LCUI_ObjectRec _num_1;
LCUI_Object _num_2;
LCUI_ObjectRec _str_1;
LCUI_Object _str_2;
LCUI_Object _str_3;
LCUI_ObjectRec _num_3;
LCUI_Object _str_4;
LCUI_Object _str_5;

// a = 1
Number_Init(&a, 1);
// 2
Number_Init(&_num_1, 2);
// a + 2
_num_2 = Object_Operate(&a, "+", _num_1);
// 'str'
String_Init(_str_1, "str");
// (a + 2).toString()
_str_2 = Object_ToString(_num_2);
// (a + 2).toString() + 'str'
_str_3 = Object_Operate(_str_2, "+", _str_1);
// 3
Number_Init(&_num_3, 3);
// 3.toString()
_str_4 = Object_ToString(&number_3);
// (a + 2).toString() + 'str' + 3.ToString()
_str_5 = Object_Operate(_str_3, "+", _str_4);
// b
String_Init(&b);
// b = (a + 2).toString() + 'str' + 3.ToString()
Object_Operate(&b, "=", _str_5);
Object_Destroy(&a);
Object_Destroy(&b);
Object_Destroy(&_num_1);
Object_Delete(_num_2);
Object_Destroy(&_num_3);
Object_Destroy(&_str_1);
Object_Delete(_str_2);
Object_Delete(_str_3);
Object_Delete(_str_4);
Object_Delete(_str_5);

JSX

Widget

WidgetStyle

Literal assignment:

// input
// Widget widget
widget.style.width = '100px'
widget.style.display = 'none'
widget.style.zIndex = 100
widget.style.backgroundColor = '#fff'
// output
Widget_SetStyleString(widget, key_width, "100px");
Widget_SetStyleString(widget, key_display, "none");
Widget_SetStyleString(widget, key_z_index, "100");
Widget_SetStyleString(widget, key_background_color, "#fff");

Object assignment:

// input
const zIndex = 100
widget.style.zIndex = zIndex
// input
LCUI_ObjectRec zIndex;
LCUI_Object _str_zIndex;

Number_Init(&zIndex, 100);
_str_zIndex = Object_ToString(&zIndex);
Widget_SetStyleString(widget, key_z_index, _str_zIndex->value.string);
Object_Destroy(&zIndex);
Object_Delete(_str_zIndex);

如果你对这篇文章有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!

关于作者

JSmiles

生命进入颠沛而奔忙的本质状态,并将以不断告别和相遇的陈旧方式继续下去。

2482 文章
30 评论
71647 人气
更多

推荐作者

坏尐絯

文章 0 评论 1005

qq_q3iGo

文章 0 评论 0

佚名

文章 0 评论 0

ternence_li

文章 0 评论 0

离去的眼神

文章 0 评论 983