implement string handling!
This commit is contained in:
parent
e998c286d7
commit
4727312da4
32
builtins.go
32
builtins.go
@ -58,7 +58,7 @@ func (b *Builtins) OpenComment(c *Context) func(string) error {
|
||||
}
|
||||
}
|
||||
|
||||
// CloseComment resumes parsing
|
||||
// CloseComment resumes parsing by consuming itself and resetting the immediate flag
|
||||
func (b *Builtins) CloseComment(c *Context) func(string) error {
|
||||
return func(_ string) error {
|
||||
c.Flags.SetFlag("Immediate", false)
|
||||
@ -66,6 +66,36 @@ func (b *Builtins) CloseComment(c *Context) func(string) error {
|
||||
}
|
||||
}
|
||||
|
||||
// OpenQuote consumes text until its closing pair to output
|
||||
func (b *Builtins) OpenQuote(out io.Writer, r *Stack, close byte) func(string) error {
|
||||
if out == nil {
|
||||
out = os.Stdout
|
||||
}
|
||||
return func(next string) error {
|
||||
w := []byte{}
|
||||
for i := 0; i < len(next); i = i + 1 {
|
||||
switch next[i] {
|
||||
case close:
|
||||
fmt.Fprint(out, string(w))
|
||||
j, _ := r.Pop()
|
||||
r.Push(j + i - 1) // push the end-point onto the stack
|
||||
return nil
|
||||
default:
|
||||
w = append(w, next[i])
|
||||
continue
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// CloseQuote consumes itself.
|
||||
func (b *Builtins) CloseQuote(c *Context) func(string) error {
|
||||
return func(next string) error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Eq compares TOS and NOS and puts -1 on the stack if they're equal, 0 otherwise.
|
||||
func (b *Builtins) Eq(s *Stack) func(string) error {
|
||||
return func(_ string) error {
|
||||
|
4
main.go
4
main.go
@ -50,6 +50,10 @@ func main() {
|
||||
dict.AddWord(".", Word{Name: ".", Impl: b.Print(os.Stdout, &stack)})
|
||||
dict.AddWord("EMIT", Word{Name: "EMIT", Impl: b.Emit(os.Stdout, &stack)})
|
||||
dict.AddWord("CR", Word{Name: "CR", Source: []string{"10", "EMIT"}}) // emit a newline
|
||||
// strings
|
||||
dict.AddWord(`."`, Word{Name: `."`, Impl: b.OpenQuote(os.Stdout, &rstack, '"')})
|
||||
dict.AddWord(`"`, Word{Name: `"`, Impl: b.CloseQuote(&c)})
|
||||
dict.AddWord(`.(`, Word{Name: `.(`, Impl: b.OpenQuote(os.Stdout, &rstack, ')'), Immediate: true})
|
||||
// logic
|
||||
dict.AddWord("=", Word{Name: "=", Impl: b.Eq(&stack)})
|
||||
dict.AddWord("0=", Word{Name: "0=", Source: []string{"0", "="}})
|
||||
|
Loading…
Reference in New Issue
Block a user