Go to file
2025-06-19 09:44:54 +08:00
lexical Merge branch 'main' of git.gangary.cn:gary/Hydrogen-python 2025-06-12 13:49:33 +08:00
Optimizer 修改优化代码 2025-06-19 09:42:46 +08:00
semantic 修改UI 2025-06-12 15:14:49 +08:00
syntax 修改UI 2025-06-12 15:14:49 +08:00
.gitignore 添加expression语义 2025-06-12 00:05:54 +08:00
error.py 初始化仓库 2025-06-11 19:51:42 +08:00
main.py fix bug 2025-06-19 09:44:54 +08:00
README.md 修改UI 2025-06-12 15:14:49 +08:00
test0.hy 修改优化代码 2025-06-19 09:42:46 +08:00
test1.hy 新添测试文件 2025-06-12 14:52:25 +08:00
test2.hy 新添测试文件 2025-06-12 14:52:25 +08:00
test3.hy 新添测试文件 2025-06-12 14:52:25 +08:00
test4.hy 修改优化代码 2025-06-19 09:42:46 +08:00
test5.hy 新添测试文件 2025-06-12 14:52:25 +08:00
test6.hy 新添测试文件 2025-06-12 14:52:25 +08:00
test7.hy 新添测试文件 2025-06-12 14:52:25 +08:00
test8.hy 修改优化代码 2025-06-19 09:42:46 +08:00
test.py 优化了syntax的LL1Generator对象 , 加入debug功能, 加入导出excel表功能 2025-06-11 21:06:40 +08:00
tk_ui.py 修改优化代码 2025-06-19 09:42:46 +08:00

Hydrogen 语言大纲

[toc]

1. 语法文档

1.1 变量

变量声明

i32 f1; // 整型字变量默认为i32,支持自动类型转换
f32 f2;

i32[5] f3; // 数组声明
f32[93] f4;

支持的数值类型

实数类型 数组类型
i32 i32[num-int]
f32 f32[num-int]

1.2 注释

单行注释

单行注释使用 // 开头,后面跟随注释内容。例如:

// 这是一个单行注释

多行注释

多行注释使用 /* 开头,并以 */ 结束。例如:

/*
这是一个多行注释
可以跨越多行
*/

1.3 控制结构

if 语法

if condition {
    // 当 condition 为 true 时执行的代码
} else {
    // 当 condition 为 false 时执行的代码
}

condition 的类型必须是 bool

可以使用 else 语法来检查多个条件,示例:

fn main() {
    i32 type;
    i32 foo;

    age = 32;
 
    if foo >= 60 {
        a = 0;
        
    } else {
        if foo >= 18 {
            a = 1;
        } else {
            a = 2;
        }
    }
}

While 语法

while 语句用于循环执行代码块。Nature 语言中的 for 语句有三种主要形式:经典循环、条件循环。

  • 经典循环

经典循环用于执行固定次数的循环。基本语法如下:

fn main() {
    i32 a;
    i32 sum;
    a = 0;
    sum = 0;
    while a <= 100 {
        sum = sum + a;
        a = a + 1;
    }
}

在这个示例中,循环从 i = 1 开始,每次循环 i 增加 1直到 i 大于 100。

  • 1.5 算术运算符
优先级 关键字 使用示例 说明
1 () (1 + 1) (expr)
2 / 1 / 2
2 * 1 * 2
2 % 5 % 2 余数
3 + 1 + 1
3 - 1 - 1
4 > 1 > 2 大于
4 >= 1 >= 2 大于等于
4 < 1 < 2 小于
4 <= 1 <= 2 小于等于
4 == 1 == 2 等于
4 != 1 != 2 不等于

1.4 函数

函数声明语法如下

fn 函数名(参数名:参数类型,....) -> 返回类型 {
	...
}

注意: -> 返回类型可以省略省略后默认返回void

例子:

fn main() {

    return 0;
}

2. 实现细节

2.1 符号表

全局变量表

Name Type Width Offset
g_a i32 4 0

函数表

Function Name Return Type Local Var Table
main void main

局部变量表

表名称main

Name Type Width Offset Is Param
test i32 4 0 True
a i32[] 128 4 False

