Skip to content

Commit 6697b3d

Browse files
committed
Fix being able to call functions from maps, closes #123
1 parent 617205a commit 6697b3d

File tree

4 files changed

+67
-24
lines changed

4 files changed

+67
-24
lines changed

cmd/benchmark.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
func benchmarkCommand() {
1414
benchmarkHelloWorld()
15+
benchmarkFibonacci()
1516
}
1617

1718
func benchmarkHelloWorld() {
@@ -23,9 +24,37 @@ func benchmarkHelloWorld() {
2324
fmt.Println("==============================")
2425
fmt.Printf("Go: %s\n", goTime)
2526
fmt.Printf("Ghost: %s\n", ghostTime)
27+
fmt.Printf("Difference: %s\n", ghostTime-goTime)
28+
fmt.Printf("Difference (%%): +%.2f%%\n", (float64(ghostTime-goTime)/float64(goTime))*100)
2629
fmt.Printf("-- Scanner: %s\n", scanTime)
2730
fmt.Printf("-- Parser: %s\n", parseTime)
28-
fmt.Printf("-- Interpreter: %s\n", interpretTime)
31+
fmt.Printf("-- Interpreter: %s\n\n", interpretTime)
32+
}
33+
34+
func benchmarkFibonacci() {
35+
goTime := nativeFibonacci()
36+
scanTime, parseTime, interpretTime, ghostTime := benchmark(`
37+
function fibonacci(n) {
38+
if (n <= 1) {
39+
return n
40+
}
41+
42+
return fibonacci(n - 1) + fibonacci(n - 2)
43+
}
44+
45+
fibonacci(20)
46+
`)
47+
48+
fmt.Println("==============================")
49+
fmt.Println("Fibonacci benchmark")
50+
fmt.Println("==============================")
51+
fmt.Printf("Go: %s\n", goTime)
52+
fmt.Printf("Ghost: %s\n", ghostTime)
53+
fmt.Printf("Difference: %s\n", ghostTime-goTime)
54+
fmt.Printf("Difference (%%): +%.2f%%\n", (float64(ghostTime-goTime)/float64(goTime))*100)
55+
fmt.Printf("-- Scanner: %s\n", scanTime)
56+
fmt.Printf("-- Parser: %s\n", parseTime)
57+
fmt.Printf("-- Interpreter: %s\n\n", interpretTime)
2958
}
3059

3160
func nativeHelloWorld() time.Duration {
@@ -35,6 +64,14 @@ func nativeHelloWorld() time.Duration {
3564
return time.Since(start)
3665
}
3766

67+
func nativeFibonacci() time.Duration {
68+
start := time.Now()
69+
70+
_ = fibonacci(20)
71+
72+
return time.Since(start)
73+
}
74+
3875
func benchmark(source string) (scanTime time.Duration, parseTime time.Duration, interpretTime time.Duration, ghostTime time.Duration) {
3976
start := time.Now()
4077

@@ -57,3 +94,11 @@ func benchmark(source string) (scanTime time.Duration, parseTime time.Duration,
5794

5895
return scanTime, parseTime, interpretTime, ghostTime
5996
}
97+
98+
func fibonacci(n int) int {
99+
if n <= 1 {
100+
return n
101+
}
102+
103+
return fibonacci(n-1) + fibonacci(n-2)
104+
}

cmd/ghost.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ func main() {
4747

4848
if flagHelp {
4949
helpCommand()
50-
os.Exit(2)
50+
os.Exit(0)
5151
}
5252

5353
if flagBenchmark {
5454
benchmarkCommand()
55-
os.Exit(2)
55+
os.Exit(0)
5656
}
5757

5858
args := flag.Args()

evaluator/method.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ func evaluateMethod(node *ast.Method, scope *object.Scope) object.Object {
2525
}
2626

2727
switch receiver := left.(type) {
28+
case *object.Map:
29+
method := node.Method.(*ast.Identifier)
30+
31+
property := &object.String{Value: method.Value}
32+
33+
if function, ok := receiver.Pairs[property.MapKey()]; ok {
34+
return unwrapCall(node.Token, function.Value, arguments, scope)
35+
}
36+
37+
return newError("%d:%d:%s: runtime error: unknown method: %s.%s", node.Token.Line, node.Token.Column, node.Token.File, receiver.Type(), method.Value)
2838
case *object.Instance:
2939
method := node.Method.(*ast.Identifier)
3040
evaluated := evaluateInstanceMethod(node, receiver, method.Value, arguments)

examples/scratch.ghost

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
bar = true
2-
list = [1, 2, 3]
1+
data = {
2+
name: 'Test',
3+
handler: function(n) {
4+
print('n * n')
35

4-
// for (i in list) {
5-
// if (foo == true) {
6-
// print("foo is true")
7-
// }
6+
return n * n
7+
}
8+
}
89

9-
// if (bar == true) {
10-
// print("bar is true")
11-
// }
12-
// }
13-
14-
for (i = 0; i < 10; i = i + 1) {
15-
foo
16-
// if (foo == true) {
17-
// print("foo is true")
18-
// }
19-
20-
// if (bar == true) {
21-
// print("bar is true")
22-
// }
23-
}
10+
print(data.name)
11+
print(data.handler(6))

0 commit comments

Comments
 (0)