From 3c186dd989b65f63df367b55e7235df4ab66f24b Mon Sep 17 00:00:00 2001 From: metaleap Date: Wed, 20 Jun 2018 22:57:08 +0200 Subject: [PATCH] =?UTF-8?q?[autopush]=20=C2=AByeah=20ditch=20that=20DE=20s?= =?UTF-8?q?tuff,=20makes=20your=20computer=20go=20slow=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl-91-buggy-tmplinst/nodes.go | 14 +- .../impl-91-buggy-tmplinst/ti-machine.go | 54 ++--- .../impl-91-gmachine-mark7/gm-basics.go | 212 +++++++++--------- .../impl-91-gmachine-mark7/gm-compile.go | 138 ++++++------ .../impl-91-gmachine-mark7/instruction.go | 30 +-- .../impl-92-stg-machine/stg-basics.go | 10 +- .../impl-92-stg-machine/stg-syn-pprint.go | 94 ++++---- .../impl-92-stg-machine/stg-syn.go | 48 ++-- 1990s-fp-corelang/prettyprint.go | 68 +++--- 1990s-fp-corelang/syn/basics.go | 8 +- 1990s-fp-corelang/syn/non-expr.go | 10 +- 1990s-fp-corelang/syn/walk-freevars.go | 40 ++-- 1990s-fp-corelang/syn/walk-misc.go | 31 ++- 1990s-fp-corelang/util/util.go | 134 +++++------ dummy-multmachine/main.go | 22 +- 15 files changed, 453 insertions(+), 460 deletions(-) diff --git a/1990s-fp-corelang/impl-91-buggy-tmplinst/nodes.go b/1990s-fp-corelang/impl-91-buggy-tmplinst/nodes.go index 64e522c..652f5c1 100644 --- a/1990s-fp-corelang/impl-91-buggy-tmplinst/nodes.go +++ b/1990s-fp-corelang/impl-91-buggy-tmplinst/nodes.go @@ -24,22 +24,22 @@ func isDataNode(node clutil.INode) (isvalue bool) { return } -func (me *tiMachine) instantiate(expression clsyn.IExpr) (resultAddr clutil.Addr) { +func (this *tiMachine) instantiate(expression clsyn.IExpr) (resultAddr clutil.Addr) { switch expr := expression.(type) { case *clsyn.ExprLitFloat: - resultAddr = me.Heap.Alloc(nodeNumFloat(expr.Lit)) + resultAddr = this.Heap.Alloc(nodeNumFloat(expr.Lit)) case *clsyn.ExprLitUInt: - resultAddr = me.Heap.Alloc(nodeNumUint(expr.Lit)) + resultAddr = this.Heap.Alloc(nodeNumUint(expr.Lit)) case *clsyn.ExprCall: - resultAddr = me.Heap.Alloc(&nodeAp{me.instantiate(expr.Callee), me.instantiate(expr.Arg)}) + resultAddr = this.Heap.Alloc(&nodeAp{this.instantiate(expr.Callee), this.instantiate(expr.Arg)}) case *clsyn.ExprIdent: - resultAddr = me.Env.LookupOrPanic(expr.Name) + resultAddr = this.Env.LookupOrPanic(expr.Name) case *clsyn.ExprLetIn: for _, def := range expr.Defs { ndef := nodeDef(*def) - me.Env[def.Name] = me.Heap.Alloc(&ndef) + this.Env[def.Name] = this.Heap.Alloc(&ndef) } - resultAddr = me.instantiate(expr.Body) + resultAddr = this.instantiate(expr.Body) case *clsyn.ExprCaseOf, *clsyn.ExprCtor: panic("instantiate: expr type coming soon") default: diff --git a/1990s-fp-corelang/impl-91-buggy-tmplinst/ti-machine.go b/1990s-fp-corelang/impl-91-buggy-tmplinst/ti-machine.go index b7d053d..8afd082 100644 --- a/1990s-fp-corelang/impl-91-buggy-tmplinst/ti-machine.go +++ b/1990s-fp-corelang/impl-91-buggy-tmplinst/ti-machine.go @@ -26,70 +26,70 @@ func CompileToMachine(mod *clsyn.SynMod) (clutil.IMachine, []error) { return me, nil } -func (me *tiMachine) String(result interface{}) string { return fmt.Sprintf("%v", result) } +func (this *tiMachine) String(result interface{}) string { return fmt.Sprintf("%v", result) } -func (me *tiMachine) Eval(name string) (val interface{}, stats clutil.Stats, err error) { +func (this *tiMachine) Eval(name string) (val interface{}, stats clutil.Stats, err error) { defer clutil.Catch(&err) - addr := me.Env[name] + addr := this.Env[name] if addr == 0 { panic("undefined: " + name) } else { - me.Stack = clutil.StackA{addr} - me.eval() - val, stats = me.Heap[me.Stack[0]], me.Stats + this.Stack = clutil.StackA{addr} + this.eval() + val, stats = this.Heap[this.Stack[0]], this.Stats } return } -func (me *tiMachine) eval() { - for me.Stats.NumSteps, me.Stats.NumAppls = 0, 0; !me.isFinalState(); me.step() { +func (this *tiMachine) eval() { + for this.Stats.NumSteps, this.Stats.NumAppls = 0, 0; !this.isFinalState(); this.step() { } } -func (me *tiMachine) isFinalState() bool { - return len(me.Stack) == 1 && isDataNode(me.Heap[me.Stack[0]]) +func (this *tiMachine) isFinalState() bool { + return len(this.Stack) == 1 && isDataNode(this.Heap[this.Stack[0]]) } -func (me *tiMachine) step() { - if me.Stats.NumSteps++; me.Stats.NumSteps > 9999 { +func (this *tiMachine) step() { + if this.Stats.NumSteps++; this.Stats.NumSteps > 9999 { panic("infinite loop") } - addr := me.Stack.Top(0) - obj := me.Heap[addr] + addr := this.Stack.Top(0) + obj := this.Heap[addr] switch n := obj.(type) { case nodeNumFloat, nodeNumUint: panic("number applied as a function") case *nodeAp: - me.Stats.NumAppls++ - me.Stack.Push(n.Callee) + this.Stats.NumAppls++ + this.Stack.Push(n.Callee) case *nodeDef: - oldenv := me.Env - me.Env = make(map[string]clutil.Addr, len(n.Args)+len(oldenv)) + oldenv := this.Env + this.Env = make(map[string]clutil.Addr, len(n.Args)+len(oldenv)) for k, v := range oldenv { - me.Env[k] = v + this.Env[k] = v } - argsaddrs := me.getArgs(n.Name, len(n.Args)) + argsaddrs := this.getArgs(n.Name, len(n.Args)) for i, argname := range n.Args { - me.Env[argname] = argsaddrs[i] + this.Env[argname] = argsaddrs[i] } - resultaddr := me.instantiate(n.Body) - me.Stack = me.Stack.Dropped(1 + len(n.Args)).Pushed(resultaddr) + resultaddr := this.instantiate(n.Body) + this.Stack = this.Stack.Dropped(1 + len(n.Args)).Pushed(resultaddr) // me.Env = oldenv } } -func (me *tiMachine) getArgs(name string, count int) (argsaddrs []clutil.Addr) { - pos := me.Stack.Pos(count) +func (this *tiMachine) getArgs(name string, count int) (argsaddrs []clutil.Addr) { + pos := this.Stack.Pos(count) if pos < 0 { panic(name + ": not enough arguments given") } argsaddrs = make([]clutil.Addr, count) for i := 0; i < count; i++ { - addr := me.Stack[pos+i] - nap, _ := me.Heap[addr].(*nodeAp) + addr := this.Stack[pos+i] + nap, _ := this.Heap[addr].(*nodeAp) argsaddrs[count-1-i] = nap.Arg } return diff --git a/1990s-fp-corelang/impl-91-gmachine-mark7/gm-basics.go b/1990s-fp-corelang/impl-91-gmachine-mark7/gm-basics.go index 97a1428..84e6ff3 100644 --- a/1990s-fp-corelang/impl-91-gmachine-mark7/gm-basics.go +++ b/1990s-fp-corelang/impl-91-gmachine-mark7/gm-basics.go @@ -24,89 +24,89 @@ type dumpedState struct { Stack clutil.StackA } -func (me *gMachine) Eval(name string) (val interface{}, stats clutil.Stats, err error) { +func (this *gMachine) Eval(name string) (val interface{}, stats clutil.Stats, err error) { defer clutil.Catch(&err) - me.StackA, me.StackDump, me.StackInts, me.StackStrs = make(clutil.StackA, 0, 64), make([]dumpedState, 0, 16), make(clutil.StackI, 0, 64), make(clutil.StackS, 0, 16) - me.Code = code{{Op: INSTR_PUSHGLOBAL, Name: name}, {Op: INSTR_EVAL}} + this.StackA, this.StackDump, this.StackInts, this.StackStrs = make(clutil.StackA, 0, 64), make([]dumpedState, 0, 16), make(clutil.StackI, 0, 64), make(clutil.StackS, 0, 16) + this.Code = code{{Op: INSTR_PUSHGLOBAL, Name: name}, {Op: INSTR_EVAL}} // println(me.Heap[me.Globals["times"]].(nodeGlobal).Code.String()) - me.eval() - stats, val = me.Stats, me.Heap[me.StackA.Top0()] + this.eval() + stats, val = this.Stats, this.Heap[this.StackA.Top0()] return } -func (me *gMachine) eval() { - for me.Stats.NumSteps, me.Stats.NumAppls, me.Stats.MaxStack = 0, 0, 0; len(me.Code) > 0; me.Stats.NumSteps++ { - next := me.Code[1:] +func (this *gMachine) eval() { + for this.Stats.NumSteps, this.Stats.NumAppls, this.Stats.MaxStack = 0, 0, 0; len(this.Code) > 0; this.Stats.NumSteps++ { + next := this.Code[1:] - switch me.Code[0].Op { + switch this.Code[0].Op { case INSTR_PUSHGLOBAL: - addr := me.Globals.LookupOrPanic(me.Code[0].Name) - me.StackA.Push(addr) + addr := this.Globals.LookupOrPanic(this.Code[0].Name) + this.StackA.Push(addr) case INSTR_PUSHINT: - addr := me.Heap.Alloc(nodeInt(me.Code[0].Int)) - me.StackA.Push(addr) + addr := this.Heap.Alloc(nodeInt(this.Code[0].Int)) + this.StackA.Push(addr) case INSTR_PUSHARG: - me.StackA.Push(me.StackA.Top(me.Code[0].Int)) + this.StackA.Push(this.StackA.Top(this.Code[0].Int)) case INSTR_MAKEAPPL: - addrcallee := me.StackA.Top0() - addrarg := me.StackA.Top1() - addr := me.Heap.Alloc(nodeAppl{Callee: addrcallee, Arg: addrarg}) - me.StackA[me.StackA.Pos1()] = addr - me.StackA = me.StackA.Dropped(1) + addrcallee := this.StackA.Top0() + addrarg := this.StackA.Top1() + addr := this.Heap.Alloc(nodeAppl{Callee: addrcallee, Arg: addrarg}) + this.StackA[this.StackA.Pos1()] = addr + this.StackA = this.StackA.Dropped(1) case INSTR_UPDATE: - pointee := me.StackA.Top0() - addrptr := me.Heap.Alloc(nodeIndirection{Addr: pointee}) - me.StackA = me.StackA.Dropped(1) - me.StackA[me.StackA.Pos(me.Code[0].Int)] = addrptr + pointee := this.StackA.Top0() + addrptr := this.Heap.Alloc(nodeIndirection{Addr: pointee}) + this.StackA = this.StackA.Dropped(1) + this.StackA[this.StackA.Pos(this.Code[0].Int)] = addrptr case INSTR_POP: - me.StackA = me.StackA.Dropped(me.Code[0].Int) + this.StackA = this.StackA.Dropped(this.Code[0].Int) case INSTR_SLIDE: - keep := me.StackA.Top0() - me.StackA = me.StackA.Dropped(me.Code[0].Int) - me.StackA[me.StackA.Pos0()] = keep + keep := this.StackA.Top0() + this.StackA = this.StackA.Dropped(this.Code[0].Int) + this.StackA[this.StackA.Pos0()] = keep case INSTR_ALLOC: - for i := 0; i < me.Code[0].Int; i++ { - me.StackA.Push(me.Heap.Alloc(nodeIndirection{})) + for i := 0; i < this.Code[0].Int; i++ { + this.StackA.Push(this.Heap.Alloc(nodeIndirection{})) } case INSTR_EVAL: - pos := me.StackA.Pos0() - me.StackDump = append(me.StackDump, dumpedState{Code: next, Stack: me.StackA[:pos]}) - me.StackA = me.StackA[pos:] + pos := this.StackA.Pos0() + this.StackDump = append(this.StackDump, dumpedState{Code: next, Stack: this.StackA[:pos]}) + this.StackA = this.StackA[pos:] next = code{{Op: INSTR_UNWIND}} case INSTR_UNWIND: - addr := me.StackA.Top0() - node := me.Heap[addr] + addr := this.StackA.Top0() + node := this.Heap[addr] switch n := node.(type) { case nodeInt, nodeCtor: - if len(me.StackDump) == 0 { + if len(this.StackDump) == 0 { next = code{} } else { - restore := me.StackDump[len(me.StackDump)-1] - next, me.StackDump, me.StackA = - restore.Code, me.StackDump[:len(me.StackDump)-1], append(restore.Stack, addr) + restore := this.StackDump[len(this.StackDump)-1] + next, this.StackDump, this.StackA = + restore.Code, this.StackDump[:len(this.StackDump)-1], append(restore.Stack, addr) } case nodeIndirection: - me.StackA[me.StackA.Pos0()] = n.Addr + this.StackA[this.StackA.Pos0()] = n.Addr next = code{instr{Op: INSTR_UNWIND}} // unwind again case nodeAppl: - me.Stats.NumAppls++ - me.StackA.Push(n.Callee) + this.Stats.NumAppls++ + this.StackA.Push(n.Callee) next = code{instr{Op: INSTR_UNWIND}} // unwind again case nodeGlobal: - if (len(me.StackA) - 1) < n.NumArgs { - if len(me.StackDump) == 0 { + if (len(this.StackA) - 1) < n.NumArgs { + if len(this.StackDump) == 0 { panic("unwinding with too few arguments") } - restore := me.StackDump[len(me.StackDump)-1] - me.StackDump = me.StackDump[:len(me.StackDump)-1] + restore := this.StackDump[len(this.StackDump)-1] + this.StackDump = this.StackDump[:len(this.StackDump)-1] next = restore.Code - me.StackA = restore.Stack.Pushed(me.StackA[0]) + this.StackA = restore.Stack.Pushed(this.StackA[0]) } else { nustack := make(clutil.StackA, 0, n.NumArgs) for i := n.NumArgs; i > 0; i-- { - nustack.Push(me.Heap[me.StackA.Top(i)].(nodeAppl).Arg) + nustack.Push(this.Heap[this.StackA.Top(i)].(nodeAppl).Arg) } - me.StackA = append(me.StackA.Dropped(n.NumArgs), nustack...) + this.StackA = append(this.StackA.Dropped(n.NumArgs), nustack...) next = n.Code } default: @@ -114,9 +114,9 @@ func (me *gMachine) eval() { } case INSTR_PRIM_CMP_EQ, INSTR_PRIM_CMP_NEQ, INSTR_PRIM_CMP_LT, INSTR_PRIM_CMP_LEQ, INSTR_PRIM_CMP_GT, INSTR_PRIM_CMP_GEQ: if _MARK7 { - num1, num2 := me.StackInts.Top0(), me.StackInts.Top1() + num1, num2 := this.StackInts.Top0(), this.StackInts.Top1() var istrue bool - switch me.Code[0].Op { + switch this.Code[0].Op { case INSTR_PRIM_CMP_EQ: istrue = (num1 == num2) case INSTR_PRIM_CMP_NEQ: @@ -136,12 +136,12 @@ func (me *gMachine) eval() { } else { result = "False" } - me.StackInts = me.StackInts.Dropped(2) - me.StackStrs.Push(result) + this.StackInts = this.StackInts.Dropped(2) + this.StackStrs.Push(result) } else { - node1, node2 := me.Heap[me.StackA.Top0()].(nodeInt), me.Heap[me.StackA.Top1()].(nodeInt) + node1, node2 := this.Heap[this.StackA.Top0()].(nodeInt), this.Heap[this.StackA.Top1()].(nodeInt) var istrue bool - switch me.Code[0].Op { + switch this.Code[0].Op { case INSTR_PRIM_CMP_EQ: istrue = (node1 == node2) case INSTR_PRIM_CMP_NEQ: @@ -161,15 +161,15 @@ func (me *gMachine) eval() { } else { result.Tag = "False" } - addr := me.Heap.Alloc(result) - me.StackA = me.StackA.Dropped(1) - me.StackA[me.StackA.Pos0()] = addr + addr := this.Heap.Alloc(result) + this.StackA = this.StackA.Dropped(1) + this.StackA[this.StackA.Pos0()] = addr } case INSTR_PRIM_AR_ADD, INSTR_PRIM_AR_SUB, INSTR_PRIM_AR_MUL, INSTR_PRIM_AR_DIV: if _MARK7 { - num1, num2 := me.StackInts.Top0(), me.StackInts.Top1() + num1, num2 := this.StackInts.Top0(), this.StackInts.Top1() var result int - switch me.Code[0].Op { + switch this.Code[0].Op { case INSTR_PRIM_AR_ADD: result = num1 + num2 case INSTR_PRIM_AR_SUB: @@ -179,12 +179,12 @@ func (me *gMachine) eval() { case INSTR_PRIM_AR_DIV: result = num1 / num2 } - me.StackInts = me.StackInts.Dropped(1) - me.StackInts[me.StackInts.Pos0()] = result + this.StackInts = this.StackInts.Dropped(1) + this.StackInts[this.StackInts.Pos0()] = result } else { - node1, node2 := me.Heap[me.StackA.Top0()].(nodeInt), me.Heap[me.StackA.Top1()].(nodeInt) + node1, node2 := this.Heap[this.StackA.Top0()].(nodeInt), this.Heap[this.StackA.Top1()].(nodeInt) var result nodeInt - switch me.Code[0].Op { + switch this.Code[0].Op { case INSTR_PRIM_AR_ADD: result = node1 + node2 case INSTR_PRIM_AR_SUB: @@ -194,100 +194,100 @@ func (me *gMachine) eval() { case INSTR_PRIM_AR_DIV: result = node1 / node2 } - addr := me.Heap.Alloc(result) - me.StackA = me.StackA.Dropped(1) - me.StackA[me.StackA.Pos0()] = addr + addr := this.Heap.Alloc(result) + this.StackA = this.StackA.Dropped(1) + this.StackA[this.StackA.Pos0()] = addr } case INSTR_PRIM_AR_NEG: if _MARK7 { - me.StackInts[me.StackInts.Pos0()] = -me.StackInts[me.StackInts.Pos0()] + this.StackInts[this.StackInts.Pos0()] = -this.StackInts[this.StackInts.Pos0()] } else { - node := me.Heap[me.StackA.Top0()].(nodeInt) - addr := me.Heap.Alloc(-node) - me.StackA[me.StackA.Pos0()] = addr + node := this.Heap[this.StackA.Top0()].(nodeInt) + addr := this.Heap.Alloc(-node) + this.StackA[this.StackA.Pos0()] = addr } case INSTR_PRIM_COND: if _MARK7 { - ctortag := me.StackStrs.Top0() - me.StackStrs = me.StackStrs.Dropped(1) + ctortag := this.StackStrs.Top0() + this.StackStrs = this.StackStrs.Dropped(1) if ctortag == "True" { - next = append(me.Code[0].CondThen, next...) + next = append(this.Code[0].CondThen, next...) } else if ctortag == "False" { - next = append(me.Code[0].CondElse, next...) + next = append(this.Code[0].CondElse, next...) } else { panic(ctortag) } } else { - if node := me.Heap[me.StackA.Top0()].(nodeCtor); node.Tag == "True" { - next = append(me.Code[0].CondThen, next...) + if node := this.Heap[this.StackA.Top0()].(nodeCtor); node.Tag == "True" { + next = append(this.Code[0].CondThen, next...) } else if node.Tag == "False" { - next = append(me.Code[0].CondElse, next...) + next = append(this.Code[0].CondElse, next...) } else { panic(node.Tag) } - me.StackA = me.StackA.Dropped(1) + this.StackA = this.StackA.Dropped(1) } case INSTR_CTOR_PACK: - arity := me.Code[0].CtorArity - node := nodeCtor{Tag: me.Code[0].Name, Items: make([]clutil.Addr, arity)} + arity := this.Code[0].CtorArity + node := nodeCtor{Tag: this.Code[0].Name, Items: make([]clutil.Addr, arity)} for i := 0; i < arity; i++ { - node.Items[i] = me.StackA.Top(i) + node.Items[i] = this.StackA.Top(i) } - me.StackA = me.StackA.Dropped(arity).Pushed(me.Heap.Alloc(node)) + this.StackA = this.StackA.Dropped(arity).Pushed(this.Heap.Alloc(node)) case INSTR_CASE_JUMP: - node := me.Heap[me.StackA.Top0()].(nodeCtor) - if code := me.Code[0].CaseJump[node.Tag]; len(code) > 0 { + node := this.Heap[this.StackA.Top0()].(nodeCtor) + if code := this.Code[0].CaseJump[node.Tag]; len(code) > 0 { next = append(code, next...) - } else if code = me.Code[0].CaseJump["_"]; len(code) > 0 { // jump to default case + } else if code = this.Code[0].CaseJump["_"]; len(code) > 0 { // jump to default case next = append(code, next...) } else { panic("no matching alternative in CASE OF for ‹" + node.Tag + "," + strconv.Itoa(len(node.Items)) + "› and no default (tag 0) alternative either") } case INSTR_CASE_SPLIT: - node := me.Heap[me.StackA.Top0()].(nodeCtor) - me.StackA = me.StackA.Dropped(1) - for i := /*len(node.Items)*/ me.Code[0].Int - 1; i > -1; i-- { - me.StackA.Push(node.Items[i]) + node := this.Heap[this.StackA.Top0()].(nodeCtor) + this.StackA = this.StackA.Dropped(1) + for i := /*len(node.Items)*/ this.Code[0].Int - 1; i > -1; i-- { + this.StackA.Push(node.Items[i]) } case INSTR_MARK7_PUSHINTVAL: - me.StackInts.Push(me.Code[0].Int) + this.StackInts.Push(this.Code[0].Int) case INSTR_MARK7_MAKENODEBOOL: - me.StackA.Push(me.Heap.Alloc(nodeCtor{Tag: me.StackStrs.Top0()})) - me.StackStrs = me.StackStrs.Dropped(1) + this.StackA.Push(this.Heap.Alloc(nodeCtor{Tag: this.StackStrs.Top0()})) + this.StackStrs = this.StackStrs.Dropped(1) case INSTR_MARK7_MAKENODEINT: - me.StackA.Push(me.Heap.Alloc(nodeInt(me.StackInts.Top0()))) - me.StackInts = me.StackInts.Dropped(1) + this.StackA.Push(this.Heap.Alloc(nodeInt(this.StackInts.Top0()))) + this.StackInts = this.StackInts.Dropped(1) case INSTR_MARK7_PUSHNODEINT: - addr := me.StackA.Top0() - me.StackA = me.StackA.Dropped(1) - switch node := me.Heap[addr].(type) { + addr := this.StackA.Top0() + this.StackA = this.StackA.Dropped(1) + switch node := this.Heap[addr].(type) { case nodeCtor: - me.StackStrs.Push(node.Tag) + this.StackStrs.Push(node.Tag) case nodeInt: - me.StackInts.Push(int(node)) + this.StackInts.Push(int(node)) } default: - panic(me.Code[0].Op) + panic(this.Code[0].Op) } - if me.Code = next; me.Stats.MaxStack < len(me.StackA) { - me.Stats.MaxStack = len(me.StackA) + if this.Code = next; this.Stats.MaxStack < len(this.StackA) { + this.Stats.MaxStack = len(this.StackA) } - if me.Stats.NumSteps > 9999999 { + if this.Stats.NumSteps > 9999999 { panic("exceeded 10 million steps: probable infinite loop, stopping evaluation") } } - me.Stats.HeapSize = len(me.Heap) + this.Stats.HeapSize = len(this.Heap) } -func (me *gMachine) String(result interface{}) string { +func (this *gMachine) String(result interface{}) string { switch res := result.(type) { case nodeInt: return "#" + strconv.Itoa(int(res)) case nodeCtor: s := "‹" + res.Tag for _, addr := range res.Items { - s += " " + me.String(me.Heap[addr]) + s += " " + this.String(this.Heap[addr]) } return s + "›" case nodeIndirection: diff --git a/1990s-fp-corelang/impl-91-gmachine-mark7/gm-compile.go b/1990s-fp-corelang/impl-91-gmachine-mark7/gm-compile.go index 019901c..da88893 100644 --- a/1990s-fp-corelang/impl-91-gmachine-mark7/gm-compile.go +++ b/1990s-fp-corelang/impl-91-gmachine-mark7/gm-compile.go @@ -85,113 +85,111 @@ func CompileToMachine(mod *SynMod) (util.IMachine, []error) { return &me, errs } -func (me *gMachine) compileGlobal_SchemeSC(global *SynDef) (node nodeGlobal, err error) { +func (this *gMachine) compileGlobal_SchemeSC(global *SynDef) (node nodeGlobal, err error) { defer util.Catch(&err) argsenv := make(env, len(global.Args)) for i, arg := range global.Args { argsenv[arg] = i } if _MARK7 { - node = nodeGlobal{len(global.Args), me.compileExprMark7_SchemeR(len(global.Args))(global.Body, argsenv)} + node = nodeGlobal{len(global.Args), this.compileExprMark7_SchemeR(len(global.Args))(global.Body, argsenv)} } else { - node = nodeGlobal{len(global.Args), me.compileGlobalBody_SchemeR(global.Body, argsenv)} + node = nodeGlobal{len(global.Args), this.compileGlobalBody_SchemeR(global.Body, argsenv)} } return } -func (me *gMachine) compileGlobalBody_SchemeR(bodyexpr IExpr, argsEnv env) code { - return append(me.compileExprStrict_SchemeE(bodyexpr, argsEnv), +func (this *gMachine) compileGlobalBody_SchemeR(bodyexpr IExpr, argsEnv env) code { + return append(this.compileExprStrict_SchemeE(bodyexpr, argsEnv), instr{Op: INSTR_UPDATE, Int: len(argsEnv)}, instr{Op: INSTR_POP, Int: len(argsEnv)}, instr{Op: INSTR_UNWIND}, ) } -func (me *gMachine) compileExprMark7_SchemeR(d int) compilation { +func (this *gMachine) compileExprMark7_SchemeR(d int) compilation { return func(expr IExpr, argsenv env) code { - return me.compileExprMark7_SchemeR_(expr, argsenv, d) + return this.compileExprMark7_SchemeR_(expr, argsenv, d) } } - -func (me *gMachine) compileExprMark7_SchemeR_(expression IExpr, argsEnv env, d int) code { +func (this *gMachine) compileExprMark7_SchemeR_(expression IExpr, argsEnv env, d int) code { switch expr := expression.(type) { case *ExprLetIn: - return me.compileLet(me.compileExprMark7_SchemeR(d+len(expr.Defs)), expr, argsEnv, 0) + return this.compileLet(this.compileExprMark7_SchemeR(d+len(expr.Defs)), expr, argsEnv, 0) case *ExprCall: if callee, _ := expr.Callee.(*ExprCall); callee != nil { - if ifcode := me.compilePrimIfMaybe(me.compileExprMark7_SchemeR(d), expr, callee, argsEnv, true); len(ifcode) > 0 { + if ifcode := this.compilePrimIfMaybe(this.compileExprMark7_SchemeR(d), expr, callee, argsEnv, true); len(ifcode) > 0 { return ifcode } } case *ExprCaseOf: - return append(me.compileExprStrict_SchemeE(expr.Scrut, argsEnv), - instr{Op: INSTR_CASE_JUMP, CaseJump: me.compileCaseAlts_SchemeD(me.compileExprMark7StrictSplit_SchemeR(d), expr.Alts, argsEnv)}) + return append(this.compileExprStrict_SchemeE(expr.Scrut, argsEnv), + instr{Op: INSTR_CASE_JUMP, CaseJump: this.compileCaseAlts_SchemeD(this.compileExprMark7StrictSplit_SchemeR(d), expr.Alts, argsEnv)}) } - return append(me.compileExprStrict_SchemeE(expression, argsEnv), + return append(this.compileExprStrict_SchemeE(expression, argsEnv), instr{Op: INSTR_UPDATE, Int: d}, instr{Op: INSTR_POP, Int: d}, instr{Op: INSTR_UNWIND}) } - -func (me *gMachine) compileExprStrict_SchemeE(expression IExpr, argsEnv env) code { +func (this *gMachine) compileExprStrict_SchemeE(expression IExpr, argsEnv env) code { switch expr := expression.(type) { case *ExprLitUInt: return code{{Op: INSTR_PUSHINT, Int: int(expr.Lit)}} case *ExprLetIn: - return me.compileLet(me.compileExprStrict_SchemeE, expr, argsEnv, INSTR_SLIDE) + return this.compileLet(this.compileExprStrict_SchemeE, expr, argsEnv, INSTR_SLIDE) case *ExprCtor: - comp := me.compileExprStrict_SchemeE + comp := this.compileExprStrict_SchemeE if _MARK7 { - comp = me.compileExprLazy_SchemeC + comp = this.compileExprLazy_SchemeC } - return me.compileCtorAppl(comp, expr, nil, argsEnv, _MARK7) + return this.compileCtorAppl(comp, expr, nil, argsEnv, _MARK7) case *ExprCaseOf: - return append(me.compileExprStrict_SchemeE(expr.Scrut, argsEnv), instr{Op: INSTR_CASE_JUMP, - CaseJump: me.compileCaseAlts_SchemeD(me.compileExprStrictSplitSlide_SchemeA, expr.Alts, argsEnv)}) + return append(this.compileExprStrict_SchemeE(expr.Scrut, argsEnv), instr{Op: INSTR_CASE_JUMP, + CaseJump: this.compileCaseAlts_SchemeD(this.compileExprStrictSplitSlide_SchemeA, expr.Alts, argsEnv)}) case *ExprCall: if ctor, ctorrevargs := expr.FlattenedIfCtor(); ctor != nil { - comp := me.compileExprStrict_SchemeE + comp := this.compileExprStrict_SchemeE if _MARK7 { - comp = me.compileExprLazy_SchemeC + comp = this.compileExprLazy_SchemeC } - return me.compileCtorAppl(comp, ctor, ctorrevargs, argsEnv, _MARK7) + return this.compileCtorAppl(comp, ctor, ctorrevargs, argsEnv, _MARK7) } - if instrs := me.compilePrimsMaybe(me.compileExprStrict_SchemeE, expr, argsEnv, 1, _MARK7); len(instrs) > 0 { + if instrs := this.compilePrimsMaybe(this.compileExprStrict_SchemeE, expr, argsEnv, 1, _MARK7); len(instrs) > 0 { return instrs } } - return append(me.compileExprLazy_SchemeC(expression, argsEnv), instr{Op: INSTR_EVAL}) + return append(this.compileExprLazy_SchemeC(expression, argsEnv), instr{Op: INSTR_EVAL}) } -func (me *gMachine) compileExprStrictSplitSlide_SchemeA(offset int, expr IExpr, argsEnv env) code { - return append(me.compileExprStrictSplit(me.compileExprStrict_SchemeE, expr, argsEnv, offset), +func (this *gMachine) compileExprStrictSplitSlide_SchemeA(offset int, expr IExpr, argsEnv env) code { + return append(this.compileExprStrictSplit(this.compileExprStrict_SchemeE, expr, argsEnv, offset), instr{Op: INSTR_SLIDE, Int: offset}) } -func (me *gMachine) compileExprMark7StrictSplit_SchemeR(d int) compilationN { +func (this *gMachine) compileExprMark7StrictSplit_SchemeR(d int) compilationN { return func(offset int, expr IExpr, argsEnv env) code { - return me.compileExprStrictSplit(me.compileExprMark7_SchemeR(d), expr, argsEnv, offset) + return this.compileExprStrictSplit(this.compileExprMark7_SchemeR(d), expr, argsEnv, offset) } } -func (me *gMachine) compileExprStrictSplit(comp compilation, expr IExpr, argsEnv env, offset int) code { +func (this *gMachine) compileExprStrictSplit(comp compilation, expr IExpr, argsEnv env, offset int) code { return append(code{{Op: INSTR_CASE_SPLIT, Int: offset}}, comp(expr, argsEnv)...) } -func (me *gMachine) compileExprStrictMark7_SchemeB(expression IExpr, argsEnv env) code { +func (this *gMachine) compileExprStrictMark7_SchemeB(expression IExpr, argsEnv env) code { switch expr := expression.(type) { case *ExprLitUInt: return code{{Op: INSTR_MARK7_PUSHINTVAL, Int: int(expr.Lit)}} case *ExprLetIn: - return me.compileLet(me.compileExprStrictMark7_SchemeB, expr, argsEnv, INSTR_POP) + return this.compileLet(this.compileExprStrictMark7_SchemeB, expr, argsEnv, INSTR_POP) case *ExprCall: - if instrs := me.compilePrimsMaybe(me.compileExprStrictMark7_SchemeB, expr, argsEnv, 0, false); len(instrs) > 0 { + if instrs := this.compilePrimsMaybe(this.compileExprStrictMark7_SchemeB, expr, argsEnv, 0, false); len(instrs) > 0 { return instrs } } - return append(me.compileExprStrict_SchemeE(expression, argsEnv), instr{Op: INSTR_MARK7_PUSHNODEINT}) + return append(this.compileExprStrict_SchemeE(expression, argsEnv), instr{Op: INSTR_MARK7_PUSHNODEINT}) } -func (me *gMachine) compileExprLazy_SchemeC(expression IExpr, argsEnv env) code { +func (this *gMachine) compileExprLazy_SchemeC(expression IExpr, argsEnv env) code { switch expr := expression.(type) { case *ExprLitUInt: return code{{Op: INSTR_PUSHINT, Int: int(expr.Lit)}} @@ -202,40 +200,40 @@ func (me *gMachine) compileExprLazy_SchemeC(expression IExpr, argsEnv env) code return code{{Op: INSTR_PUSHGLOBAL, Name: expr.Name}} case *ExprCall: if ctor, ctorrevargs := expr.FlattenedIfCtor(); ctor != nil { - return me.compileCtorAppl(me.compileExprLazy_SchemeC, ctor, ctorrevargs, argsEnv, false) + return this.compileCtorAppl(this.compileExprLazy_SchemeC, ctor, ctorrevargs, argsEnv, false) } return append(append( - me.compileExprLazy_SchemeC(expr.Arg, argsEnv), - me.compileExprLazy_SchemeC(expr.Callee, me.envOffsetBy(argsEnv, 1))..., + this.compileExprLazy_SchemeC(expr.Arg, argsEnv), + this.compileExprLazy_SchemeC(expr.Callee, this.envOffsetBy(argsEnv, 1))..., ), instr{Op: INSTR_MAKEAPPL}) case *ExprLetIn: - return me.compileLet(me.compileExprLazy_SchemeC, expr, argsEnv, INSTR_SLIDE) + return this.compileLet(this.compileExprLazy_SchemeC, expr, argsEnv, INSTR_SLIDE) case *ExprCtor: - return me.compileCtorAppl(me.compileExprLazy_SchemeC, expr, nil, argsEnv, false) + return this.compileCtorAppl(this.compileExprLazy_SchemeC, expr, nil, argsEnv, false) case *ExprCaseOf: dynname := "#case" + strconv.FormatInt(time.Now().UnixNano(), 16) - dynglobaldef, dynglobalcall := expr.ExtractIntoDef(dynname, true, NewLookupEnv(nil, me.Globals, nil, nil)) - if dynglobalnode, err := me.compileGlobal_SchemeSC(dynglobaldef); err != nil { + dynglobaldef, dynglobalcall := expr.ExtractIntoDef(dynname, true, NewLookupEnv(nil, this.Globals, nil, nil)) + if dynglobalnode, err := this.compileGlobal_SchemeSC(dynglobaldef); err != nil { panic(err) } else { - me.Globals[dynglobaldef.Name] = me.Heap.Alloc(dynglobalnode) + this.Globals[dynglobaldef.Name] = this.Heap.Alloc(dynglobalnode) } - return me.compileExprLazy_SchemeC(dynglobalcall, argsEnv) + return this.compileExprLazy_SchemeC(dynglobalcall, argsEnv) default: panic(expr) } } -func (me *gMachine) compileCtorAppl(comp compilation, ctor *ExprCtor, reverseArgs []IExpr, argsEnv env, fromMark7E bool) code { +func (this *gMachine) compileCtorAppl(comp compilation, ctor *ExprCtor, reverseArgs []IExpr, argsEnv env, fromMark7E bool) code { if len(reverseArgs) != ctor.Arity { dynglobalname := "#ctor#" + ctor.Tag + "#" + strconv.Itoa(ctor.Arity) - dynglobaladdr := me.Globals[dynglobalname] + dynglobaladdr := this.Globals[dynglobalname] if dynglobaladdr == 0 { dynglobaldef := ctor.ExtractIntoDef(dynglobalname, true) - if dynglobalnode, err := me.compileGlobal_SchemeSC(dynglobaldef); err != nil { + if dynglobalnode, err := this.compileGlobal_SchemeSC(dynglobaldef); err != nil { panic(err) } else { - me.Globals[dynglobalname] = me.Heap.Alloc(dynglobalnode) + this.Globals[dynglobalname] = this.Heap.Alloc(dynglobalnode) } } var dynglobalcall IExpr = Id(dynglobalname) @@ -246,7 +244,7 @@ func (me *gMachine) compileCtorAppl(comp compilation, ctor *ExprCtor, reverseArg } instrs := make(code, 0, len(reverseArgs)*3) // arbitrary extra cap, exact need not known for i, arg := range reverseArgs { - instrs = append(instrs, comp(arg, me.envOffsetBy(argsEnv, i))...) + instrs = append(instrs, comp(arg, this.envOffsetBy(argsEnv, i))...) if fromMark7E { instrs = append(instrs, instr{Op: INSTR_EVAL}) } @@ -254,22 +252,22 @@ func (me *gMachine) compileCtorAppl(comp compilation, ctor *ExprCtor, reverseArg return append(instrs, instr{Op: INSTR_CTOR_PACK, Name: ctor.Tag, CtorArity: ctor.Arity}) } -func (me *gMachine) compileLet(compbody compilation, let *ExprLetIn, argsEnv env, finalOp instruction) (instrs code) { +func (this *gMachine) compileLet(compbody compilation, let *ExprLetIn, argsEnv env, finalOp instruction) (instrs code) { n := len(let.Defs) if let.Rec { instrs = code{{Op: INSTR_ALLOC, Int: n}} } - bodyargsenv := me.envOffsetBy(argsEnv, n) + bodyargsenv := this.envOffsetBy(argsEnv, n) for i, def := range let.Defs { if bodyargsenv[def.Name] = n - (i + 1); !let.Rec { - instrs = append(instrs, me.compileExprLazy_SchemeC(def.Body, me.envOffsetBy(argsEnv, i))...) + instrs = append(instrs, this.compileExprLazy_SchemeC(def.Body, this.envOffsetBy(argsEnv, i))...) } } if let.Rec { for i, def := range let.Defs { - instrs = append(instrs, me.compileExprLazy_SchemeC(def.Body, bodyargsenv)...) + instrs = append(instrs, this.compileExprLazy_SchemeC(def.Body, bodyargsenv)...) instrs = append(instrs, instr{Op: INSTR_UPDATE, Int: n - (i + 1)}) } } @@ -280,29 +278,29 @@ func (me *gMachine) compileLet(compbody compilation, let *ExprLetIn, argsEnv env return } -func (me *gMachine) compileCaseAlts_SchemeD(compn compilationN, caseAlts []*SynCaseAlt, argsEnv env) (jumpblocks map[string]code) { +func (this *gMachine) compileCaseAlts_SchemeD(compn compilationN, caseAlts []*SynCaseAlt, argsEnv env) (jumpblocks map[string]code) { jumpblocks = make(map[string]code, len(caseAlts)) for _, alt := range caseAlts { - jumpblocks[alt.Tag] = me.compileCaseAlt_SchemeA(compn, alt, argsEnv) + jumpblocks[alt.Tag] = this.compileCaseAlt_SchemeA(compn, alt, argsEnv) } return } -func (me *gMachine) compileCaseAlt_SchemeA(compn compilationN, alt *SynCaseAlt, argsEnv env) code { +func (this *gMachine) compileCaseAlt_SchemeA(compn compilationN, alt *SynCaseAlt, argsEnv env) code { n := len(alt.Binds) - bodyargsenv := me.envOffsetBy(argsEnv, n) + bodyargsenv := this.envOffsetBy(argsEnv, n) for i, name := range alt.Binds { bodyargsenv[name] = i // = n - (i + 1) } return compn(n, alt.Body, bodyargsenv) } -func (me *gMachine) compilePrimsMaybe(comp compilation, expr *ExprCall, argsEnv env, dyadicOffset int, mark7WrapInB bool) code { +func (this *gMachine) compilePrimsMaybe(comp compilation, expr *ExprCall, argsEnv env, dyadicOffset int, mark7WrapInB bool) code { switch callee := expr.Callee.(type) { case *ExprIdent: if callee.Name == "neg" { if mark7WrapInB { - return append(me.compileExprStrictMark7_SchemeB(expr, argsEnv), instr{Op: INSTR_MARK7_MAKENODEINT}) + return append(this.compileExprStrictMark7_SchemeB(expr, argsEnv), instr{Op: INSTR_MARK7_MAKENODEINT}) } return append(comp(expr.Arg, argsEnv), instr{Op: INSTR_PRIM_AR_NEG}) } @@ -314,42 +312,42 @@ func (me *gMachine) compilePrimsMaybe(comp compilation, expr *ExprCall, argsEnv if primdyadic == INSTR_PRIM_AR_ADD || primdyadic == INSTR_PRIM_AR_SUB || primdyadic == INSTR_PRIM_AR_MUL || primdyadic == INSTR_PRIM_AR_DIV { finalop = INSTR_MARK7_MAKENODEINT } - return append(me.compileExprStrictMark7_SchemeB(expr, argsEnv), instr{Op: finalop}) + return append(this.compileExprStrictMark7_SchemeB(expr, argsEnv), instr{Op: finalop}) } return append(append( comp(expr.Arg, argsEnv), - comp(callee.Arg, me.envOffsetBy(argsEnv, dyadicOffset))..., + comp(callee.Arg, this.envOffsetBy(argsEnv, dyadicOffset))..., ), instr{Op: primdyadic}) } } else if maybeif, _ := callee.Callee.(*ExprCall); maybeif != nil { if ifname, _ := maybeif.Callee.(*ExprIdent); ifname != nil && ifname.Name == "if" { compcond, cond, condthen, condelse := comp, maybeif.Arg, callee.Arg, expr.Arg if mark7WrapInB { - compcond = me.compileExprStrictMark7_SchemeB + compcond = this.compileExprStrictMark7_SchemeB } - return me.compilePrimIf(comp, compcond, cond, condthen, condelse, argsEnv) + return this.compilePrimIf(comp, compcond, cond, condthen, condelse, argsEnv) } - } else if ifcode := me.compilePrimIfMaybe(comp, expr, callee, argsEnv, mark7WrapInB); len(ifcode) > 0 { + } else if ifcode := this.compilePrimIfMaybe(comp, expr, callee, argsEnv, mark7WrapInB); len(ifcode) > 0 { return ifcode } } return nil } -func (me *gMachine) compilePrimIfMaybe(comp compilation, expr *ExprCall, exprCallee *ExprCall, argsEnv env, mark7WrapInB bool) code { +func (this *gMachine) compilePrimIfMaybe(comp compilation, expr *ExprCall, exprCallee *ExprCall, argsEnv env, mark7WrapInB bool) code { if maybeif, _ := exprCallee.Callee.(*ExprCall); maybeif != nil { if ifname, _ := maybeif.Callee.(*ExprIdent); ifname != nil && ifname.Name == "if" { compcond, cond, condthen, condelse := comp, maybeif.Arg, exprCallee.Arg, expr.Arg if mark7WrapInB { - compcond = me.compileExprStrictMark7_SchemeB + compcond = this.compileExprStrictMark7_SchemeB } - return me.compilePrimIf(comp, compcond, cond, condthen, condelse, argsEnv) + return this.compilePrimIf(comp, compcond, cond, condthen, condelse, argsEnv) } } return nil } -func (me *gMachine) compilePrimIf(comp compilation, compcond compilation, cond IExpr, condthen IExpr, condelse IExpr, argsEnv env) code { +func (this *gMachine) compilePrimIf(comp compilation, compcond compilation, cond IExpr, condthen IExpr, condelse IExpr, argsEnv env) code { return append(compcond(cond, argsEnv), instr{ Op: INSTR_PRIM_COND, CondThen: comp(condthen, argsEnv), diff --git a/1990s-fp-corelang/impl-91-gmachine-mark7/instruction.go b/1990s-fp-corelang/impl-91-gmachine-mark7/instruction.go index b52da08..1f3263f 100644 --- a/1990s-fp-corelang/impl-91-gmachine-mark7/instruction.go +++ b/1990s-fp-corelang/impl-91-gmachine-mark7/instruction.go @@ -6,9 +6,9 @@ import ( type code []instr -func (me code) String() (s string) { +func (this code) String() (s string) { s = "[" - for i, instr := range me { + for i, instr := range this { if i > 0 { s += " · " } @@ -69,26 +69,26 @@ type instr struct { CaseJump map[string]code } -func (me instr) String() string { - switch me.Op { +func (this instr) String() string { + switch this.Op { case INSTR_UNWIND: return "Unwd" case INSTR_PUSHGLOBAL: - return "Push`" + me.Name + return "Push`" + this.Name case INSTR_PUSHINT: - return "Push=" + strconv.Itoa(me.Int) + return "Push=" + strconv.Itoa(this.Int) case INSTR_PUSHARG: - return "Push@" + strconv.Itoa(me.Int) + return "Push@" + strconv.Itoa(this.Int) case INSTR_SLIDE: - return "Slide:" + strconv.Itoa(me.Int) + return "Slide:" + strconv.Itoa(this.Int) case INSTR_MAKEAPPL: return "MkAp" case INSTR_UPDATE: - return "Upd@" + strconv.Itoa(me.Int) + return "Upd@" + strconv.Itoa(this.Int) case INSTR_POP: - return "Pop@" + strconv.Itoa(me.Int) + return "Pop@" + strconv.Itoa(this.Int) case INSTR_ALLOC: - return "Alloc=" + strconv.Itoa(me.Int) + return "Alloc=" + strconv.Itoa(this.Int) case INSTR_EVAL: return "Eval" case INSTR_PRIM_AR_ADD: @@ -116,11 +116,11 @@ func (me instr) String() string { case INSTR_PRIM_COND: return "Cond" case INSTR_CTOR_PACK: - return "Ctor:" + me.Name + ":" + strconv.Itoa(me.CtorArity) + return "Ctor:" + this.Name + ":" + strconv.Itoa(this.CtorArity) case INSTR_CASE_JUMP: - return "CJmp#" + strconv.Itoa(len(me.CaseJump)) + return "CJmp#" + strconv.Itoa(len(this.CaseJump)) case INSTR_CASE_SPLIT: - return "CSpl=" + strconv.Itoa(me.Int) + return "CSpl=" + strconv.Itoa(this.Int) case INSTR_MARK7_PUSHINTVAL: return "Push:" case INSTR_MARK7_PUSHNODEINT: @@ -130,5 +130,5 @@ func (me instr) String() string { case INSTR_MARK7_MAKENODEBOOL: return "MkNB" } - return strconv.Itoa(int(me.Op)) + return strconv.Itoa(int(this.Op)) } diff --git a/1990s-fp-corelang/impl-92-stg-machine/stg-basics.go b/1990s-fp-corelang/impl-92-stg-machine/stg-basics.go index b0c6910..5d18c83 100644 --- a/1990s-fp-corelang/impl-92-stg-machine/stg-basics.go +++ b/1990s-fp-corelang/impl-92-stg-machine/stg-basics.go @@ -8,17 +8,17 @@ type stgMachine struct { mod synMod } -func (me *stgMachine) Eval(argLessDefName string) (result interface{}, stats util.Stats, err error) { - for i := range me.mod.Binds { - if me.mod.Binds[i].Name == argLessDefName { - result = me.mod.Binds[i] +func (this *stgMachine) Eval(argLessDefName string) (result interface{}, stats util.Stats, err error) { + for i := range this.mod.Binds { + if this.mod.Binds[i].Name == argLessDefName { + result = this.mod.Binds[i] return } } return } -func (me *stgMachine) String(result interface{}) string { +func (this *stgMachine) String(result interface{}) string { if syn, _ := result.(iSyn); syn != nil { return syn.String() } diff --git a/1990s-fp-corelang/impl-92-stg-machine/stg-syn-pprint.go b/1990s-fp-corelang/impl-92-stg-machine/stg-syn-pprint.go index 7ee0d6d..188fc55 100644 --- a/1990s-fp-corelang/impl-92-stg-machine/stg-syn-pprint.go +++ b/1990s-fp-corelang/impl-92-stg-machine/stg-syn-pprint.go @@ -7,118 +7,114 @@ import ( var uglyHackyIndent int -func (me synMod) String() (s string) { - for i := range me.Binds { +func (this synMod) String() (s string) { + for i := range this.Binds { if uglyHackyIndent = 0; i > 0 { s += "\n\n" } - s += me.Binds[i].String() + s += this.Binds[i].String() } return } -func (me synBinding) String() (s string) { - if s = me.Name; me.LamForm.Upd { +func (this synBinding) String() (s string) { + if s = this.Name; this.LamForm.Upd { s += " ¤" } else { s += " Ø" } - if len(me.LamForm.Free) > 0 { + if len(this.LamForm.Free) > 0 { s += " ‹" - for i := range me.LamForm.Free { + for i := range this.LamForm.Free { if i > 0 { s += "," } - s += me.LamForm.Free[i].String() + s += this.LamForm.Free[i].String() } s += "›" } s += " = \\" - for i := range me.LamForm.Args { - s += " " + me.LamForm.Args[i].String() + for i := range this.LamForm.Args { + s += " " + this.LamForm.Args[i].String() } s += " -> " - if _, isatomic := me.LamForm.Body.(iSynExprAtom); isatomic { - s += me.LamForm.Body.String() + if _, isatomic := this.LamForm.Body.(iSynExprAtom); isatomic { + s += this.LamForm.Body.String() } else { uglyHackyIndent++ - s += "\n" + strings.Repeat(" ", uglyHackyIndent) + me.LamForm.Body.String() + s += "\n" + strings.Repeat(" ", uglyHackyIndent) + this.LamForm.Body.String() uglyHackyIndent-- } return } -func (me synExprAtomIdent) String() string { return me.Name } +func (this synExprAtomIdent) String() string { return this.Name } +func (this synExprAtomLitFloat) String() string { return strconv.FormatFloat(this.Lit, 'g', -1, 64) } +func (this synExprAtomLitUInt) String() string { return strconv.FormatUint(this.Lit, 10) } +func (this synExprAtomLitRune) String() string { return strconv.QuoteRune(this.Lit) } +func (this synExprAtomLitText) String() string { return strconv.Quote(this.Lit) } -func (me synExprAtomLitFloat) String() string { return strconv.FormatFloat(me.Lit, 'g', -1, 64) } - -func (me synExprAtomLitUInt) String() string { return strconv.FormatUint(me.Lit, 10) } - -func (me synExprAtomLitRune) String() string { return strconv.QuoteRune(me.Lit) } - -func (me synExprAtomLitText) String() string { return strconv.Quote(me.Lit) } - -func (me synExprLet) String() (s string) { +func (this synExprLet) String() (s string) { s = "LET" - if me.Rec { + if this.Rec { s += " REC" } uglyHackyIndent++ - for i := range me.Binds { - s += "\n" + strings.Repeat(" ", uglyHackyIndent) + me.Binds[i].String() + for i := range this.Binds { + s += "\n" + strings.Repeat(" ", uglyHackyIndent) + this.Binds[i].String() } uglyHackyIndent-- s += "\n" + strings.Repeat(" ", uglyHackyIndent) + "IN\n" uglyHackyIndent++ - s += strings.Repeat(" ", uglyHackyIndent) + me.Body.String() + s += strings.Repeat(" ", uglyHackyIndent) + this.Body.String() uglyHackyIndent-- return } -func (me synExprCall) String() (s string) { - s = "(" + me.Callee.String() - for i := range me.Args { - s += " " + me.Args[i].String() +func (this synExprCall) String() (s string) { + s = "(" + this.Callee.String() + for i := range this.Args { + s += " " + this.Args[i].String() } s += ")" return } -func (me synExprCtor) String() (s string) { - s += "‹" + me.Tag.String() - for i := range me.Args { - s += " " + me.Args[i].String() +func (this synExprCtor) String() (s string) { + s += "‹" + this.Tag.String() + for i := range this.Args { + s += " " + this.Args[i].String() } s += "›" return } -func (me synExprPrimOp) String() string { - return "(" + me.Left.String() + " " + me.PrimOp + " " + me.Right.String() + ")" +func (this synExprPrimOp) String() string { + return "(" + this.Left.String() + " " + this.PrimOp + " " + this.Right.String() + ")" } -func (me synExprCaseOf) String() (s string) { - s = "CASE " + me.Scrut.String() + " OF" +func (this synExprCaseOf) String() (s string) { + s = "CASE " + this.Scrut.String() + " OF" uglyHackyIndent++ - for i := range me.Alts { - s += "\n" + strings.Repeat(" ", uglyHackyIndent) + me.Alts[i].String() + for i := range this.Alts { + s += "\n" + strings.Repeat(" ", uglyHackyIndent) + this.Alts[i].String() } uglyHackyIndent-- return } -func (me synCaseAlt) String() (s string) { - if me.Atom != nil { - s = me.Atom.String() - } else if me.Ctor.Tag.Name != "" { - s = "‹" + me.Ctor.Tag.String() - for i := range me.Ctor.Vars { - s += " " + me.Ctor.Vars[i].String() +func (this synCaseAlt) String() (s string) { + if this.Atom != nil { + s = this.Atom.String() + } else if this.Ctor.Tag.Name != "" { + s = "‹" + this.Ctor.Tag.String() + for i := range this.Ctor.Vars { + s += " " + this.Ctor.Vars[i].String() } s += "›" } else { s = "_" } - s += " -> " + me.Body.String() + s += " -> " + this.Body.String() return } diff --git a/1990s-fp-corelang/impl-92-stg-machine/stg-syn.go b/1990s-fp-corelang/impl-92-stg-machine/stg-syn.go index b435569..1869edc 100644 --- a/1990s-fp-corelang/impl-92-stg-machine/stg-syn.go +++ b/1990s-fp-corelang/impl-92-stg-machine/stg-syn.go @@ -12,7 +12,7 @@ type iSyn interface { type syn struct{} -func (me *syn) setUpd(...func(string) *synBinding) {} +func (this *syn) setUpd(...func(string) *synBinding) {} func (*syn) taggedSyn() {} @@ -21,14 +21,14 @@ type synMod struct { Binds synBindings } -func (me *synMod) setUpd(resolvers ...func(string) *synBinding) { - me.Binds.setUpd() +func (this *synMod) setUpd(resolvers ...func(string) *synBinding) { + this.Binds.setUpd() } type synBindings []*synBinding -func (me synBindings) byName(name string) *synBinding { - for _, bind := range me { +func (this synBindings) byName(name string) *synBinding { + for _, bind := range this { if bind.Name == name { return bind } @@ -36,9 +36,9 @@ func (me synBindings) byName(name string) *synBinding { return nil } -func (me synBindings) setUpd(resolvers ...func(string) *synBinding) { - r := append(resolvers, me.byName) - for _, bind := range me { +func (this synBindings) setUpd(resolvers ...func(string) *synBinding) { + r := append(resolvers, this.byName) + for _, bind := range this { bind.setUpd(r...) } } @@ -54,20 +54,20 @@ type synBinding struct { } } -func (me *synBinding) setUpd(resolvers ...func(string) *synBinding) { - if me.LamForm.Upd = true; len(me.LamForm.Args) > 0 { - me.LamForm.Upd = false - } else if call, iscall := me.LamForm.Body.(*synExprCall); iscall { +func (this *synBinding) setUpd(resolvers ...func(string) *synBinding) { + if this.LamForm.Upd = true; len(this.LamForm.Args) > 0 { + this.LamForm.Upd = false + } else if call, iscall := this.LamForm.Body.(*synExprCall); iscall { for _, r := range resolvers { if def := r(call.Callee.Name); def != nil && len(def.LamForm.Args) != len(call.Args) { - me.LamForm.Upd = false + this.LamForm.Upd = false break } } - } else if _, isctor := me.LamForm.Body.(*synExprCtor); isctor { - me.LamForm.Upd = false + } else if _, isctor := this.LamForm.Body.(*synExprCtor); isctor { + this.LamForm.Upd = false } - me.LamForm.Body.setUpd(resolvers...) + this.LamForm.Body.setUpd(resolvers...) } type iSynExpr interface { @@ -129,9 +129,9 @@ type synExprLet struct { Rec bool } -func (me *synExprLet) setUpd(resolvers ...func(string) *synBinding) { - me.Binds.setUpd(resolvers...) - me.Body.setUpd(append(resolvers, me.Binds.byName)...) +func (this *synExprLet) setUpd(resolvers ...func(string) *synBinding) { + this.Binds.setUpd(resolvers...) + this.Body.setUpd(append(resolvers, this.Binds.byName)...) } type synExprCall struct { @@ -159,9 +159,9 @@ type synExprCaseOf struct { Alts []*synCaseAlt } -func (me *synExprCaseOf) setUpd(resolvers ...func(string) *synBinding) { - me.Scrut.setUpd(resolvers...) - for _, alt := range me.Alts { +func (this *synExprCaseOf) setUpd(resolvers ...func(string) *synBinding) { + this.Scrut.setUpd(resolvers...) + for _, alt := range this.Alts { alt.setUpd(resolvers...) } } @@ -175,6 +175,6 @@ type synCaseAlt struct { Body iSynExpr } -func (me *synCaseAlt) setUpd(resolvers ...func(string) *synBinding) { - me.Body.setUpd(resolvers...) +func (this *synCaseAlt) setUpd(resolvers ...func(string) *synBinding) { + this.Body.setUpd(resolvers...) } diff --git a/1990s-fp-corelang/prettyprint.go b/1990s-fp-corelang/prettyprint.go index 9523d43..7ce26dd 100644 --- a/1990s-fp-corelang/prettyprint.go +++ b/1990s-fp-corelang/prettyprint.go @@ -13,36 +13,36 @@ type SyntaxTreePrinter struct { curIndent int } -func (me *SyntaxTreePrinter) Mod(mod *SynMod) string { +func (this *SyntaxTreePrinter) Mod(mod *SynMod) string { var buf bytes.Buffer for _, def := range mod.Defs { - me.curIndent = 0 - me.def(&buf, def) + this.curIndent = 0 + this.def(&buf, def) buf.WriteString("\n\n") } return buf.String() } -func (me *SyntaxTreePrinter) def(w *bytes.Buffer, def *SynDef) { +func (this *SyntaxTreePrinter) def(w *bytes.Buffer, def *SynDef) { w.WriteString(def.Name) for _, defarg := range def.Args { w.WriteRune(' ') w.WriteString(defarg) } w.WriteString(" =\n") - me.curIndent++ - w.WriteString(strings.Repeat(" ", me.curIndent)) - me.expr(w, def.Body, false) - me.curIndent-- + this.curIndent++ + w.WriteString(strings.Repeat(" ", this.curIndent)) + this.expr(w, def.Body, false) + this.curIndent-- } -func (me *SyntaxTreePrinter) Def(def *SynDef) string { +func (this *SyntaxTreePrinter) Def(def *SynDef) string { var buf bytes.Buffer - me.def(&buf, def) + this.def(&buf, def) return buf.String() } -func (me *SyntaxTreePrinter) expr(w *bytes.Buffer, expression IExpr, parensUnlessAtomic bool) { +func (this *SyntaxTreePrinter) expr(w *bytes.Buffer, expression IExpr, parensUnlessAtomic bool) { if parensUnlessAtomic && !expression.IsAtomic() { w.WriteRune('(') } @@ -75,26 +75,26 @@ func (me *SyntaxTreePrinter) expr(w *bytes.Buffer, expression IExpr, parensUnles w.WriteRune(' ') } w.WriteString("-> ") - me.expr(w, expr.Body, true) + this.expr(w, expr.Body, true) case *ExprCall: - me.expr(w, expr.Callee, true) + this.expr(w, expr.Callee, true) w.WriteRune(' ') - me.expr(w, expr.Arg, true) + this.expr(w, expr.Arg, true) case *ExprLetIn: w.WriteString("LET\n") - me.curIndent++ + this.curIndent++ for _, letdef := range expr.Defs { - w.WriteString(strings.Repeat(" ", me.curIndent)) - me.def(w, letdef) + w.WriteString(strings.Repeat(" ", this.curIndent)) + this.def(w, letdef) w.WriteRune('\n') } - me.curIndent-- - w.WriteString(strings.Repeat(" ", me.curIndent)) + this.curIndent-- + w.WriteString(strings.Repeat(" ", this.curIndent)) w.WriteString("IN\n") - me.curIndent++ - w.WriteString(strings.Repeat(" ", me.curIndent)) - me.expr(w, expr.Body, false) - me.curIndent-- + this.curIndent++ + w.WriteString(strings.Repeat(" ", this.curIndent)) + this.expr(w, expr.Body, false) + this.curIndent-- case *ExprCtor: w.WriteRune('(') w.WriteString(expr.Tag) @@ -103,10 +103,10 @@ func (me *SyntaxTreePrinter) expr(w *bytes.Buffer, expression IExpr, parensUnles w.WriteRune(')') case *ExprCaseOf: w.WriteString("CASE ") - me.expr(w, expr.Scrut, false) + this.expr(w, expr.Scrut, false) w.WriteString(" OF\n") - me.curIndent++ - w.WriteString(strings.Repeat(" ", me.curIndent)) + this.curIndent++ + w.WriteString(strings.Repeat(" ", this.curIndent)) for _, alt := range expr.Alts { w.WriteString(alt.Tag) for _, bind := range alt.Binds { @@ -114,14 +114,14 @@ func (me *SyntaxTreePrinter) expr(w *bytes.Buffer, expression IExpr, parensUnles w.WriteString(bind) } w.WriteString(" ->\n") - me.curIndent++ - w.WriteString(strings.Repeat(" ", me.curIndent)) - me.expr(w, alt.Body, false) - me.curIndent-- + this.curIndent++ + w.WriteString(strings.Repeat(" ", this.curIndent)) + this.expr(w, alt.Body, false) + this.curIndent-- w.WriteRune('\n') - w.WriteString(strings.Repeat(" ", me.curIndent)) + w.WriteString(strings.Repeat(" ", this.curIndent)) } - me.curIndent-- + this.curIndent-- default: panic(fmt.Errorf("unknown expression type %T — %#v", expr, expr)) } @@ -131,8 +131,8 @@ func (me *SyntaxTreePrinter) expr(w *bytes.Buffer, expression IExpr, parensUnles return } -func (me *SyntaxTreePrinter) Expr(expr IExpr) string { +func (this *SyntaxTreePrinter) Expr(expr IExpr) string { var buf bytes.Buffer - me.expr(&buf, expr, false) + this.expr(&buf, expr, false) return buf.String() } diff --git a/1990s-fp-corelang/syn/basics.go b/1990s-fp-corelang/syn/basics.go index 096d049..14cf476 100644 --- a/1990s-fp-corelang/syn/basics.go +++ b/1990s-fp-corelang/syn/basics.go @@ -25,13 +25,13 @@ type syn struct { // parent ISyn } -func (me *syn) init(toks lex.Tokens) { me.toks = toks } +func (this *syn) init(toks lex.Tokens) { this.toks = toks } func (*syn) isExpr() bool { return false } -func (me *syn) Pos() *lex.TokenMeta { return &me.toks[0].Meta } +func (this *syn) Pos() *lex.TokenMeta { return &this.toks[0].Meta } -func (me *syn) Toks() lex.Tokens { return me.toks } +func (this *syn) Toks() lex.Tokens { return this.toks } type expr struct{ syn } @@ -59,4 +59,4 @@ func errTok(tok *lex.Token, msg string) *Error { return errPos(&tok.Meta.Position, msg, len(tok.String())) } -func (me *Error) Error() string { return me.msg } +func (this *Error) Error() string { return this.msg } diff --git a/1990s-fp-corelang/syn/non-expr.go b/1990s-fp-corelang/syn/non-expr.go index b611704..235a6f5 100644 --- a/1990s-fp-corelang/syn/non-expr.go +++ b/1990s-fp-corelang/syn/non-expr.go @@ -4,15 +4,15 @@ type SynMod struct { Defs []*SynDef } -func (me *SynMod) Def(name string) *SynDef { - if i := me.IndexOf(name); i > -1 { - return me.Defs[i] +func (this *SynMod) Def(name string) *SynDef { + if i := this.IndexOf(name); i > -1 { + return this.Defs[i] } return nil } -func (me *SynMod) IndexOf(name string) int { - for i, def := range me.Defs { +func (this *SynMod) IndexOf(name string) int { + for i, def := range this.Defs { if def.Name == name { return i } diff --git a/1990s-fp-corelang/syn/walk-freevars.go b/1990s-fp-corelang/syn/walk-freevars.go index 8eb82af..5714884 100644 --- a/1990s-fp-corelang/syn/walk-freevars.go +++ b/1990s-fp-corelang/syn/walk-freevars.go @@ -1,47 +1,47 @@ package clsyn -func (me *syn) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) {} +func (this *syn) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) {} -func (me *SynDef) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { - me.Body.FreeVars(freeVarNames, append(lookupEnvs, NewLookupEnv(nil, nil, nil, me.Args))...) +func (this *SynDef) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { + this.Body.FreeVars(freeVarNames, append(lookupEnvs, NewLookupEnv(nil, nil, nil, this.Args))...) } -func (me *ExprIdent) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { - if !freeVarNames[me.Name] { +func (this *ExprIdent) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { + if !freeVarNames[this.Name] { for _, lookupenv := range lookupEnvs { - if lookupenv[me.Name] { + if lookupenv[this.Name] { return } } - freeVarNames[me.Name] = true + freeVarNames[this.Name] = true } } -func (me *ExprCall) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { - me.Callee.FreeVars(freeVarNames, lookupEnvs...) - me.Arg.FreeVars(freeVarNames, lookupEnvs...) +func (this *ExprCall) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { + this.Callee.FreeVars(freeVarNames, lookupEnvs...) + this.Arg.FreeVars(freeVarNames, lookupEnvs...) } -func (me *ExprLambda) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { - me.Body.FreeVars(freeVarNames, append(lookupEnvs, NewLookupEnv(nil, nil, nil, me.Args))...) +func (this *ExprLambda) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { + this.Body.FreeVars(freeVarNames, append(lookupEnvs, NewLookupEnv(nil, nil, nil, this.Args))...) } -func (me *ExprLetIn) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { - defsenv := NewLookupEnv(me.Defs, nil, nil, nil) +func (this *ExprLetIn) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { + defsenv := NewLookupEnv(this.Defs, nil, nil, nil) combined := append(lookupEnvs, defsenv) - for _, def := range me.Defs { - if me.Rec { + for _, def := range this.Defs { + if this.Rec { def.FreeVars(freeVarNames, combined...) } else { def.FreeVars(freeVarNames, lookupEnvs...) } } - me.Body.FreeVars(freeVarNames, combined...) + this.Body.FreeVars(freeVarNames, combined...) } -func (me *ExprCaseOf) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { - me.Scrut.FreeVars(freeVarNames, lookupEnvs...) - for _, alt := range me.Alts { +func (this *ExprCaseOf) FreeVars(freeVarNames map[string]bool, lookupEnvs ...map[string]bool) { + this.Scrut.FreeVars(freeVarNames, lookupEnvs...) + for _, alt := range this.Alts { alt.Body.FreeVars(freeVarNames, append(lookupEnvs, NewLookupEnv(nil, nil, nil, alt.Binds))...) } } diff --git a/1990s-fp-corelang/syn/walk-misc.go b/1990s-fp-corelang/syn/walk-misc.go index c6975d7..fc03aa6 100644 --- a/1990s-fp-corelang/syn/walk-misc.go +++ b/1990s-fp-corelang/syn/walk-misc.go @@ -4,14 +4,14 @@ import ( "strconv" ) -func (me *ExprCaseOf) ExtractIntoDef(name string, topLevel bool, lookupEnv map[string]bool) (*SynDef, IExpr) { - return me.extractIntoDef(me, name, topLevel, lookupEnv) +func (this *ExprCaseOf) ExtractIntoDef(name string, topLevel bool, lookupEnv map[string]bool) (*SynDef, IExpr) { + return this.extractIntoDef(this, name, topLevel, lookupEnv) } -func (me *exprComp) extractIntoDef(this IExpr, name string, topLevel bool, lookupEnv map[string]bool) (*SynDef, IExpr) { - i, free, def := 0, make(map[string]bool, 8), SynDef{Name: name, TopLevel: topLevel, Body: this} - this.FreeVars(free, lookupEnv) - def.toks, def.Args = me.toks, make([]string, len(free)) +func (this *exprComp) extractIntoDef(self IExpr, name string, topLevel bool, lookupEnv map[string]bool) (*SynDef, IExpr) { + i, free, def := 0, make(map[string]bool, 8), SynDef{Name: name, TopLevel: topLevel, Body: self} + self.FreeVars(free, lookupEnv) + def.toks, def.Args = this.toks, make([]string, len(free)) for name := range free { def.Args[i], i = name, i+1 } @@ -23,28 +23,27 @@ func (me *exprComp) extractIntoDef(this IExpr, name string, topLevel bool, looku for i = 1; i < len(def.Args); i++ { call = ExprCall{Callee: &call, Arg: Id(def.Args[i])} } - call.toks = me.toks + call.toks = this.toks return &def, &call } - -func (me *ExprCtor) ExtractIntoDef(name string, topLevel bool) *SynDef { - def := SynDef{Name: name, TopLevel: topLevel, Body: me, Args: make([]string, me.Arity)} - for i := 0; i < me.Arity; i++ { +func (this *ExprCtor) ExtractIntoDef(name string, topLevel bool) *SynDef { + def := SynDef{Name: name, TopLevel: topLevel, Body: this, Args: make([]string, this.Arity)} + for i := 0; i < this.Arity; i++ { def.Args[i] = "v" + strconv.Itoa(i) def.Body = Ap(def.Body, Id(def.Args[i])) } return &def } -func (me *ExprCall) Flattened() (callee IExpr, reverseArgs []IExpr) { - for ; me != nil; me, _ = callee.(*ExprCall) { - callee, reverseArgs = me.Callee, append(reverseArgs, me.Arg) +func (this *ExprCall) Flattened() (callee IExpr, reverseArgs []IExpr) { + for ; this != nil; this, _ = callee.(*ExprCall) { + callee, reverseArgs = this.Callee, append(reverseArgs, this.Arg) } return } -func (me *ExprCall) FlattenedIfCtor() (ctor *ExprCtor, reverseArgs []IExpr) { - callee, revargs := me.Flattened() +func (this *ExprCall) FlattenedIfCtor() (ctor *ExprCtor, reverseArgs []IExpr) { + callee, revargs := this.Flattened() if ctormaybe, ok := callee.(*ExprCtor); ok { ctor, reverseArgs = ctormaybe, revargs } diff --git a/1990s-fp-corelang/util/util.go b/1990s-fp-corelang/util/util.go index 5773527..495399d 100644 --- a/1990s-fp-corelang/util/util.go +++ b/1990s-fp-corelang/util/util.go @@ -14,12 +14,12 @@ type INode interface { type Addr int -func (me Addr) String() string { return strconv.Itoa(int(me)) } +func (this Addr) String() string { return strconv.Itoa(int(this)) } type Env map[string]Addr -func (me Env) LookupOrPanic(name string) (addr Addr) { - if addr = me[name]; addr == 0 { +func (this Env) LookupOrPanic(name string) (addr Addr) { + if addr = this[name]; addr == 0 { panic("undefined: " + name) } return @@ -27,138 +27,138 @@ func (me Env) LookupOrPanic(name string) (addr Addr) { type StackA []Addr -func (me StackA) Dropped(n int) StackA { - return me[:len(me)-n] +func (this StackA) Dropped(n int) StackA { + return this[:len(this)-n] } -func (me StackA) Pos0() int { - return len(me) - 1 +func (this StackA) Pos0() int { + return len(this) - 1 } -func (me StackA) Pos1() int { - return len(me) - 2 +func (this StackA) Pos1() int { + return len(this) - 2 } -func (me StackA) Pos(i int) int { - return len(me) - (1 + i) +func (this StackA) Pos(i int) int { + return len(this) - (1 + i) } -func (me *StackA) Push(addr Addr) { - *me = append(*me, addr) +func (this *StackA) Push(addr Addr) { + *this = append(*this, addr) } -func (me StackA) Pushed(addr Addr) StackA { - return append(me, addr) +func (this StackA) Pushed(addr Addr) StackA { + return append(this, addr) } -func (me StackA) Top0() Addr { - return me[len(me)-1] +func (this StackA) Top0() Addr { + return this[len(this)-1] } -func (me StackA) Top1() Addr { - return me[len(me)-2] +func (this StackA) Top1() Addr { + return this[len(this)-2] } -func (me StackA) Top(i int) Addr { - return me[len(me)-(1+i)] +func (this StackA) Top(i int) Addr { + return this[len(this)-(1+i)] } type StackI []int -func (me StackI) Dropped(n int) StackI { - return me[:len(me)-n] +func (this StackI) Dropped(n int) StackI { + return this[:len(this)-n] } -func (me StackI) Pos0() int { - return len(me) - 1 +func (this StackI) Pos0() int { + return len(this) - 1 } -func (me StackI) Pos1() int { - return len(me) - 2 +func (this StackI) Pos1() int { + return len(this) - 2 } -func (me StackI) Pos(i int) int { - return len(me) - (1 + i) +func (this StackI) Pos(i int) int { + return len(this) - (1 + i) } -func (me *StackI) Push(i int) { - *me = append(*me, i) +func (this *StackI) Push(i int) { + *this = append(*this, i) } -func (me StackI) Pushed(i int) StackI { - return append(me, i) +func (this StackI) Pushed(i int) StackI { + return append(this, i) } -func (me StackI) Top0() int { - return me[len(me)-1] +func (this StackI) Top0() int { + return this[len(this)-1] } -func (me StackI) Top1() int { - return me[len(me)-2] +func (this StackI) Top1() int { + return this[len(this)-2] } -func (me StackI) Top(i int) int { - return me[len(me)-(1+i)] +func (this StackI) Top(i int) int { + return this[len(this)-(1+i)] } type StackS []string -func (me StackS) Dropped(n int) StackS { - return me[:len(me)-n] +func (this StackS) Dropped(n int) StackS { + return this[:len(this)-n] } -func (me StackS) Pos0() int { - return len(me) - 1 +func (this StackS) Pos0() int { + return len(this) - 1 } -func (me StackS) Pos1() int { - return len(me) - 2 +func (this StackS) Pos1() int { + return len(this) - 2 } -func (me StackS) Pos(i int) int { - return len(me) - (1 + i) +func (this StackS) Pos(i int) int { + return len(this) - (1 + i) } -func (me *StackS) Push(i string) { - *me = append(*me, i) +func (this *StackS) Push(i string) { + *this = append(*this, i) } -func (me StackS) Pushed(i string) StackS { - return append(me, i) +func (this StackS) Pushed(i string) StackS { + return append(this, i) } -func (me StackS) Top0() string { - return me[len(me)-1] +func (this StackS) Top0() string { + return this[len(this)-1] } -func (me StackS) Top1() string { - return me[len(me)-2] +func (this StackS) Top1() string { + return this[len(this)-2] } -func (me StackS) Top(i int) string { - return me[len(me)-(1+i)] +func (this StackS) Top(i int) string { + return this[len(this)-(1+i)] } type HeapM map[Addr]INode -func (me HeapM) Alloc(obj INode) (addr Addr) { - addr = me.NextAddr() - me[addr] = obj +func (this HeapM) Alloc(obj INode) (addr Addr) { + addr = this.NextAddr() + this[addr] = obj return } -func (me HeapM) NextAddr() Addr { - return Addr(1 + len(me)) +func (this HeapM) NextAddr() Addr { + return Addr(1 + len(this)) } type HeapA []INode -func (me *HeapA) Alloc(obj INode) (addr Addr) { - addr = me.NextAddr() - *me = append(*me, obj) +func (this *HeapA) Alloc(obj INode) (addr Addr) { + addr = this.NextAddr() + *this = append(*this, obj) return } -func (me HeapA) NextAddr() Addr { - return Addr(len(me)) +func (this HeapA) NextAddr() Addr { + return Addr(len(this)) } diff --git a/dummy-multmachine/main.go b/dummy-multmachine/main.go index 8ff8990..5804c0a 100644 --- a/dummy-multmachine/main.go +++ b/dummy-multmachine/main.go @@ -30,25 +30,25 @@ func main() { } } -func (me *multiplicator) eval(op1 int64, op2 int64) int64 { - for me.init(op1, op2); !me.finalState(); me.step() { +func (this *multiplicator) eval(op1 int64, op2 int64) int64 { + for this.init(op1, op2); !this.finalState(); this.step() { // nothing else to do here while we step } - return me.RunningTotal + return this.RunningTotal } -func (me *multiplicator) init(op1 int64, op2 int64) { - me.Operand1, me.Operand2, me.Jobber, me.RunningTotal = op1, op2, 0, 0 +func (this *multiplicator) init(op1 int64, op2 int64) { + this.Operand1, this.Operand2, this.Jobber, this.RunningTotal = op1, op2, 0, 0 } -func (me *multiplicator) finalState() bool { - return me.Operand2 == 0 && me.Jobber == 0 +func (this *multiplicator) finalState() bool { + return this.Operand2 == 0 && this.Jobber == 0 } -func (me *multiplicator) step() { - if me.Jobber == 0 { - me.Jobber, me.Operand2 = me.Operand1, me.Operand2-1 +func (this *multiplicator) step() { + if this.Jobber == 0 { + this.Jobber, this.Operand2 = this.Operand1, this.Operand2-1 } else { - me.Jobber, me.RunningTotal = me.Jobber-1, me.RunningTotal+1 + this.Jobber, this.RunningTotal = this.Jobber-1, this.RunningTotal+1 } }