From 9010b17d801728a1897621ffb27cfcaa12d43b1b Mon Sep 17 00:00:00 2001 From: oysheng <33340252+oysheng@users.noreply.github.com> Date: Wed, 31 Oct 2018 14:13:16 +0800 Subject: [PATCH] delete check that the lock value disposed of once (#23) * modify check for amountType and intType * delete check that the locks's value disposed once --- compiler/checks.go | 57 ----------------------------------------------------- compiler/compile.go | 13 ++++++------ 2 files changed, 6 insertions(+), 64 deletions(-) diff --git a/compiler/checks.go b/compiler/checks.go index a705137..e78d8c4 100644 --- a/compiler/checks.go +++ b/compiler/checks.go @@ -147,63 +147,6 @@ func references(expr expression, name string) bool { return false } -func requireAllValuesDisposedOnce(contract *Contract, clause *Clause) error { - err := valueDisposedOnce(contract.Value, clause) - if err != nil { - return err - } - return nil -} - -func valueDisposedOnce(value ValueInfo, clause *Clause) error { - var count int - for _, stmt := range clause.statements { - count = valueDisposedCount(value, stmt, count) - } - switch count { - case -1: - return fmt.Errorf("valueAmount \"%s\" and valueAsset \"%s\" used times is not equal between if and else statement in clause \"%s\"", - value.Amount, value.Asset, clause.Name) - case 0: - return fmt.Errorf("valueAmount \"%s\" or valueAsset \"%s\" not disposed in clause \"%s\"", value.Amount, value.Asset, clause.Name) - case 1: - return nil - default: - return fmt.Errorf("valueAmount \"%s\" or valueAsset \"%s\" disposed multiple times in clause \"%s\"", value.Amount, value.Asset, clause.Name) - } -} - -func valueDisposedCount(value ValueInfo, stat statement, count int) int { - switch stmt := stat.(type) { - case *ifStatement: - var trueCount int - var falseCount int - for _, trueStmt := range stmt.body.trueBody { - trueCount = valueDisposedCount(value, trueStmt, count) - } - - for _, falseStmt := range stmt.body.falseBody { - falseCount = valueDisposedCount(value, falseStmt, count) - } - - if trueCount != falseCount { - return -1 - } - count = trueCount - - case *unlockStatement: - if references(stmt.unlockedAmount, value.Amount) && references(stmt.unlockedAsset, value.Asset) { - count++ - } - case *lockStatement: - if references(stmt.lockedAmount, value.Amount) && references(stmt.lockedAsset, value.Asset) { - count++ - } - } - - return count -} - func referencedBuiltin(expr expression) *builtin { if v, ok := expr.(varRef); ok { for _, b := range builtins { diff --git a/compiler/compile.go b/compiler/compile.go index 7145274..ddaf496 100644 --- a/compiler/compile.go +++ b/compiler/compile.go @@ -342,10 +342,6 @@ func compileClause(b *builder, contractStk stack, contract *Contract, env *envir } } - err = requireAllValuesDisposedOnce(contract, clause) - if err != nil { - return err - } err = typeCheckClause(contract, clause, env) if err != nil { return err @@ -619,12 +615,14 @@ func compileExpr(b *builder, stk stack, contract *Contract, clause *Clause, env } lType := e.left.typ(env) - if e.op.left != "" && !(lType == e.op.left || lType == amountType) { + if e.op.left != "" && ((e.op.left == intType && !(lType == amountType || lType == intType)) || + (e.op.left == boolType && !(lType == boolType))) { return stk, fmt.Errorf("in \"%s\", left operand has type \"%s\", must be \"%s\"", e, lType, e.op.left) } rType := e.right.typ(env) - if e.op.right != "" && !(rType == e.op.right || rType == amountType) { + if e.op.right != "" && ((e.op.right == intType && !(rType == amountType || rType == intType)) || + (e.op.right == boolType && !(rType == boolType))) { return stk, fmt.Errorf("in \"%s\", right operand has type \"%s\", must be \"%s\"", e, rType, e.op.right) } @@ -680,7 +678,8 @@ func compileExpr(b *builder, stk stack, contract *Contract, clause *Clause, env for i := len(e.args) - 1; i >= 0; i-- { arg := e.args[i] - if entry.c.Params[i].Type != "" && arg.typ(env) != entry.c.Params[i].Type { + if entry.c.Params[i].Type != "" && arg.typ(env) != entry.c.Params[i].Type && + !(arg.typ(env) == intType && entry.c.Params[i].Type == amountType) { return stk, fmt.Errorf("argument %d to contract \"%s\" has type \"%s\", must be \"%s\"", i, entry.c.Name, arg.typ(env), entry.c.Params[i].Type) } stk, err = compileExpr(b, stk, contract, clause, env, counts, arg) -- 2.11.0