2.2 四元式

  1. 函数定义
    • 表示函数入口,例如:(fun, _, _, main)(定义主函数)。
  2. 赋值操作
    • 将常量或变量赋给临时变量或目标变量,例如:
      • (=, 32, _, _v0)(常量赋值)
      • (=, _v0, _, age)(变量赋值)
      • (=, 0, _, _v18)(常量赋值给临时变量)。
  3. 比较操作
    • 包括大于等于 (>=) 和小于等于 (<=),生成布尔结果,例如:
      • (>=, foo, _v1, _v2)
      • (<=, foo, _v10, _v11)
  4. 条件跳转
    • 根据条件判断跳转到指定基本块,例如:(if, _v3, goto, __b3)。
  5. 无条件跳转
    • 直接跳转到指定基本块,例如:(goto, _, _, __b5)。
  6. 基本块标记
    • 定义控制流中的基本块,例如:(block, _, _, __b3)。
  7. 算术操作
    • 执行加法运算,例如:(+, age, a, _v13)。
  8. 返回操作
    • 表示函数返回特定值,例如:(return, _, _, _v18)(返回临时变量 _v18 的值)。
  9. 函数调用
    • 表示调用函数,例如:(call, product, 0, _) product是函数名0是参数个数

2.3 添加语义动作的文法推导式

# 0
Production('program', ['define-lists'],
           'Program0S', [None], 'Program0E'),
# 1
Production('define-lists', ['define-list', 'define-lists'],
           None, [None, None], 'DefineLists1E'),
# 2
Production('define-lists', [],
           None, [], 'DefineLists2E'),
# 3
Production('define-list', ['fun-define'],
           None, [None], 'DefineList3E',
           ),
# 4
Production('define-list', ['global-var-define'],
           None, [None], 'DefineList4E',
           ),
# 5
Production('fun-define',
           ['fn', 'id', 'left-parentheses', 'params', 'right-parentheses', 'fun-return', 'left-brace', 'code-block',
            'right-brace'],
           None, [None, None, None, 'FunDefine5C3', None, 'FunDefine5C5', None, 'FunDefine5C7', None], 'FunDefine5E',
           ),
# 6
Production('params', [],
           'Params6S', [], None,
           ),
# 7
Production('params', ['paramlist'],
           'Params7S', ['Params7C0'], None,
           ),
# 8
Production('paramlist', ['param', 'param-follow'],
           None, ['ParamList8C0', 'ParamList8C1'], None,
           ),
# 9
Production('param-follow', ['comma', 'param', 'param-follow'],
           None, [None, 'ParamFollow9C1', 'ParamFollow9C2'], None,
           ),
# 10
Production('param-follow', [],
           None, [], None,
           ),
# 11
Production('param', ['id', 'colon', 'type', 'arraylist'],
           None, [None, None, None, None], 'Param11E',
           ),
# 12
Production('arraylist', [],
           'ArrayList12S', [], None,
           ),
# 13
Production('arraylist', ['left-bracket', 'right-bracket'],
           'ArrayList13S', [None, None], None,
           ),
# 14
Production('fun-return', [],
           'FunReturn14S', [], None,
           ),
# 15
Production('fun-return', ['right-arrow', 'type', 'arraylist'],
           None, [None, None, None], 'FunReturn15E',
           ),
# 16
Production('type', ['i32'],
           'Type16S', [None], None,
           ),
# 17
Production('type', ['f32'],
           'Type17S', [None], None,
           ),
# 18
Production('type', ['void'],
           "Type18S", [None], None,
           ),
# 19
Production('global-var-define', ['type', 'arraylist-with-num', 'id', 'global-var-define-follow'],
           None, [None, None, None, 'GlobalVarDefine19C3'], 'GlobalVarDefine19E',
           ),
# 20
Production('global-var-define-follow', ['comma', 'id', 'global-var-define-follow'],
           None, [None, None, 'GlobalVarDefineFllow20C2'], 'GlobalVarDefineFllow20E',
           ),
# 21
Production('global-var-define-follow', ['semicolon'],
           None, [None], None,
           ),
# 22
Production('code-block', ['code-list'],
           None, ['CodeBlock22C0'], 'CodeBlock22E',
           ),
