commit ab098c9b15348758bff4f9874acb175f4ab6efb7
parent 4d677354277f0b76e493fa59979a11e4b4686692
Author: prenev <an2qzavok@gmail.com>
Date: Fri, 1 Oct 2021 19:57:49 +0300
some work
Diffstat:
M | fortum.go | | | 83 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------- |
1 file changed, 70 insertions(+), 13 deletions(-)
diff --git a/fortum.go b/fortum.go
@@ -1,6 +1,9 @@
package main
-import "fmt"
+import (
+ "fmt"
+ "os"
+)
type Word struct {
name string
@@ -9,10 +12,11 @@ type Word struct {
}
var stack = make([]int, 0)
-var rstack = make([]int, 0)
-var mem = make([]int, 0)
+var mem = make([]int, 4096)
var dict = make([]Word, 0)
+const rsp = 0
+
func builtin(dict []Word, name string, f func()) []Word {
word := Word{name, f, nil}
dict = append(dict, word)
@@ -23,25 +27,67 @@ func TOS(stack []int) int {
return stack[len(stack) - 1]
}
+func pop(stack *[]int) int {
+ x := TOS(*stack)
+ s := *stack
+ *stack = s[:len(s) - 1]
+ return x
+}
+
func noop() {
}
+func minus() {
+ stack[len(stack) - 2] -= TOS(stack)
+ pop(&stack)
+}
+
+func multiply() {
+ stack[len(stack) - 2] *= TOS(stack)
+ pop(&stack)
+}
+
+func divide() {
+ stack[len(stack) - 2] /= TOS(stack)
+ pop(&stack)
+}
+
+func lessthan0() {
+ x := TOS(stack)
+ if (x < 0) {
+ stack[len(stack) - 1] = 1
+ } else {
+ stack[len(stack) - 1] = 0
+ }
+}
+
+func echo() {
+ x := pop(&stack)
+ fmt.Printf("%c", x)
+}
+
+func key() {
+ mem[rsp]++
+ mem[mem[rsp]] = os.Stdin.ReadRune()
+}
+
func execword() {
- word := dict[TOS(rstack)]
+ word := dict[mem[mem[rsp]]]
if (word.builtin != nil) {
word.builtin()
} else {
for w := range word.code {
- rstack = append(rstack, w)
+ mem[rsp]++
+ mem[mem[rsp]] = w
execword()
}
}
- rstack = rstack[:len(rstack) - 1]
+ mem[rsp]--
}
func store() {
addr := TOS(stack)
- stack = stack[:len(stack)-1]
+ pop(&stack)
if len(mem) < addr {
bump := make([]int, addr - len(mem) + 1)
mem = append(mem, bump...)
@@ -51,7 +97,7 @@ func store() {
func fetch() {
addr := TOS(stack)
- if (addr > len(mem) {
+ if addr > len(mem) {
stack[len(stack)-1] = 0
} else {
stack[len(stack)-1] = mem[addr]
@@ -61,11 +107,22 @@ func fetch() {
func main() {
dict = builtin(dict, "noop", noop)
dict = builtin(dict, "execword", execword)
- stack = append(stack, 1, 2, 3, 4)
- rstack = append(rstack, 0)
+ dict = builtin(dict, "-", minus)
+ dict = builtin(dict, "*", multiply)
+ dict = builtin(dict, "/", divide)
+ dict = builtin(dict, "echo", echo)
+ dict = builtin(dict, "key", key)
+ dict = builtin(dict, "@", fetch)
+ dict = builtin(dict, "!", store)
+
+ stack = append(stack, 10, 79, 76, 76, 69, 72)
+ mem[rsp] = 1
execword()
- store()
- fetch()
- fmt.Println(mem)
+ echo()
+ echo()
+ echo()
+ echo()
+ echo()
+ echo()
fmt.Println(stack)
}