skx / yal

Yet another lisp interpreter
GNU General Public License v2.0
16 stars 0 forks source link

Support character literals #64

Closed skx closed 1 year ago

skx commented 1 year ago

Something like this is sufficient as a proof of concept:

git diff eval/eval.go 
diff --git a/eval/eval.go b/eval/eval.go
index 1cae1b0..694b05d 100644
--- a/eval/eval.go
+++ b/eval/eval.go
@@ -98,10 +98,17 @@ func (ev *Eval) atom(token string) primitive.Primitive {
        case "nil":
                return primitive.Nil{}
        }
+
+       // string
        if token[0] == '"' {
                return primitive.String(strings.ReplaceAll(strings.Trim(token, `"`), `\"`, `"`))
        }

+       // character literal
+       if strings.HasPrefix(token,"#\\") {
+               return primitive.String( token[2:] )
+       }
+
        // if it isn't a number then it is a symbol
        f, err := strconv.ParseFloat(token, 64)
        if err == nil {

Once that is done we should obviously have "primitive.Char(acter)", and implement some character primitives:

And probably look at some kinda way to split a string into characters, and vice versa. (split) is used for that right now, but it officially returns strings - and I just split into characters via an empty second argument