Improves stackdump, changes convert to floor rather than trunc when going FLOAT to INT, adds to std.fe

This commit is contained in:
sloum 2023-06-12 16:14:28 -07:00
parent ecd9a77d8c
commit fe4824649b
5 changed files with 80 additions and 11 deletions

View File

@ -14,6 +14,7 @@ package main
const (
STACK_SIZE int = 256
stackFormat string = "[ %-6s | %-15s ]\n"
// Begin types exposed to the user
INT int = iota

View File

@ -15,7 +15,8 @@ package main
import (
"bufio"
"fmt"
"io"
"io"
"math"
"net"
"net/http"
"net/url"
@ -263,12 +264,24 @@ func libDivide(line int, fp string) error {
}
err = globalStack.Push(token{LIST, list{ts}, line, fp})
} else if v1.kind == INT && v2.kind == INT {
if v2.val.(int) == 0 {
return fmt.Errorf("Division by zero")
}
err = globalStack.Push(token{INT, v1.val.(int) / v2.val.(int), line, fp})
} else if v1.kind == FLOAT && v2.kind == FLOAT {
if v2.val.(float64) == 0.0 {
return fmt.Errorf("Division by zero")
}
err = globalStack.Push(token{FLOAT, v1.val.(float64) / v2.val.(float64), line, fp})
} else if v1.kind == FLOAT && v2.kind == INT {
if v2.val.(int) == 0 {
return fmt.Errorf("Division by zero")
}
err = globalStack.Push(token{FLOAT, v1.val.(float64) / float64(v2.val.(int)), line, fp})
} else if v1.kind == INT && v2.kind == FLOAT {
if v2.val.(float64) == 0.0 {
return fmt.Errorf("Division by zero")
}
err = globalStack.Push(token{FLOAT, float64(v1.val.(int)) / v2.val.(float64) , line, fp})
} else {
return fmt.Errorf("Cannot `/` %s and %s", kindToString(v1.kind), kindToString(v2.kind))
@ -854,7 +867,7 @@ func libConvertType(line int, fp string) error {
err = globalStack.Push(token{FLOAT, float64(fromToken.val.(int)), line, fp})
} else if fromKind == FLOAT && toKind == INT {
err = globalStack.Push(token{INT, int(fromToken.val.(float64)), line, fp})
err = globalStack.Push(token{INT, int(math.Floor(fromToken.val.(float64))), line, fp})
} else if toKind == STRING {
err = globalStack.Push(token{STRING, toString(fromToken, false), line, fp})

View File

@ -99,19 +99,64 @@ Notes: Takes TOS and leaves it's absolute value on TOS |
.
.
proc %
| Stack: FLOAT/INT
proc dup2
| Stack: ANY1 ANY2
Read :
Push : FLOAT/INT
Push : ANY1 ANY2
Notes: Duplicates the top two stack values such that the stack starts and ends as: v1 v2 -- v1 v2 v1 v2 |
over over
.
proc %
| Stack: INT
Read :
Push : INT
Notes: Calculates the remainder (n1 n2 -- n3) |
"Coming soon" throw
dup2
INT var! div
FLOAT cast / INT cast set! div
div * -
.
proc not
| Stack: ANY
Read:
Push: BOOL
Read :
Push : BOOL
Notes: Treats TOS as a BOOL (regardless of actual type) and inverts the BOOL |
if false else true
if false else true .
.
proc ipow
| Stack: INT INT
Read :
Push : INT
Notes: Raise n1 by n2 (n1 n2 -- n3) |
INT var! exp INT cast set! exp
INT var! base INT cast set! base
exp 0 < if
"`pow` with negative exponent not allowed" throw .
INT var! result 1 set! result
proc loop? exp 0 > .
loop? while
result base * set! result
--! exp
loop?
.
result
.
proc square
| Stack: INT
Read :
Push : INT
Notes: Squares the given INT and leaves the result on TOS |
dup *
.
proc nip
| Stack: ANY1 ANY2
Read :
Push :
Notes: Drops the item under TOS (a1 a2 -- a2) |
swap drop
.

View File

@ -96,7 +96,6 @@ func VersionString() string {
func seedBaseEnv(en *env) {
lexParseInterpret(std, en, "std.fe")
fmt.Println(*en)
}

View File

@ -15,6 +15,7 @@ package main
import (
"errors"
"fmt"
"strings"
)
type stack struct {
@ -53,7 +54,17 @@ func (p stack) Depth() int {
}
func (p stack) String() string {
return fmt.Sprintf("%v <- TOS\n", p.s[:p.sp])
var b strings.Builder
b.WriteString("============TOS.============\n")
for i := p.sp-1; i >= 0; i-- {
t := p.s[i]
s := toString(t, true)
if len(s) > 15 {
s = s[:15]
}
b.WriteString(fmt.Sprintf(stackFormat, kindToString(t.kind), s))
}
return b.String()
}
func NewStack() stack {