-
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
d5eac1a
commit 1d75089
Showing
3 changed files
with
103 additions
and
38 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
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,57 @@ | ||
package wdte_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/DeedleFake/wdte" | ||
"github.com/DeedleFake/wdte/wdteutil" | ||
) | ||
|
||
const src = ` | ||
let i => import 'some/import/path/or/another'; | ||
i.print 3; | ||
+ 5 2 -> i.print; | ||
7 -> + 5 -> i.print; | ||
` | ||
|
||
func im(from string) (*wdte.Scope, error) { | ||
return wdte.S().Map(map[wdte.ID]wdte.Func{ | ||
"print": wdteutil.Func("print", func(v interface{}) interface{} { | ||
fmt.Println(v) | ||
return v | ||
}), | ||
}), nil | ||
} | ||
|
||
func Sum(frame wdte.Frame, args ...wdte.Func) wdte.Func { | ||
frame = frame.Sub("+") | ||
|
||
if len(args) < 2 { | ||
return wdteutil.SaveArgs(wdte.GoFunc(Sum), args...) | ||
} | ||
|
||
var sum wdte.Number | ||
for _, arg := range args { | ||
sum += arg.(wdte.Number) | ||
} | ||
return sum | ||
} | ||
|
||
func ExampleREADME() { | ||
m, err := wdte.Parse(strings.NewReader(src), wdte.ImportFunc(im), nil) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error parsing script: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
scope := wdte.S().Add("+", wdte.GoFunc(Sum)) | ||
|
||
r := m.Call(wdte.F().WithScope(scope)) | ||
if err, ok := r.(error); ok { | ||
fmt.Fprintf(os.Stderr, "Error running script: %v\n", err) | ||
os.Exit(1) | ||
} | ||
} |