Skip to content

Commit

Permalink
refactor: replace manual type checking with VMFuncXParams
Browse files Browse the repository at this point in the history
VMFuncOneParam, VMFuncTwoParams, VMFuncThreeParams принимают несколько женериков которые указывают необходимые типы аргументов
  • Loading branch information
renbou committed Jul 6, 2022
1 parent 2a0a3b6 commit b5186cf
Show file tree
Hide file tree
Showing 33 changed files with 432 additions and 306 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"args": ["core/slice_test.gnc"],
"args": [],
"env": {},
"cwd": "./test"
"cwd": ""
}
]
}
12 changes: 8 additions & 4 deletions ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,12 @@ type AssocExpr struct {
}

func (x *AssocExpr) Simplify() Expr {
x.Lhs = x.Lhs.Simplify()
x.Rhs = x.Rhs.Simplify()
if x.Lhs != nil {
x.Lhs = x.Lhs.Simplify()
}
if x.Rhs != nil {
x.Rhs = x.Rhs.Simplify()
}
return x
}

Expand Down Expand Up @@ -764,7 +768,7 @@ func (x *ConstExpr) Simplify() Expr {
}

func (e *ConstExpr) BinTo(bins *binstmt.BinStmts, reg int, lid *int, inStmt bool, maxreg *int) {
var v core.VMValuer
var v core.VMValue

switch names.FastToLower(e.Value) {
case "истина", "true":
Expand Down Expand Up @@ -962,7 +966,7 @@ func (e *MakeArrayExpr) BinTo(bins *binstmt.BinStmts, reg int, lid *int, inStmt
// хранит реальное значение, рассчитанное на этапе оптимизации AST
type NativeExpr struct {
ExprImpl
Value core.VMValuer
Value core.VMValue
}

func (x *NativeExpr) Simplify() Expr {
Expand Down
4 changes: 2 additions & 2 deletions bincode/binstmt/binstmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ type BinLOAD struct {
BinStmtImpl

Reg int
Val core.VMValuer
Val core.VMValue
IsId bool
}

Expand All @@ -241,7 +241,7 @@ func (v BinLOAD) String() string {
return fmt.Sprintf("LOAD r%d, %#v", v.Reg, v.Val)
}

func NewBinLOAD(reg int, val core.VMValuer, isid bool, e pos.Pos) *BinLOAD {
func NewBinLOAD(reg int, val core.VMValue, isid bool, e pos.Pos) *BinLOAD {
v := &BinLOAD{
Reg: reg,
Val: val,
Expand Down
14 changes: 6 additions & 8 deletions bincode/binvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func putRegs(sl core.VMSlice) {
}

// Run запускает код на исполнение, например, после загрузки из файла
func Run(stmts binstmt.BinCode, env *core.Env) (retval core.VMValuer, reterr error) {
func Run(stmts binstmt.BinCode, env *core.Env) (retval core.VMValue, reterr error) {
defer func() {
// если это не паника из кода языка
// if os.Getenv("GONEC_DEBUG") == "" {
Expand Down Expand Up @@ -120,7 +120,6 @@ func Run(stmts binstmt.BinCode, env *core.Env) (retval core.VMValuer, reterr err
panic(err)
}
rets.Append(rv)
return nil
} else {
_, bins, err := ParseSrc(string(body))
if err != nil {
Expand All @@ -137,7 +136,6 @@ func Run(stmts binstmt.BinCode, env *core.Env) (retval core.VMValuer, reterr err
panic(err)
}
rets.Append(rv)
return nil
}
return nil
}
Expand All @@ -153,7 +151,7 @@ func Run(stmts binstmt.BinCode, env *core.Env) (retval core.VMValuer, reterr err
}

// RunWorker исполняет кусок кода, начиная с инструкции idx
func RunWorker(stmts binstmt.BinStmts, labels []int, numofregs int, env *core.Env, idx int) (retval core.VMValuer, reterr error) {
func RunWorker(stmts binstmt.BinStmts, labels []int, numofregs int, env *core.Env, idx int) (retval core.VMValue, reterr error) {
defer func() {
// если это не паника из кода языка
// if os.Getenv("GONEC_DEBUG") == "" {
Expand Down Expand Up @@ -344,7 +342,7 @@ func RunWorker(stmts binstmt.BinStmts, labels []int, numofregs int, env *core.En
var err error

// функцию на языке Гонец можно вызывать прямо с аргументами из слайса в регистре
var fgnc core.VMValuer
var fgnc core.VMValue
var argsl core.VMSlice
if s.Name == 0 {
fgnc = registers[s.RegArgs]
Expand Down Expand Up @@ -863,7 +861,7 @@ func RunWorker(stmts binstmt.BinStmts, labels []int, numofregs int, env *core.En
} else {
v = reflect.Zero(rt)
}
if vv, ok := v.Interface().(core.VMValuer); ok {
if vv, ok := v.Interface().(core.VMValue); ok {
invalidArgs := false
if vobj, ok := vv.(core.VMMetaObject); ok {
vobj.VMInit(vobj)
Expand Down Expand Up @@ -956,7 +954,7 @@ func RunWorker(stmts binstmt.BinStmts, labels []int, numofregs int, env *core.En

case *binstmt.BinINC:
v := registers[s.Reg]
var x core.VMValuer
var x core.VMValue
if vv, ok := v.(core.VMInt); ok {
x = core.VMInt(int64(vv) + 1)
} else if vv, ok := v.(core.VMDecNum); ok {
Expand All @@ -966,7 +964,7 @@ func RunWorker(stmts binstmt.BinStmts, labels []int, numofregs int, env *core.En

case *binstmt.BinDEC:
v := registers[s.Reg]
var x core.VMValuer
var x core.VMValue
if vv, ok := v.(core.VMInt); ok {
x = core.VMInt(int64(vv) - 1)
} else if vv, ok := v.(core.VMDecNum); ok {
Expand Down
Loading

0 comments on commit b5186cf

Please sign in to comment.