Giter VIP home page Giter VIP logo

sweet_dsl's Introduction

sweet_dsl

介绍

sweet_dsl是基于go/parse暴露出来的API做的一个简单的规则引擎,简单点说就是一个dsl,其语法规则完全兼容Go,只是对Go的一些不必要的语法做了裁剪。

语法规则

兼容大部分Go语言语法,可以认为是语法不严格的Go语言

数据类型

int、float、string、map、slice

操作符

< > >= <= && || !=

语句

for、for range、if

支持函数

Println、VersionCompare、JsonEncode、JsonDecode

理论上任意函数都可以进行支持

Demo

package test

import (
	"reflect"
	"sweet_dsl/engine"
	"testing"
)

const IfStmtScripts = `
package main

func main() {
	if a == 1 {
		if b == 6.0 {
			return 1
		}

		return 10
	} else {
		return 3
	}

	return 2
}
`

func TestIfStmt(t *testing.T) {
	inputPar := map[string]interface{}{
		"a": 12,
		"b": 6.0,
	}

	e := engine.NewEngin(IfStmtScripts)
	res, _ := e.Run(inputPar)

	if _, ok := res.(int64); !ok {
		t.Error(reflect.TypeOf(res), res)
		return
	}

	if res.(int64) != 3 {
		t.Error(reflect.TypeOf(res), res)
		return
	}
}

说明

如在使用过程中有需要支持的语法可以提issues或者直接联系我

sweet_dsl's People

Contributors

wsx864321 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

James Cloos avatar  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.