Add support for very basic string implementation

This commit is contained in:
Sameer Rahmani 2020-12-16 22:40:59 +00:00
parent 4b17c0d740
commit 1d66c2a56e
4 changed files with 71 additions and 4 deletions

View File

@ -34,6 +34,7 @@ const (
Fn
NativeFn
Namespace
String
Block // Dont' mistake it with block from other programming languages
)

View File

@ -36,6 +36,9 @@ func evalForm(rt *Runtime, scope IScope, form IExpr) (IExpr, IError) {
case ast.Number:
return form, nil
case ast.String:
return form, nil
// Symbol evaluation rules:
// * If it's a NS qualified symbol (NSQS), Look it up in the external symbol table of
// the current namespace.

View File

@ -208,8 +208,24 @@ func readRawSymbol(parser IParsable) (IExpr, IError) {
return sym, nil
}
func readString(parser IParsable) (IExpr, IError) {
str := ""
// readNumber reads a number with respect to its sign and whether it's
for {
c := parser.next(false)
if c == nil {
return nil, makeErrorAtPoint(parser, "reached end of file while scanning a string")
}
if *c == "\"" {
node := MakeNode(parser.Buffer(), parser.GetLocation()-len(str), parser.GetLocation())
return MakeString(node, str), nil
}
str = str + *c
}
}
// readNumber reads a number with respect to its sign and whether it's, a ...interface{}
// a decimal or a float
func readNumber(parser IParsable, neg bool) (IExpr, IError) {
isDouble := false
@ -267,9 +283,10 @@ func readSymbol(parser IParsable) (IExpr, IError) {
return nil, makeErrorAtPoint(parser, "unexpected end of file while scanning a symbol")
}
// if c == "\"" {
// return readString(parser)
// }
if *c == "\"" {
parser.next(false)
return readString(parser)
}
// Weird, But go won't stop complaining without this swap
char := *c

View File

@ -0,0 +1,46 @@
/*
Serene --- Yet an other Lisp
Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package core
import (
"fmt"
"serene-lang.org/bootstrap/pkg/ast"
)
type String struct {
Node
content string
}
func (s *String) GetType() ast.NodeType {
return ast.String
}
func (s *String) String() string {
return s.content
}
func (s *String) ToDebugStr() string {
return fmt.Sprintf("<%s at %p>", s.content, s)
}
func MakeString(n Node, s string) *String {
return &String{n, s}
}