- 计划包含内容:
- 类c语言的编译器前端,但生成自定义的汇编指令
- 类c源码-> 抽象语法树 -> ASM
- 汇编解释器
- 类c语言的编译器前端,但生成自定义的汇编指令
- 语法定义
- 效果应当类似于动态语言的jit解释执行器
- 编译生成汇编:
- 词法分析(√)
- 语法分析(√)
- 语义分析(√)
- 汇编生成(...)
- 汇编解释器:
- 寄存器与内存分段(√)
- 自定义指令集(√)
- VM/指令执行(...)
- 输入源代码:
- 包含变量声明、枚举声明、函数声明、表达式计算、注释等
int a = 10086;
int b = 0;
int c = 0xf7A0;
int d = 07650;
int* f = &d;
String str = "qiqi"; // String* str = &"qiqi"; 不允许对字面量取地址
int test(int& a, int** &b) {
return (a + b);
}
enum Hello_e {
A1,
B2,
CC,
// A1,
};
int main(char** args, int size) {
d += b;
c -= b;
d /= b;
c *= b;
a ??= b ?? c;
const int ret = test(1, 2);
// ret = 123; // 限制 const 不可变
test(3, test(1, 5));
test(ret, 4, ); // 允许尾部多余的 ,
// test(3,); // 检查函数参数个数
// test();
// test_undef(); // 检查未定义符号
if (a == b || (b == c && a == c)) {
d = b;
}
// int ok = *c; // 检查 读址 操作数类型为指针
String ch = 'b';
// disable=124
bool k = false;
// disable = fdsa
bool g = false;
/*disable=uu*/
/*ll
disable=bbc*/
String s = "sss";
s = "adsf 123";
return 0;
}
- 编译,词法/语法/语义分析后得到AST树:
├──┐
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ int
│ │ │ └──[值类型]
│ │ └──id -------- a
│ ├──operator -- =
│ └──┐
│ └──number ---- 10086
├──┐
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ int
│ │ │ └──[值类型]
│ │ └──id -------- b
│ ├──operator -- =
│ └──┐
│ └──number ---- 0
├──┐
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ int
│ │ │ └──[值类型]
│ │ └──id -------- c
│ ├──operator -- =
│ └──┐
│ └──number ---- 63392
├──┐
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ int
│ │ │ └──[值类型]
│ │ └──id -------- d
│ ├──operator -- =
│ └──┐
│ └──number ---- 4008
├──┐
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ int
│ │ │ └──*
│ │ └──id -------- f
│ ├──operator -- =
│ └──┐
│ └──┐
│ ├──operator -- &
│ └──id -------- d
├──┐
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ String
│ │ │ └──[值类型]
│ │ └──id -------- str
│ ├──operator -- =
│ └──┐
│ └──string ---- qiqi
├──┐
│ ├──User-Function
│ ├──┐
│ │ ├──type ------ int
│ │ └──[值类型]
│ ├──id -------- test
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ int
│ │ │ └── &
│ │ └──id -------- a
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ int
│ │ │ └──** &
│ │ └──id -------- b
│ └──┐
│ └──┐
│ ├──return
│ └──┐
│ └──┐
│ └──┐
│ ├──operator -- +
│ ├──id -------- b
│ └──id -------- a
├──┐
│ ├──Enum
│ ├──id -------- Hello_e
│ ├──┐
│ │ ├──┐
│ │ │ ├──┐
│ │ │ │ └──[值类型]
│ │ │ └──id -------- A1
│ │ ├──operator -- =
│ │ └──┐
│ │ └──number ---- 0
│ ├──┐
│ │ ├──┐
│ │ │ ├──┐
│ │ │ │ └──[值类型]
│ │ │ └──id -------- B2
│ │ ├──operator -- =
│ │ └──┐
│ │ └──number ---- 1
│ └──┐
│ ├──┐
│ │ ├──┐
│ │ │ └──[值类型]
│ │ └──id -------- CC
│ ├──operator -- =
│ └──┐
│ └──number ---- 2
└──┐
├──User-Function
├──┐
│ ├──type ------ int
│ └──[值类型]
├──id -------- main
├──┐
│ ├──┐
│ │ ├──id -------- char
│ │ └──**
│ └──id -------- args
├──┐
│ ├──┐
│ │ ├──type ------ int
│ │ └──[值类型]
│ └──id -------- size
└──┐
├──┐
│ └──┐
│ ├──operator -- +=
│ ├──id -------- b
│ └──id -------- d
├──┐
│ └──┐
│ ├──operator -- -=
│ ├──id -------- b
│ └──id -------- c
├──┐
│ └──┐
│ ├──operator -- /=
│ ├──id -------- b
│ └──id -------- d
├──┐
│ └──┐
│ ├──operator -- *=
│ ├──id -------- b
│ └──id -------- c
├──┐
│ └──┐
│ ├──operator -- ??=
│ ├──┐
│ │ ├──operator -- ??
│ │ ├──id -------- c
│ │ └──id -------- b
│ └──id -------- a
├──┐
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ int
│ │ │ └──[值类型]
│ │ └──id -------- ret
│ ├──operator -- =
│ └──┐
│ └──┐
│ ├──id -------- test
│ ├──┐
│ │ └──number ---- 1
│ └──┐
│ └──number ---- 2
├──┐
│ ├──id -------- test
│ ├──┐
│ │ └──number ---- 3
│ └──┐
│ └──┐
│ ├──id -------- test
│ ├──┐
│ │ └──number ---- 1
│ └──┐
│ └──number ---- 5
├──┐
│ ├──id -------- test
│ ├──┐
│ │ └──id -------- ret
│ └──┐
│ └──number ---- 4
├──┐
│ ├──if
│ └──┐
│ ├──┐
│ │ └──┐
│ │ ├──operator -- ||
│ │ ├──┐
│ │ │ └──┐
│ │ │ ├──operator -- &&
│ │ │ ├──┐
│ │ │ │ ├──operator -- ==
│ │ │ │ ├──id -------- c
│ │ │ │ └──id -------- a
│ │ │ └──┐
│ │ │ ├──operator -- ==
│ │ │ ├──id -------- c
│ │ │ └──id -------- b
│ │ └──┐
│ │ ├──operator -- ==
│ │ ├──id -------- b
│ │ └──id -------- a
│ └──┐
│ └──┐
│ └──┐
│ ├──operator -- =
│ ├──id -------- b
│ └──id -------- d
├──┐
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ String
│ │ │ └──[值类型]
│ │ └──id -------- ch
│ ├──operator -- =
│ └──┐
│ └──string ---- b
├──┐
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ bool
│ │ │ └──[值类型]
│ │ └──id -------- k
│ ├──operator -- =
│ └──┐
│ └──value ----- false
├──┐
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ bool
│ │ │ └──[值类型]
│ │ └──id -------- g
│ ├──operator -- =
│ └──┐
│ └──value ----- false
├──┐
│ ├──┐
│ │ ├──┐
│ │ │ ├──type ------ String
│ │ │ └──[值类型]
│ │ └──id -------- s
│ ├──operator -- =
│ └──┐
│ └──string ---- sss
├──┐
│ └──┐
│ ├──operator -- =
│ ├──string ---- adsf 123
│ └──id -------- s
└──┐
├──return
└──┐
└──number ---- 0