Skip to content

feat: Add pointer support #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions evaluator/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ func evalPrefixExpression(operator string, right object.Object, line int) object
return evalMinusPrefixOperatorExpression(right, line)
case "+":
return evalPlusPrefixOperatorExpression(right, line)
case "*":
if right == nil {
return newError("Mstari %d: huwezi kufikia thamani ya pointer isiyo na kitu (nil)", line)
}
if p, ok := right.(*object.Pointer); ok {
if p.Ref == nil {
return newError("Mstari %d: pointer haina kitu (nil)", line)
}
return p.Ref
}
return newError("Mstari %d: huwezi kufikia thamani ya kitu ambacho si pointer", line)
case "&":
if right == nil {
return newError("Mstari %d: huwezi kupata anwani ya kitu kisicho na thamani (nil)", line)
}
return &object.Pointer{Ref: right}
default:
return newError("Mstari %d: Operesheni Haieleweki: %s%s", line, operator, right.Type())
}
Expand Down
5 changes: 5 additions & 0 deletions examples/pointer.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fanya x = 42 * 2
fanya p = &x
andika(p)
andika("POINTER is",p)
andika("VALUE is",*p)
2 changes: 2 additions & 0 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func (l *Lexer) NextToken() token.Token {
ch := l.ch
l.readChar()
tok = token.Token{Type: token.AND, Literal: string(ch) + string(l.ch), Line: l.line}
} else {
tok = newToken(token.AMPERSAND, l.line, l.ch)
}
case rune('|'):
if l.peekChar() == rune('|') {
Expand Down
1 change: 1 addition & 0 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
BYTE_OBJ = "BYTE"
PACKAGE_OBJ = "PAKEJI"
INSTANCE = "PAKEJI"
POINTER_OBJ = "POINTER"
AT = "@"
)

Expand Down
20 changes: 20 additions & 0 deletions object/pointer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package object

import (
"fmt"
)

type Pointer struct {
Ref Object
}

func (p *Pointer) Type() ObjectType {
return POINTER_OBJ
}

func (p *Pointer) Inspect() string {
if p.Ref == nil {
return "Pointer(anwani=nil, thamani=nil)"
}
return fmt.Sprintf("Pointer(anwani=%p, thamani=%s)", p.Ref, p.Ref.Inspect())
}
2 changes: 2 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func New(l *lexer.Lexer) *Parser {
p.registerPrefix(token.IMPORT, p.parseImport)
p.registerPrefix(token.PACKAGE, p.parsePackage)
p.registerPrefix(token.AT, p.parseAt)
p.registerPrefix(token.AMPERSAND, p.parsePrefixExpression)
p.registerPrefix(token.ASTERISK, p.parsePrefixExpression)

p.infixParseFns = make(map[token.TokenType]infixParseFn)
p.registerInfix(token.AND, p.parseInfixExpression)
Expand Down
1 change: 1 addition & 0 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
MINUS = "-"
BANG = "!"
ASTERISK = "*"
AMPERSAND = "&"
POW = "**"
SLASH = "/"
MODULUS = "%"
Expand Down