# 23
Production('code-list', ['code', 'code-list'],
           None, ["CodeList23C0", 'CodeList23C1'], 'CodeList23E',
           ),
# 24
Production('code-list', [],
           None, [], 'CodeList24E',
           ),
# 25
Production('code', ['local-define'],
           None, ['Code25C0'], 'Code25E',
           ),
# 26
Production('code', ['normal-statement'],
           None, ['Code26C0'], 'Code26E',
           ),
# 27
Production('code', ['selection-statement'],
           None, ['Code27C0'], 'Code27E',
           ),
# 28
Production('code', ['iteration-statement'],
           None, ['Code28C0'], 'Code28E',
           ),
# 29
Production('code', ['return-statement'],
           None, ['Code29C0'], 'Code29E',
           ),
# 30
Production('local-define', ['type', 'arraylist-with-num', 'id', 'local-define-follow'],
           None, ['LocalDefine30C0', "LocalDefine30C1", 'LocalDefine30C2', 'LocalDefine30C3'], 'LocalDefine30E',
           ),
# 31
Production('local-define-follow', ['comma', 'id', 'local-define-follow'],
           None, [None, 'LocalDefineFollow31C1', 'LocalDefineFollow31C2'], 'LocalDefineFollow31E',
           ),
# 32
Production('local-define-follow', ['semicolon'],
           None, [None], None,
           ),
# 33
Production('normal-statement', ['semicolon'],
           None, [None], None,
           ),
# 34
Production('normal-statement', ['id', 'normal-statement-follow'],
           None, [None, 'NormalStatement34C1'], 'NormalStatement34E',
           ),
# 35
Production('normal-statement-follow', ['var-follow', 'evaluate', 'exp', 'semicolon'],
           None, ['NormalStatementFollow35C0', None, 'NormalStatementFollow35C2', None], 'NormalStatement35E',
           ),
# 36
Production('normal-statement-follow', ['call-follow', 'semicolon'],
           None, ['NormalStatementFollow36C0', None], 'NormalStatementFollow36E',
           ),
# 37
Production('var-follow', [],
           None, [], 'VarFollow37E',
           ),
# 38
Production('var-follow', ['left-bracket', 'exp', 'right-bracket'],
           None, [None, None, None], 'VarFollow38E',
           ),
# 39
Production('call-follow', ['left-parentheses', 'call-params', 'right-parentheses'],
           None, [None, 'CallFollow39C1', None], 'CallFollow39E',
           ),
# 40
Production('call-params', ['call-param-list'],
           None, ['CallParams40C0'], 'CallParams40E',
           ),
# 41
Production('call-params', [],
           None, [], 'CallParams41E',
           ),
# 42
Production('call-param-list', ['exp', 'call-param-follow'],
           None, ['CallParamList42C0', None], 'CallParamList42E',
           ),
# 43
Production('call-param-follow', ['comma', 'exp', 'call-param-follow'],
           None, [None, 'CallParamFollow43C1', None], 'CallParamFollow43E',
           ),
# 44
Production('call-param-follow', [],
           None, [], 'CallParamFollow44E',
           ),

# 45
Production('selection-statement', ['if', 'exp', 'left-brace', 'code-list', 'right-brace', 'selection-follow'],
           None, [None, 'SelectionStatement45C1', None, 'SelectionStatement45C3', None, 'SelectionStatement45C5'], 'SelectionStatement45E',
           ),
# 46
Production('selection-follow', ['else', 'left-brace', 'code-list', 'right-brace'],
           None, [None, None, 'SelectionFollow46C2', None], 'SelectionFollow46E',
           ),
# 47
Production('selection-follow', [],
           None, [], 'SelectionFollow47E',
           ),
# 48
Production('iteration-statement', ['while', 'exp', 'left-brace', 'code-list', 'right-brace'],
           None, [None, "IterationStatement48C1", None, 'IterationStatement48C3', None], 'IterationStatement48E',
           ),
# 49
Production('return-statement', ['return', 'return-follow'],
           None, [None, 'ReturnStatement49C1'], "ReturnStatement49E",
           ),
# 50
Production('return-follow', ['semicolon'],
           None, [None], "ReturnFollow50E",
           ),
