From 09a6d92c08d30e75eafd0f46027e1a583b44adfd Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Thu, 14 Jan 2021 18:17:38 +0000 Subject: [PATCH] Replace Tre/False types with a Bool type --- bootstrap/pkg/ast/ast.go | 3 +- bootstrap/pkg/core/{false.go => bool.go} | 38 +++++++++++++------ bootstrap/pkg/core/eval.go | 10 ++--- bootstrap/pkg/core/true.go | 48 ------------------------ 4 files changed, 33 insertions(+), 66 deletions(-) rename bootstrap/pkg/core/{false.go => bool.go} (59%) delete mode 100644 bootstrap/pkg/core/true.go diff --git a/bootstrap/pkg/ast/ast.go b/bootstrap/pkg/ast/ast.go index 189e73c..cacfda4 100644 --- a/bootstrap/pkg/ast/ast.go +++ b/bootstrap/pkg/ast/ast.go @@ -25,8 +25,7 @@ type NodeType int const ( Nil NodeType = iota Nothing - True - False + Bool Instruction Symbol Keyword diff --git a/bootstrap/pkg/core/false.go b/bootstrap/pkg/core/bool.go similarity index 59% rename from bootstrap/pkg/core/false.go rename to bootstrap/pkg/core/bool.go index 1b3046b..670f0ed 100644 --- a/bootstrap/pkg/core/false.go +++ b/bootstrap/pkg/core/bool.go @@ -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} } diff --git a/bootstrap/pkg/core/eval.go b/bootstrap/pkg/core/eval.go index 4a94df7..a855348 100644 --- a/bootstrap/pkg/core/eval.go +++ b/bootstrap/pkg/core/eval.go @@ -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 diff --git a/bootstrap/pkg/core/true.go b/bootstrap/pkg/core/true.go deleted file mode 100644 index e05416d..0000000 --- a/bootstrap/pkg/core/true.go +++ /dev/null @@ -1,48 +0,0 @@ -/* - Serene --- Yet an other Lisp - -Copyright (c) 2020 Sameer Rahmani - -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 . -*/ - -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} -}