Git Product home page Git Product logo

tolangc's Introduction

tolangc: tolang compiler

tolang 指 toy lang,是一款用于教学目的的简单编程语言。tolangc 即 tolang compiler,是 tolang 语言的样例编译器,可以为编译技术的学习者提供对编译器架构的初步认识。

tolangc 介绍

  • 使用 C++ 编写
  • 以 LLVM 作为中间代码形式

文法定义

tolang 语言(v1)的文法由下面的 EBNF 范式确定:

CompUnit: {FuncDef} {VarDecl} {Stmt}

FuncDef: 'fn' Ident '(' [FuncFParams] ')' '=>' Exp ';'

FuncFParams: Ident { ',' Ident }

VarDecl: 'var' Ident ';'

Stmt:
    'get' Ident ';'
    | 'put' Exp ';'
    | 'tag' Ident ';'
    | 'let' Ident '=' Exp ';'
    | 'if' Cond 'to' Ident ';'
    | 'to' Ident ';'

Exp: AddExp

AddExp:
    MulExp
    | AddExp ('+' | '-') MulExp

MulExp:
    UnaryExp
    | MulExp ('*' | '/' | '%') UnaryExp

UnaryExp:
    PrimaryExp
    | Ident '(' [FuncRParams] ')'
    | ('+' | '-') UnaryExp

PrimaryExp:
    '(' Exp ')'
    | Ident
    | Number

FuncRParams: Exp { ',' Exp }

Cond:
    Exp ('<' | '>' | '<=' | '>=' | '==' | '!=') Exp

其中

Ident: [a-zA-Z_][0-9a-zA-Z_]*
Number: (0|[1-9][0-9]*)(\.[0-9]+)?

项目维护

CONTRIBUTING.md

Todo

  • 编译器前端

    • 词法分析
    • 语法树数据结构
    • 语法分析
    • 符号表
    • 中间代码数据结构
    • 中间代码构建接口
    • 语义分析
    • 中间代码输出
  • 编译器后端

    • 寄存器分配
    • 目标代码生成

tolangc's People

Contributors

wokron avatar hhhesc avatar bohaocheung avatar lord-turmoil avatar yangyzzzz avatar

Watchers

 avatar

tolangc's Issues

实现 tolangc 的符号表

实现 tolangc 的符号表。虽然 tolang 十分简单,但应当尽可能考虑符号表的可扩展性,包括对子程序的支持以及类型系统

该模块应在项目最开始完成开发。

tolangc 中间代码生成:结构缺失

缺少 label 标识实现(没有 LabelTy 相关类别,没有LabelInstruction子类):
缺少 br 语句实现(BranchInstTy没有被分配,没有BranchInstruction子类):

相关语法结构:
'tag' Ident ';'
'if' Cond 'to' Ident ';' | 'to' Ident ';'

实现 tolangc 的词法分析

实现 tolangc 的词法分析模块,从输入流中读取字符并进行分词。需要完成 Token 定义和词法分析流程两部分内容。

该模块应在项目最开始完成开发。

实现 tolang 的中间代码输出

实现 tolang 的中间代码输出模块。该模块用于将中间代码数据结构输出为文本形式,以便与 LLVM 后端对接。(当然,后续也应当编写 tolangc 自身的后端。)

该模块应在定义中间代码数据结构完成后进行开发。

实现 tolangc 的语义分析

实现 tolangc 的语义分析模块,读入语法树并使用访问者模式遍历语法树,通过中间代码构建接口完成语法树到中间代码的翻译过程。在翻译过程中还需要符号表的支持。

该模块应在语法树数据结构符号表以及中间代码构建接⼝完成后进行开发。

实现 tolangc 的语法分析

实现 tolangc 的语法分析模块,逐个读入 Token 序列并使用递归子程序法实现 tolang 文法的解析。在解析的过程中完成语法树的构建。

该模块应在词法分析语法树数据结构完成后进行开发。

定义 tolangc 的语法树数据结构

定义 tolangc 的语法树数据结构。需要考虑:

  • 使用统一节点类型或区分不同节点
  • 使用类继承、enum+union 还是 std::variant 实现
  • 使用严格的树状结构或可以使用 std::vector 等容器

等等问题。

该模块应在项目最开始完成开发。

修正词法分析并补充测试

词法分析部分存在似乎存在 bug,同时 Token 没有 COMMA 类型,应当进行修正。修改之后需要补充词法分析部分的单元测试。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.