Browse Source

FIX.修复函数环境变量被函数名覆盖的问题

RegMs If 4 years ago
parent
commit
73fe1718e9
3 changed files with 137 additions and 111 deletions
  1. 136 109
      in1
  2. BIN
      mua
  3. 1 2
      mua.go

+ 136 - 109
in1

@@ -1,119 +1,146 @@
-print not gt 3 1
-make "d read
-1234dd
-print isname "d
-print :d
-make "x eq :d "1234dd
-print :x
-erase "d
-print not isname "d
-
-make "a 2
-print isname "a
-erase "a
-print isname "a
-
-make "a 5
-make "b 3
-make "c :b
-print gt "a "b
-print gt :a :b
-print lt :a :b
-print eq :a :b
-print eq :b :c
-print and gt :a :b eq :b :c
-print or gt :b :a eq :b :c
-print not gt :a :b
-print or gt :b :a not eq :b :c
-
-make "testlist [print gt :a :b]
-run :testlist
-make "a 2
-run :testlist
-
-make "a 1
-run [make "a add :a 1 print :a]
-print :a
-
-if gt :a :b [print "A] [print "B]
-
-print if eq :a :b [1] [0]
-
-print isnumber "2
-print isnumber "a
-print isnumber :a
-print isword :a
-print isword "a
-print islist "testlist
-print islist :testlist
-print isbool run :testlist
-
-print isempty :testlist
-print isempty "a
-print isempty "d
-
-make "a 3
-make "a add :a 4
-make "f [
-	[a]
-	[return add mul :a :a 1]
+make "f [[x] [
+  make "g [[y] [return add :x :y]]
+  return g 42
+]]
+
+print f 233
+
+make "f1 [[x] [
+    make "g1 [[y] [return add :x :y]]
+    return :g1
+  ]
 ]
 ]
-print f 3
-make "f
-[
-	[]
-	[print "a]
+make "c1 f1 42
+make "c2 f1 24
+print c1 1
+print c2 2
+
+make "curry_two [[f x] [
+  return [[y] [return f :x :y]]
+]]
+make "f2 [[x y] [
+  return add :x :y
+]]
+make "f2p curry_two :f2 42
+
+print f2p 233
+
+make "fact_base [
+    [rec x]
+    [
+        if eq :x 0
+        [return 1]
+        [return mul :x rec :rec sub :x 1]
+    ]
 ]
 ]
-f
-make "n 5
-make "f [
-	[n]
-	[
-		if lt :n 2
-			[return 1]
-			[return mul :n f sub :n 1]
-	]
+
+make "fact_simple [
+    [x]
+    [return fact_base :fact_base :x]
 ]
 ]
-print f :n
-print :n
-make "let [
-	[__a __b]
-	[
-		make :__a :__b
-		export :__a
-	]
+
+print fact_simple 5
+
+make "fact_nice [
+    [rec]
+    [
+        make "g [
+            [x]
+            [
+                if eq :x 0
+                [return 1]
+                [
+                    make "tmp rec :rec
+                    return mul :x tmp sub :x 1
+                ]
+            ]
+        ]
+        return :g
+    ]
 ]
 ]
-let "a 6
-print :a
-make "a 1
-make "repeat [
-    [n s]
+
+make "fact_curry fact_nice :fact_nice
+print fact_curry 5
+
+make "fact_nicer [
+    [rec]
     [
     [
-		if eq :n 0 
-			[] 
-			[run :s repeat sub :n 1 :s]
-	]
+        make "g [
+            [x]
+            [
+                if eq :x 0
+                [return 1]
+                [return mul :x rec sub :x 1]
+            ]
+        ]
+        return :g
+    ]
 ]
 ]
-repeat 4 [make "a add :a 1]
-print :a
-make "n 5
-make "factorial [
-	[n]
-	[
-		if lt :n 2
-			[return 1]
-			[return mul :n factorial sub :n 1]
-	]
+
+make "z_comb [
+    [g]
+    [
+        make "t [
+            [r]
+            [
+                make "y [
+                    [yy]
+                    [
+                        make "tmp r :r
+                        return tmp :yy
+                    ]
+                ]
+                return g :y
+            ]
+        ]
+        return t :t
+    ]
 ]
 ]
-print factorial :n
-print :n
-make "gcd [
-    [a b]
+
+make "fact_z z_comb :fact_nicer
+print fact_z 5
+
+make "logic_test [[b] [return b 1 0]]
+
+make "F_TRUE [[x y] [return :x]]
+make "F_FALSE [[x y] [return :y]]
+
+make "F_AND [[p q] [return p :q :p]]
+make "F_OR [[p q] [return p :p :q]]
+make "F_NOT [[p] [return p :F_FALSE :F_TRUE]]
+
+print logic_test F_AND F_AND :F_TRUE :F_TRUE :F_FALSE
+print logic_test F_AND F_OR :F_TRUE :F_FALSE :F_FALSE
+print logic_test F_NOT F_AND :F_TRUE :F_FALSE :F_TRUE
+print logic_test F_OR F_NOT :F_TRUE F_NOT :F_FALSE :F_TRUE
+
+make "church_test [
+    [n]
     [
     [
-        if eq :b 0
-            [return :a]
-            [return gcd :b mod :a :b]
+        make "inc [[x] [return add :x 1]]
+        return n :inc 0
     ]
     ]
 ]
 ]
-print gcd 18 14
-print gcd 18 13
+
+make "O [
+    [f x]
+    [return :x]
+]
+
+make "succ [
+    [n]
+    [
+        make "t [
+            [f x]
+            [
+                make "tt n :f :x
+                return f :tt
+            ]
+        ]
+        return :t
+    ]
+]
+
+print church_test succ :O
+print church_test succ succ :O
+print church_test succ succ succ :O

BIN
mua


+ 1 - 2
mua.go

@@ -189,11 +189,10 @@ func interpret(scanner *scanProvider, envs []environ) (val value, returned bool)
 						for j := i + 1; j < len(stack); j++ {
 						for j := i + 1; j < len(stack); j++ {
 							stack[j] = toValue(stack[j], envs)
 							stack[j] = toValue(stack[j], envs)
 						}
 						}
-						local := environ{}
+						local := environ{op: val}
 						for name, val := range val.env {
 						for name, val := range val.env {
 							local[name] = val
 							local[name] = val
 						}
 						}
-						local[op] = val
 						for j, name := range val.param {
 						for j, name := range val.param {
 							local[name] = stack[i+1+j]
 							local[name] = stack[i+1+j]
 						}
 						}