commit 0a5b956077484cc08827e4914a1c042fb5996bd1
parent 4153f536774625b83553b63331b558cf02f972b9
Author: prenev <an2qzavok@gmail.com>
Date: Sun, 10 Oct 2021 21:56:22 +0300
remove files
Diffstat:
D | fortum.go | | | 328 | ------------------------------------------------------------------------------- |
D | third.go | | | 378 | ------------------------------------------------------------------------------- |
2 files changed, 0 insertions(+), 706 deletions(-)
diff --git a/fortum.go b/fortum.go
@@ -1,328 +0,0 @@
-package main
-
-import (
- "fmt"
- "bufio"
- "os"
- "io"
- "strconv"
- "strings"
-)
-
-const (
- rsp int = 1
- dep int = 0
-
- progstart = 100
- rstackstart = 5
-)
-
-var laswor string
-
-type Mem struct {
- data []int
-}
-
-func NewMem() *Mem {
- var mem = new(Mem)
- mem.data = make([]int, 0)
- return mem
-}
-
-func (m *Mem) Fetch(addr int) (int, error) {
- if (addr >= len(m.data)) || (addr < 0) {
- return 0, fmt.Errorf("memfetch: addr %d out of bonds", addr)
- }
- return m.data[addr], nil
-}
-
-func (m *Mem) Store(addr, val int) {
- if addr >= len(m.data) {
- buf := make([]int, addr - len(m.data) + 1)
- m.data = append(m.data, buf...)
- }
- m.data[addr] = val
-}
-
-type Stack struct {
- data []int
-}
-
-func NewStack() *Stack {
- stack := new(Stack)
- stack.data = make([]int, 1)
- return stack
-}
-
-func (s *Stack) Push(val int) {
- s.data = append(s.data, val)
-}
-
-func (s *Stack) Pop() (int, error) {
- if s.data == nil {
- return 0, fmt.Errorf("stack is empty")
- }
- x := s.data[len(s.data) - 1]
- s.data = s.data[:len(s.data) - 1]
- return x, nil
-}
-
-func (s *Stack) TOS() *int {
- return &s.data[len(s.data) - 1]
-}
-
-type First struct {
- stack *Stack
- mem *Mem
- strings []string
- pc, lwp int
- run bool
- in *bufio.Reader
-}
-
-func NewFirst() (*First, error) {
- const builtins string = "halt : immediate"
- var first = new(First)
- *first = First{
- NewStack(),
- NewMem(),
- make([]string, 0),
- 0, 0,
- true,
- nil,
- }
- first.mem.Store(dep, progstart)
- first.mem.Store(rsp, rstackstart)
- first.in = bufio.NewReader(strings.NewReader(builtins))
- for i := 0; i < 3; i++ {
- first.define()
- first.mem.Store(first.mem.data[dep] - 1, -3)
- first.Compile(i, 10)
- }
- first.in = bufio.NewReader(strings.NewReader(
- "_read @ ! - * / <0 exit echo key _pick _loop"))
- for i:=3 ; i < 14; i++ {
- first.define()
- first.mem.Store(first.mem.data[dep] - 1, i)
- }
-
- first.define();
- first.mem.Store(first.mem.data[dep] -1, -3)
- first.Compile(3, first.lwp + 3)
- first.pc = first.lwp + 3
- return first, nil
-}
-
-func (F *First) findWord(s string) int {
- var err error
- for wp := F.lwp; wp != 0; wp, err = F.mem.Fetch(wp) {
- if err != nil {
- return 0
- }
- id, err := F.mem.Fetch(wp + 1)
- if err != nil {
- return 0
- }
- if F.strings[id] == s {
- return wp
- }
- }
- return 0
-}
-
-func (F *First) rpush(val int) (error) {
- if F.mem.data[rsp] >= progstart {
- return fmt.Errorf("rstack is full")
- }
- F.mem.data[rsp]++
- F.mem.Store(F.mem.data[rsp], val)
- return nil
-}
-
-func (F *First) rpop() (int, error) {
- if F.mem.data[rsp] <= rstackstart {
- return 0, fmt.Errorf("rstack is empty")
- }
- x, err := F.mem.Fetch(F.mem.data[rsp])
- F.mem.data[rsp]--
- return x, err
-}
-
-func (F *First) Compile(vals ...int) {
- for _, val := range vals {
- F.mem.Store(F.mem.data[dep], val)
- F.mem.data[dep]++
- }
-}
-
-func (F *First) define() error {
- var s string
- nwp := F.mem.data[dep]
- if _, err := fmt.Fscan(F.in, &s); err != nil {
- return err
- }
- F.in.ReadRune()
- F.strings = append(F.strings, s)
- id := len(F.strings) - 1
- F.Compile(F.lwp, id, -2)
- F.lwp = nwp
- return nil
-}
-
-func (F *First) _read() error {
- var (
- s string
- err error
- num, wp int
- )
- if _, err = fmt.Fscan(F.in, &s); err != nil {
- return err
- }
- F.in.ReadRune()
- switch s {
- case "S":
- fmt.Println(F.stack.data)
- return nil
- case "R":
- fmt.Println(F.mem.data[4:F.mem.data[rsp]])
- return nil
- case "M":
- fmt.Println(F.mem.data[progstart:])
- return nil
- }
- wp = F.findWord(s)
- if wp != 0 {
- wp += 2
- inst, err := F.mem.Fetch(wp)
- switch {
- case inst == -3: // "run me"
- err = F.rpush(F.pc)
- F.pc = wp + 1
- case inst == -2: // "compile me"
- F.Compile(wp + 1)
- case (inst >=0) && (inst <= 13):
- F.Compile(inst)
- default:
- err = fmt.Errorf("invalid code pointer in word %s, %d", s, inst)
- }
- return err
- }
- if num, err = strconv.Atoi(s); err != nil {
- return err
- }
- F.Compile(-1, num)
- return nil
-}
-
-func (F *First) Run(input *bufio.Reader) error {
- var (
- err error
- inst, x, y int
- )
- F.in = input
- for err == nil {
- inst, err = F.mem.Fetch(F.pc)
- // fmt.Print(" ", inst)
- F.pc++
- switch inst {
- case -1: // internal builtin "pushint"
- x, err = F.mem.Fetch(F.pc)
- F.stack.Push(x)
- // fmt.Print("=", x)
- F.pc++
- case 0: // builtin "halt"
- fmt.Println(F.pc - 1, "halt")
- F.pc--
- return err
- case 1: // builtin "define", ":"
- err = F.define()
- case 2: // builtin "immediate"
- F.mem.Store(F.mem.data[dep] - 1, -3)
- case 3: // builtin "_read"
- err = F._read()
- case 4: // builtin "fetch", "@"
- x, err = F.stack.Pop()
- y, err = F.mem.Fetch(x)
- F.stack.Push(y)
- case 5: // builtin "store", "!"
- x, err = F.stack.Pop()
- y, err = F.stack.Pop()
- F.mem.Store(x, y)
- case 6: // builtin "minus", "-"
- x, err = F.stack.Pop()
- y, err = F.stack.Pop()
- F.stack.Push(y - x)
- case 7: // builtin "mulitply", "*"
- x, err = F.stack.Pop()
- y, err = F.stack.Pop()
- F.stack.Push(y * x)
- case 8: // builtin "divide", "/"
- x, err = F.stack.Pop()
- y, err = F.stack.Pop()
- F.stack.Push(y / x)
- case 9: // builtin "less than 0", "<0"
- x, err = F.stack.Pop()
- if (x < 0) {
- F.stack.Push(1)
- } else {
- F.stack.Push(0)
- }
- case 10: // builtin "exit"
- F.pc, err = F.rpop()
- case 11: // builtin "echo"
- x, err = F.stack.Pop()
- if err == nil {
- fmt.Printf("%c", x)
- }
- case 12: // builtin "key"
- var r rune
- //k := bufio.NewReader(F.in)
- r, _, err = F.in.ReadRune()
- // fmt.Printf("[%c][%d]\n", r, r)
- F.stack.Push(int(r))
- case 13: // builtin "_pick"
- x, err = F.stack.Pop()
- y = F.stack.data[len(F.stack.data) - x - 1]
- F.stack.Push(y)
- default:
- err = F.rpush(F.pc)
- F.pc = inst
- }
- }
- if err == io.EOF {
- F.pc--
- return nil
- }
- return err
-}
-
-func debugout(first *First) {
- fmt.Println("Words:", first.strings)
- fmt.Println("Stack:", first.stack.data)
- fmt.Println("Rstack:", first.mem.data[rstackstart:first.mem.data[rsp]])
- memdump, _ := os.Create("memdump")
- out := bufio.NewWriter(memdump)
- for _, m := range first.mem.data {
- fmt.Fprintln(out, m)
- }
- out.Flush()
- memdump.Close()
-}
-
-func main() {
- first, err := NewFirst()
-
- defer debugout(first)
-
- if err != nil {
- fmt.Println(err)
- return
- }
- err = first.Run(bufio.NewReader(strings.NewReader(third)))
- fmt.Println("---")
- if err != nil {
- fmt.Println(err)
- } else if err = first.Run(bufio.NewReader(os.Stdin)); err != nil {
- fmt.Println(err)
- }
-}
diff --git a/third.go b/third.go
@@ -1,378 +0,0 @@
-package main
-
-var third string = `
-
-: r 1 exit
-
-: ]
- r @
- 1 -
- r !
- _read
- ]
-
-: main immediate ]
-
-
-main
-
-: '"' 32 exit
-: ')' 41 exit
-: '\n' 10 exit
-: 'space' 32 exit
-: '0' 48 exit
-: '-' 45 exit
-
-: cr '\n' echo exit
-
-: _x 3 @ exit
-: _x! 3 ! exit
-: _y 4 @ exit
-: _y! 4 ! exit
-
-: swap _x! _y! _x _y exit
-
-: +
- 0 swap -
- -
- exit
-
-: dup _x! _x _x exit
-
-: h 0 exit
-
-: inc
- dup @
- 1 +
- swap
- ! exit
-
-: ,
- h @
- !
- h inc
- exit
-
-: '
- r @
- @
- dup
- 1 +
- r @ !
- @
- exit
-
-: ; immediate
-
- ' exit
- ,
- exit
-
-: drop 0 * + ;
-
-: dec dup @ 1 - swap ! ;
-
-: tor
- r @ @
- swap
- r @ !
- r @ 1 + r !
- r @ !
-;
-
-: fromr
- r @ @
- r @ 1 - r !
- r @ @
- swap
- r @ !
-;
-
-: tail fromr fromr drop tor ;
-
-: minus 0 swap - ;
-
-: bnot 1 swap - ;
-
-: < - <0 ;
-
-: logical
- dup
- 0 <
- swap minus
- 0 <
- +
-;
-
-: not logical bnot ;
-
-: = - not ;
-
-: branch
- r @
- @
- @
- r @ @
- +
- r + !
-;
-
-: computebranch 1 - * 1 + ;
-
-: notbranch
- not
- r @ @ @
- computebranch
- r @ @ +
- r @ !
-;
-
-: here h @ ;
-
-: if immediate
- ' notbranch ,
- here
- 0 ,
-;
-
-: then immediate
- dup
- here
- swap -
- swap !
-;
-
-: find-)
- key
- ')' =
- not if
- tail find-)
- then
-;
-
-: ( immediate
- find-)
-;
-
-( we should be able to do FORTH-style comments now )
-
-( now that we've got comments, we can comment the rest of the code
- in a legitimate [self parsing] fashion. Note that you can't
- nest parentheses... )
-
-: else immediate
- ' branch ,
- here
- 0 ,
- swap
- dup here swap -
- swap !
-;
-
-: over _x! _y! _y _x _y ;
-
-: add
- _x!
- _x @
- +
- _x !
-;
-
-: allot h add ;
-
-: maybebranch
- logical
- r @ @ @
- computebranch
- r @ @ +
- r @ !
-;
-
-: mod _x! _y!
- _y _y _x / _x *
- -
-;
-
-: printnum
- dup
- 10 mod '0' +
- swap 10 / dup
- if
- printnum
- echo
- else
- drop
- echo
- then
-;
-
-: .
- dup 0 <
- if
- '-' echo minus
- then
- printnum
- 'space' echo
-;
-
-: debugprint dup . cr ;
-
-: _print
- dup 1 +
- swap @
- dup '"' =
- if
- drop exit
- then
- echo
- tail _print
-;
-
-: print _print ;
-
-: immprint
- r @ @
- print
- r @ !
-;
-
-: find-"
- key dup ,
- '"' =
- if
- exit
- then
- tail find-"
-;
-
-: " immediate
- key drop
- ' immprint ,
- find-"
-;
-
-: do immediate
- ' swap ,
- ' tor ,
- ' tor ,
- here
-;
-
-: i r @ 1 - @ ;
-: j r @ 3 - @ ;
-
-: > swap < ;
-: <= 1 + < ;
-: >= swap <= ;
-
-: inci
- r @ 1 -
- inc
- r @ 1 - @
- r @ 2 - @
- <=
- if
- r @ @ @ r @ @ + r @ ! exit
- then
- fromr 1 +
- fromr drop
- fromr drop
- tor
-;
-
-: loop immediate ' inci @ here - , ;
-
-: loopexit
- fromr drop
- fromr drop
- fromr drop
-;
-
-: execute
- 8 !
- ' exit 9 !
- 8 tor
-;
-
-: :: j
-
-: fix-:: immediate 3 ' :: ! ;
-fix-::
-
-: : immediate :: ] ;
-
-: command
- here 5 !
- _read
- here 5 @
- = if
- tail command
- then
- here 1 - h !
- here 5 @
- = if
- here @
- execute
- else
- here @
- here 1 - h !
- then
- tail command
-;
-
-: make-immediate
- here 1 -
- dup dup
- h !
- @
- swap
- 1 -
- !
-;
-
-: <build immediate
- make-immediate
- ' :: ,
- -1 , ( compile 'pushint' was 2 in original )
- here 0 ,
- ' , ,
-;
-
-: does> immediate
- ' command ,
- here swap !
- -2 , (compile run-code primitive, was 2 in original )
- ' fromr ,
-;
-
-: _dump
- dup " (" . " , "
- dup @
- dup ' exit
- = if
- " ;)" cr exit
- then
- . " ), "
- 1 +
- tail _dump
-;
-
-: dump _dump ;
-
-: # . cr ;
-
-: var <build , does> ;
-: constant <build , does> @ ;
-: array <build allot does> + ;
-
-: [ immediate command ;
-: _welcome " Welcome to THIRD.
-Ok.
-" ;
-
-: ; immdeiate ' exit , command exit
-
-[
-
-_welcome
-`