From 194da3b96e9078572513c8267256bde450830dee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Santamaria?= Date: Mon, 20 Nov 2023 23:21:58 +0100 Subject: [PATCH] Fix parser bug --- internal/obj/object.go | 12 ++++++++++++ internal/parser/parser.go | 14 +++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/internal/obj/object.go b/internal/obj/object.go index a370c12..9c29c3f 100644 --- a/internal/obj/object.go +++ b/internal/obj/object.go @@ -121,6 +121,10 @@ func (o Object) Type() Type { return o._type } +func (o Object) TypeString() string { + return C.GoString(C.otype_str(o._type)) +} + func (o Object) String() string { cstr := C.object_str(o) defer C.free(unsafe.Pointer(cstr)) @@ -181,6 +185,14 @@ func GoError(o Object) error { return nil } +func NewBool(b bool) Object { + if b { + return C.true_obj + } else { + return C.false_obj + } +} + func NewInteger(i int64) Object { return C.new_integer_obj(C.int64_t(i)) } diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 8397997..c88248f 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -216,17 +216,21 @@ func (p *Parser) parseReturn() ast.Node { return ret } +func (p Parser) hasSemicolon() bool { + return p.cur.Is(item.Semicolon) || p.peek.Is(item.Semicolon) +} + func (p *Parser) parseExpr(precedence int) ast.Node { if prefixFn, ok := p.prefixParsers[p.cur.Typ]; ok { leftExp := prefixFn() - for !p.peek.Is(item.Semicolon) && precedence < p.peekPrecedence() { - if infixFn, ok := p.infixParsers[p.peek.Typ]; ok { - p.next() - leftExp = infixFn(leftExp) - } else { + for !p.hasSemicolon() && precedence < p.peekPrecedence() { + infixFn, ok := p.infixParsers[p.peek.Typ] + if !ok { break } + p.next() + leftExp = infixFn(leftExp) } if p.peek.Is(item.Semicolon) {