Skip to content

Commit

Permalink
docs: add a todo
Browse files Browse the repository at this point in the history
  • Loading branch information
Aden-Q committed May 9, 2024
1 parent 9bbe48b commit cad39f7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Monkey is an interpreted language written in Go. *This project is still under de
+ Works on 64-bit machines
+ Not production ready yet, need a few more details to be addressed
+ Function that allows for closures
+ Use Go's GC to prevent memory leak

## Components

Expand Down Expand Up @@ -59,7 +60,6 @@ Monkey is an interpreted language written in Go. *This project is still under de
+ [ ] check whether we need the token field in AST
+ [ ] test: increase test coverage to at least 80%
+ [ ] chore: dockerize it and publish an image to Docker Hub
+ [ ] feat: check how to trigger the GC during runtime (in REPL)
+ [ ] feat: return can only be used in functions, do not allow plain return in REPL
+ [ ] feat: if expression with multiple branches
+ [ ] feat: check peek token type in parseExpression
Expand All @@ -69,7 +69,6 @@ Monkey is an interpreted language written in Go. *This project is still under de
+ [ ] feat: PrettyPrint, color, AST, etc
+ [ ] feat: sys call such as print(...)
+ [ ] feat: add a helper for available functions
+ [ ] feat: consider how to write a GC
+ [ ] feat: switch between multiple value representation system using some flag
+ [ ] feat: class (object system)
+ [ ] feat: configuration with yaml or envrc
Expand Down
18 changes: 16 additions & 2 deletions internal/object/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package object
var _ Environment = (*environment)(nil)

type Environment interface {
Keys() []string
Get(name string) (Object, bool)
Set(name string, val Object)
Copy() Environment
copy() Environment
}

type environment struct {
Expand All @@ -19,6 +20,19 @@ func NewEnvironment() Environment {
}
}

func NewClosureEnvironment(env Environment) Environment {
return env.copy()
}

func (e *environment) Keys() []string {
keys := make([]string, 0, len(e.store))
for k := range e.store {
keys = append(keys, k)
}

return keys
}

func (e *environment) Get(name string) (Object, bool) {
obj, ok := e.store[name]
return obj, ok
Expand All @@ -29,7 +43,7 @@ func (e *environment) Set(name string, val Object) {
}

// return a deep copy of the env
func (e *environment) Copy() Environment {
func (e *environment) copy() Environment {
env := NewEnvironment()
for k, v := range e.store {
env.Set(k, v)
Expand Down
3 changes: 0 additions & 3 deletions internal/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,12 @@ func (e *Error) IsTruthy() bool {
type Func struct {
Parameters []*ast.IdentifierExpression
Body *ast.BlockStatement
// the environment for the function scope, allowing closure
Env Environment
}

func NewFunc(params []*ast.IdentifierExpression, body *ast.BlockStatement, env Environment) *Func {
return &Func{
Parameters: params,
Body: body,
Env: env.Copy(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ type repl struct {

func New(config Config) REPL {
return &repl{
history: make([]string, config.MaxHistory),
history: make([]string, 0, config.MaxHistory),
}
}

func (r *repl) Start(in io.ReadCloser, out io.WriteCloser, userName string) {
scanner := bufio.NewScanner(in)
l := lexer.New()
p := parser.New(l)
e := evaluator.New()
e := evaluator.New(object.NewEnvironment())

fmt.Print(MONKEY_FACE)
fmt.Printf("Hello %s! This is the Monkey programming language!\n", userName)
Expand Down

0 comments on commit cad39f7

Please sign in to comment.