Trad 语言设计文档
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()
anddelete()
functions for wrappingconstructor()
anddestructor()
functions - If this class is not exported, it's methods are always defined as static
constructor()
anddestructor()
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论