q-form renaming effort

This commit is contained in:
Luxferre
2026-07-04 16:56:08 +03:00
parent 33a94f0084
commit 46ff217e14
6 changed files with 96 additions and 97 deletions
+28 -28
View File
@@ -182,19 +182,19 @@ func normToken(t string) any {
return t
}
func parseQuot(tokens *[]any) ([]any, error) {
var quot []any
func parseQForm(tokens *[]any) ([]any, error) {
var qform []any
for {
if len(*tokens) == 0 { return nil, fmt.Errorf("Syntax error: unbalanced brackets") }
t := (*tokens)[0]
*tokens = (*tokens)[1:]
if s, ok := t.(string); ok && s == "]" { return quot, nil }
if s, ok := t.(string); ok && s == "]" { return qform, nil }
if s, ok := t.(string); ok && s == "[" {
sub, err := parseQuot(tokens)
sub, err := parseQForm(tokens)
if err != nil { return nil, err }
quot = append(quot, sub)
qform = append(qform, sub)
} else {
if s, ok := t.(string); ok { quot = append(quot, normToken(s)) } else { quot = append(quot, t) }
if s, ok := t.(string); ok { qform = append(qform, normToken(s)) } else { qform = append(qform, t) }
}
}
}
@@ -225,17 +225,17 @@ func (c *Clyx) reprStack() string {
return "[" + strings.Join(parts, ", ") + "]"
}
func (c *Clyx) execQuot(q []any) {
func (c *Clyx) execQForm(q []any) {
c.tokenStreams = append(c.tokenStreams, c.tokens)
c.tokens = make([]any, len(q))
copy(c.tokens, q)
for len(c.tokens) > 0 {
t := c.tokens[0]
c.tokens = c.tokens[1:]
if listVal, ok := t.([]any); ok {
c.stack = append(c.stack, listVal)
if qformVal, ok := t.([]any); ok {
c.stack = append(c.stack, qformVal)
} else if s, ok := t.(string); ok && s == "[" {
qParsed, err := parseQuot(&c.tokens)
qParsed, err := parseQForm(&c.tokens)
if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err); break }
c.stack = append(c.stack, qParsed)
} else {
@@ -247,7 +247,7 @@ func (c *Clyx) execQuot(q []any) {
if isWord {
switch val := v.(type) {
case func(): val()
case []any: c.execQuot(val)
case []any: c.execQForm(val)
default: c.stack = append(c.stack, val)
}
} else {
@@ -266,7 +266,7 @@ func (c *Clyx) popCallerToken() any {
t := (*stream)[0]
*stream = (*stream)[1:]
if s, ok := t.(string); ok && s == "[" {
qParsed, err := parseQuot(stream)
qParsed, err := parseQForm(stream)
if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err); return nil }
return qParsed
}
@@ -279,9 +279,9 @@ func (c *Clyx) cmdDefw() {
var val any
if len(*stream) > 0 {
t0 := (*stream)[0]
isQuot := false
if s, ok := t0.(string); ok && s == "[" { isQuot = true } else if _, ok := t0.([]any); ok { isQuot = true }
if isQuot { val = c.popCallerToken() }
isQForm := false
if s, ok := t0.(string); ok && s == "[" { isQForm = true } else if _, ok := t0.([]any); ok { isQForm = true }
if isQForm { val = c.popCallerToken() }
}
if val == nil { val = c.pop() }
name := c.pop().(string)
@@ -299,7 +299,7 @@ func (c *Clyx) cmdType() {
}
func (c *Clyx) Run(code string) {
c.execQuot(toAnySlice(tokenize(code)))
c.execQForm(toAnySlice(tokenize(code)))
}
func (c *Clyx) Eval(q any) {
@@ -307,11 +307,11 @@ func (c *Clyx) Eval(q any) {
if r := recover(); r != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", r) }
}()
if s, ok := q.(string); ok {
c.execQuot(toAnySlice(tokenize(s)))
c.execQForm(toAnySlice(tokenize(s)))
} else if slice, ok := q.([]any); ok {
c.execQuot(slice)
c.execQForm(slice)
} else {
panic("eval expects string/quotation")
panic("eval expects string or Q-form")
}
}
@@ -345,7 +345,7 @@ func NewClyx(libfname string, libCode string) *Clyx {
cVal, tVal, fVal := c.pop(), c.pop(), c.pop()
runVal := fVal
if isTruthy(cVal) { runVal = tVal }
if listVal, ok := runVal.([]any); ok { c.execQuot(listVal) } else { c.execQuot([]any{runVal}) }
if qformVal, ok := runVal.([]any); ok { c.execQForm(qformVal) } else { c.execQForm([]any{runVal}) }
}
c.words["cons"] = func() { q, x := c.pop().([]any), c.pop(); c.stack = append(c.stack, append([]any{x}, q...)) }
c.words["uncons"] = func() {
@@ -420,16 +420,16 @@ func NewClyx(libfname string, libCode string) *Clyx {
c.words["write"] = func() {
data, fd := c.pop(), c.pop()
var toWrite string
if listVal, ok := data.([]any); ok {
if qformVal, ok := data.([]any); ok {
var sb strings.Builder
for _, val := range listVal {
for _, val := range qformVal {
sb.WriteByte(byte(toInt(val)))
}
toWrite = sb.String()
} else if strVal, ok := data.(string); ok {
toWrite = strVal
} else {
panic("write expects string or list")
panic("write expects string or Q-form")
}
n, err := fd.(ClyxFile).Write(toWrite)
if err != nil {
@@ -450,11 +450,11 @@ func NewClyx(libfname string, libCode string) *Clyx {
if err != nil {
panic(err)
}
var list []any
var qform []any
for i := 0; i < len(res); i++ {
list = append(list, int64(res[i]))
qform = append(qform, int64(res[i]))
}
c.stack = append(c.stack, list)
c.stack = append(c.stack, qform)
}
}
c.words["close"] = func() { if fd, ok := c.pop().(ClyxFile); ok { fd.Close() } }
@@ -464,8 +464,8 @@ func NewClyx(libfname string, libCode string) *Clyx {
c.words["deld"] = func() { if err := os.Remove(c.pop().(string)); err != nil { panic(err) } }
c.words["rand"] = func() { limit := toInt(c.pop()); if limit <= 0 { c.stack = append(c.stack, int64(0)) } else { c.stack = append(c.stack, rand.Int63n(limit)) } }
c.words[".s"] = func() { printVal(c.reprStack()) }
c.words["dip"] = func() { q, y := c.pop().([]any), c.pop(); c.execQuot(q); c.stack = append(c.stack, y) }
c.words["loop"] = func() { body, cond := c.pop().([]any), c.pop().([]any); for { c.execQuot(cond); if !isTruthy(c.pop()) { break }; c.execQuot(body) } }
c.words["dip"] = func() { q, y := c.pop().([]any), c.pop(); c.execQForm(q); c.stack = append(c.stack, y) }
c.words["loop"] = func() { body, cond := c.pop().([]any), c.pop().([]any); for { c.execQForm(cond); if !isTruthy(c.pop()) { break }; c.execQForm(body) } }
c.words["hsargs"] = func() { c.stack = append(c.stack, toAnySlice(os.Args)) }
c.words["hsexit"] = func() { os.Exit(int(toInt(c.pop()))) }
c.words["l2s"] = func() {