-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42c7766
commit 66e28d2
Showing
27 changed files
with
3,450 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.DS_STORE | ||
monkey | ||
fibonacci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Attach to Process", | ||
"type": "go", | ||
"request": "attach", | ||
"mode": "local", | ||
"processId": 99800 | ||
}, | ||
{ | ||
"name": "Launch Benchmark", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${workspaceFolder}/benchmark", | ||
"args": ["-engine", "eval"] | ||
}, | ||
{ | ||
"name": "Launch Main", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "debug", | ||
"program": "${workspaceFolder}/cmd", | ||
|
||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/dikaeinstein/monkey/compile" | ||
"github.com/dikaeinstein/monkey/eval" | ||
"github.com/dikaeinstein/monkey/lexer" | ||
"github.com/dikaeinstein/monkey/object" | ||
"github.com/dikaeinstein/monkey/parser" | ||
"github.com/dikaeinstein/monkey/vm" | ||
) | ||
|
||
func main() { | ||
engine := flag.String("engine", "vm", "use 'vm' or 'eval'") | ||
flag.Parse() | ||
|
||
var input = ` | ||
let fibonacci = fn(x) { | ||
if (x == 0) { | ||
0 | ||
} else { | ||
if (x == 1) { | ||
return 1; | ||
} else { | ||
fibonacci(x - 1) + fibonacci(x - 2); | ||
} | ||
} | ||
}; | ||
fibonacci(35); | ||
` | ||
|
||
l := lexer.New(input) | ||
p := parser.New(l) | ||
program := p.ParseProgram() | ||
if len(p.Errors()) != 0 { | ||
fmt.Println("parser errors:", p.Errors()) | ||
return | ||
} | ||
|
||
var duration time.Duration | ||
var result object.Object | ||
|
||
if *engine == "vm" { | ||
compiler := compile.NewCompilerWithBuiltins([]object.Object{}) | ||
err := compiler.Compile(program) | ||
if err != nil { | ||
fmt.Printf("compiler error: %s", err) | ||
return | ||
} | ||
|
||
machine := vm.New(compiler.Bytecode()) | ||
|
||
start := time.Now() | ||
|
||
err = machine.Run() | ||
if err != nil { | ||
fmt.Printf("vm error: %s", err) | ||
return | ||
} | ||
|
||
duration = time.Since(start) | ||
result = machine.LastPoppedStackElem() | ||
} else { | ||
env := object.NewEnvironment() | ||
start := time.Now() | ||
result = eval.Eval(program, env) | ||
duration = time.Since(start) | ||
} | ||
|
||
fmt.Printf( | ||
"engine=%s, result=%s, duration=%s\n", | ||
*engine, | ||
result.Inspect(), | ||
duration) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.