Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/engine/runtimev2/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package runtimev2
39 changes: 39 additions & 0 deletions pkg/engine/vm/insn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package vm

type OPCode int

const (
OP_NOP OPCode = iota

Check failure on line 6 in pkg/engine/vm/insn.go

View workflow job for this annotation

GitHub Actions / lint

ST1003: should not use ALL_CAPS in Go names; use CamelCase instead (stylecheck)

OP_ST

Check failure on line 8 in pkg/engine/vm/insn.go

View workflow job for this annotation

GitHub Actions / lint

ST1003: should not use ALL_CAPS in Go names; use CamelCase instead (stylecheck)
OP_LD

OP_ADD
OP_SUB
OP_MUL
OP_DIV
OP_MOD
OP_UNM // unary minus

OP_AND
OP_OR
OP_NOT

OP_EQ
OP_NEQ
OP_LT
OP_LTE
OP_GT
OP_GTE
OP_LEN

OP_GET
OP_SET
OP_APPEND

OP_JMP
OP_JMP_IF

OP_CALL
OP_RET
)
1 change: 1 addition & 0 deletions pkg/engine/vm/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package vm
46 changes: 46 additions & 0 deletions pkg/v2/ast/ast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.

// Package ast pipeline ast node
package ast

import (
"fmt"
"sort"
"strings"
)

type Stmts []Node

type KwArgs map[string]Node

type FuncArgList []Node

func (e KwArgs) String() string {
keys := []string{}
for k := range e {
keys = append(keys, k)
}
sort.Strings(keys)

arr := []string{}
for _, key := range keys {
arr = append(arr, fmt.Sprintf("%s = %s", key, e[key]))
}
return strings.Join(arr, ", ")
}

func (e Stmts) String() string {
arr := []string{}
for i, x := range e {
arr = append(arr, x.String())
_ = i
}
return strings.Join(arr, "\n")
}

type Node interface {
String() string
}
185 changes: 185 additions & 0 deletions pkg/v2/ast/dtype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.

package ast

import (
"bytes"

"github.com/GuanceCloud/platypus/pkg/token"
)

type DType uint

func (t DType) String() string {
switch t {
case Invalid:
return "invalid"
case Bool:
return "bool"
case Int:
return "int"
case Float:
return "float"
case String:
return "str"
case List:
return "list"
case Map:
return "map"
case Func:
return "func"
case Class:
return "class"
}
return ""
}

const (
Invalid DType = iota

Any // any

Bool
Int // int64
Float // float64
String // string

List
Map

Class

Func
)

func AllTyp() []DType {
return []DType{Bool, Int, Float, String, List, Map, Class, Func}
}

type TypeNode interface {
IsType()
String() string
}

type TypeID struct {
Name Node
}

func (*TypeID) IsType() {}
func (t *TypeID) String() string {
return t.Name.String()
}

type FnParam struct {
Name Node
DType Node
Varb bool
DefaultVal Node
}

func (p *FnParam) String() string {
s := p.Name.String()
if p.DType != nil {
if p.Varb {
s += ": ..." + p.DType.String()
} else {
s += ": " + p.DType.String()
}
} else if p.Varb {
s += ": ..."
}
if p.DefaultVal != nil {
s += " = " + p.DefaultVal.String()
}
return s
}

type TypeAny struct{}

func (*TypeAny) IsType() {}

func (t *TypeAny) String() string {
return "any"
}

type TypeFn struct {
Params []FnParam
Results []Node
}

func (*TypeFn) IsType() {}

func (t *TypeFn) String() string {
return "fn " + t.string()
}

func (t *TypeFn) string() string {
b := bytes.NewBuffer([]byte{})
b.WriteString("(")
for i, p := range t.Params {
b.WriteString(p.String())
if i < len(t.Params)-1 {
b.WriteString(", ")
}
}
b.WriteString(")")
if len(t.Results) > 0 {
b.WriteString(" -> ")
}

if len(t.Results) > 1 {
b.WriteString("(")
}
for i, r := range t.Results {
b.WriteString(r.String())
if i < len(t.Results)-1 {
b.WriteString(", ")
}
}
if len(t.Results) > 1 {
b.WriteString(")")
}

return b.String()
}

type TypeClass struct {
Fields []Node
Methods []Node
}

func (*TypeClass) IsType() {}

type TypeMap struct {
KeyType Node
ValueType Node
}

func (t *TypeMap) String() string {
return "map"
}
func (*TypeMap) IsType() {}

type TypeList struct {
VType Node
}

func (t *TypeList) String() string {
return "list"
}

func (*TypeList) IsType() {}

type TypeBasic struct {
Pos token.LnColPos
DType DType
}

func (*TypeBasic) IsType() {}

func (t *TypeBasic) String() string {
return t.DType.String()
}
Loading
Loading