GithubHelp home page GithubHelp logo

young_engine's Introduction

YoungEngine

本项目实现了一个简单的规则引擎。

  • 引擎自定义了一套词法、语法。
  • 在自定义词法语法的基础上实现了一个典型的编译器前端,能够生成表达式对应的抽象语法树。
  • 基于编译构建的抽象语法树实现了go版本的虚拟机。通过注入参数可以获得执行结果。

词法

引擎支持指定的运算符和数据类型

运算符

  • 一元计算符 : ! - +
  • 二元计算符 : + - / * %
  • 二元比较符 : > >= < <= == !=
  • 逻辑操作符 : || &&
  • 括号 : ( )

数据类型

  • 字符串 "abc" 'def'
  • 十进制int 123
  • 十进制float 123.4
  • bool true
  • 变量 id

表达式词法

  • 表达式以换行结束、不支持多行表达式。形如a + 7 > 100
  • 支持字面量 (上述数据类型的常量)、变量和运算符(上述运算符)
  • 变量:由字母数字下划线构成且必须以字母开头,形如:_idfoo
  • 关键字:系统内置部分关键字
    • true: bool类型常量
    • false: bool类型常量

语法

支持简单的表达式语法

  • 一元运算: !true
  • 二元运算: a + b > c
  • 逻辑运算: a || b == 100
  • 括号: (a + b) * c

运算符的优先级

优先级 运算符
0 or
1 &&
2 ! - +
3 > >= < <= == !=
4 + -
5 * /

项目结构

.
├── README.md
├── compiler.go
├── compiler_test.go
├── compiler
│   ├── lexical.go 
│   ├── parser.go   # 语法分析
│   ├── parser_test.go
│   ├── planner.go  # 构建语法树
│   ├── scanner.go  # 词法分析
│   └── scanner_test.go
├── executor
│   ├── ast.go      # 抽象语法树定义
│   ├── operator.go # 语法树执行
│   ├── svg.go      # 可视化打印语法树 - 辅助工具
│   ├── symbol.go   # 符号定义
│   ├── type.go     # 类型定义
│   └── type_checker.go # 类型检查
└── token
    ├── kind.go      # token类型
    ├── kind_test.go
    ├── lexer.go     # 词法定义
    └── token.go     # token定义

项目运行

启动DB

docker-compose up

运行项目

go run ./main.go

young_engine's People

Contributors

qimengxingyuan avatar zztaki avatar

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.