Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP p/jaekwon/... #461

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
.PHONY: logos goscan gnoland gnokey gnofaucet logos reset gnoweb gnotxport
all: build

build: gnoland gnokey goscan logos gnoweb gnotxport
build: gnoland gnokey gnodev goscan logos gnoweb gnotxport

install: install_gnodev install_gnokey

Expand Down
8 changes: 7 additions & 1 deletion cmd/gnodev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ type (
)

var mainApps AppList = []AppItem{
{
App: runApp,
Name: "run",
Desc: "run a Gno file",
Defaults: defaultRunOptions,
},
{
App: buildApp,
Name: "build",
Expand Down Expand Up @@ -55,7 +61,7 @@ var mainApps AppList = []AppItem{
// graph
// vendor -- download deps from the chain in vendor/
// list -- list packages
// run -- call render(), or maybe create a new main?
// render -- call render()?
// publish/release
// generate
// doc -- godoc
Expand Down
1 change: 1 addition & 0 deletions cmd/gnodev/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func runRepl(rootDir string, verbose bool) error {
}
m := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "test",
Output: stdout,
Store: testStore,
})

Expand Down
68 changes: 68 additions & 0 deletions cmd/gnodev/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"os"

"github.com/gnolang/gno/pkgs/command"
"github.com/gnolang/gno/pkgs/errors"
gno "github.com/gnolang/gno/pkgs/gnolang"
"github.com/gnolang/gno/tests"
)

type runOptions struct {
Verbose bool `flag:"verbose" help:"verbose"`
RootDir string `flag:"root-dir" help:"clone location of github.com/gnolang/gno (gnodev tries to guess it)"`
// Timeout time.Duration `flag:"timeout" help:"max execution time"`
// VM Options
// UseNativeLibs bool // experimental, but could be useful for advanced developer needs
}

var defaultRunOptions = runOptions{
Verbose: false,
RootDir: "",
}

func runApp(cmd *command.Command, args []string, iopts interface{}) error {
opts := iopts.(runOptions)
if len(args) == 0 {
cmd.ErrPrintfln("Usage: run [flags] file.gno [file2.gno...]")
return errors.New("invalid args")
}

if opts.RootDir == "" {
opts.RootDir = guessRootDir()
}

fnames := args

return runRun(opts.RootDir, opts.Verbose, fnames)
}

func runRun(rootDir string, verbose bool, fnames []string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

stdin := os.Stdin
stdout := os.Stdout
stderr := os.Stderr

// init store and machine
testStore := tests.TestStore(rootDir, "", stdin, stdout, stderr, tests.ImportModeStdlibsOnly)
if verbose {
testStore.SetLogStoreOps(true)
}
m := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "main",
Output: stdout,
Store: testStore,
})

// read files
files := make([]*gno.FileNode, len(fnames))
for i, fname := range fnames {
files[i] = gno.MustReadFile(fname)
}

// run files
m.RunFiles(files...)
m.RunMain()

return nil
}
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/avl/node.gno
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package avl

//----------------------------------------
// Node

type Node struct {
Expand Down
36 changes: 32 additions & 4 deletions examples/gno.land/p/demo/avl/tree.gno
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,39 @@ func (tree *Tree) Remove(key string) (value interface{}, removed bool) {
}

// Shortcut for TraverseInRange.
func (tree *Tree) Iterate(start, end string, cb func(*Node) bool) bool {
return tree.node.TraverseInRange(start, end, true, true, cb)
func (tree *Tree) Iterate(start, end string, cb IterCbFn) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came across this PR. It looks like a lot of this is already implemented. Perhaps this PR is no longer necessary?

return tree.node.TraverseInRange(start, end, true, true,
func(node *Node) {
cb(node.Key(), node.Value())
},
)
}

// Shortcut for TraverseInRange.
func (tree *Tree) IterateReverse(start, end string, cb func(*Node) bool) bool {
return tree.node.TraverseInRange(start, end, false, true, cb)
func (tree *Tree) ReverseIterate(start, end string, cb IterCbFn) bool {
return tree.node.TraverseInRange(start, end, false, true,
func(node *Node) {
cb(node.Key(), node.Value())
},
)
}

// Shortcut for TraverseByOffset.
func (tree *Tree) IterateByOffset(offset int, count int, cb IterCbFn) bool {
return tree.node.TraverseByOffset(offset, count, true, true,
func(node *Node) {
cb(node.Key(), node.Value())
},
)
}

// Shortcut for TraverseByOffset.
func (tree *Tree) ReverseIterateByOffset(start, end string, cb IterCbFn) bool {
return tree.node.TraverseByOffset(offset, count, false, true,
func(node *Node) {
cb(node.Key(), node.Value())
},
)
}

func IterCbFn func(key string, value interface{}) bool
Empty file.
97 changes: 97 additions & 0 deletions examples/gno.land/p/jaekwon/paginate/paginate.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package paginate

import "strconv"

type pageFn func(page int, size int, reverse bool) string

// TODO maybe put somewhere else.
type renderer interface {
Render() string
}

// TODO this function belongs somewhere convenient.
// escape a URL for markdown.
func escMDURL(s string) string {
// XXX implement
}


var itoa = strconv.Itoa

// pageFn: path function to render paginattion links. if nil, no links are
// rendered. result must start with ':'.
// t: the tree with elements. each value must implement Render() string.
// page: page number starting from 1.
// size: page size.
// reverse: reverse order. // XXX implement
func RenderTreeByPage(
t avl.Tree,
pageFn pageFn,
page int, size int, reverse bool) {

// sanity checks
if page < 0 {
panic("invalid negative page to RenderTreeByPage")
}
if size <= 0 {
panic("invalid nonpositive size to RenderTreeByPage")
}

str := ""
numpages := (t.Size() / size) + 1

// render pagination
if pageFn == nil {
// do not render pagination
} else if page > numpages {
// page out of bounds.
// [1](...) [2](...) ... [101](...) [102)(...)
// where ... is result of pageFn().
// TODO: improve this function by renderring
// only the first and last few pages. XXX
for p := 1; p <= numPages; p++ {
pageurl := pageFn(p, size, reverse)
str += "[" + itoa(page) + "](" +
escMDURL(pageurl) + ") "
}
str += "\n"
str += "page " + itoa(page) + " out of bounds\n"
return str
}
// [1](...) *2* [3](...) [4](...) ... [101](...) [102)(...)
// where ... is result of pageFn().
// TODO: improve this function by only printing the first few,
// the last few, and the ones surrounding the current page.
// Must make this improvement to be strictly "memory correct".
if numpages > 0 {
for p := 1; p <= numPages; p++ {
pageurl := pageFn(p, size, reverse)
str += "[" + itoa(page) + "](" +
escMDURL(pageurl) + ") "
}
str += "\n"
}

// render tree items
offset := (page-1) * size
t.IterateByOffset(offset, size,
func(key string, value interface{}) bool {
var r = value.(renderer)
str += r.Render()
str += "\n"
str += "\n"
},
)

// render pagination again if there are pages.
if numpages > 0 {
for p := 1; p <= numPages; p++ {
pageurl := pageFn(p, size, reverse)
str += "[" + itoa(page) + "](" +
escMDURL(pageurl) + ") "
}
str += "\n"
}

return str
}
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions examples/gno.land/p/jaekwon/tests/main.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tests

/* The intent is for this file to run the main test, with data that will go on the chain upon launch */

func main() {
// XXX
}