# 51
Production('return-follow', ['exp', 'semicolon'],
           None, ['ReturnFollow51C0', None], 'ReturnFollow51E',
           ),
# 52
Production('exp', ['additive-expr', 'exp-follow'],
           None, ['Exp52C0', 'Exp52C1'], 'Exp52E'),
# 53
Production('exp-follow', ['rel-op', 'additive-expr'],
           None, [None, 'ExpFollow53C1'], 'ExpFollow53E'),
# 54
Production('exp-follow', [],
           None, [], 'ExpFollow54E'),
# 55
Production('rel-op', ['smaller-equal'],
           None, [None], 'RelOp55E'),
# 56
Production('rel-op', ['smaller'],
           None, [None], 'RelOp56E'),
# 57
Production('rel-op', ['bigger'],
           None, [None], 'RelOp57E'),
# 58
Production('rel-op', ['bigger-equal'],
           None, [None], 'RelOp58E'),
# 59
Production('rel-op', ['equal'],
           None, [None], 'RelOp59E'),
# 60
Production('rel-op', ['not-equal'],
           None, [None], 'RelOp60E'),
# 61
Production('additive-expr', ['term', 'additive-expr-follow'],
           None, ['AdditiveExpr61C0', 'AdditiveExpr61C1'], 'AdditiveExpr61E'),
# 62
Production('additive-expr-follow', ['add-op', 'term', 'additive-expr-follow'],
           None, [None, 'AdditiveExprFollow62C1', 'AdditiveExprFollow62C2'], 'AdditiveExprFollow62E'),
# 63
Production('additive-expr-follow', [],
           None, [], 'AdditiveExprFollow63E'),
# 64
Production('add-op', ['addition'],
           None, [None], 'AddOp64E'),
# 65
Production('add-op', ['subtraction'],
           None, [None], 'AddOp65E'),
# 66
Production('term', ['factor', 'term-follow'],
           None, ['Term66C0', 'Term66C1'], 'Term66E'),
# 67
Production('term-follow', ['mul-op', 'factor', 'term-follow'],
           None, [None, 'TermFollow67C1', 'TermFollow67C2'], 'TermFollow67E'),
# 68
Production('term-follow', [],
           None, [], 'TermFollow68E'),
# 69
Production('mul-op', ['multiplication'],
           None, [None], 'MulOp69E'),
# 70
Production('mul-op', ['division'],
           None, [None], 'MulOp70E'),
# 71
Production('factor', ['left-parentheses', 'exp', 'right-parentheses'],
           None, [None, 'Factor71C1', None], 'Factor71E'),
# 72
Production('factor', ['id', 'id-factor-follow'],
           None, [None, 'Factor72C1'], 'Factor72E'),
# 73
Production('factor', ['int-num'],
           None, [None], 'Factor73E'),
# 74
Production('id-factor-follow', ['var-follow'],
           None, [None], 'IdFactorFollow74E'),
# 75
Production('id-factor-follow', ['left-parentheses', 'args', 'right-parentheses'],
           None, [None, 'IdFactorFollow75C1', None], 'IdFactorFollow75E'),
# 76
Production('args', ['arg-list'],
           None, ['Args76C0'], 'Args76E'),
# 77
Production('args', [],
           None, [], 'Args77E'),
# 78
Production('arg-list', ['exp', 'arg-list-follow'],
           None, ['ArgList78C0', 'ArgList78C1'], 'ArgList78E'),
# 79
Production('arg-list-follow', ['comma', 'exp', 'arg-list-follow'],
           None, [None, 'ArgListFollow79C1', 'ArgListFollow79C2'], 'ArgListFollow79E'),
# 80
Production('arg-list-follow', [],
           None, [], 'ArgListFollow80E'),

# 81
Production('arraylist-with-num', [],
           None, [], 'ArrayListWithNum81E',
           ),
# 82
Production('arraylist-with-num', ['left-bracket', 'int-num', 'right-bracket'],
           None, [None, None, None], 'ArrayListWithNum82E',
           ),

# 83
Production('factor', ['float-num'],
           None, [None], 'Factor83E'),

# 84
Production('mul-op', ['yushu'],
           None, [None], 'MulOp84E'),