1
0
forked from x/icebergs
This commit is contained in:
IT 老营长 @云轩领航-创始人 2023-04-02 15:39:58 +08:00
parent ec75142f96
commit 9e2bc71dff
4 changed files with 216 additions and 196 deletions

View File

@ -9,24 +9,25 @@ import (
) )
const ( const (
SPACE = "\t " SPACE = "\t "
QUOTE = "\"" QUOTE = "\""
TRANS = " " TRANS = " "
BLOCK = "[:](,){;}*/+-<>!=&|" BLOCK = "[:](,){;}*/+-<>!=&|"
DEFINE = ":=" DEFINE = ":="
ASSIGN = "=" ASSIGN = "="
SUBS = "[" SUBS = "["
DEFS = ":" DEFS = ":"
SUPS = "]" SUPS = "]"
OPEN = "(" OPEN = "("
FIELD = "," FIELD = ","
CLOSE = ")" CLOSE = ")"
BEGIN = "{" BEGIN = "{"
SPLIT = ";" SPLIT = ";"
END = "}" END = "}"
DISABLE = -1
) )
var keyword = regexp.MustCompile("[_a-zA-Z][_a-zA-Z0-9]*")
var level = map[string]int{ var level = map[string]int{
"//": 200, "/*": 200, "*/": 200, "//": 200, "/*": 200, "*/": 200,
"!": 100, "++": 100, "--": 100, "[": 100, "]": 100, "!": 100, "++": 100, "--": 100, "[": 100, "]": 100,
@ -38,7 +39,6 @@ var level = map[string]int{
type Expr struct { type Expr struct {
list ice.List list ice.List
s *Stack s *Stack
m *ice.Message
n int n int
} }
@ -50,6 +50,7 @@ func (s *Expr) pop(n int) *Expr {
s.list = s.list[:len(s.list)-n] s.list = s.list[:len(s.list)-n]
return s return s
} }
func (s *Expr) pops(n int, v Any) *Expr { return s.pop(n).push(v) }
func (s *Expr) get(p int) (v Any) { func (s *Expr) get(p int) (v Any) {
kit.If(0 <= p+len(s.list) && p+len(s.list) < len(s.list), func() { v = s.list[p+len(s.list)] }) kit.If(0 <= p+len(s.list) && p+len(s.list) < len(s.list), func() { v = s.list[p+len(s.list)] })
kit.If(0 <= p && p < len(s.list), func() { v = s.list[p] }) kit.If(0 <= p && p < len(s.list), func() { v = s.list[p] })
@ -66,23 +67,19 @@ func (s *Expr) getv(m *ice.Message, p int) (v Any) {
} }
} }
func (s *Expr) setv(m *ice.Message, p int, op string, v Any) Any { func (s *Expr) setv(m *ice.Message, p int, op string, v Any) Any {
k := s.gets(p) if !s.s.runable() {
kit.If(s.s.runable(), func() { return nil
switch v := v.(type) { }
case string: switch k := s.gets(p); v := v.(type) {
s.s.value(m, k, s.s.value(m, v), op) case string:
case Value: return s.s.value(m, k, s.s.value(m, v), op)
s.s.value(m, k, v.list[0], op) case Value:
default: return s.s.value(m, k, v.list[0], op)
s.s.value(m, k, v, op) default:
} return s.s.value(m, k, v, op)
}) }
return s.s.value(m, k)
} }
func (s *Expr) opv(m *ice.Message, p int, op string, v Any) Any { func (s *Expr) opv(m *ice.Message, p int, op string, v Any) Any {
if s.n++; s.n > 100 {
panic(s.n)
}
if !s.s.runable() { if !s.s.runable() {
return s.getv(m, p) return s.getv(m, p)
} }
@ -95,12 +92,16 @@ func (s *Expr) ops(m *ice.Message) {
if !s.s.runable() || s.getl(-2) < 10 { if !s.s.runable() || s.getl(-2) < 10 {
return return
} }
v := s.opv(m, -3, s.gets(-2), s.getv(m, -1)) if s.n++; s.n > 100 {
s.pop(3).push(v) panic(s.n)
}
s.pops(3, s.opv(m, -3, s.gets(-2), s.getv(m, -1)))
} }
func (s *Expr) end(m *ice.Message) Any { func (s *Expr) end(m *ice.Message) Any {
if !s.s.runable() || len(s.list) == 0 { if !s.s.runable() || len(s.list) == 0 {
return nil return nil
} else if len(s.list) == 1 {
return s.getv(m, 0)
} }
m.Debug("expr ops %v", s.list) m.Debug("expr ops %v", s.list)
for len(s.list) > 1 { for len(s.list) > 1 {
@ -127,9 +128,7 @@ func (s *Expr) end(m *ice.Message) Any {
switch v := s.getv(m, -1).(type) { switch v := s.getv(m, -1).(type) {
case Value: case Value:
for i := 0; i < len(s.list)-1; i += 2 { for i := 0; i < len(s.list)-1; i += 2 {
if i/2 < len(v.list) { kit.If(i/2 < len(v.list), func() { list = append(list, s.setv(m, i, s.gets(-2), v.list[i/2])) })
list = append(list, s.setv(m, i, s.gets(-2), v.list[i/2]))
}
} }
default: default:
s.setv(m, -3, s.gets(-2), s.getv(m, -1)) s.setv(m, -3, s.gets(-2), s.getv(m, -1))
@ -157,58 +156,34 @@ func (s *Expr) cals(m *ice.Message) Any {
case END: case END:
s.s.skip-- s.s.skip--
return true return true
case CLOSE:
if s.gets(-2) == OPEN {
s.pops(2, s.get(-1))
return false
}
return true
} }
if len(s.list) > 0 && s.getl(-1) == 0 { if len(s.list) > 0 && s.getl(-1) == 0 {
switch k { switch k {
case "++": case "++", "--":
v := s.opv(m, -1, "+", 1) s.pops(1, s.setv(m, -1, ASSIGN, s.opv(m, -1, k, nil)))
s.setv(m, -1, ASSIGN, v)
s.pop(1).push(v)
return false
case "--":
v := s.opv(m, -1, "-", 1)
s.setv(m, -1, ASSIGN, v)
s.pop(1).push(v)
return false return false
case SUBS: case SUBS:
v := s.opv(m, -1, SUBS, s.s.cals(m)) s.pops(1, s.opv(m, -1, SUBS, s.s.cals(m)))
s.pop(1).push(v)
return false return false
case OPEN: case OPEN:
if s.gets(-1) == FUNC { if s.gets(-1) == FUNC && s.s.skip > 1 {
name := kit.Format("%s:%d:%d", s.s.name, s.s.line, s.s.skip)
s.s.skip-- s.s.skip--
s.s.rest[s.s.skip] = name s.pops(1, s.s.value(m, s.s.funcs(m)))
s.s.skip--
m.Cmd(FUNC, name)
s.s.skip++
sub := NewStack()
sub.Position = s.s.Position
sub.pushf(m, FUNC).status = DISABLE
sub.run(m.Options(STACK, sub))
s.s.Position = sub.Position
m.Options(STACK, s.s)
s.s.popf(m)
s.pop(1).push(s.s.value(m, name))
} else { } else {
v := s.call(m, s.s, s.gets(-1)) s.pops(1, s.call(m, s.s, s.gets(-1)))
s.pop(1).push(v)
} }
return false return false
case CLOSE:
if s.gets(-2) == OPEN {
v := s.get(-1)
s.pop(2).push(v)
return false
}
m.Debug("what %v", k)
return true
} }
if level[k] == 0 { if level[k] == 0 {
if strings.HasPrefix(k, ice.PT) && kit.Select("", s.s.rest, s.s.skip+1) == OPEN { if strings.HasPrefix(k, ice.PT) && kit.Select("", s.s.rest, s.s.skip+1) == OPEN {
s.s.skip++ s.s.skip++
v := s.call(m, s.getv(m, -1), strings.TrimPrefix(k, ice.PT)) s.pops(1, s.call(m, s.getv(m, -1), strings.TrimPrefix(k, ice.PT)))
s.pop(1).push(v)
return false return false
} else { } else {
s.s.skip-- s.s.skip--
@ -217,7 +192,7 @@ func (s *Expr) cals(m *ice.Message) Any {
} }
} }
if level[k] > 0 { if level[k] > 0 {
for level[k] >= 9 && level[k] <= s.getl(-2) { for level[k] >= 9 && level[k] <= s.getl(-2) && level[k] < 100 {
s.ops(m) s.ops(m)
} }
s.push(k) s.push(k)
@ -228,11 +203,14 @@ func (s *Expr) cals(m *ice.Message) Any {
s.push(Boolean{value: true}) s.push(Boolean{value: true})
} else if k == ice.FALSE { } else if k == ice.FALSE {
s.push(Boolean{value: false}) s.push(Boolean{value: false})
} else if b, e := regexp.MatchString("[_a-zA-Z][_a-zA-Z0-9]*", k); e == nil && b { } else if keyword.MatchString(k) {
s.push(k) s.push(k)
} else { } else {
s.push(Number{value: k}) s.push(Number{value: k})
} }
if s.gets(-2) == "!" {
s.pops(2, s.opv(-1, "!"))
}
} }
return false return false
}) })
@ -247,36 +225,11 @@ func (s *Expr) call(m *ice.Message, obj Any, key string) Any {
list = append(list, v) list = append(list, v)
} }
if !s.s.runable() { if !s.s.runable() {
return nil return list
}
kit.For(kit.Slice(kit.Split(key, ice.PT), 0, -1), func(k string) {
switch v := obj.(type) {
case *Stack:
obj, key = v.value(m, k), strings.TrimPrefix(key, k+ice.PT)
}
})
m.Debug("expr call %T %s %v", obj, key, kit.Format(list))
switch obj := obj.(type) {
case *Stack:
return obj.call(m, obj.value(m, key), nil, list...)
case Caller:
return obj.Call(key, list...)
case func(string, ...Any) Any:
return obj(key, list...)
default:
args := kit.List(key)
for _, v := range list {
switch v := v.(type) {
case Value:
args = append(args, v.list...)
default:
args = append(args, trans(v))
}
}
return Message{m.Cmd(args...)}
} }
return s.s.call(m, obj, key, nil, list...)
} }
func NewExpr(m *ice.Message, s *Stack) *Expr { return &Expr{kit.List(), s, m, 0} } func NewExpr(s *Stack) *Expr { return &Expr{kit.List(), s, 0} }
const EXPR = "expr" const EXPR = "expr"
@ -285,7 +238,6 @@ func init() {
EXPR: {Name: "expr a = 1", Hand: func(m *ice.Message, arg ...string) { EXPR: {Name: "expr a = 1", Hand: func(m *ice.Message, arg ...string) {
s := _parse_stack(m) s := _parse_stack(m)
arg = s.rest arg = s.rest
m.Debug("what %v %d %d", s.rest[:], s.skip, s.line)
if v := s.cals(m); !s.runable() { if v := s.cals(m); !s.runable() {
return return
} else if v != nil { } else if v != nil {

View File

@ -53,6 +53,11 @@ func (s *Stack) pushf(m *ice.Message, key string) *Frame {
} }
func (s *Stack) popf(m *ice.Message) *Frame { func (s *Stack) popf(m *ice.Message) *Frame {
f := s.peekf() f := s.peekf()
list := kit.List(f.value["_defer"])
delete(f.value, "_defer")
for i := len(list) - 1; i >= 0; i-- {
list[i].(func())()
}
kit.If(f.pop != nil, func() { f.pop() }) kit.If(f.pop != nil, func() { f.pop() })
m.Debug("stack pop %d %v %s:%d", len(s.frame)-1, f.key, f.name, f.line) m.Debug("stack pop %d %v %s:%d", len(s.frame)-1, f.key, f.name, f.line)
kit.If(len(s.frame) > 0, func() { s.frame = s.frame[:len(s.frame)-1] }) kit.If(len(s.frame) > 0, func() { s.frame = s.frame[:len(s.frame)-1] })
@ -68,7 +73,7 @@ func (s *Stack) stack(cb func(*Frame, int) bool) {
} }
func (s *Stack) value(m *ice.Message, key string, arg ...Any) Any { func (s *Stack) value(m *ice.Message, key string, arg ...Any) Any {
f, n := s.peekf(), len(s.frame)-1 f, n := s.peekf(), len(s.frame)-1
if len(arg) < 2 || arg[1] == ASSIGN { if len(arg) < 2 || arg[1] != DEFINE {
s.stack(func(_f *Frame, i int) bool { s.stack(func(_f *Frame, i int) bool {
if _f.value[key] != nil { if _f.value[key] != nil {
f, n = _f, i f, n = _f, i
@ -83,7 +88,7 @@ func (s *Stack) value(m *ice.Message, key string, arg ...Any) Any {
}) })
return f.value[key] return f.value[key]
} }
func (s *Stack) runable() bool { return s.peekf().status > DISABLE } func (s *Stack) runable() bool { return s.peekf().status > STATUS_DISABLE }
func (s *Stack) token() string { return kit.Select("", s.rest, s.skip) } func (s *Stack) token() string { return kit.Select("", s.rest, s.skip) }
func (s *Stack) read(m *ice.Message) (text string, ok bool) { func (s *Stack) read(m *ice.Message) (text string, ok bool) {
isvoid := func(text string) bool { isvoid := func(text string) bool {
@ -120,77 +125,102 @@ func (s *Stack) reads(m *ice.Message, cb func(k string) bool) {
} }
} }
func (s *Stack) run(m *ice.Message) { func (s *Stack) run(m *ice.Message) {
begin := len(s.frame)
s.reads(m, func(k string) bool { s.reads(m, func(k string) bool {
if s.n++; s.n > 100 { if s.n++; s.n > 300 {
return true panic(s.n)
} }
if k == SPLIT { if k == SPLIT {
} else if k == END { } else if k == END {
s.last = s.popf(m) if s.last = s.popf(m); len(s.frame) < begin {
return true
}
} else if _, ok := m.Target().Commands[k]; ok { } else if _, ok := m.Target().Commands[k]; ok {
m.Cmdy(k, kit.Slice(s.rest, s.skip+1)) m.Cmdy(k, kit.Slice(s.rest, s.skip+1))
} else { } else {
s.skip-- s.skip--
m.Cmd(EXPR, kit.Slice(s.rest, s.skip)) m.Cmd(EXPR, kit.Slice(s.rest, s.skip))
} }
if len(s.frame) == 0 {
return true
}
return false return false
}) })
} }
func (s *Stack) call(m *ice.Message, v Any, cb func(*Frame, Function), arg ...Any) Any { func (s *Stack) call(m *ice.Message, obj Any, key Any, cb func(*Frame, Function), arg ...Any) Any {
switch v := v.(type) { if _k, ok := key.(string); ok {
case Function: kit.For(kit.Split(_k, ice.PT), func(k string) {
f := s.pushf(m, CALL) switch v := obj.(type) {
kit.For(v.res, func(k string) { f.value[k] = "" }) case *Stack:
kit.For(v.arg, func(i int, k string) { if v := v.value(m, _k); v != nil {
if i < len(arg) { obj = v
f.value[k] = arg[i] break
} else { }
f.value[k] = "" obj, key = v.value(m, k), strings.TrimPrefix(_k, k+ice.PT)
} }
}) })
f.value["_return"], f.value["_res"] = "", v.res }
m.Debug("expr call %T %s %v", obj, key, kit.Format(arg))
switch obj := obj.(type) {
case Function:
f := s.pushf(m, CALL)
kit.For(obj.res, func(k string) { f.value[k] = "" })
kit.For(obj.arg, func(i int, k string) {
kit.If(i < len(arg), func() { f.value[k] = arg[i] }, func() { f.value[k] = "" })
})
value, pos := Value{list: kit.List()}, s.Position value, pos := Value{list: kit.List()}, s.Position
f.pop, s.Position = func() { f.value["_return"] = func(arg ...Any) {
if len(v.res) == 0 { if len(obj.res) > 0 {
value.list = append(value.list, f.value["_return"]) kit.For(obj.res, func(i int, k string) {
kit.If(i < len(arg), func() { value.list = append(value.list, f.value[k]) }, func() { value.list = append(value.list, "") })
})
} else { } else {
kit.For(v.res, func(k string) { value.list = append(value.list, f.value[k]) }) value.list = arg
}
}
f.pop, s.Position = func() {
if len(obj.res) > 0 && len(value.list) == 0 {
kit.For(obj.res, func(i int, k string) { value.list = append(value.list, f.value[k]) })
} }
s.Position = pos s.Position = pos
}, v.Position }, obj.Position
cb(f, v) kit.If(cb != nil, func() { cb(f, obj) })
s.run(m.Options(STACK, s)) s.run(m.Options(STACK, s))
return value return value
default: case Caller:
return obj.Call(kit.Format(key), arg...)
case func(string, ...Any) Any:
return obj(kit.Format(key), arg...)
case func():
obj()
return nil return nil
default:
args := kit.List(key)
for _, v := range arg {
switch v := v.(type) {
case Value:
args = append(args, v.list...)
default:
args = append(args, trans(v))
}
}
return Message{m.Cmd(args...)}
} }
} }
func (s *Stack) cals(m *ice.Message) Any { return NewExpr(m, s).cals(m) } func (s *Stack) cals(m *ice.Message) Any { return NewExpr(s).cals(m) }
func (s *Stack) expr(m *ice.Message, pos ...Position) string { func (s *Stack) expr(m *ice.Message, pos ...Position) string {
kit.If(len(pos) > 0, func() { s.Position = pos[0] }) kit.If(len(pos) > 0, func() { s.Position = pos[0] })
return m.Cmdx(EXPR, kit.Slice(s.rest, s.skip)) return m.Cmdx(EXPR, kit.Slice(s.rest, s.skip))
} }
func (s *Stack) load(m *ice.Message) *Stack { func (s *Stack) funcs(m *ice.Message) string {
f := s.pushf(m.Options(STACK, s), "") name := kit.Format("%s:%d:%d", s.name, s.line, s.skip)
f.value["kit"] = func(key string, arg ...Any) Any { s.rest[s.skip] = name
kit.For(arg, func(i int, v Any) { arg[i] = trans(v) }) s.skip--
switch key { m.Cmd(FUNC, name)
case "Dict": f := s.peekf()
return Dict{kit.Dict(arg...)} status := f.status
case "List": defer func() { f.status = status }()
return List{kit.List(arg...)} f.status = STATUS_DISABLE
default: s.run(m)
m.ErrorNotImplement(key) return name
return nil
}
}
f.value["m"] = Message{m}
return s
} }
func (s *Stack) parse(m *ice.Message, name string, r io.Reader, cb func(*Frame)) *Stack { func (s *Stack) parse(m *ice.Message, name string, r io.Reader, cb func(*Frame)) *Stack {
s.Buffer = &Buffer{name: name, input: bufio.NewScanner(r)} s.Buffer = &Buffer{name: name, input: bufio.NewScanner(r)}
@ -206,6 +236,10 @@ func _parse_frame(m *ice.Message) (*Stack, *Frame) {
return _parse_stack(m), _parse_stack(m).pushf(m, "") return _parse_stack(m), _parse_stack(m).pushf(m, "")
} }
const (
STATUS_NORMAL = 0
STATUS_DISABLE = -1
)
const STACK = "stack" const STACK = "stack"
func init() { func init() {
@ -246,7 +280,7 @@ func init() {
arg = arg[1:] arg = arg[1:]
} }
i := 0 i := 0
s.call(m, s.value(m, action), func(f *Frame, v Function) { s.call(m, s, action, func(f *Frame, v Function) {
kit.For(v.arg, func(k string) { kit.For(v.arg, func(k string) {
switch k { switch k {
case "m": case "m":
@ -268,19 +302,9 @@ func init() {
return return
} }
nfs.Open(m, path.Join(nfs.SRC, path.Join(arg...)), func(r io.Reader, p string) { nfs.Open(m, path.Join(nfs.SRC, path.Join(arg...)), func(r io.Reader, p string) {
s := NewStack().parse(m, p, r, nil) if NewStack().parse(m, p, r, nil); m.Option(ice.DEBUG) != ice.TRUE {
if m.Option(ice.DEBUG) != ice.TRUE {
return return
} }
m.EchoLine("").EchoLine("script: %s", arg[0])
span := func(s, k, t string) string {
return strings.ReplaceAll(s, k, kit.Format("<span class='%s'>%s</span>", t, k))
}
kit.For(s.list, func(i int, s string) {
kit.For([]string{LET, IF, FOR, FUNC}, func(k string) { s = span(s, k, KEYWORD) })
kit.For([]string{PWD, INFO, SOURCE}, func(k string) { s = span(s, k, FUNCTION) })
m.EchoLine("%2d: %s", i, s)
})
m.EchoLine("").EchoLine("stack: %s", arg[0]).Cmdy(INFO) m.EchoLine("").EchoLine("stack: %s", arg[0]).Cmdy(INFO)
}) })

View File

@ -10,7 +10,6 @@ import (
) )
const ( const (
LET = "let"
IF = "if" IF = "if"
ELSE = "else" ELSE = "else"
FOR = "for" FOR = "for"
@ -21,6 +20,7 @@ const (
DEFAULT = "default" DEFAULT = "default"
FUNC = "func" FUNC = "func"
CALL = "call" CALL = "call"
DEFER = "defer"
RETURN = "return" RETURN = "return"
SOURCE = "source" SOURCE = "source"
INFO = "info" INFO = "info"
@ -33,25 +33,24 @@ const (
func init() { func init() {
Index.MergeCommands(ice.Commands{ Index.MergeCommands(ice.Commands{
LET: {Name: "let a, b = 1, 2", Hand: func(m *ice.Message, arg ...string) { m.Cmd(EXPR) }},
IF: {Name: "if a = 1; a > 1 {", Hand: func(m *ice.Message, arg ...string) { IF: {Name: "if a = 1; a > 1 {", Hand: func(m *ice.Message, arg ...string) {
s, f := _parse_frame(m) s, f := _parse_frame(m)
res := s.expr(m) res := s.expr(m)
kit.If(s.token() == SPLIT, func() { res = s.expr(m) }) kit.If(s.token() == SPLIT, func() { res = s.expr(m) })
kit.If(res == ice.FALSE, func() { f.status = DISABLE }) kit.If(res == ice.FALSE, func() { f.status = STATUS_DISABLE })
}}, }},
ELSE: {Name: "else if a = 1; a > 1 {", Hand: func(m *ice.Message, arg ...string) { ELSE: {Name: "else if a = 1; a > 1 {", Hand: func(m *ice.Message, arg ...string) {
s, f := _parse_frame(m) s, f := _parse_frame(m)
if s.last.status == DISABLE { if s.last.status == STATUS_DISABLE {
f.status = 0 f.status = 0
} else { } else {
f.status, f.pop = DISABLE, func() { f.status = 0 } f.status, f.pop = STATUS_DISABLE, func() { f.status = 0 }
} }
s.reads(m, func(k string) bool { s.reads(m, func(k string) bool {
if k == IF { if k == IF {
res := s.expr(m) res := s.expr(m)
kit.If(s.token() == SPLIT, func() { res = s.expr(m) }) kit.If(s.token() == SPLIT, func() { res = s.expr(m) })
kit.If(res == ice.FALSE, func() { f.status = DISABLE }) kit.If(res == ice.FALSE, func() { f.status = STATUS_DISABLE })
} }
return true return true
}) })
@ -59,7 +58,7 @@ func init() {
FOR: {Name: "for a = 1; a < 10; a++ {", Hand: func(m *ice.Message, arg ...string) { FOR: {Name: "for a = 1; a < 10; a++ {", Hand: func(m *ice.Message, arg ...string) {
s, f := _parse_frame(m) s, f := _parse_frame(m)
list, status := []Position{s.Position}, f.status list, status := []Position{s.Position}, f.status
for f.status = DISABLE; s.token() != BEGIN; list = append(list, s.Position) { for f.status = STATUS_DISABLE; s.token() != BEGIN; list = append(list, s.Position) {
s.expr(m) s.expr(m)
} }
f.status = status f.status = status
@ -76,7 +75,7 @@ func init() {
} }
res = s.expr(m, list[1]) res = s.expr(m, list[1])
} }
kit.If(res == ice.FALSE, func() { f.status = DISABLE }) kit.If(res == ice.FALSE, func() { f.status = STATUS_DISABLE })
s.Position, f.pop = list[len(list)-1], func() { s.Position, f.pop = list[len(list)-1], func() {
if s.runable() { if s.runable() {
kit.If(len(list) > 3, func() { s.expr(m, list[2]) }) kit.If(len(list) > 3, func() { s.expr(m, list[2]) })
@ -91,10 +90,10 @@ func init() {
return return
} }
s.stack(func(f *Frame, i int) bool { s.stack(func(f *Frame, i int) bool {
f.status = DISABLE f.status = STATUS_DISABLE
defer s.popf(m) defer s.popf(m)
switch f.key { switch f.key {
case FOR: case FOR, SWITCH:
return true return true
default: default:
return false return false
@ -127,7 +126,7 @@ func init() {
f := s.peekf() f := s.peekf()
f.status = 0 f.status = 0
v := s.cals(m) v := s.cals(m)
f.status = DISABLE f.status = STATUS_DISABLE
if res, ok := v.(Operater); ok { if res, ok := v.(Operater); ok {
if res, ok := res.Operate("==", trans(s.value(m, "_switch"))).(Boolean); ok && res.value { if res, ok := res.Operate("==", trans(s.value(m, "_switch"))).(Boolean); ok && res.value {
f.status, f.value["_case"] = 0, "done" f.status, f.value["_case"] = 0, "done"
@ -138,7 +137,7 @@ func init() {
s := _parse_stack(m) s := _parse_stack(m)
f := s.peekf() f := s.peekf()
if f.status = 0; f.value["_case"] == "done" { if f.status = 0; f.value["_case"] == "done" {
f.status = DISABLE f.status = STATUS_DISABLE
} }
s.skip++ s.skip++
}}, }},
@ -163,33 +162,57 @@ func init() {
kit.If(len(list) < 2, func() { list = append(list, []string{}) }) kit.If(len(list) < 2, func() { list = append(list, []string{}) })
kit.If(len(list) < 3, func() { list = append(list, []string{}) }) kit.If(len(list) < 3, func() { list = append(list, []string{}) })
s.value(m, kit.Select("", list[0], -1), Function{obj: list[0], arg: list[1], res: list[2], Position: s.Position}) s.value(m, kit.Select("", list[0], -1), Function{obj: list[0], arg: list[1], res: list[2], Position: s.Position})
s.pushf(m, "").status = DISABLE s.pushf(m, "").status = STATUS_DISABLE
}},
DEFER: {Name: "defer func() {} ()", Hand: func(m *ice.Message, arg ...string) {
s := _parse_stack(m)
s.reads(m, func(k string) bool {
kit.If(k == FUNC, func() { k = s.funcs(m) })
s.skip++
v := s.cals(m)
if !s.runable() {
return true
}
if vv, ok := v.(Value); ok {
v = vv.list
} else {
v = []Any{v}
}
s.stack(func(f *Frame, i int) bool {
if kit.IsIn(f.key, CALL, SOURCE, STACK) {
f.value["_defer"] = append(kit.List(f.value["_defer"]), func() {
s.call(m, s, k, nil, v.([]Any)...)
})
return true
}
return false
})
return true
})
}}, }},
RETURN: {Name: "return show", Hand: func(m *ice.Message, arg ...string) { RETURN: {Name: "return show", Hand: func(m *ice.Message, arg ...string) {
s := _parse_stack(m) s := _parse_stack(m)
res := kit.Simple(trans(s.value(m, "_res"))) v := s.cals(m)
if v := s.cals(m); len(res) == 0 {
f := s.peekf()
f.value["_return"] = v
} else {
switch v := v.(type) {
case Value:
kit.For(res, func(i int, k string) { kit.If(i < len(v.list), func() { s.value(m, k, v.list[i]) }) })
default:
s.value(m, kit.Select("_res", res, 0), v)
}
}
s.stack(func(f *Frame, i int) bool { s.stack(func(f *Frame, i int) bool {
f.status = DISABLE f.status = STATUS_DISABLE
switch f.key { switch f.key {
case FUNC: case FUNC:
case CALL: case CALL:
switch cb := f.value["_return"].(type) {
case STACK: case func(...Any):
s.input = nil switch v := v.(type) {
case Value:
cb(v.list...)
default:
cb(v)
}
}
case SOURCE: case SOURCE:
s.input = nil s.input = nil
case STACK:
s.input = nil
default: default:
return false return false
} }
@ -219,7 +242,7 @@ func init() {
res := []string{kit.Format("%d:%d", s.line, s.skip)} res := []string{kit.Format("%d:%d", s.line, s.skip)}
s.stack(func(f *Frame, i int) bool { s.stack(func(f *Frame, i int) bool {
kit.If(i > 0, func() { kit.If(i > 0, func() {
res = append(res, kit.Format("%s:%d:%d %s %v", f.name, f.line, f.skip, f.key, kit.Select(ice.FALSE, ice.TRUE, f.status > DISABLE))) res = append(res, kit.Format("%s:%d:%d %s %v", f.name, f.line, f.skip, f.key, kit.Select(ice.FALSE, ice.TRUE, f.status > STATUS_DISABLE)))
}) })
return false return false
}) })

View File

@ -101,6 +101,10 @@ func (s String) Operate(op string, v Any) Any {
} }
func (s Number) Operate(op string, v Any) Any { func (s Number) Operate(op string, v Any) Any {
switch a, b := kit.Int(s.value), kit.Int(v); op { switch a, b := kit.Int(s.value), kit.Int(v); op {
case "++":
return Number{kit.Format(a + 1)}
case "--":
return Number{kit.Format(a - 1)}
case "*": case "*":
return Number{kit.Format(a * b)} return Number{kit.Format(a * b)}
case "/": case "/":
@ -151,8 +155,6 @@ func (m Message) Call(cmd string, arg ...Any) Any {
return String{m.Option(str(args[0]), args[1:]...)} return String{m.Option(str(args[0]), args[1:]...)}
case "Cmd": case "Cmd":
return Message{m.Cmd(args...)} return Message{m.Cmd(args...)}
case "Action":
m.Action(args...)
case "Cmdy": case "Cmdy":
m.Cmdy(args...) m.Cmdy(args...)
case "Copy": case "Copy":
@ -161,15 +163,34 @@ func (m Message) Call(cmd string, arg ...Any) Any {
m.Push(str(args[0]), args[1], args[2:]...) m.Push(str(args[0]), args[1], args[2:]...)
case "Echo": case "Echo":
m.Echo(str(args[0]), args[1:]...) m.Echo(str(args[0]), args[1:]...)
case "Sleep":
m.Sleep(str(args[0]))
case "Table": case "Table":
s := _parse_stack(m.Message) s := _parse_stack(m.Message)
var value Any var value Any
m.Table(func(v ice.Maps) { value = s.call(m.Message, v, nil, Dict{kit.Dict(v)}) }) m.Table(func(val ice.Maps) { value = s.call(m.Message, arg[0], nil, nil, Dict{kit.Dict(val)}) })
return value return value
case "Sleep":
m.Sleep(str(args[0]))
case "Action":
m.Action(args...)
default: default:
m.ErrorNotImplement(cmd) m.ErrorNotImplement(cmd)
} }
return m return m
} }
func (s *Stack) load(m *ice.Message) *Stack {
f := s.pushf(m.Options(STACK, s), "")
f.value["m"] = Message{m}
f.value["kit"] = func(key string, arg ...Any) Any {
kit.For(arg, func(i int, v Any) { arg[i] = trans(v) })
switch key {
case "Dict":
return Dict{kit.Dict(arg...)}
case "List":
return List{kit.List(arg...)}
default:
m.ErrorNotImplement(key)
return nil
}
}
return s
}