关于`MiniMoonBit.g4`与`top.mbt`的不一致性的一些澄清与疑惑

MiniMoonBit.g4 描述了minimoonbit的语法,其对应的结构是用top.mbt中的Syntax表示的,但是两者之间并不存在一一对应的关系,下面列出一些映射方法和问题,以便其他实现者有一个有帮助的参考。

  1. top_level无法直接映射到Syntax中
    ANTLR中定义如下:

    prog: top_level* EOF;
    
    // Top-level
    // 
    // Top level declarations should start at the beginning of the line, i.e.
    // token.column == 0. Since this is non-context-free, it is not included in this
    // backend-agnostic ANTLR grammar.
    top_level: top_let_decl | toplevel_fn_decl;
    top_let_decl:
    	'let' IDENTIFIER ':' type '=' expr ';';
    toplevel_fn_decl: (main_fn_decl | top_fn_decl) ';';
    

    比如有以下代码:

    let a: Int = 1;
    let b: Int = 2;
    fn sum(a: Int, b: Int) -> Int { a + b };
    fn main { 3 };
    fn init { 4 };
    

    应该desugar为以下形式以映射到Syntax中:

    let a: Int = 1;
    let b: Int = 1;
    letrec {name: ("sum", Int), args: (a: Int, b: Int)} = {a + b};
    letrec {name: ("main", Unit), args:() } = { 3 }; 
    letrec {name: ("init", Unit), args:() } = { 4 };
    () 
    
  2. Fundef的name参数的第二个Type参数应该填什么类型?
    Fundef定义如下:

    pub struct Fundef {
      name : (String, Type)
      args : Array[(String, Type)]
      body : Syntax
    } derive(Show)
    

    此处name的第二个参数应该填什么?返回值的类型还是函数的类型?

  3. 1-tuple处理
    ANTLR中定义如下:

    tuple_expr: '(' expr (',' expr)* ')'; // (x, y)
    

    假设我们对(1)进行语法分析,输出的结果应该为Int(1)而非Tuple([Int(1)])

  4. 省略的类型标注
    语法允许在一些情况下省略类型注释:

    nontop_fn_decl:
    	'fn' IDENTIFIER '(' nontop_param_list? ')' (
    		'->' type
    	)? fn_body;
    nontop_param_list:
    	nontop_param (',' nontop_param)*;
    nontop_param: IDENTIFIER type_annotation?;
    
    let_tuple_stmt:
    	'let' '(' IDENTIFIER (',' IDENTIFIER)* ')' type_annotation? '=' expr ';'
    		stmt;
    let_stmt:
    	'let' IDENTIFIER type_annotation? '=' expr ';' stmt;
    type_annotation: COLON type;
    

    然而,在Syntax对象中,类型无法省略,是否应该标记为Var({ val: None})?