# Trad Language Design Document
> This document is not complete, we are welcome you to improve it.
## Table of Contents
- Base
1. [ClassDeclaration](#ClassDeclaration)
- LCUI Extensions
1. [VariableDeclaration](#VariableDeclaration)
1. [BinaryExpression](#BinaryExpression)
1. [JSX](#JSX)
1. [Widget](#Widget)
1. [WidgetStyle](#WidgetStyle)
## 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:**
``` jsx
// input
class Foo {
constructor() {
this.text = "hello, world!"
}
bar() {
printf(this.text)
}
}
```
``` c
// 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:**
``` jsx
// input
class Foo {
constructor() {
this.text = "hello, world!"
}
bar() {
printf(this.text)
}
}
export Foo
```
``` c
// output .h
typedef struct FooRec_* Foo;
void Foo_Bar(Foo _this):
Foo Foo_New();
void Foo_Delete(Foo _this);
```
``` c
// 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:**
``` jsx
// input
const a = 1
```
``` c
// output
LCUI_ObjectRec a;
Number_Init(&a, 1);
Object_Destroy(&a);
```
## BinaryExpression
**Number operation:**
``` jsx
// input
const a = 1
const b = a * 200
```
``` c
// 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:**
``` jsx
// input
const a = 'hello,'
const b = a + ' world!'
```
``` c
// 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:**
``` jsx
// input
const a = 1
const b = a + 2 + 'str' + 3
```
```c
// 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:**
``` jsx
// input
// Widget widget
widget.style.width = '100px'
widget.style.display = 'none'
widget.style.zIndex = 100
widget.style.backgroundColor = '#fff'
```
``` c
// 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:**
``` jsx
// input
const zIndex = 100
widget.style.zIndex = zIndex
```
``` c
// 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);
```
没有合适的资源?快使用搜索试试~ 我知道了~
trad-master.zip
共80个文件
js:35个
md:18个
json:9个
0 下载量 74 浏览量
2023-01-09
17:42:58
上传
评论
收藏 701KB ZIP 举报
温馨提示
基于 JavaScript 语法的编程语言,能够编译为 C 代码,为 LCUI 提供了类似于 React 的声明式 UI 开发体验。
资源推荐
资源详情
资源评论
收起资源包目录
trad-master.zip (80个子文件)
trad-master
CONTRIBUTING.zh-cn.md 4KB
.vscode
settings.json 639B
launch.json 1KB
.commitlintrc.json 182B
.travis.yml 693B
.github
ISSUE_TEMPLATE
cn_feature_request.md 1KB
en_feature_request.md 1KB
cn_bug_report.md 640B
en_bug_report.md 687B
workflows
nodejs.yml 893B
CODE_OF_CONDUCT.zh-cn.md 3KB
LICENSE 1KB
CONTRIBUTING.md 2KB
docs
design
README.md 5KB
README.md 51B
package.json 1KB
bin
tradc 58B
package-lock.json 93KB
CODE_OF_CONDUCT.md 3KB
.eslintrc.json 541B
test
test.js 84B
.gitignore 211B
images
example.gif 646KB
example
include
config.h.in 0B
config.h 0B
CMakeLists.txt 766B
src
app.jsx.c 12KB
components
progress.jsx.c 8KB
CMakeLists.txt 67B
progress.jsx.h 310B
progress.jsx.json 3KB
progress.css 122B
progress.jsx 965B
app.css 484B
app.jsx 1KB
app.jsx.h 103B
app.jsx.json 215B
packages
trad-utils
index.js 465B
README.md 38B
trad-compiler
src
json.js 3KB
super.js 505B
literal.js 438B
declaration.js 2KB
identifier.js 281B
class.js 3KB
function.js 885B
export.js 715B
expression.js 4KB
logger.js 1KB
parser.js 231B
statement.js 2KB
import.js 9KB
index.js 7KB
README.md 42B
acorn-trad
README.md 47B
trad-ports
lcui.json 2KB
index.js 68B
README.md 78B
trad
index.js 33KB
README.md 63B
trad-lcui
src
computed.js 3KB
event.js 4KB
css.js 1KB
props.js 6KB
types.js 12KB
lib.js 794B
app.js 4KB
helper.js 3KB
functions.js 4KB
widget.js 6KB
base.js 2KB
state.js 5KB
jsx.js 9KB
index.js 786B
README.md 91B
trad-cli
tradc.js 431B
index.js 566B
README.md 45B
trad-css-loader
index.js 1KB
README.md 60B
共 80 条
- 1
资源评论
m0_72731342
- 粉丝: 2
- 资源: 1832
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功