implement line-comments

This commit is contained in:
David 2021-02-25 22:09:59 -05:00
parent 0784661e7c
commit 7291ca86c8
2 changed files with 10 additions and 0 deletions

View File

@ -58,6 +58,15 @@ func (b *Builtins) OpenComment(c *Context) func(string) error {
} }
} }
// EOLComment discards the rest of the parsed line
func (b *Builtins) EOLComment(c *Context) func(string) error {
return func(next string) error {
j, _ := c.RStack.Pop()
c.RStack.Push(j + len(next)) // push the end-point onto the stack
return nil
}
}
// CloseComment resumes parsing by consuming itself and resetting the immediate flag // CloseComment resumes parsing by consuming itself and resetting the immediate flag
func (b *Builtins) CloseComment(c *Context) func(string) error { func (b *Builtins) CloseComment(c *Context) func(string) error {
return func(_ string) error { return func(_ string) error {

View File

@ -37,6 +37,7 @@ func main() {
// comments and strings // comments and strings
dict.AddWord("(", Word{Name: "(", Impl: b.OpenComment(&c), Immediate: true}) dict.AddWord("(", Word{Name: "(", Impl: b.OpenComment(&c), Immediate: true})
dict.AddWord(")", Word{Name: ")", Impl: b.CloseComment(&c), Immediate: true}) dict.AddWord(")", Word{Name: ")", Impl: b.CloseComment(&c), Immediate: true})
dict.AddWord(`\`, Word{Name: `\`, Impl: b.EOLComment(&c), Immediate: true})
dict.AddWord(`."`, Word{Name: `."`, Impl: b.OpenQuote(os.Stdout, &rstack, '"')}) dict.AddWord(`."`, Word{Name: `."`, Impl: b.OpenQuote(os.Stdout, &rstack, '"')})
dict.AddWord(`"`, Word{Name: `"`, Impl: b.CloseQuote(&c)}) dict.AddWord(`"`, Word{Name: `"`, Impl: b.CloseQuote(&c)})
dict.AddWord(`.(`, Word{Name: `.(`, Impl: b.OpenQuote(os.Stdout, &rstack, ')'), Immediate: true}) dict.AddWord(`.(`, Word{Name: `.(`, Impl: b.OpenQuote(os.Stdout, &rstack, ')'), Immediate: true})