Replace Tre/False types with a Bool type

This commit is contained in:
Sameer Rahmani 2021-01-14 18:17:38 +00:00
parent c88c7dad96
commit 09a6d92c08
4 changed files with 33 additions and 66 deletions

View File

@ -25,8 +25,7 @@ type NodeType int
const (
Nil NodeType = iota
Nothing
True
False
Bool
Instruction
Symbol
Keyword

View File

@ -23,28 +23,44 @@ import (
"serene-lang.org/bootstrap/pkg/hash"
)
type False struct {
type Bool struct {
Node
ExecutionScope
value bool
}
func (f *False) GetType() ast.NodeType {
return ast.False
func (t *Bool) GetType() ast.NodeType {
return ast.Bool
}
func (f *False) String() string {
func (t *Bool) String() string {
if t.value {
return "true"
}
return "false"
}
func (f *False) ToDebugStr() string {
return "false"
func (t *Bool) ToDebugStr() string {
return t.String()
}
func (f *False) Hash() uint32 {
bytes := []byte("false")
return hash.Of(append([]byte{byte(ast.False)}, bytes...))
func (t *Bool) Hash() uint32 {
bytes := []byte(t.String())
return hash.Of(append([]byte{byte(ast.Bool)}, bytes...))
}
func MakeFalse(n Node) *False {
return &False{Node: n}
func (t *Bool) isTrue() bool {
return t.value
}
func (t *Bool) isFalse() bool {
return !t.value
}
func MakeTrue(n Node) *Bool {
return &Bool{Node: n, value: true}
}
func MakeFalse(n Node) *Bool {
return &Bool{Node: n, value: false}
}

View File

@ -451,13 +451,13 @@ tco:
return nil, err
}
if result != ast.False && result != ast.Nil {
// Truthy clause
exprs = append([]IExpr{args.Rest().First()}, restOfExprs(exprs, i)...)
} else {
if (result == ast.Bool && pred.(*Bool).isFalse()) || result == ast.Nil {
// Falsy clause
exprs = append([]IExpr{args.Rest().Rest().First()}, restOfExprs(exprs, i)...)
} else {
// Truthy clause
exprs = append([]IExpr{args.Rest().First()}, restOfExprs(exprs, i)...)
}
i = 0
goto body // rewrite

View File

@ -1,48 +0,0 @@
/*
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 (
"serene-lang.org/bootstrap/pkg/ast"
"serene-lang.org/bootstrap/pkg/hash"
)
type True struct {
Node
ExecutionScope
}
func (t *True) GetType() ast.NodeType {
return ast.True
}
func (t *True) String() string {
return "true"
}
func (t *True) ToDebugStr() string {
return "true"
}
func (t *True) Hash() uint32 {
bytes := []byte("true")
return hash.Of(append([]byte{byte(ast.True)}, bytes...))
}
func MakeTrue(n Node) *True {
return &True{Node: n}
}