Add the simplest possible Number implementation

This commit is contained in:
Sameer Rahmani 2020-11-14 12:22:13 +00:00
parent 70bb8d1997
commit c0fc5b152e
4 changed files with 203 additions and 16 deletions

View File

@ -44,7 +44,7 @@ for details take a look at the LICENSE file.
`,
Run: func(cmd *cobra.Command, args []string) {
reader.ReadString("sameer mary")
ast, _ := parser.ParseToAST("(asd 'mary '(1 2 3) `(asd ~asd ~@zxc))")
ast, _ := parser.ParseToAST("(asd 'mary '(1 2 3.4 -4 -0.3) `(asd ~asd ~@zxc))")
fmt.Printf("%s\n", ast.String())
},
}

View File

@ -25,6 +25,7 @@ type NodeType int
const (
Nil NodeType = iota
Symbol
Number
List
)

View File

@ -127,33 +127,81 @@ func readRawSymbol(parser IParsable) (types.IExpr, error) {
return types.MakeSymbol(symbol), nil
}
func readNumber(parser IParsable, neg bool) (types.IExpr, error) {
isDouble := false
result := ""
if neg {
result = "-"
}
for {
c := parser.next(false)
if c == nil {
break
}
if *c == "." && isDouble {
fmt.Println(result)
return nil, errors.New("a double with more that one '.' ???")
}
if *c == "." {
isDouble = true
result = result + *c
continue
}
// Weird, But go won't stop complaining without this swap
char := *c
r := rune(char[0])
if unicode.IsDigit(r) {
result = result + *c
} else {
parser.back()
break
}
}
fmt.Println(result)
return types.MakeNumberFromStr(result, isDouble)
}
func readSymbol(parser IParsable) (types.IExpr, error) {
c := parser.peek(false)
if c == nil {
return nil, errors.New("Unexpected end of file while scanning a symbol")
return nil, errors.New("unexpected end of file while scanning a symbol")
}
// if c == "\"" {
// return readString(parser)
// }
// if unicode.IsDigit(c) {
// readNumber(parser, false)
// }
// Weird, But go won't stop complaining without this swap
char := *c
r := rune(char[0])
if unicode.IsDigit(r) {
return readNumber(parser, false)
}
// if c == "-" {
// parser.next(true)
// c := parser.peek(false)
// if unicode.IsDigit(c) {
// return readNumber(parser, true)
// } else {
// // Unread '-'
// parser.back()
// return readRawSymbol(parser)
// }
if *c == "-" {
parser.next(true)
c := parser.peek(false)
// }
// Weird, But go won't stop complaining without this swap
char := *c
r := rune(char[0])
if unicode.IsDigit(r) {
return readNumber(parser, true)
} else {
// Unread '-'
parser.back()
return readRawSymbol(parser)
}
}
return readRawSymbol(parser)
}

View File

@ -0,0 +1,138 @@
/*
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 types
import (
"fmt"
"strconv"
"serene-lang.org/bootstrap/pkg/ast"
)
type INumber interface {
IExpr
// Int() int
// I8() int8
// I16() int16
// I32() int32
I64() int64
// UInt() uint
// UI8() uint8
// UI16() uint16
// UI32() uint32
// UI64() uint64
//F32() float32
F64() float64
// Add(n Number) Number
// TDOD: Add basic operators here
}
/** WARNING:
These are really stupid implementations of numbers we
need better implmentations later, but it's ok for something
to begin with
*/
type Integer struct {
Node
value int64
}
func (i Integer) Eval() IExpr {
return &i
}
func (i Integer) GetType() ast.NodeType {
return ast.Number
}
func (i Integer) String() string {
return fmt.Sprintf("%d", i.value)
}
func (i Integer) ToDebugStr() string {
return fmt.Sprintf("%#v", i)
}
func (i Integer) I64() int64 {
return i.value
}
func (i Integer) F64() float64 {
return float64(i.value)
}
type Double struct {
Node
value float64
}
func (d Double) Eval() IExpr {
return &d
}
func (d Double) GetType() ast.NodeType {
return ast.Number
}
func (d Double) String() string {
return fmt.Sprintf("%f", d.value)
}
func (d Double) ToDebugStr() string {
return fmt.Sprintf("%#v", d)
}
func (d Double) I64() int64 {
return int64(d.value)
}
func (d Double) F64() float64 {
return d.value
}
func MakeNumberFromStr(strValue string, isDouble bool) (INumber, error) {
var ret INumber
if isDouble {
v, err := strconv.ParseFloat(strValue, 64)
if err != nil {
return nil, err
}
ret = Double{
value: v,
}
} else {
v, err := strconv.ParseInt(strValue, 10, 64)
if err != nil {
return nil, err
}
ret = Integer{
value: v,
}
}
return ret, nil
}