def run(m: Module) -> Module: filename = Symbol("filename") doSomething = Symbol("doSomething") # todo: import with m.func("Run", LazyFormat("{}: string", filename), return_="error"): config = load_config(m, filename) m.return_(doSomething(config)) # or "return nil" return m
import typing as t from appkit import Config, Error, Cleanup, gen, Symbol from prestring.go import Module class Foo: __gomodule__ = "github.com/podhmo/appkit/foo" def FromConfig(c: Config) -> Foo: pass c = Symbol("c", Config) m = Module() with m.func("run", return_="error"): m.stmt("filename := *config") m.stmt("c, err := conf.LoadConfig(filename)") with m.if_("err != nil"): m.return_("err") m = gen(m, c, FromConfig) m.stmt("fooOb := foo.FromConfig(c)") m.return_("use(fooOb)") print(m)
from prestring.go import Module m = Module() m.package("main") with m.import_group() as im: im.import_("fmt") with m.func("main"): m.stmt("fmt.Println(`hello world`)") print(m)
from prestring.go import Module m = Module() m.package('main') with m.import_group() as im: pass with m.func('main'): im.import_('log') m.stmt('log.Println("hmm")') print(m)
from prestring.go import Module m = Module() m.package('main') with m.import_group() as im: im.import_("fmt") with m.func('Fizzbuzz', 'v int', return_='(string, error)'): with m.if_("v == 1"): m.return_('"1", nil') for i in range(2, 101): with m.elif_("v == {}".format(i)): if i % 15 == 0: m.return_('"fizzbuzz", nil') elif i % 3 == 0: m.return_('"fizz", nil') elif i % 5 == 0: m.return_('"buzz", nil') else: m.return_('"{}", nil'.format(i)) with m.else_(): m.return_('"", fmt.Errorf("unsupported value: %q", v)') print(m)
from prestring.go import Module m = Module() m.package('main') with m.import_group() as im: im.import_("fmt") with m.func('main'): with m.for_("i := 1; i <= 100; i++"): m.stmt("v, err := FizzBuzz(i)") with m.if_("err != nil"): m.stmt("panic(err)") m.stmt("fmt.Println(v)") with m.func('FizzBuzz', 'v int', return_='(string, error)'): with m.if_("v == 1"): m.return_('"1", nil') for i in range(2, 101): with m.elif_("v == {}".format(i)): if i % 15 == 0: m.return_('"fizzbuzz", nil') elif i % 3 == 0: m.return_('"fizz", nil') elif i % 5 == 0: m.return_('"buzz", nil') else: m.return_('"{}", nil'.format(i)) with m.else_(): m.return_('"", fmt.Errorf("unsupported value: %q", v)')
from prestring.go import Module import sys m = Module() N = int(sys.argv[1]) m.package('main') with m.import_group() as im: im('sync') im('fmt') im('math/rand') with m.type_('S struct'): for i in range(N): m.stmt(f'Value{i} int') with m.func('main'): m.stmt('var wg sync.WaitGroup') m.stmt("var m sync.Mutex") m.stmt('s := S{}') for i in range(N): m.stmt('wg.Add(1)') m.stmt('go func(){') with m.scope(): m.stmt("m.Lock()") m.stmt("defer m.Unlock()") m.stmt(f's.Value{i} = rand.Int()') m.stmt('wg.Done()') m.unnewline() m.stmt('}()') m.stmt("wg.Wait()") m.stmt('fmt.Printf("%#+v\\n", s)')