Added module and import support #6

Merged
prologic merged 3 commits from module_import_support into master 3 years ago
prologic commented 3 years ago
Owner

Fixes #4

Example:

With a module called foo.monkey in the current directory:

A : = 5
Sum := fn(a, b) { return a + b }

The following snippet works in the evalulator:

foo := import("foo")
foo.A // 5
foo.Sum(2, 3) // 5

TODO:

  • Implement in Compiler/VM
  • Only "export" capitalised bindings.
  • Add support for MONKEYPATH search path(s).
Fixes #4 Example: With a module called `foo.monkey` in the current directory: ```#!monkey A : = 5 Sum := fn(a, b) { return a + b } ``` The following snippet works in the evalulator: ```#!monkey foo := import("foo") foo.A // 5 foo.Sum(2, 3) // 5 ``` TODO: - [x] Implement in Compiler/VM - [x] Only "export" capitalised bindings. - [x] Add support for `MONKEYPATH` search path(s).
codecov[bot] commented 3 years ago (Migrated from github.com)
Poster
Owner

Codecov Report

Merging #6 into master will decrease coverage by 0.02%.
The diff coverage is 60%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master       #6      +/-   ##
==========================================
- Coverage   64.65%   64.62%   -0.03%     
==========================================
  Files          21       22       +1     
  Lines        2422     2553     +131     
==========================================
+ Hits         1566     1650      +84     
- Misses        737      768      +31     
- Partials      119      135      +16
Impacted Files Coverage Δ
code/code.go 76.47% <ø> (ø) ⬆️
object/module.go 0% <0%> (ø)
ast/ast.go 9.14% <0%> (-0.5%) ⬇️
object/environment.go 0% <0%> (ø) ⬆️
compiler/symbol_table.go 100% <100%> (ø) ⬆️
parser/parser.go 79.2% <60%> (-0.61%) ⬇️
eval/eval.go 73.69% <66.66%> (-0.67%) ⬇️
compiler/compiler.go 74.63% <71.42%> (-0.06%) ⬇️
vm/vm.go 74.65% <72.85%> (+1.69%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0b08371...144933b. Read the comment docs.

# [Codecov](https://codecov.io/gh/prologic/monkey-lang/pull/6?src=pr&el=h1) Report > Merging [#6](https://codecov.io/gh/prologic/monkey-lang/pull/6?src=pr&el=desc) into [master](https://codecov.io/gh/prologic/monkey-lang/commit/0b0837128faf7acec14312db23059c187f794766?src=pr&el=desc) will **decrease** coverage by `0.02%`. > The diff coverage is `60%`. [![Impacted file tree graph](https://codecov.io/gh/prologic/monkey-lang/pull/6/graphs/tree.svg?width=650&token=CP7p2ym3Xm&height=150&src=pr)](https://codecov.io/gh/prologic/monkey-lang/pull/6?src=pr&el=tree) ```diff @@ Coverage Diff @@ ## master #6 +/- ## ========================================== - Coverage 64.65% 64.62% -0.03% ========================================== Files 21 22 +1 Lines 2422 2553 +131 ========================================== + Hits 1566 1650 +84 - Misses 737 768 +31 - Partials 119 135 +16 ``` | [Impacted Files](https://codecov.io/gh/prologic/monkey-lang/pull/6?src=pr&el=tree) | Coverage Δ | | |---|---|---| | [code/code.go](https://codecov.io/gh/prologic/monkey-lang/pull/6/diff?src=pr&el=tree#diff-Y29kZS9jb2RlLmdv) | `76.47% <ø> (ø)` | :arrow_up: | | [object/module.go](https://codecov.io/gh/prologic/monkey-lang/pull/6/diff?src=pr&el=tree#diff-b2JqZWN0L21vZHVsZS5nbw==) | `0% <0%> (ø)` | | | [ast/ast.go](https://codecov.io/gh/prologic/monkey-lang/pull/6/diff?src=pr&el=tree#diff-YXN0L2FzdC5nbw==) | `9.14% <0%> (-0.5%)` | :arrow_down: | | [object/environment.go](https://codecov.io/gh/prologic/monkey-lang/pull/6/diff?src=pr&el=tree#diff-b2JqZWN0L2Vudmlyb25tZW50Lmdv) | `0% <0%> (ø)` | :arrow_up: | | [compiler/symbol\_table.go](https://codecov.io/gh/prologic/monkey-lang/pull/6/diff?src=pr&el=tree#diff-Y29tcGlsZXIvc3ltYm9sX3RhYmxlLmdv) | `100% <100%> (ø)` | :arrow_up: | | [parser/parser.go](https://codecov.io/gh/prologic/monkey-lang/pull/6/diff?src=pr&el=tree#diff-cGFyc2VyL3BhcnNlci5nbw==) | `79.2% <60%> (-0.61%)` | :arrow_down: | | [eval/eval.go](https://codecov.io/gh/prologic/monkey-lang/pull/6/diff?src=pr&el=tree#diff-ZXZhbC9ldmFsLmdv) | `73.69% <66.66%> (-0.67%)` | :arrow_down: | | [compiler/compiler.go](https://codecov.io/gh/prologic/monkey-lang/pull/6/diff?src=pr&el=tree#diff-Y29tcGlsZXIvY29tcGlsZXIuZ28=) | `74.63% <71.42%> (-0.06%)` | :arrow_down: | | [vm/vm.go](https://codecov.io/gh/prologic/monkey-lang/pull/6/diff?src=pr&el=tree#diff-dm0vdm0uZ28=) | `74.65% <72.85%> (+1.69%)` | :arrow_up: | | ... and [1 more](https://codecov.io/gh/prologic/monkey-lang/pull/6/diff?src=pr&el=tree-more) | | ------ [Continue to review full report at Codecov](https://codecov.io/gh/prologic/monkey-lang/pull/6?src=pr&el=continue). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta) > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data` > Powered by [Codecov](https://codecov.io/gh/prologic/monkey-lang/pull/6?src=pr&el=footer). Last update [0b08371...144933b](https://codecov.io/gh/prologic/monkey-lang/pull/6?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
Poster
Owner

This is almost working aside from two bugs:

The following snippets (defined in a module) do not work:

Get := fn() { return A }

Adder := fn(x) {
  return fn(y) {
    return x + y
  }
}

This causes the VM to crash because the code currently doesn't take into account referenced constants or globals in the module. I'm not yet sure how to resolve this.

This is *almost* working aside from two bugs: The following snippets (*defined in a module*) do not work: ``` Get := fn() { return A } Adder := fn(x) { return fn(y) { return x + y } } ``` This causes the VM to crash because the code currently doesn't take into account referenced constants or globals in the module. I'm not yet sure how to resolve this.
Poster
Owner

Example crashes:

Globals:

$ ./monkey-lang
Hello prologic! This is the Monkey programming language!
Feel free to type in commands
>> foo := import("foo")
>> foo.Get()
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x4105d12]

