Skip to content

Commit

Permalink
Fix parser bug
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoNex committed Nov 20, 2023
1 parent 53f49a2 commit 194da3b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
12 changes: 12 additions & 0 deletions internal/obj/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
}
Expand Down
14 changes: 9 additions & 5 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 194da3b

Please sign in to comment.