goroutine 1 [running]:
github.com/prologic/monkey-lang/repl.(*REPL).StartExecLoop(0xc0002d1f58, 0x4177a00, 0xc00000e010, 0x4177a20, 0xc00000e018, 0xc000060300)
	/Users/prologic/Projects/monkey-lang/repl/repl.go:209 +0x6a2
github.com/prologic/monkey-lang/repl.(*REPL).Run(0xc00004df58)
	/Users/prologic/Projects/monkey-lang/repl/repl.go:225 +0x1e5
main.main()
	/Users/prologic/Projects/monkey-lang/main.go:124 +0xaed

Closures:

$ ./monkey-lang
Hello prologic! This is the Monkey programming language!
Feel free to type in commands
>> foo := import("foo")
>> foo.Adder(2)
panic: runtime error: index out of range

goroutine 1 [running]:
github.com/prologic/monkey-lang/vm.(*VM).pushClosure(0xc0002f7b90, 0x4, 0x1, 0x0, 0x0)
	/Users/prologic/Projects/monkey-lang/vm/vm.go:611 +0x281
github.com/prologic/monkey-lang/vm.(*VM).Run(0xc0002f7b90, 0x800, 0x800)
	/Users/prologic/Projects/monkey-lang/vm/vm.go:850 +0x14fb
github.com/prologic/monkey-lang/repl.(*REPL).StartExecLoop(0xc0002f7f58, 0x4177a00, 0xc0000b0000, 0x4177a20, 0xc0000b0008, 0xc0000a02c0)
	/Users/prologic/Projects/monkey-lang/repl/repl.go:201 +0x651
github.com/prologic/monkey-lang/repl.(*REPL).Run(0xc000048f58)
	/Users/prologic/Projects/monkey-lang/repl/repl.go:225 +0x1e5
main.main()
	/Users/prologic/Projects/monkey-lang/main.go:124 +0xaed
Example crashes: Globals: ``` $ ./monkey-lang Hello prologic! This is the Monkey programming language! Feel free to type in commands >> foo := import("foo") >> foo.Get() panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x4105d12] goroutine 1 [running]: github.com/prologic/monkey-lang/repl.(*REPL).StartExecLoop(0xc0002d1f58, 0x4177a00, 0xc00000e010, 0x4177a20, 0xc00000e018, 0xc000060300) /Users/prologic/Projects/monkey-lang/repl/repl.go:209 +0x6a2 github.com/prologic/monkey-lang/repl.(*REPL).Run(0xc00004df58) /Users/prologic/Projects/monkey-lang/repl/repl.go:225 +0x1e5 main.main() /Users/prologic/Projects/monkey-lang/main.go:124 +0xaed ``` Closures: ``` $ ./monkey-lang Hello prologic! This is the Monkey programming language! Feel free to type in commands >> foo := import("foo") >> foo.Adder(2) panic: runtime error: index out of range goroutine 1 [running]: github.com/prologic/monkey-lang/vm.(*VM).pushClosure(0xc0002f7b90, 0x4, 0x1, 0x0, 0x0) /Users/prologic/Projects/monkey-lang/vm/vm.go:611 +0x281 github.com/prologic/monkey-lang/vm.(*VM).Run(0xc0002f7b90, 0x800, 0x800) /Users/prologic/Projects/monkey-lang/vm/vm.go:850 +0x14fb github.com/prologic/monkey-lang/repl.(*REPL).StartExecLoop(0xc0002f7f58, 0x4177a00, 0xc0000b0000, 0x4177a20, 0xc0000b0008, 0xc0000a02c0) /Users/prologic/Projects/monkey-lang/repl/repl.go:201 +0x651 github.com/prologic/monkey-lang/repl.(*REPL).Run(0xc000048f58) /Users/prologic/Projects/monkey-lang/repl/repl.go:225 +0x1e5 main.main() /Users/prologic/Projects/monkey-lang/main.go:124 +0xaed ```
Poster
Owner

And 0cf0f30 fixes this

And 0cf0f30 fixes this
The pull request has been merged as c09707eec4.
Sign in to join this conversation.
No reviewers
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Dependencies

This pull request currently doesn't have any dependencies.

Loading…
There is no content yet.