├── .gitattributes ├── .github ├── ISSUE_TEMPLATE │ ├── bug-report.yml │ ├── config.yml │ └── enhancement.yml ├── codecov.yml ├── dependabot.yml └── workflows │ ├── check_goreleaser_config.py │ ├── go.yml │ └── release-build.yml ├── .gitignore ├── .goreleaser.yaml ├── CODE_OF_CONDUCT.md ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── all.bash ├── all.bat ├── ast ├── ast.go ├── ast_gop.go ├── commentmap.go ├── filter.go ├── fromgo │ ├── gopast.go │ ├── gopast_test.go │ ├── typeparams │ │ ├── typeparams_go117.go │ │ └── typeparams_go118.go │ └── typeparams_test.go ├── gopq │ ├── dom.go │ ├── gopq.go │ └── helper.go ├── goptest │ └── gopq.go ├── import.go ├── mod │ └── deps.go ├── print.go ├── resolve.go ├── scope.go ├── togo │ ├── goast.go │ └── goast_test.go └── walk.go ├── builtin └── doc.xgo ├── cl ├── _testc │ └── hello │ │ ├── in.xgo │ │ └── out.go ├── _testgop │ ├── _matrix │ │ └── in.xgo │ ├── append1 │ │ ├── in.xgo │ │ └── out.go │ ├── append2 │ │ ├── in.xgo │ │ └── out.go │ ├── cap │ │ ├── in.xgo │ │ └── out.go │ ├── domaintext-html │ │ ├── go.mod │ │ ├── go.sum │ │ ├── in.xgo │ │ └── out.go │ ├── domaintext-huh │ │ ├── in.xgo │ │ └── out.go │ ├── domaintext-json │ │ ├── in.xgo │ │ └── out.go │ ├── domaintext-md │ │ ├── go.mod │ │ ├── go.sum │ │ ├── in.xgo │ │ └── out.go │ ├── domaintext-regexp │ │ ├── in.xgo │ │ └── out.go │ ├── domaintext-tpl │ │ ├── in.xgo │ │ └── out.go │ ├── domaintpl │ │ ├── in.xgo │ │ └── out.go │ ├── enumlines-rdr │ │ ├── in.xgo │ │ └── out.go │ ├── enumlines-stdin │ │ ├── in.xgo │ │ └── out.go │ ├── fatal │ │ ├── in.xgo │ │ └── out.go │ ├── rangeexpr │ │ ├── in.xgo │ │ └── out.go │ ├── repeatuntil │ │ ├── in.xgo │ │ └── out.go │ └── unit │ │ ├── in.xgo │ │ └── out.go ├── _testpy │ ├── _matrix │ │ └── in.xgo │ ├── hello │ │ ├── in.xgo │ │ └── out.go │ └── pycall │ │ ├── in.xgo │ │ └── out.go ├── _testspx │ ├── basic │ │ ├── Game.tgmx │ │ ├── Kai.tspx │ │ └── out.go │ ├── init │ │ ├── init.tspx │ │ └── out.go │ ├── multiworks │ │ ├── foo_prompt.gox │ │ ├── hello_tool.gox │ │ ├── main_mcp.gox │ │ └── out.go │ ├── newobj │ │ ├── Kai_spx.gox │ │ ├── main_spx.gox │ │ └── out.go │ ├── nogame │ │ ├── bar.tspx │ │ └── out.go │ └── singlework │ │ ├── Kai_spx.gox │ │ ├── main_spx.gox │ │ └── out.go ├── builtin.go ├── builtin_test.go ├── c.go ├── classfile.go ├── cltest │ ├── cltest.go │ ├── error_msg.go │ ├── recorder.go │ └── spx.go ├── compile.go ├── compile_gop_test.go ├── compile_spx_test.go ├── compile_test.go ├── compile_testdir_test.go ├── error_msg_test.go ├── expr.go ├── func_type_and_var.go ├── internal │ ├── .gitignore │ ├── gop-in-go │ │ └── foo │ │ │ ├── foo.xgo │ │ │ ├── foo_test.xgo │ │ │ └── footest_test.xgo │ ├── huh │ │ └── huh.go │ ├── llgo-hello │ │ └── hello.go │ ├── mcp │ │ └── classfile.go │ ├── overload │ │ ├── bar │ │ │ └── bar.go │ │ └── foo │ │ │ └── foo.go │ ├── spx │ │ ├── game.go │ │ ├── pkg │ │ │ └── pkg.go │ │ └── sprite.go │ ├── spx2 │ │ └── spx2.go │ ├── spx3 │ │ ├── jwt │ │ │ └── jwt.go │ │ └── spx3.go │ ├── spx4 │ │ ├── game.go │ │ ├── pkg │ │ │ └── pkg.go │ │ └── sprite.go │ ├── test │ │ ├── case.go │ │ └── match.go │ ├── typesalias │ │ ├── alias_go121.go │ │ ├── alias_go122.go │ │ ├── typeparams_go122.go │ │ └── typeparams_go123.go │ ├── typesutil │ │ ├── api.go │ │ ├── api_test.go │ │ ├── mode.go │ │ └── mode_go123.go │ └── unit │ │ └── unit.go ├── outline │ └── outline.go ├── recorder.go ├── run_test.go ├── stmt.go ├── typeparams.go └── typeparams_test.go ├── cmd ├── chore │ ├── goptestgo │ │ └── goptestgo.go │ ├── xgobuiltingen │ │ ├── builtin.gox │ │ ├── builtingen.gox │ │ ├── helper.xgo │ │ └── reference.gox │ ├── xgofullspec │ │ └── fullspec.xgo │ └── xgominispec │ │ └── minispec.xgo ├── gop │ └── main.go ├── internal │ ├── base │ │ ├── base.go │ │ └── pass.go │ ├── bug │ │ └── bug.go │ ├── build │ │ └── build.go │ ├── clean │ │ └── clean.go │ ├── deps │ │ └── deps.go │ ├── doc │ │ └── doc.go │ ├── env │ │ └── env.go │ ├── gengo │ │ └── go.go │ ├── gopfmt │ │ └── fmt.go │ ├── gopget │ │ └── get.go │ ├── help │ │ └── help.go │ ├── install │ │ └── install.go │ ├── list │ │ └── list.go │ ├── mod │ │ ├── download.go │ │ ├── init.go │ │ ├── mod.go │ │ └── tidy.go │ ├── run │ │ └── run.go │ ├── serve │ │ └── serve.go │ ├── test │ │ ├── test.go │ │ └── testflag.go │ ├── version │ │ └── ver.go │ └── watch │ │ └── watch.go ├── make.go ├── make_test.go └── xgo │ ├── bug_cmd.gox │ ├── build_cmd.gox │ ├── clean_cmd.gox │ ├── doc_cmd.gox │ ├── env_cmd.gox │ ├── fmt_cmd.gox │ ├── get_cmd.gox │ ├── go_cmd.gox │ ├── install_cmd.gox │ ├── main_app.gox │ ├── mod_cmd.gox │ ├── mod_download_cmd.gox │ ├── mod_init_cmd.gox │ ├── mod_tidy_cmd.gox │ ├── run_cmd.gox │ ├── serve_cmd.gox │ ├── test_cmd.gox │ ├── version_cmd.gox │ ├── watch_cmd.gox │ └── xgo_autogen.go ├── demo ├── _llgo │ ├── callpy │ │ └── callpy.xgo │ ├── chello │ │ └── hello.xgo │ ├── cpphello │ │ └── cpphello.xgo │ ├── defer │ │ └── defer.xgo │ ├── errors │ │ └── errors.xgo │ ├── go.mod │ ├── go.sum │ ├── goroutine │ │ └── goroutine.xgo │ ├── hello │ │ └── hello.xgo │ ├── hellollgo │ │ ├── README.md │ │ └── hello.xgo │ ├── matrix │ │ └── matrix.xgo │ ├── pyhello │ │ └── hello.xgo │ ├── pymax │ │ └── pymax.xgo │ ├── pyprint │ │ └── print.xgo │ ├── pytensor │ │ └── tensor.xgo │ ├── qsort │ │ └── qsort.xgo │ ├── reflect │ │ └── reflect.xgo │ ├── sqlitedemo │ │ └── sqlitedemo.xgo │ ├── statistics │ │ └── statistics.xgo │ └── tetris │ │ └── tetris.xgo ├── _tinygo │ ├── go.mod │ └── sortdemo │ │ └── sort.xgo ├── fullspec │ ├── mixgo-complex │ │ ├── bar.go │ │ ├── foo.xgo │ │ └── xgo_autogen.go │ ├── overloadfunc1 │ │ └── add.xgo │ ├── overloadfunc2 │ │ └── mul.xgo │ ├── overloadmethod │ │ └── method.xgo │ ├── overloadop1 │ │ └── overloadop.xgo │ └── overloadop2 │ │ └── overloadop.xgo ├── gsh-exec │ ├── exec.gsh │ └── foo │ │ └── foo.xgo ├── lambda1 │ └── lambda.xgo ├── mapliteral │ └── mapliteral.xgo ├── mixgo │ ├── README.md │ ├── a.go │ ├── b.xgo │ └── xgo_autogen.go ├── sliceliteral │ └── sliceliteral.xgo ├── stringtrans │ └── transform.xgo ├── tpl-calc-dump │ └── calc_dump.xgo ├── tpl-calc │ └── calc.xgo ├── tpl-gen-ast │ └── gen_calc_ast.xgo ├── tpl-intlist │ └── ints.xgo ├── tpl-natural-lang │ └── nlang.xgo ├── tpl-parser-demo │ └── demo.xgo ├── tpl-pseudo │ ├── gauss.pseudo │ └── pseudo.gox ├── tpl-vcalc │ └── variant_calc.xgo ├── typeasparamsfunc │ ├── col.go │ └── typeAsParamsFunc.xgo ├── typeasparamsmethod │ ├── col.go │ └── typeAsParamsMethod.xgo ├── typeparamscast │ ├── foo.go │ └── typecast.xgo ├── unit-test │ ├── foo.xgo │ └── foo_test.gox ├── unitliteral │ └── unitlit.xgo ├── xgo-calc │ └── calc.xgo ├── xgo-parser │ └── parser.xgo ├── xgo-sample │ ├── a.xgo │ ├── b.xgo │ └── cpkag │ │ └── b │ │ └── ab.go ├── xgo-scanner │ ├── rpncalc │ │ └── rpncalc.xgo │ ├── scanner.xgo │ └── simplecalc │ │ └── calc.xgo └── xgo-typeof │ └── typeof.xgo ├── doc ├── _testdata │ ├── gopoFn │ │ ├── in.go │ │ └── out.expect │ ├── gopoMethod │ │ ├── in.go │ │ └── out.expect │ ├── overloadFn │ │ ├── in.go │ │ └── out.expect │ └── overloadMethod │ │ ├── in.go │ │ └── out.expect ├── classfile.md ├── code-coverage.md ├── contributing.md ├── docs.md ├── domian-text-lit.md ├── goodbye-printf.md ├── goplus-vs-go.md ├── images │ └── dtl │ │ ├── image-1.png │ │ └── image-2.png ├── overload.md ├── spec-mini.md ├── spec.md ├── spec │ └── mini │ │ └── mini.xgo ├── z_gop.go ├── z_test.go └── z_transform.go ├── env ├── build.go ├── goenv.go ├── gop_nonwindows.go ├── path.go ├── path_test.go ├── sys_others.go ├── sys_plan9.go ├── sys_windows.go ├── version.go └── version_test.go ├── format ├── format.go ├── formatutil │ ├── _testdata │ │ ├── format │ │ │ ├── basic │ │ │ │ ├── in.data │ │ │ │ └── out.expect │ │ │ └── nondecl │ │ │ │ ├── in.data │ │ │ │ └── out.expect │ │ ├── rearrange │ │ │ ├── noeol │ │ │ │ ├── in.data │ │ │ │ └── out.expect │ │ │ └── nondecl │ │ │ │ ├── in.data │ │ │ │ └── out.expect │ │ └── splitstmts │ │ │ └── basic │ │ │ ├── in.data │ │ │ └── out.expect │ ├── format_gop.go │ └── format_test.go └── internal.go ├── go.mod ├── go.sum ├── make.bash ├── make.bat ├── parser ├── _instance │ ├── instance1 │ │ ├── cmd.xgo │ │ └── parser.expect │ ├── instance2 │ │ ├── cmd.xgo │ │ └── parser.expect │ ├── instance3 │ │ ├── cmd.xgo │ │ └── parser.expect │ ├── instance4 │ │ ├── cmd.xgo │ │ └── parser.expect │ └── instance5 │ │ ├── cmd.xgo │ │ └── parser.expect ├── _nofmt │ ├── cmdlinestyle1 │ │ ├── cmd.xgo │ │ └── parser.expect │ ├── cmdlinestyle2 │ │ ├── cmd2.xgo │ │ └── parser.expect │ ├── cmdlinestyle3 │ │ ├── cmd.xgo │ │ └── parser.expect │ ├── exists │ │ ├── exists.xgo │ │ └── parser.expect │ ├── forloop │ │ ├── forloop.xgo │ │ └── parser.expect │ ├── listcompr │ │ ├── listcompr.xgo │ │ └── parser.expect │ ├── matrix1 │ │ ├── matrix.xgo │ │ └── parser.expect │ ├── printvariadic │ │ ├── parser.expect │ │ └── printv.xgo │ ├── rangeexpr1 │ │ ├── parser.expect │ │ └── rangeexpr.xgo │ └── selectdata │ │ ├── parser.expect │ │ └── select.xgo ├── _testdata │ ├── append1 │ │ ├── append.xgo │ │ └── parser.expect │ ├── append2 │ │ ├── append.xgo │ │ └── parser.expect │ ├── arrowop │ │ ├── arrowop.xgo │ │ └── parser.expect │ ├── autoprop │ │ ├── goto.xgo │ │ └── parser.expect │ ├── build │ │ ├── build.xgo │ │ └── parser.expect │ ├── c2gohello │ │ ├── hello.xgo │ │ └── parser.expect │ ├── cmdlinestyle1 │ │ ├── cmd.xgo │ │ └── parser.expect │ ├── cmdlinestyle2 │ │ ├── cmd2.xgo │ │ └── parser.expect │ ├── cmdlinestyle3 │ │ ├── cmd3.xgo │ │ └── parser.expect │ ├── cmdlinestyle4 │ │ ├── cmd4.xgo │ │ └── parser.expect │ ├── collection │ │ ├── collection.xgo │ │ └── parser.expect │ ├── complit │ │ ├── complit.xgo │ │ └── parser.expect │ ├── domainhuh │ │ ├── huh.xgo │ │ └── parser.expect │ ├── domaintext │ │ ├── parser.expect │ │ └── tpl.xgo │ ├── domaintpl │ │ ├── parser.expect │ │ └── tpl.xgo │ ├── embedded1 │ │ ├── embtype.xgo │ │ └── parser.expect │ ├── envop1 │ │ ├── envop.xgo │ │ └── parser.expect │ ├── envop2 │ │ ├── envop.xgo │ │ └── parser.expect │ ├── errwrap1 │ │ ├── errwrap.xgo │ │ └── parser.expect │ ├── errwrap2 │ │ ├── errwrap2.xgo │ │ └── parser.expect │ ├── errwrap3 │ │ ├── errwrap3.xgo │ │ └── parser.expect │ ├── exists │ │ ├── exists.xgo │ │ └── parser.expect │ ├── fnbody │ │ ├── fnbody.xgo │ │ └── parser.expect │ ├── fncall │ │ ├── fncall.xgo │ │ └── parser.expect │ ├── forloop │ │ ├── forloop.xgo │ │ └── parser.expect │ ├── funcdecl1 │ │ ├── fndecl.xgo │ │ └── parser.expect │ ├── funcdecl2 │ │ ├── fndecl.xgo │ │ └── parser.expect │ ├── funcdecl3 │ │ ├── fndecl.xgo │ │ └── parser.expect │ ├── funcdoc │ │ ├── funcdoc.xgo │ │ └── parser.expect │ ├── funclit │ │ ├── funclit.xgo │ │ └── parser.expect │ ├── functype │ │ ├── dummy │ │ │ └── dummy.md │ │ ├── functype.go │ │ └── parser.expect │ ├── gmxtest │ │ ├── foo.gmx │ │ └── parser.expect │ ├── goto1 │ │ ├── goto.xgo │ │ └── parser.expect │ ├── goto2 │ │ ├── goto.xgo │ │ └── parser.expect │ ├── goxtest1 │ │ ├── bar.gox │ │ └── parser.expect │ ├── goxtest2 │ │ ├── bar.gox │ │ └── parser.expect │ ├── lambda1 │ │ ├── lambda.xgo │ │ └── parser.expect │ ├── lambda2 │ │ ├── lambda2.xgo │ │ └── parser.expect │ ├── lambda3 │ │ ├── lambda3.xgo │ │ └── parser.expect │ ├── lambda4 │ │ ├── lambda4.xgo │ │ └── parser.expect │ ├── listcompr │ │ ├── listcompr.xgo │ │ └── parser.expect │ ├── mapfunc │ │ ├── map.xgo │ │ └── parser.expect │ ├── matrix1 │ │ ├── matrix.xgo │ │ └── parser.expect │ ├── matrix2 │ │ ├── matrix.xgo │ │ └── parser.expect │ ├── mytest │ │ ├── mytest.xgo │ │ └── parser.expect │ ├── overload1 │ │ ├── overload.xgo │ │ └── parser.expect │ ├── overload2 │ │ ├── overload2.xgo │ │ └── parser.expect │ ├── overloadop │ │ ├── op_overload.xgo │ │ └── parser.expect │ ├── printvariadic │ │ ├── parser.expect │ │ └── printv.xgo │ ├── pystr │ │ ├── parser.expect │ │ └── pystr.xgo │ ├── rangeexpr1 │ │ ├── parser.expect │ │ └── rangeexpr.xgo │ ├── rangeexpr2 │ │ ├── parser.expect │ │ └── rangeexpr.xgo │ ├── rangeexpr3 │ │ ├── parser.expect │ │ └── rangeexpr.xgo │ ├── rational │ │ ├── parser.expect │ │ └── rational.xgo │ ├── selectdata │ │ ├── parser.expect │ │ └── select.xgo │ ├── slice1 │ │ ├── parser.expect │ │ └── slice.xgo │ ├── slice2 │ │ ├── parser.expect │ │ └── slice2.xgo │ ├── spxtest │ │ ├── foo.spx │ │ └── parser.expect │ ├── staticmthd1 │ │ ├── parser.expect │ │ └── static_method.xgo │ ├── staticmthd2 │ │ ├── a.gox │ │ └── parser.expect │ ├── stdtype │ │ ├── parser.expect │ │ └── stdtype.xgo │ ├── stringex1 │ │ ├── parser.expect │ │ └── string_lit.xgo │ ├── stringex2 │ │ ├── parser.expect │ │ └── string_lit.xgo │ ├── stringex3 │ │ ├── parser.expect │ │ └── string_lit.xgo │ ├── typeof │ │ ├── parser.expect │ │ └── typeof.xgo │ ├── typeswitch │ │ ├── parser.expect │ │ └── typeswitch.xgo │ └── unit │ │ ├── parser.expect │ │ └── step.xgo ├── _testexpr │ └── lambda │ │ ├── in.xgo │ │ └── out.expect ├── fsx │ ├── fsys.go │ └── memfs │ │ ├── fs.go │ │ └── memfs.go ├── interface.go ├── iox │ └── io.go ├── parser.go ├── parser_gop.go ├── parser_test.go ├── parserdir_go118_test.go ├── parserdir_test.go └── parsertest │ └── parsertest.go ├── printer ├── _testdata │ ├── 02-Var-and-operator │ │ └── var_and_op.xgo │ ├── 03-Import-go-package │ │ └── import.xgo │ ├── 04-Func │ │ └── func.xgo │ ├── 05-Closure │ │ └── closure.xgo │ ├── 06-String-Map-Array-Slice │ │ └── datastruct.xgo │ ├── 07-MapLit │ │ └── maplit.xgo │ ├── 08-SliceLit │ │ └── slicelit.xgo │ ├── 09-IfElse-SwitchCase │ │ └── flow.xgo │ ├── 10-List-comprehension │ │ └── list_comprehens.xgo │ ├── 11-Map-comprehension │ │ └── map_comprehens.xgo │ ├── 12-Select-comprehension │ │ └── select.xgo │ ├── 12-Select-comprehension2 │ │ └── findscore.xgo │ ├── 13-Exists-comprehension │ │ └── exists.xgo │ ├── 14-Using-goplus-in-Go │ │ └── foo │ │ │ ├── foo.xgo │ │ │ ├── foo_test.xgo │ │ │ └── footest_test.xgo │ ├── 15-ErrWrap │ │ └── err_wrap.xgo │ ├── 16-Fib │ │ └── fib.xgo │ ├── 17-Fibtc │ │ └── fibtc.xgo │ ├── 18-Rational │ │ └── rational.xgo │ ├── 21-Break-continue-goto │ │ └── flow.xgo │ ├── 22-For-loop │ │ └── for.xgo │ ├── 23-Defer │ │ └── defer.xgo │ ├── 24-Goroutine │ │ └── goroutine.xgo │ ├── 25-Struct │ │ └── struct.xgo │ ├── 26-Method │ │ └── method.xgo │ ├── 27-Func-Set │ │ └── func.xgo │ ├── 28-Chan │ │ └── chan.xgo │ ├── 29-CompareToNil │ │ └── ref.xgo │ ├── 30-Recover │ │ └── recover.xgo │ ├── 31-Builtin-Typecast │ │ └── builtin_and_typecast.xgo │ ├── 32-Import-gop-package │ │ └── import_gop_pkg.xgo │ ├── 33-Interface │ │ └── shape.xgo │ ├── 34-Type-assert │ │ └── type_assert.xgo │ ├── 35-Chan-select │ │ └── select.xgo │ ├── 36-Auto-Property │ │ └── autoprop.xgo │ ├── 37-Cmdline │ │ └── cmdline.xgo │ ├── 38-Overload-operator │ │ └── overload_op.xgo │ ├── 39-Lambda-expression │ │ └── lambda.xgo │ ├── 40-Deduce-struct-type │ │ └── deduce.xgo │ ├── 41-UDT-RangeForEach │ │ └── udt_range.xgo │ ├── 42-UDT-RangeIterator │ │ └── udt_range_iter.xgo │ └── 43-RangeExpr │ │ └── rangeexpr.xgo ├── bugfix_test.go ├── gop_test.go ├── nodes.go ├── printer.go └── printer_test.go ├── scanner ├── gop.go └── scanner.go ├── test └── classfile.go ├── token ├── internal │ └── tokenutil │ │ ├── lines_go118.go │ │ ├── lines_go120.go │ │ ├── lines_go121.go │ │ └── lines_test.go ├── token.go ├── token_test.go └── types.go ├── tool ├── _gendeps.go ├── build_install_run.go ├── gengo.go ├── imp.go ├── load.go ├── outline.go └── tidy.go ├── tpl ├── README.md ├── ast │ └── ast.go ├── cl │ └── compile.go ├── encoding │ ├── csv │ │ └── csv.go │ ├── json │ │ └── json.go │ ├── regexp │ │ └── regexp.go │ ├── regexposix │ │ └── regexp.go │ └── xml │ │ └── xml.go ├── matcher │ └── match.go ├── parser │ ├── _testdata │ │ ├── adjoin │ │ │ ├── in.xgo │ │ │ └── out.expect │ │ ├── pseudo │ │ │ ├── in.xgo │ │ │ └── out.expect │ │ ├── simple1 │ │ │ ├── in.xgo │ │ │ └── out.expect │ │ └── simple2 │ │ │ ├── in.xgo │ │ │ └── out.expect │ ├── parser.go │ ├── parser_test.go │ └── parsertest │ │ └── parsertest.go ├── scanner │ ├── _testdata │ │ ├── cstr │ │ │ ├── go.expect │ │ │ ├── gop.expect │ │ │ ├── in.xgo │ │ │ └── tpl.expect │ │ ├── num │ │ │ ├── go.expect │ │ │ ├── gop.expect │ │ │ ├── in.xgo │ │ │ └── tpl.expect │ │ ├── pow │ │ │ ├── go.expect │ │ │ ├── gop.expect │ │ │ ├── in.xgo │ │ │ └── tpl.expect │ │ ├── rat │ │ │ ├── go.expect │ │ │ ├── gop.expect │ │ │ ├── in.xgo │ │ │ └── tpl.expect │ │ └── unit │ │ │ ├── go.expect │ │ │ ├── gop.expect │ │ │ ├── in.xgo │ │ │ └── tpl.expect │ ├── error.go │ ├── scandir_test.go │ ├── scanner.go │ ├── scanner_test.go │ └── scannertest │ │ └── scannertest.go ├── token │ ├── token.go │ ├── token_test.go │ └── types.go ├── tpl.go ├── types │ └── types.go └── variant │ ├── builtin │ └── builtin.go │ ├── delay │ └── delay.go │ ├── math │ └── math.go │ ├── module.go │ ├── time │ └── time.go │ └── variant.go └── x ├── build ├── _testdata │ ├── hello │ │ ├── hello.expect │ │ └── main.xgo │ ├── multi │ │ ├── Rect.gox │ │ ├── main.xgo │ │ └── multi.expect │ └── pkg │ │ ├── pkg.expect │ │ └── pkg.xgo ├── build.go └── build_test.go ├── fakenet └── conn.go ├── format ├── README.md ├── _testdata │ ├── collection │ │ ├── format.expect │ │ └── index.xgo │ ├── gopsyntax │ │ ├── format.expect │ │ └── index.xgo │ └── syntax │ │ ├── format.expect │ │ └── index.xgo ├── format.go ├── gopstyle.go ├── gopstyle_test.go ├── gopstyledir_test.go └── stmt_expr_or_type.go ├── fsnotify └── fsnotify.go ├── gocmd ├── build_install.go ├── gocmd.go └── run.go ├── jsonrpc2 ├── conn.go ├── frame.go ├── internal │ └── stack │ │ ├── parse.go │ │ ├── process.go │ │ ├── stack.go │ │ └── stacktest │ │ └── stacktest.go ├── jsonrpc2.go ├── jsonrpc2test │ ├── cases │ │ └── testcase.go │ ├── jsonrpc2_test.go │ ├── pipe.go │ └── pipe_test.go ├── messages.go ├── serve.go ├── stdio │ ├── server.go │ └── stdio.go └── wire.go ├── langserver ├── client.go ├── serve_dial.go └── server.go ├── typesutil ├── api.go ├── builtin_test.go ├── check.go ├── check_test.go ├── eval.go ├── exprstring.go ├── exprstring_test.go ├── gopinfo.go ├── info_test.go ├── internal │ └── typesutil │ │ └── types.go └── typeparams │ └── typeparams.go ├── watcher ├── changes.go └── watch.go ├── xgoenv └── env.go └── xgoprojs ├── proj.go └── proj_test.go /.gitattributes: -------------------------------------------------------------------------------- 1 | *.expect text eol=lf 2 | *.golden text eol=lf 3 | 4 | *.xgo text eol=lf 5 | *.gop text eol=lf 6 | *.go text eol=lf 7 | *.gox text eol=lf 8 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | contact_links: 2 | - name: Visit the XGo website 3 | url: https://xgo.dev 4 | about: Much help can be found there 5 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/enhancement.yml: -------------------------------------------------------------------------------- 1 | name: Feature request 2 | description: Propose a change to XGo 3 | 4 | body: 5 | - type: markdown 6 | attributes: 7 | value: | 8 | ⚠️ Make sure to browse the opened and closed issues before submit your issue. 9 | 10 | - type: textarea 11 | id: proposal 12 | attributes: 13 | label: Proposal 14 | description: Write your feature request in the form of a proposal to be considered for implementation. 15 | validations: 16 | required: true 17 | 18 | - type: textarea 19 | id: background 20 | attributes: 21 | label: Background 22 | description: Describe the background problem or need that led to this feature request. 23 | validations: 24 | required: true 25 | 26 | - type: textarea 27 | id: workarounds 28 | attributes: 29 | label: Workarounds 30 | description: Are there any current workarounds that you're using that others in similar positions should know about? 31 | validations: 32 | required: true 33 | -------------------------------------------------------------------------------- /.github/codecov.yml: -------------------------------------------------------------------------------- 1 | coverage: 2 | ignore: 3 | - "builtin" 4 | - "ast" 5 | - "test" 6 | - "scanner" 7 | - "format" 8 | - "demo" 9 | - "demo/unit-test" 10 | - "doc/spec" 11 | - "cmd" 12 | - "cl/cltest" 13 | - "cl/outline" 14 | - "cl/internal/huh" 15 | - "cl/internal/llgo-hello" 16 | - "cl/internal/mcp" 17 | - "cl/internal/overload" 18 | - "cl/internal/spx" 19 | - "cl/internal/spx2" 20 | - "cl/internal/spx3" 21 | - "cl/internal/test" 22 | - "cl/internal/unit" 23 | - "cl/internal/typesalias" 24 | - "parser/fsx" 25 | - "parser/parsertest" 26 | - "x/jsonrpc2" 27 | - "x/jsonrpc2/jsonrpc2test" 28 | - "x/watcher" 29 | - "x/langserver" 30 | - "x/fsnotify" 31 | - "x/fakenet" 32 | - "x/typesutil" 33 | - "x/gocmd" 34 | - "x/gopenv" 35 | - "tool" 36 | - "tpl" 37 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: github-actions 9 | directory: / 10 | labels: 11 | - dependabot 12 | - actions 13 | schedule: 14 | interval: daily 15 | 16 | - package-ecosystem: "gomod" # See documentation for possible values 17 | directory: "/" # Location of package manifests 18 | schedule: 19 | interval: "daily" 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | *.txt 6 | *.cache 7 | .DS_Store 8 | test.db 9 | go.json 10 | x.mod 11 | 12 | # gop 13 | # gopfmt 14 | # goptestgo 15 | 16 | # Folders 17 | _obj 18 | _test 19 | _old 20 | _t 21 | 22 | # Architecture specific extensions/prefixes 23 | *.[568vq] 24 | [568vq].out 25 | 26 | *.cgo1.go 27 | *.cgo2.c 28 | _cgo_defun.c 29 | _cgo_gotypes.go 30 | _cgo_export.* 31 | 32 | _testmain.go 33 | coverage.txt 34 | 35 | .xgo/ 36 | .gop/ 37 | gop_autogen*.go 38 | xgo_autogen*.go 39 | !cmd/xgo/gop_autogen*.go 40 | !cmd/xgo/xgo_autogen*.go 41 | !demo/fullspec/mixgo-complex/xgo_autogen.go 42 | !demo/mixgo/xgo_autogen.go 43 | go.json 44 | go.work* 45 | format.result 46 | *.cache 47 | 48 | *.exe 49 | *.test 50 | *.prof 51 | 52 | .vscode 53 | .idea 54 | 55 | cmd/qfmt/qfmt 56 | 57 | bin/ 58 | go-num/ 59 | _todo*.go 60 | 61 | .dist/ 62 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | ARG BASE_IMAGE=golang:1.24-bookworm 2 | 3 | FROM $BASE_IMAGE AS build 4 | ARG USE_GORELEASER_ARTIFACTS=0 5 | ARG GORELEASER_ARTIFACTS_TARBALL 6 | WORKDIR /usr/local/src/xgo 7 | COPY . . 8 | ENV XGOROOT=/usr/local/xgo 9 | RUN set -eux; \ 10 | mkdir $XGOROOT; \ 11 | if [ $USE_GORELEASER_ARTIFACTS -eq 1 ]; then \ 12 | tar -xzf "${GORELEASER_ARTIFACTS_TARBALL}" -C $XGOROOT; \ 13 | else \ 14 | git ls-tree --full-tree --name-only -r HEAD | grep -vE "^\." | xargs -I {} cp --parents {} $XGOROOT/; \ 15 | ./all.bash; \ 16 | mv bin $XGOROOT/; \ 17 | fi 18 | 19 | FROM $BASE_IMAGE 20 | ENV XGOROOT=/usr/local/xgo 21 | COPY --from=build $XGOROOT/ $XGOROOT/ 22 | ENV PATH=$XGOROOT/bin:$PATH 23 | WORKDIR /xgo 24 | -------------------------------------------------------------------------------- /all.bash: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | 3 | # 4 | # Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | set -ex 20 | 21 | go run cmd/make.go --install --regtest --autoproxy 22 | -------------------------------------------------------------------------------- /all.bat: -------------------------------------------------------------------------------- 1 | go run cmd/make.go --install --regtest --autoproxy 2 | -------------------------------------------------------------------------------- /ast/fromgo/typeparams/typeparams_go117.go: -------------------------------------------------------------------------------- 1 | //go:build !go1.18 2 | // +build !go1.18 3 | 4 | package typeparams 5 | 6 | import ( 7 | "go/ast" 8 | "go/token" 9 | ) 10 | 11 | func unsupported() { 12 | panic("type parameters are unsupported at this go version") 13 | } 14 | 15 | // IndexListExpr is a placeholder type, as type parameters are not supported at 16 | // this Go version. Its methods panic on use. 17 | type IndexListExpr struct { 18 | ast.Expr 19 | X ast.Expr // expression 20 | Lbrack token.Pos // position of "[" 21 | Indices []ast.Expr // index expressions 22 | Rbrack token.Pos // position of "]" 23 | } 24 | 25 | func (*IndexListExpr) Pos() token.Pos { unsupported(); return token.NoPos } 26 | func (*IndexListExpr) End() token.Pos { unsupported(); return token.NoPos } 27 | 28 | // ForFuncType returns an empty field list, as type parameters are not 29 | // supported at this Go version. 30 | func ForFuncType(*ast.FuncType) *ast.FieldList { 31 | return nil 32 | } 33 | 34 | // ForTypeSpec returns an empty field list, as type parameters are not 35 | // supported at this Go version. 36 | func ForTypeSpec(*ast.TypeSpec) *ast.FieldList { 37 | return nil 38 | } 39 | -------------------------------------------------------------------------------- /ast/fromgo/typeparams/typeparams_go118.go: -------------------------------------------------------------------------------- 1 | //go:build go1.18 2 | // +build go1.18 3 | 4 | package typeparams 5 | 6 | import "go/ast" 7 | 8 | // IndexListExpr is an alias for ast.IndexListExpr. 9 | type IndexListExpr = ast.IndexListExpr 10 | 11 | // ForFuncType returns n.TypeParams. 12 | func ForFuncType(n *ast.FuncType) *ast.FieldList { 13 | if n == nil { 14 | return nil 15 | } 16 | return n.TypeParams 17 | } 18 | 19 | // ForTypeSpec returns n.TypeParams. 20 | func ForTypeSpec(n *ast.TypeSpec) *ast.FieldList { 21 | if n == nil { 22 | return nil 23 | } 24 | return n.TypeParams 25 | } 26 | -------------------------------------------------------------------------------- /ast/fromgo/typeparams_test.go: -------------------------------------------------------------------------------- 1 | //go:build go1.18 2 | // +build go1.18 3 | 4 | package fromgo 5 | 6 | import "testing" 7 | 8 | func TestIndexListExpr(t *testing.T) { 9 | test(t, `package main 10 | 11 | type N1 = T1[int] 12 | 13 | type N2 = T2[string, int] 14 | 15 | var f = test[string, int](1, 2) 16 | `) 17 | } 18 | -------------------------------------------------------------------------------- /cl/_testc/hello/in.xgo: -------------------------------------------------------------------------------- 1 | import "c" 2 | 3 | c.Printf c"Hello, world!\n" 4 | -------------------------------------------------------------------------------- /cl/_testc/hello/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/goplus/lib/c" 4 | 5 | func main() { 6 | c.Printf(c.Str("Hello, world!\n")) 7 | } 8 | -------------------------------------------------------------------------------- /cl/_testgop/_matrix/in.xgo: -------------------------------------------------------------------------------- 1 | echo [ 2 | 1, 2, 3 3 | 4, 5, 6 4 | ] 5 | -------------------------------------------------------------------------------- /cl/_testgop/append1/in.xgo: -------------------------------------------------------------------------------- 1 | type foo struct { 2 | a []int 3 | } 4 | 5 | a := [1, 2, 3] 6 | a <- 4 7 | echo a 8 | 9 | f := foo{a: [1, 2, 3]} 10 | f.a <- 4 11 | echo f 12 | 13 | f2 := [2]chan int{} 14 | f2[0] <- 4 15 | -------------------------------------------------------------------------------- /cl/_testgop/append1/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type foo struct { 6 | a []int 7 | } 8 | 9 | func main() { 10 | a := []int{1, 2, 3} 11 | a = append(a, 4) 12 | fmt.Println(a) 13 | f := foo{a: []int{1, 2, 3}} 14 | f.a = append(f.a, 4) 15 | fmt.Println(f) 16 | f2 := [2]chan int{} 17 | f2[0] <- 4 18 | } 19 | -------------------------------------------------------------------------------- /cl/_testgop/append2/in.xgo: -------------------------------------------------------------------------------- 1 | a := [1, 2, 3] 2 | a <- 4, 5 3 | echo a 4 | 5 | b := [7, 8] 6 | a <- b... 7 | echo a 8 | -------------------------------------------------------------------------------- /cl/_testgop/append2/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | a := []int{1, 2, 3} 7 | a = append(a, 4, 5) 8 | fmt.Println(a) 9 | b := []int{7, 8} 10 | a = append(a, b...) 11 | fmt.Println(a) 12 | } 13 | -------------------------------------------------------------------------------- /cl/_testgop/cap/in.xgo: -------------------------------------------------------------------------------- 1 | a := ["hello", "world", "123"] 2 | echo a.capitalize 3 | -------------------------------------------------------------------------------- /cl/_testgop/cap/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/qiniu/x/stringslice" 6 | ) 7 | 8 | func main() { 9 | a := []string{"hello", "world", "123"} 10 | fmt.Println(stringslice.Capitalize(a)) 11 | } 12 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-html/go.mod: -------------------------------------------------------------------------------- 1 | module example 2 | 3 | go 1.22 4 | 5 | require golang.org/x/net v0.34.0 6 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-html/go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0= 2 | golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k= 3 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-html/in.xgo: -------------------------------------------------------------------------------- 1 | import "golang.org/x/net/html" 2 | 3 | echo html`

hello

` 4 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-html/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "golang.org/x/net/html" 6 | "strings" 7 | ) 8 | 9 | func main() { 10 | fmt.Println(html.Parse(strings.NewReader(`

hello

`))) 11 | } 12 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-huh/in.xgo: -------------------------------------------------------------------------------- 1 | import "github.com/goplus/xgo/cl/internal/huh" 2 | 3 | form := huh`> "1", 2 4 |
5 |
6 | ` 7 | 8 | form.run 9 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-huh/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/goplus/xgo/cl/internal/huh" 4 | 5 | func main() { 6 | form := huh.New("
\n
\n", "1", 2) 7 | form.Run() 8 | } 9 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-json/in.xgo: -------------------------------------------------------------------------------- 1 | echo json`{"a":1, "b":2}` 2 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-json/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/goplus/xgo/tpl/encoding/json" 7 | ) 8 | 9 | func main() { 10 | fmt.Println(json.New(`{"a":1, "b":2}`)) 11 | } 12 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-md/go.mod: -------------------------------------------------------------------------------- 1 | module example 2 | 3 | go 1.22 4 | 5 | require github.com/xushiwei/markdown v0.1.0 6 | 7 | require github.com/yuin/goldmark v1.7.8 // indirect 8 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-md/go.sum: -------------------------------------------------------------------------------- 1 | github.com/xushiwei/markdown v0.1.0 h1:Vjw4MVcwSEJge9ObA/3C1lrmNYX60nJwuVKdLYrf0+o= 2 | github.com/xushiwei/markdown v0.1.0/go.mod h1:6hyvHMBrprwcCYXaw71W9WwBX1st9r+Rfsj+cKXW3EE= 3 | github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic= 4 | github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= 5 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-md/in.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "os" 3 | 4 | "github.com/xushiwei/markdown" 5 | ) 6 | 7 | md := markdown` 8 | # Title 9 | 10 | Hello world 11 | ` 12 | 13 | md.convert os.Stdout 14 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-md/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/xushiwei/markdown" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | md := markdown.New(` 10 | # Title 11 | 12 | Hello world 13 | `) 14 | md.Convert(os.Stdout) 15 | } 16 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-regexp/in.xgo: -------------------------------------------------------------------------------- 1 | re, err := regexp`^[a-z]+\[[0-9]+\]$` 2 | echo re.matchString("adam[23]") 3 | _ = err 4 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-regexp/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/goplus/xgo/tpl/encoding/regexp" 7 | ) 8 | 9 | func main() { 10 | re, err := regexp.New(`^[a-z]+\[[0-9]+\]$`) 11 | fmt.Println(re.MatchString("adam[23]")) 12 | _ = err 13 | } 14 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-tpl/in.xgo: -------------------------------------------------------------------------------- 1 | cl, err := tpl`expr = INT % ("+" | "-")` 2 | cl.parseExpr "1+2", nil 3 | _ = err 4 | -------------------------------------------------------------------------------- /cl/_testgop/domaintext-tpl/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/goplus/xgo/tpl" 4 | 5 | func main() { 6 | cl, err := tpl.NewEx(`expr = INT % ("+" | "-")`, "cl/_testgop/domaintext-tpl/in.xgo", 1, 15) 7 | cl.ParseExpr("1+2", nil) 8 | _ = err 9 | } 10 | -------------------------------------------------------------------------------- /cl/_testgop/domaintpl/in.xgo: -------------------------------------------------------------------------------- 1 | tpl` 2 | file = stmts => { 3 | return self 4 | } 5 | 6 | stmts = *(stmt ";") => { 7 | return [n.([]any)[0] for n in self] 8 | } 9 | 10 | stmt = varStmt | constStmt | outputStmt | inputStmt | ifStmt | whileStmt | untilStmt | assignStmt 11 | 12 | varStmt = "DECLARE" namelist ":" typeExpr 13 | 14 | constStmt = "CONSTANT" IDENT "<-" expr 15 | 16 | assignStmt = IDENT "<-" expr 17 | 18 | outputStmt = "OUTPUT" exprlist 19 | 20 | inputStmt = "INPUT" namelist 21 | 22 | ifStmt = "IF" expr "THEN" ";" stmts ?("ELSE" ";" stmts) "ENDIF" 23 | 24 | whileStmt = "WHILE" expr "DO" ";" stmts "ENDWHILE" 25 | 26 | untilStmt = "REPEAT" ";" stmts "UNTIL" expr 27 | 28 | typeExpr = "INTEGER" | "REAL" | "STRING" | "BOOLEAN" 29 | 30 | expr = binaryExpr2 % ("<" | "<=" | ">" | ">=" | "=" | "<>") 31 | 32 | binaryExpr2 = binaryExpr1 % ("+" | "-") 33 | 34 | binaryExpr1 = operand % ("*" | "/") 35 | 36 | operand = basicLit | ident | parenExpr | unaryExpr 37 | 38 | unaryExpr = "-" operand 39 | 40 | basicLit = INT | FLOAT | STRING 41 | 42 | ident = IDENT 43 | 44 | parenExpr = "(" expr ")" 45 | 46 | exprlist = expr % "," 47 | 48 | namelist = IDENT % "," 49 | ` 50 | -------------------------------------------------------------------------------- /cl/_testgop/enumlines-rdr/in.xgo: -------------------------------------------------------------------------------- 1 | import "io" 2 | 3 | var r io.Reader 4 | 5 | for line <- lines(r) { 6 | println line 7 | } 8 | -------------------------------------------------------------------------------- /cl/_testgop/enumlines-rdr/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | 7 | "github.com/qiniu/x/osx" 8 | ) 9 | 10 | var r io.Reader 11 | 12 | func main() { 13 | for _gop_it := osx.Lines(r).Gop_Enum(); ; { 14 | var _gop_ok bool 15 | line, _gop_ok := _gop_it.Next() 16 | if !_gop_ok { 17 | break 18 | } 19 | fmt.Println(line) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /cl/_testgop/enumlines-stdin/in.xgo: -------------------------------------------------------------------------------- 1 | import "os" 2 | 3 | for line <- os.Stdin { 4 | println line 5 | } 6 | -------------------------------------------------------------------------------- /cl/_testgop/enumlines-stdin/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | "github.com/qiniu/x/osx" 8 | ) 9 | 10 | func main() { 11 | for _gop_it := osx.EnumLines(os.Stdin); ; { 12 | var _gop_ok bool 13 | line, _gop_ok := _gop_it.Next() 14 | if !_gop_ok { 15 | break 16 | } 17 | fmt.Println(line) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cl/_testgop/fatal/in.xgo: -------------------------------------------------------------------------------- 1 | import "os" 2 | 3 | f, err := os.open("hello.txt") 4 | if err != nil { 5 | errorln "[WARN] an error" 6 | fatal "open file failed: ${err}" 7 | } 8 | f.close 9 | -------------------------------------------------------------------------------- /cl/_testgop/fatal/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/qiniu/x/osx" 5 | "github.com/qiniu/x/stringutil" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | f, err := os.Open("hello.txt") 11 | if err != nil { 12 | osx.Errorln("[WARN] an error") 13 | osx.Fatal(stringutil.Concat("open file failed: ", err.Error())) 14 | } 15 | f.Close() 16 | } 17 | -------------------------------------------------------------------------------- /cl/_testgop/rangeexpr/in.xgo: -------------------------------------------------------------------------------- 1 | println [x for x <- 0:3:1] 2 | -------------------------------------------------------------------------------- /cl/_testgop/rangeexpr/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/qiniu/x/gop" 6 | ) 7 | 8 | func main() { 9 | fmt.Println(func() (_gop_ret []int) { 10 | for _gop_it := gop.NewRange__0(0, 3, 1).Gop_Enum(); ; { 11 | var _gop_ok bool 12 | x, _gop_ok := _gop_it.Next() 13 | if !_gop_ok { 14 | break 15 | } 16 | _gop_ret = append(_gop_ret, x) 17 | } 18 | return 19 | }()) 20 | } 21 | -------------------------------------------------------------------------------- /cl/_testgop/repeatuntil/in.xgo: -------------------------------------------------------------------------------- 1 | func RepeatUntil(cond func() bool, body func()) { 2 | for !cond() { 3 | body() 4 | } 5 | } 6 | 7 | x := 0 8 | repeatUntil x >= 3, => { 9 | echo x 10 | x++ 11 | } 12 | repeatUntil func() bool { return false }, => {} 13 | -------------------------------------------------------------------------------- /cl/_testgop/repeatuntil/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func RepeatUntil(cond func() bool, body func()) { 6 | for !cond() { 7 | body() 8 | } 9 | } 10 | func main() { 11 | x := 0 12 | RepeatUntil(func() bool { 13 | return x >= 3 14 | }, func() { 15 | fmt.Println(x) 16 | x++ 17 | }) 18 | RepeatUntil(func() bool { 19 | return false 20 | }, func() { 21 | }) 22 | } 23 | -------------------------------------------------------------------------------- /cl/_testgop/unit/in.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "time" 3 | 4 | "github.com/goplus/xgo/cl/internal/unit" 5 | ) 6 | 7 | func Wait(time.Duration) {} 8 | func Step(unit.Distance) {} 9 | 10 | wait 0.5µs 11 | wait 1m 12 | step 1m 13 | -------------------------------------------------------------------------------- /cl/_testgop/unit/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "time" 5 | 6 | "github.com/goplus/xgo/cl/internal/unit" 7 | ) 8 | 9 | func Wait(time.Duration) { 10 | } 11 | func Step(unit.Distance) { 12 | } 13 | func main() { 14 | Wait(500) 15 | Wait(60000000000) 16 | Step(1000) 17 | } 18 | -------------------------------------------------------------------------------- /cl/_testpy/_matrix/in.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "py/numpy" 3 | "py/std" 4 | ) 5 | 6 | a := [ 7 | [1.0, 2.0, 3.0], 8 | [4.0, 5.0, 6.0], 9 | [7.0, 8.0, 9.0], 10 | ] 11 | b := [ 12 | [9.0, 8.0, 7.0], 13 | [6.0, 5.0, 4.0], 14 | [3.0, 2.0, 1.0], 15 | ] 16 | x := numpy.add(a, b) 17 | std.print "a+b =", x 18 | -------------------------------------------------------------------------------- /cl/_testpy/hello/in.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "py/std" 3 | ) 4 | 5 | std.print py"Hello, World!" 6 | -------------------------------------------------------------------------------- /cl/_testpy/hello/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/goplus/lib/py" 5 | "github.com/goplus/lib/py/std" 6 | ) 7 | 8 | func main() { 9 | std.Print(py.Str("Hello, World!")) 10 | } 11 | -------------------------------------------------------------------------------- /cl/_testpy/pycall/in.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "c" 3 | "py" 4 | "py/math" 5 | ) 6 | 7 | x := math.sqrt(py.float(2)) 8 | c.printf c"sqrt(2) = %f\n", x.float64 9 | -------------------------------------------------------------------------------- /cl/_testpy/pycall/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/goplus/lib/c" 5 | "github.com/goplus/lib/py" 6 | "github.com/goplus/lib/py/math" 7 | ) 8 | 9 | func main() { 10 | x := math.Sqrt(py.Float(2)) 11 | c.Printf(c.Str("sqrt(2) = %f\n"), x.Float64()) 12 | } 13 | -------------------------------------------------------------------------------- /cl/_testspx/basic/Game.tgmx: -------------------------------------------------------------------------------- 1 | func onInit() { 2 | for { 3 | } 4 | } 5 | 6 | initGameApp 7 | -------------------------------------------------------------------------------- /cl/_testspx/basic/Kai.tspx: -------------------------------------------------------------------------------- 1 | func onMsg(msg string) { 2 | for { 3 | say "Hi" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /cl/_testspx/basic/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/goplus/xgo/cl/internal/spx" 4 | 5 | type Game struct { 6 | *spx.MyGame 7 | } 8 | type Kai struct { 9 | spx.Sprite 10 | *Game 11 | } 12 | 13 | func (this *Game) onInit() { 14 | for { 15 | spx.SchedNow() 16 | } 17 | } 18 | func (this *Game) MainEntry() { 19 | this.InitGameApp() 20 | } 21 | func (this *Game) Main() { 22 | spx.Gopt_MyGame_Main(this) 23 | } 24 | func (this *Kai) onMsg(msg string) { 25 | for { 26 | spx.Sched() 27 | this.Say("Hi") 28 | } 29 | } 30 | func (this *Kai) Main() { 31 | } 32 | func main() { 33 | new(Game).Main() 34 | } 35 | -------------------------------------------------------------------------------- /cl/_testspx/init/init.tspx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/goplus/xgo/0a0667c8ad573e65ffe6348ddb93df976578cd16/cl/_testspx/init/init.tspx -------------------------------------------------------------------------------- /cl/_testspx/init/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/goplus/xgo/cl/internal/spx" 4 | 5 | type _init struct { 6 | spx.Sprite 7 | *MyGame 8 | } 9 | type MyGame struct { 10 | *spx.MyGame 11 | } 12 | 13 | func (this *MyGame) Main() { 14 | spx.Gopt_MyGame_Main(this) 15 | } 16 | func (this *_init) Main() { 17 | } 18 | func main() { 19 | new(MyGame).Main() 20 | } 21 | -------------------------------------------------------------------------------- /cl/_testspx/multiworks/foo_prompt.gox: -------------------------------------------------------------------------------- 1 | return "Hi" 2 | -------------------------------------------------------------------------------- /cl/_testspx/multiworks/hello_tool.gox: -------------------------------------------------------------------------------- 1 | return -1 2 | -------------------------------------------------------------------------------- /cl/_testspx/multiworks/main_mcp.gox: -------------------------------------------------------------------------------- 1 | server "protos" 2 | -------------------------------------------------------------------------------- /cl/_testspx/multiworks/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/goplus/xgo/cl/internal/mcp" 4 | 5 | type foo struct { 6 | mcp.Prompt 7 | *Game 8 | } 9 | type Tool_hello struct { 10 | mcp.Tool 11 | *Game 12 | } 13 | type Game struct { 14 | mcp.Game 15 | foo *foo 16 | } 17 | 18 | func (this *Game) MainEntry() { 19 | this.Server("protos") 20 | } 21 | func (this *Game) Main() { 22 | _xgo_obj0 := &Tool_hello{Game: this} 23 | _xgo_lst1 := []mcp.ToolProto{_xgo_obj0} 24 | _xgo_obj1 := &foo{Game: this} 25 | this.foo = _xgo_obj1 26 | _xgo_lst2 := []mcp.PromptProto{_xgo_obj1} 27 | mcp.Gopt_Game_Main(this, nil, _xgo_lst1, _xgo_lst2) 28 | } 29 | func (this *foo) Main(_xgo_arg0 *mcp.Tool) string { 30 | this.Prompt.Main(_xgo_arg0) 31 | return "Hi" 32 | } 33 | func (this *Tool_hello) Main(_xgo_arg0 string) int { 34 | this.Tool.Main(_xgo_arg0) 35 | return -1 36 | } 37 | func main() { 38 | new(Game).Main() 39 | } 40 | -------------------------------------------------------------------------------- /cl/_testspx/newobj/Kai_spx.gox: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/goplus/xgo/0a0667c8ad573e65ffe6348ddb93df976578cd16/cl/_testspx/newobj/Kai_spx.gox -------------------------------------------------------------------------------- /cl/_testspx/newobj/main_spx.gox: -------------------------------------------------------------------------------- 1 | a := new 2 | a.run 3 | b := new(Sprite) 4 | echo b.name 5 | -------------------------------------------------------------------------------- /cl/_testspx/newobj/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/goplus/xgo/cl/internal/spx3" 6 | ) 7 | 8 | type Kai struct { 9 | spx3.Sprite 10 | *Game 11 | } 12 | type Game struct { 13 | spx3.Game 14 | } 15 | 16 | func (this *Game) MainEntry() { 17 | a := spx3.New() 18 | a.Run() 19 | b := new(spx3.Sprite) 20 | fmt.Println(b.Name()) 21 | } 22 | func (this *Game) Main() { 23 | _xgo_obj0 := &Kai{Game: this} 24 | spx3.Gopt_Game_Main(this, _xgo_obj0) 25 | } 26 | func (this *Kai) Main(_xgo_arg0 string) { 27 | this.Sprite.Main(_xgo_arg0) 28 | } 29 | func (this *Kai) Classfname() string { 30 | return "Kai" 31 | } 32 | func (this *Kai) Classclone() spx3.Handler { 33 | _xgo_ret := *this 34 | return &_xgo_ret 35 | } 36 | func main() { 37 | new(Game).Main() 38 | } 39 | -------------------------------------------------------------------------------- /cl/_testspx/nogame/bar.tspx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/goplus/xgo/0a0667c8ad573e65ffe6348ddb93df976578cd16/cl/_testspx/nogame/bar.tspx -------------------------------------------------------------------------------- /cl/_testspx/nogame/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "github.com/goplus/xgo/cl/internal/spx" 4 | 5 | type bar struct { 6 | spx.Sprite 7 | *MyGame 8 | } 9 | type MyGame struct { 10 | *spx.MyGame 11 | } 12 | 13 | func (this *MyGame) Main() { 14 | spx.Gopt_MyGame_Main(this) 15 | } 16 | func (this *bar) Main() { 17 | } 18 | func main() { 19 | new(MyGame).Main() 20 | } 21 | -------------------------------------------------------------------------------- /cl/_testspx/singlework/Kai_spx.gox: -------------------------------------------------------------------------------- 1 | echo jwt.token("Hi") 2 | -------------------------------------------------------------------------------- /cl/_testspx/singlework/main_spx.gox: -------------------------------------------------------------------------------- 1 | var ( 2 | Kai Kai 3 | ) 4 | 5 | run 6 | -------------------------------------------------------------------------------- /cl/_testspx/singlework/out.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/goplus/xgo/cl/internal/spx3" 6 | "github.com/goplus/xgo/cl/internal/spx3/jwt" 7 | ) 8 | 9 | type Kai struct { 10 | spx3.Sprite 11 | *Game 12 | } 13 | type Game struct { 14 | spx3.Game 15 | Kai Kai 16 | } 17 | 18 | func (this *Game) MainEntry() { 19 | this.Run() 20 | } 21 | func (this *Game) Main() { 22 | _xgo_obj0 := &Kai{Game: this} 23 | spx3.Gopt_Game_Main(this, _xgo_obj0) 24 | } 25 | func (this *Kai) Main(_xgo_arg0 string) { 26 | this.Sprite.Main(_xgo_arg0) 27 | fmt.Println(jwt.Token("Hi")) 28 | } 29 | func (this *Kai) Classfname() string { 30 | return "Kai" 31 | } 32 | func (this *Kai) Classclone() spx3.Handler { 33 | _xgo_ret := *this 34 | return &_xgo_ret 35 | } 36 | func main() { 37 | new(Game).Main() 38 | } 39 | -------------------------------------------------------------------------------- /cl/compile_testdir_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package cl_test 18 | 19 | import ( 20 | "testing" 21 | 22 | "github.com/goplus/xgo/cl/cltest" 23 | ) 24 | 25 | func TestTestspx(t *testing.T) { 26 | cltest.SpxFromDir(t, "", "./_testspx") 27 | } 28 | 29 | func TestTestgop(t *testing.T) { 30 | cltest.FromDir(t, "fatal", "./_testgop") 31 | } 32 | 33 | func TestTestc(t *testing.T) { 34 | cltest.FromDir(t, "", "./_testc") 35 | } 36 | 37 | func TestTestpy(t *testing.T) { 38 | cltest.FromDir(t, "", "./_testpy") 39 | } 40 | -------------------------------------------------------------------------------- /cl/internal/.gitignore: -------------------------------------------------------------------------------- 1 | .gop/ 2 | .xgo/ 3 | go.mod 4 | gop_autogen*.go 5 | xgo_autogen*.go 6 | -------------------------------------------------------------------------------- /cl/internal/gop-in-go/foo/foo.xgo: -------------------------------------------------------------------------------- 1 | package foo 2 | 3 | func ReverseMap(m map[string]int) map[int]string { 4 | return {v: k for k, v <- m} 5 | } 6 | -------------------------------------------------------------------------------- /cl/internal/gop-in-go/foo/foo_test.xgo: -------------------------------------------------------------------------------- 1 | package foo 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestReverseMap(t *testing.T) { 8 | out := ReverseMap({"a": 1}) 9 | if len(out) != 1 || out[1] != "a" { 10 | t.Fatal("ReverseMap failed:", out) 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /cl/internal/gop-in-go/foo/footest_test.xgo: -------------------------------------------------------------------------------- 1 | package foo_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/goplus/xgo/cl/internal/gop-in-go/foo" 7 | ) 8 | 9 | func TestReverseMap(t *testing.T) { 10 | out := foo.ReverseMap({"b": 2}) 11 | if len(out) != 1 || out[2] != "b" { 12 | t.Fatal("ReverseMap failed:", out) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /cl/internal/huh/huh.go: -------------------------------------------------------------------------------- 1 | package huh 2 | 3 | // ----------------------------------------------------------------------------- 4 | 5 | type Form int 6 | 7 | func (f Form) Run() { 8 | } 9 | 10 | func New(string, string, int) Form { 11 | return 0 12 | } 13 | 14 | // ----------------------------------------------------------------------------- 15 | -------------------------------------------------------------------------------- /cl/internal/llgo-hello/hello.go: -------------------------------------------------------------------------------- 1 | package hello 2 | 3 | import "github.com/goplus/lib/c" 4 | 5 | func Main() { 6 | c.Printf(c.Str("Hello world\n")) 7 | } 8 | -------------------------------------------------------------------------------- /cl/internal/mcp/classfile.go: -------------------------------------------------------------------------------- 1 | package mcp 2 | 3 | const ( 4 | GopPackage = true 5 | ) 6 | 7 | type Game struct { 8 | } 9 | 10 | func New() *Game { 11 | return nil 12 | } 13 | 14 | func (p *Game) initGame() {} 15 | 16 | func (p *Game) Server(name string) {} 17 | 18 | type Tool struct { 19 | } 20 | 21 | func (p *Tool) Main(name string) int { 22 | return 0 23 | } 24 | 25 | type Prompt struct { 26 | } 27 | 28 | func (p *Prompt) Main(*Tool) string { 29 | return "" 30 | } 31 | 32 | type Resource struct { 33 | } 34 | 35 | func (p *Resource) Main() { 36 | } 37 | 38 | type ToolProto interface { 39 | Main(name string) int 40 | } 41 | 42 | type PromptProto interface { 43 | Main(*Tool) string 44 | } 45 | 46 | type ResourceProto interface { 47 | Main() 48 | } 49 | 50 | func Gopt_Game_Main(game interface{ initGame() }, resources []ResourceProto, tools []ToolProto, prompts []PromptProto) { 51 | } 52 | -------------------------------------------------------------------------------- /cl/internal/overload/bar/bar.go: -------------------------------------------------------------------------------- 1 | package bar 2 | 3 | const GopPackage = true 4 | 5 | type M = map[string]any 6 | 7 | type basetype interface { 8 | string | int | bool | float64 9 | } 10 | 11 | type Var__0[T basetype] struct { 12 | val T 13 | } 14 | 15 | func (p *Var__0[T]) Value() T { 16 | return p.val 17 | } 18 | 19 | type Var__1[T map[string]any] struct { 20 | val T 21 | } 22 | 23 | func (p *Var__1[T]) Value() T { 24 | return p.val 25 | } 26 | 27 | func Gopx_Var_Cast__0[T basetype]() *Var__0[T] { 28 | return new(Var__0[T]) 29 | } 30 | 31 | func Gopx_Var_Cast__1[T map[string]any]() *Var__1[T] { 32 | return new(Var__1[T]) 33 | } 34 | 35 | type Player struct { 36 | } 37 | 38 | func Gopt_Player_Gopx_OnCmd__0[T any](p *Player, handler func(cmd T) error) { 39 | var t T 40 | handler(t) 41 | } 42 | 43 | func Gopt_Player_Gopx_OnCmd__1[T1 ~int, T2 any](p *Player, n T1, handler func(n T1, cmd T2) error) { 44 | var t T2 45 | handler(n, t) 46 | } 47 | -------------------------------------------------------------------------------- /cl/internal/spx/pkg/pkg.go: -------------------------------------------------------------------------------- 1 | package pkg 2 | 3 | const ( 4 | GopPackage = true 5 | ) 6 | 7 | type Vector struct { 8 | X int 9 | Y int 10 | } 11 | 12 | func NewVector(x, y int) *Vector { 13 | return &Vector{x, y} 14 | } 15 | 16 | func (v *Vector) Add__0(x int, y int) { 17 | v.X += x 18 | v.Y += y 19 | } 20 | 21 | func (v *Vector) Add__1(o *Vector) { 22 | v.Add__0(o.X, o.Y) 23 | } 24 | 25 | func (v *Vector) Self() *Vector { 26 | return v 27 | } 28 | -------------------------------------------------------------------------------- /cl/internal/spx2/spx2.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package spx2 18 | 19 | const ( 20 | Gop_sched = "Sched" 21 | Gop_work = "Sprite" 22 | ) 23 | 24 | type Game struct { 25 | } 26 | 27 | func (p *Game) Main() { 28 | } 29 | 30 | type Sprite struct { 31 | } 32 | 33 | func Sched() { 34 | } 35 | -------------------------------------------------------------------------------- /cl/internal/spx3/jwt/jwt.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package jwt 18 | 19 | func Token(v string) string { 20 | return "token: " + v 21 | } 22 | -------------------------------------------------------------------------------- /cl/internal/spx3/spx3.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package spx3 18 | 19 | const ( 20 | GopPackage = true 21 | ) 22 | 23 | type Game struct { 24 | } 25 | 26 | func New() *Game { 27 | return nil 28 | } 29 | 30 | func (p *Game) initGame() {} 31 | 32 | func (p *Game) Run() {} 33 | 34 | type Sprite struct { 35 | } 36 | 37 | func (p *Sprite) Name() string { 38 | return "sprite" 39 | } 40 | 41 | func (p *Sprite) Main(name string) {} 42 | 43 | type Handler interface { 44 | Main(name string) 45 | Classfname() string 46 | Classclone() Handler 47 | } 48 | 49 | func Gopt_Game_Main(game interface{ initGame() }, workers ...Handler) { 50 | } 51 | -------------------------------------------------------------------------------- /cl/internal/spx4/pkg/pkg.go: -------------------------------------------------------------------------------- 1 | package pkg 2 | 3 | const ( 4 | GopPackage = true 5 | ) 6 | 7 | type Vector struct { 8 | X int 9 | Y int 10 | } 11 | 12 | func NewVector(x, y int) *Vector { 13 | return &Vector{x, y} 14 | } 15 | 16 | func (v *Vector) Add__0(x int, y int) { 17 | v.X += x 18 | v.Y += y 19 | } 20 | 21 | func (v *Vector) Add__1(o *Vector) { 22 | v.Add__0(o.X, o.Y) 23 | } 24 | 25 | func (v *Vector) Self() *Vector { 26 | return v 27 | } 28 | -------------------------------------------------------------------------------- /cl/internal/test/match.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2024 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package test 18 | 19 | const ( 20 | GopPackage = true 21 | ) 22 | 23 | type basetype interface { 24 | string | int | bool | float64 25 | } 26 | 27 | type Case struct { 28 | CaseT 29 | } 30 | 31 | const ( 32 | Gopo_Gopt_Case_Match = "Gopt_Case_MatchTBase,Gopt_Case_MatchAny" 33 | ) 34 | 35 | func Gopt_Case_MatchTBase[T basetype](t CaseT, got, expected T, name ...string) { 36 | } 37 | 38 | func Gopt_Case_MatchAny(t CaseT, got, expected any, name ...string) { 39 | } 40 | -------------------------------------------------------------------------------- /cl/internal/typesalias/alias_go121.go: -------------------------------------------------------------------------------- 1 | //go:build !go1.22 2 | // +build !go1.22 3 | 4 | package typesalias 5 | 6 | import "go/types" 7 | 8 | const Support = false 9 | 10 | type Alias struct { 11 | } 12 | 13 | const unsupported = "typesAlias are unsupported at this go version" 14 | 15 | func (t *Alias) Underlying() types.Type { 16 | panic(unsupported) 17 | } 18 | 19 | func (t *Alias) String() string { 20 | panic(unsupported) 21 | } 22 | 23 | func (t *Alias) Obj() *types.TypeName { 24 | panic(unsupported) 25 | } 26 | 27 | func NewAlias(obj *types.TypeName, rhs types.Type) *Alias { 28 | return &Alias{} 29 | } 30 | 31 | func Unalias(t types.Type) types.Type { 32 | return t 33 | } 34 | -------------------------------------------------------------------------------- /cl/internal/typesalias/alias_go122.go: -------------------------------------------------------------------------------- 1 | //go:build go1.22 2 | // +build go1.22 3 | 4 | package typesalias 5 | 6 | import "go/types" 7 | 8 | const Support = true 9 | 10 | type Alias = types.Alias 11 | 12 | func NewAlias(obj *types.TypeName, rhs types.Type) *Alias { 13 | return types.NewAlias(obj, rhs) 14 | } 15 | 16 | func Unalias(t types.Type) types.Type { 17 | return types.Unalias(t) 18 | } 19 | -------------------------------------------------------------------------------- /cl/internal/typesalias/typeparams_go122.go: -------------------------------------------------------------------------------- 1 | //go:build !go1.23 2 | // +build !go1.23 3 | 4 | package typesalias 5 | 6 | import "go/types" 7 | 8 | func TypeArgs(t *Alias) *types.TypeList { 9 | return nil 10 | } 11 | 12 | func TypeParams(t *Alias) *types.TypeParamList { 13 | return nil 14 | } 15 | 16 | func SetTypeParams(t *Alias, params []*types.TypeParam) { 17 | panic("unsupport Alias.SetTypeParams") 18 | } 19 | -------------------------------------------------------------------------------- /cl/internal/typesalias/typeparams_go123.go: -------------------------------------------------------------------------------- 1 | //go:build go1.23 2 | // +build go1.23 3 | 4 | package typesalias 5 | 6 | import "go/types" 7 | 8 | func TypeArgs(t *Alias) *types.TypeList { 9 | return t.TypeArgs() 10 | } 11 | 12 | func TypeParams(t *Alias) *types.TypeParamList { 13 | return t.TypeParams() 14 | } 15 | 16 | func SetTypeParams(t *Alias, params []*types.TypeParam) { 17 | t.SetTypeParams(params) 18 | } 19 | -------------------------------------------------------------------------------- /cl/internal/unit/unit.go: -------------------------------------------------------------------------------- 1 | package unit 2 | 3 | // ----------------------------------------------------------------------------- 4 | 5 | type Distance int 6 | 7 | const Gopu_Distance = "mm=1,cm=10,dm=100,m=1000" 8 | 9 | // ----------------------------------------------------------------------------- 10 | -------------------------------------------------------------------------------- /cmd/chore/xgobuiltingen/builtin.gox: -------------------------------------------------------------------------------- 1 | import ( 2 | "go/ast" 3 | ) 4 | 5 | var ( 6 | Name string 7 | Fn reference 8 | ) 9 | 10 | func genAST() *ast.FuncDecl { 11 | f := Fn 12 | ref := f.getAST() 13 | newName := Name.capitalize 14 | return { 15 | Doc: docForFunc(ref.Doc, f.Name, newName), 16 | Name: ast.newIdent(newName), 17 | Type: reference.toFuncType(ref.Type, f.Pkg), 18 | } 19 | } 20 | 21 | func genMethodAST(methods []builtin) *ast.FuncDecl { 22 | f := Fn 23 | at := f.Pkg 24 | ref := f.getAST() 25 | ex := f.Exargs 26 | mt, recvType := reference.toMethodType(ref.Type, ex, at) 27 | if at == "" { // builtin 28 | recvType = methods[methods.len-1].genAST().Type.Params.List[0].Type 29 | } 30 | recvName := recvNameOf(recvType) 31 | return { 32 | Doc: docForMethod(ref.Doc, at, f.Name, Name, recvName, ex), 33 | Name: ast.newIdent(Name), 34 | Type: mt, 35 | Recv: {List: {{ 36 | Names: []*ast.Ident{ast.newIdent(recvName)}, 37 | Type: recvType, 38 | }}}, 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /cmd/internal/mod/download.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package mod 18 | 19 | import ( 20 | "github.com/goplus/xgo/cmd/internal/base" 21 | "github.com/goplus/xgo/cmd/internal/gopget" 22 | ) 23 | 24 | // gop mod download 25 | var CmdDownload = &base.Command{ 26 | UsageLine: "gop mod download [-x -json] [modules]", 27 | Short: "download modules to local cache", 28 | } 29 | 30 | func init() { 31 | CmdDownload.Run = gopget.Cmd.Run 32 | } 33 | -------------------------------------------------------------------------------- /cmd/internal/mod/mod.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package mod 17 | 18 | import ( 19 | "fmt" 20 | "log" 21 | "os" 22 | 23 | "github.com/goplus/xgo/cmd/internal/base" 24 | ) 25 | 26 | var Cmd = &base.Command{ 27 | UsageLine: "gop mod", 28 | Short: "Module maintenance", 29 | 30 | Commands: []*base.Command{ 31 | CmdInit, 32 | CmdDownload, 33 | CmdTidy, 34 | }, 35 | } 36 | 37 | func check(err error) { 38 | if err != nil { 39 | log.Panicln(err) 40 | } 41 | } 42 | 43 | func fatal(msg any) { 44 | fmt.Fprintln(os.Stderr, msg) 45 | os.Exit(1) 46 | } 47 | -------------------------------------------------------------------------------- /cmd/xgo/bug_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/bug" 18 | ) 19 | 20 | use "bug" 21 | 22 | short "Start a bug report" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/build_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/build" 18 | ) 19 | 20 | use "build [flags] [packages]" 21 | 22 | short "Build XGo files" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/clean_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/clean" 18 | ) 19 | 20 | use "clean [flags] " 21 | 22 | short "Clean all XGo auto generated files" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/doc_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/doc" 18 | ) 19 | 20 | use "doc [flags] [pkgPath]" 21 | 22 | short "Show documentation for package or symbol" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/env_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/env" 18 | ) 19 | 20 | use "env [flags] [var ...]" 21 | 22 | short "Prints XGo environment information" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/fmt_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/gopfmt" 18 | ) 19 | 20 | use "fmt [flags] path ..." 21 | 22 | short "Format XGo packages" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/get_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/gopget" 18 | ) 19 | 20 | use "get [flags] [packages]" 21 | 22 | short "Add dependencies to current module and install them" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/go_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/gengo" 18 | ) 19 | 20 | use "go [flags] [packages|files]" 21 | 22 | short "Convert XGo code into Go code" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/install_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/install" 18 | ) 19 | 20 | use "install [flags] [packages]" 21 | 22 | short "Build XGo files and install target to GOBIN" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/main_app.gox: -------------------------------------------------------------------------------- 1 | import ( 2 | "github.com/goplus/gogen" 3 | "github.com/qiniu/x/log" 4 | ) 5 | 6 | short "xgo is a tool for managing XGo source code." 7 | log.setFlags log.Ldefault&^log.LstdFlags 8 | 9 | gogen.GeneratedHeader = "// Code generated by xgo (XGo); DO NOT EDIT.\n\n" 10 | -------------------------------------------------------------------------------- /cmd/xgo/mod_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/mod" 18 | ) 19 | 20 | use "mod" 21 | 22 | short "Module maintenance" 23 | 24 | run => { 25 | help 26 | } 27 | -------------------------------------------------------------------------------- /cmd/xgo/mod_download_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/mod" 18 | ) 19 | 20 | use "download [flags] [modules]" 21 | 22 | short "download modules to local cache" 23 | 24 | flagOff 25 | 26 | run args => { 27 | if args.len < 1 { 28 | help 29 | return 30 | } 31 | self.CmdDownload.Run self.CmdDownload, args 32 | } 33 | -------------------------------------------------------------------------------- /cmd/xgo/mod_init_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/mod" 18 | ) 19 | 20 | use "init [flags] module-path" 21 | 22 | short "initialize new module in current directory" 23 | 24 | flagOff 25 | 26 | run args => { 27 | if args.len < 1 { 28 | help 29 | return 30 | } 31 | self.CmdInit.Run self.CmdInit, args 32 | } 33 | -------------------------------------------------------------------------------- /cmd/xgo/mod_tidy_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/mod" 18 | ) 19 | 20 | use "tidy [flags]" 21 | 22 | short "add missing and remove unused modules" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.CmdTidy.Run self.CmdTidy, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/run_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/run" 18 | ) 19 | 20 | use "run [flags] package [arguments...]" 21 | 22 | short "Compile and run a XGo program" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/serve_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/serve" 18 | ) 19 | 20 | use "serve [flags]" 21 | 22 | short "Serve as a XGo LangServer" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/test_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/test" 18 | ) 19 | 20 | use "test [flags] [packages]" 21 | 22 | short "Test XGo packages" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /cmd/xgo/version_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | "runtime" 18 | self "xgo/env" 19 | ) 20 | 21 | use "version [flags]" 22 | 23 | short "Print XGo version" 24 | 25 | run => { 26 | echo "xgo ${self.version} ${runtime.GOOS}/${runtime.GOARCH}" 27 | } 28 | -------------------------------------------------------------------------------- /cmd/xgo/watch_cmd.gox: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | import ( 17 | self "github.com/goplus/xgo/cmd/internal/watch" 18 | ) 19 | 20 | use "watch [flags] [dir]" 21 | 22 | short "Monitor code changes in a XGo workspace to generate Go files" 23 | 24 | flagOff 25 | 26 | run args => { 27 | self.Cmd.Run self.Cmd, args 28 | } 29 | -------------------------------------------------------------------------------- /demo/_llgo/callpy/callpy.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "c" 3 | "py" 4 | "py/math" 5 | ) 6 | 7 | x := math.sqrt(py.float(2)) 8 | c.printf c"sqrt(2) = %f\n", x.float64 9 | -------------------------------------------------------------------------------- /demo/_llgo/chello/hello.xgo: -------------------------------------------------------------------------------- 1 | import "c" 2 | 3 | c.printf c"Hello world\n" 4 | -------------------------------------------------------------------------------- /demo/_llgo/cpphello/cpphello.xgo: -------------------------------------------------------------------------------- 1 | import "cpp/std" 2 | 3 | println std.str("Hello world").str 4 | -------------------------------------------------------------------------------- /demo/_llgo/defer/defer.xgo: -------------------------------------------------------------------------------- 1 | import "c" 2 | 3 | func f(s string) bool { 4 | return len(s) > 2 5 | } 6 | 7 | defer func() { 8 | c.printf c"hi\n" 9 | }() 10 | if s := "hello"; f(s) { 11 | defer c.printf(c"%s\n", c.allocaCStr(s)) 12 | } else { 13 | defer c.printf(c"world\n") 14 | } 15 | defer c.printf(c"bye\n") 16 | -------------------------------------------------------------------------------- /demo/_llgo/errors/errors.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "c" 3 | ) 4 | 5 | // New returns an error that formats as the given text. 6 | // Each call to New returns a distinct error value even if the text is identical. 7 | func New(text string) error { 8 | return &errorString{text} 9 | } 10 | 11 | // errorString is a trivial implementation of error. 12 | type errorString struct { 13 | s string 14 | } 15 | 16 | func (e *errorString) Error() string { 17 | return e.s 18 | } 19 | 20 | e := new("an error") 21 | c.printf c.allocaCStr(e.error+"\n") 22 | -------------------------------------------------------------------------------- /demo/_llgo/go.mod: -------------------------------------------------------------------------------- 1 | module llgoexample 2 | 3 | go 1.18 // llgo 1.0 4 | 5 | require github.com/goplus/lib v0.2.0 6 | -------------------------------------------------------------------------------- /demo/_llgo/go.sum: -------------------------------------------------------------------------------- 1 | github.com/goplus/lib v0.2.0 h1:AjqkN1XK5H23wZMMlpaUYAMCDAdSBQ2NMFrLtSh7W4g= 2 | github.com/goplus/lib v0.2.0/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0= 3 | -------------------------------------------------------------------------------- /demo/_llgo/goroutine/goroutine.xgo: -------------------------------------------------------------------------------- 1 | import "c" 2 | 3 | done := false 4 | go func() { 5 | c.printf c"Hello, goroutine\n" 6 | done = true 7 | }() 8 | for !done { 9 | c.printf c"." 10 | } 11 | -------------------------------------------------------------------------------- /demo/_llgo/hello/hello.xgo: -------------------------------------------------------------------------------- 1 | echo "Hello world" 2 | -------------------------------------------------------------------------------- /demo/_llgo/hellollgo/README.md: -------------------------------------------------------------------------------- 1 | This is an example to show how XGo interacts with C. 2 | 3 | ```go 4 | import "c" 5 | 6 | c.printf c"Hello, llgo!\n" 7 | c.fprintf c.Stderr, c"Hi, %6.1f\n", 3.14 8 | ``` 9 | 10 | Here we use `import "c"` to import libc. It's an abbreviation for `import "github.com/goplus/lib/c"`. It is equivalent to the following code: 11 | 12 | ```go 13 | import "github.com/goplus/lib/c" 14 | 15 | c.printf c"Hello, llgo!\n" 16 | c.fprintf c.Stderr, c"Hi, %7.1f\n", 3.14 17 | ``` 18 | 19 | In this example we call two C standard functions `printf` and `fprintf`, pass a C variable `stderr` and two C strings in the form of `c"xxx"`. 20 | 21 | To run this demo, you need to set the `GOP_GOCMD` environment variable first. 22 | 23 | ```sh 24 | export GOP_GOCMD=llgo # default is `go` 25 | ``` 26 | 27 | Then execute `xgo run .` to see the output of this example: 28 | 29 | ``` 30 | Hello, llgo! 31 | Hi, 3.1 32 | ``` 33 | 34 | ### Give a Star! ⭐ 35 | 36 | If you like or are using XGo to learn or start your projects, please give it a star. Thanks! 37 | -------------------------------------------------------------------------------- /demo/_llgo/hellollgo/hello.xgo: -------------------------------------------------------------------------------- 1 | import "c" 2 | 3 | c.printf c"Hello, llgo!\n" 4 | c.fprintf c.Stderr, c"Hi, %6.1f\n", 3.14 5 | -------------------------------------------------------------------------------- /demo/_llgo/matrix/matrix.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "c" 3 | "py" 4 | "py/numpy" 5 | ) 6 | 7 | a := py.list( 8 | py.list(1.0, 2.0, 3.0), 9 | py.list(4.0, 5.0, 6.0), 10 | py.list(7.0, 8.0, 9.0), 11 | ) 12 | b := py.list( 13 | py.list(9.0, 8.0, 7.0), 14 | py.list(6.0, 5.0, 4.0), 15 | py.list(3.0, 2.0, 1.0), 16 | ) 17 | x := numpy.add(a, b) 18 | c.printf c"a+b = %s\n", x.str.cStr 19 | -------------------------------------------------------------------------------- /demo/_llgo/pyhello/hello.xgo: -------------------------------------------------------------------------------- 1 | import "py/std" 2 | 3 | std.print py"Hello world" 4 | -------------------------------------------------------------------------------- /demo/_llgo/pymax/pymax.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "py" 3 | "py/std" 4 | ) 5 | 6 | x := std.max(py.float(3.0), py.float(9.0), py.float(23.0), py.float(100.0)) 7 | std.print(x) 8 | 9 | list := py.list(3.0, 9.0, 23.0, 100.0) 10 | y := std.max(std.iter(list)) 11 | std.print(y) 12 | -------------------------------------------------------------------------------- /demo/_llgo/pyprint/print.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "py" 3 | "py/std" 4 | ) 5 | 6 | x := py.float(3.14) 7 | std.print(x) 8 | -------------------------------------------------------------------------------- /demo/_llgo/pytensor/tensor.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "py" 3 | "py/std" 4 | "py/torch" 5 | ) 6 | 7 | data := py.list( 8 | py.list(1.0, 2.0), 9 | py.list(3.0, 4.0), 10 | ) 11 | x := torch.tensor(data) 12 | std.print(x) 13 | -------------------------------------------------------------------------------- /demo/_llgo/qsort/qsort.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "c" 3 | "unsafe" 4 | ) 5 | 6 | a := [100, 8, 23, 2, 7] 7 | c.qsort unsafe.Pointer(&a[0]), 5, unsafe.Sizeof(0), (a, b) => { 8 | return c.Int(*(*int)(a) - *(*int)(b)) 9 | } 10 | for v in a { 11 | c.printf c"%d\n", v 12 | } 13 | -------------------------------------------------------------------------------- /demo/_llgo/reflect/reflect.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "c" 3 | "reflect" 4 | ) 5 | 6 | tyIntSlice := reflect.sliceOf(reflect.typeOf(0)) 7 | v := reflect.zero(tyIntSlice) 8 | v = reflect.append(v, reflect.valueOf(1), reflect.valueOf(2), reflect.valueOf(3)) 9 | for i, n := 0, v.len; i < n; i++ { 10 | item := v.index(i) 11 | c.Printf c"%d\n", item.int 12 | } 13 | -------------------------------------------------------------------------------- /demo/_llgo/statistics/statistics.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "c" 3 | "py" 4 | "py/statistics" 5 | ) 6 | 7 | list := py.list(1.0, 2.0, 3.0, 4.0, 4.0) 8 | mean := statistics.mean(list) 9 | c.printf c"mean(1, 2, 3, 4, 4) = %f\n", mean.float64 10 | -------------------------------------------------------------------------------- /demo/_tinygo/go.mod: -------------------------------------------------------------------------------- 1 | module tinygoexample 2 | 3 | go 1.18 // tinygo 0.32 4 | -------------------------------------------------------------------------------- /demo/_tinygo/sortdemo/sort.xgo: -------------------------------------------------------------------------------- 1 | import "sort" 2 | 3 | vals := [32, 58, 25, 92, 45, 78] 4 | sort.ints vals 5 | for v in vals { 6 | println v 7 | } 8 | 9 | texts := ["apple", "banana", "cherry", "date", "elderberry", "fig"] 10 | sort.slice texts, (i, j) => { 11 | leni, lenj := len(texts[i]), len(texts[j]) 12 | if leni != lenj { 13 | return leni < lenj 14 | } 15 | return texts[i] < texts[j] 16 | } 17 | for v in texts { 18 | println v 19 | } 20 | -------------------------------------------------------------------------------- /demo/fullspec/mixgo-complex/bar.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io" 5 | "log" 6 | "testing" 7 | ) 8 | 9 | type ift interface { 10 | io.Closer 11 | f(int) string 12 | g() 13 | } 14 | 15 | type impl struct { 16 | a T 17 | } 18 | 19 | func Bar(t *testing.T) int { 20 | log.Println("Hello") 21 | t.Log("Hello", 22 | "world") 23 | return 0 24 | } 25 | -------------------------------------------------------------------------------- /demo/fullspec/mixgo-complex/foo.xgo: -------------------------------------------------------------------------------- 1 | // Pkg doc 2 | 3 | import ( 4 | "io" 5 | "log" 6 | ) 7 | 8 | type ift2 interface { 9 | io.Closer 10 | f(int) string 11 | g() 12 | } 13 | 14 | // T doc 15 | type T struct { 16 | io.Closer 17 | } 18 | 19 | func (T) Close() (err error) { 20 | log.Println("Hi!") 21 | return 22 | } 23 | 24 | func (t T) g() {} 25 | 26 | func (t T) f(a int) (b string) { 27 | _ = t.Closer 28 | return 29 | } 30 | 31 | func Foo(i *impl) string { 32 | i.a.f(0) 33 | i.a.g() 34 | return "" 35 | } 36 | 37 | // foo golang/go#61561: interface instances aren't concurrency-safe 38 | // as they are not completed by the type checker. 39 | func foo(a, b int) string { 40 | return "" 41 | } 42 | 43 | println "Hello, world", T{}.f(0) 44 | -------------------------------------------------------------------------------- /demo/fullspec/overloadfunc1/add.xgo: -------------------------------------------------------------------------------- 1 | func add = ( 2 | func(a, b int) int { 3 | return a + b 4 | } 5 | func(a, b string) string { 6 | return a + b 7 | } 8 | ) 9 | 10 | println add(100, 7) 11 | println add("Hello", "World") 12 | -------------------------------------------------------------------------------- /demo/fullspec/overloadfunc2/mul.xgo: -------------------------------------------------------------------------------- 1 | func mulInt(a, b int) int { 2 | return a * b 3 | } 4 | 5 | func mulFloat(a, b float64) float64 { 6 | return a * b 7 | } 8 | 9 | func mul = ( 10 | mulInt 11 | mulFloat 12 | ) 13 | 14 | println mul(100, 7) 15 | println mul(1.2, 3.14) 16 | -------------------------------------------------------------------------------- /demo/fullspec/overloadmethod/method.xgo: -------------------------------------------------------------------------------- 1 | type foo struct { 2 | } 3 | 4 | func (a *foo) mulInt(b int) *foo { 5 | println "mulInt" 6 | return a 7 | } 8 | 9 | func (a *foo) mulFoo(b *foo) *foo { 10 | println "mulFoo" 11 | return a 12 | } 13 | 14 | func (foo).mul = ( 15 | (foo).mulInt 16 | (foo).mulFoo 17 | ) 18 | 19 | var a, b *foo 20 | var c = a.mul(100) 21 | var d = a.mul(c) 22 | -------------------------------------------------------------------------------- /demo/fullspec/overloadop1/overloadop.xgo: -------------------------------------------------------------------------------- 1 | type foo struct { 2 | } 3 | 4 | func (a foo) + (b foo) (ret foo) { 5 | println "a + b" 6 | return 7 | } 8 | 9 | func (a foo) - (b foo) (ret foo) { 10 | println "a - b" 11 | return 12 | } 13 | 14 | func (a foo) -> (b foo) { 15 | println "a -> b" 16 | } 17 | 18 | func (a foo) <> (b foo) { 19 | println "a <> b" 20 | } 21 | 22 | func -(a foo) (ret foo) { 23 | println "-a" 24 | return 25 | } 26 | 27 | func ++(a foo) { 28 | println "a++" 29 | } 30 | 31 | func (a foo) != (b foo) bool { 32 | println "a != b" 33 | return true 34 | } 35 | 36 | var a, b foo 37 | var c = a + b 38 | var d = a - b 39 | var e = -a 40 | var f = a != b 41 | 42 | println f 43 | a++ 44 | a -> b 45 | a <> b 46 | -------------------------------------------------------------------------------- /demo/fullspec/overloadop2/overloadop.xgo: -------------------------------------------------------------------------------- 1 | type foo struct { 2 | } 3 | 4 | func (a foo) mulInt(b int) (ret foo) { 5 | println "a * int" 6 | return 7 | } 8 | 9 | func (a foo) mulFoo(b foo) (ret foo) { 10 | println "a * b" 11 | return 12 | } 13 | 14 | func intMulFoo(a int, b foo) (ret foo) { 15 | println "int * b" 16 | return 17 | } 18 | 19 | func (foo).* = ( 20 | (foo).mulInt 21 | (foo).mulFoo 22 | intMulFoo 23 | ) 24 | 25 | var a, b foo 26 | var c = a * 10 27 | var d = a * b 28 | var e = 10 * a 29 | -------------------------------------------------------------------------------- /demo/gsh-exec/exec.gsh: -------------------------------------------------------------------------------- 1 | gop "run", "./foo" 2 | exec "gop run ./foo" 3 | exec "FOO=100 gop run ./foo" 4 | exec {"FOO": "101"}, "gop", "run", "./foo" 5 | exec "gop", "run", "./foo" 6 | exec "ls $HOME" 7 | ls ${HOME} 8 | -------------------------------------------------------------------------------- /demo/gsh-exec/foo/foo.xgo: -------------------------------------------------------------------------------- 1 | import "os" 2 | 3 | foo := os.getenv("FOO") 4 | if foo != "" { 5 | echo foo 6 | } else { 7 | echo "FOO not found" 8 | } 9 | -------------------------------------------------------------------------------- /demo/lambda1/lambda.xgo: -------------------------------------------------------------------------------- 1 | func f(x float64, t func(float64) float64) float64 { 2 | return t(x) 3 | } 4 | 5 | echo f(1.0, x => 2 * x) 6 | echo f(5.0, (x) => { 7 | return 2 * x 8 | }) 9 | -------------------------------------------------------------------------------- /demo/mapliteral/mapliteral.xgo: -------------------------------------------------------------------------------- 1 | func echoS2f32(vals map[string]float32) { 2 | echo vals 3 | } 4 | 5 | echo {"Monday": 1, "Sunday": 7} 6 | echoS2f32 {"Monday": 1, "Sunday": 7} 7 | 8 | var a map[string]any = {"Monday": 1, "Sunday": 7} 9 | echo a 10 | -------------------------------------------------------------------------------- /demo/mixgo/README.md: -------------------------------------------------------------------------------- 1 | This is an example to show how to mix Go/XGo code in the same package. 2 | 3 | In this example, we have a Go source file named `a.go`: 4 | 5 | ```go 6 | package main 7 | 8 | import "fmt" 9 | 10 | func p(a interface{}) { 11 | sayMix() 12 | fmt.Println("Hello,", a) 13 | } 14 | ``` 15 | 16 | And we have a XGo source file named `b.xgo`: 17 | 18 | ```go 19 | func sayMix() { 20 | println "Mix Go and XGo" 21 | } 22 | 23 | p "world" 24 | ``` 25 | 26 | You can see that Go calls a XGo function named `sayMix`, and XGo calls a Go function named `p`. As you are used to in Go programming, this kind of circular reference is allowed. 27 | 28 | Run `xgo run .` to see the output of this example: 29 | 30 | ``` 31 | Mix Go and XGo 32 | Hello, world 33 | ``` 34 | 35 | ### Give a Star! ⭐ 36 | 37 | If you like or are using XGo to learn or start your projects, please give it a star. Thanks! 38 | -------------------------------------------------------------------------------- /demo/mixgo/a.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func p(a any) { 6 | sayMix() 7 | fmt.Println("Hello,", a) 8 | } 9 | -------------------------------------------------------------------------------- /demo/mixgo/b.xgo: -------------------------------------------------------------------------------- 1 | func sayMix() { 2 | println "Mix Go and XGo" 3 | } 4 | 5 | p "world" 6 | -------------------------------------------------------------------------------- /demo/mixgo/xgo_autogen.go: -------------------------------------------------------------------------------- 1 | // Code generated by xgo (XGo); DO NOT EDIT. 2 | 3 | package main 4 | 5 | import "fmt" 6 | 7 | const _ = true 8 | //line demo/mixgo/b.xgo:1:1 9 | func sayMix() { 10 | //line demo/mixgo/b.xgo:2:1 11 | fmt.Println("Mix Go and XGo") 12 | } 13 | //line demo/mixgo/b.xgo:5 14 | func main() { 15 | //line demo/mixgo/b.xgo:5:1 16 | p("world") 17 | } 18 | -------------------------------------------------------------------------------- /demo/sliceliteral/sliceliteral.xgo: -------------------------------------------------------------------------------- 1 | func echoF32s(vals []float32) { 2 | echo vals 3 | } 4 | 5 | func anyslice() []any { 6 | return [10, 3.14, 200] 7 | } 8 | 9 | echo [10, 3.14, 200] 10 | echoF32s [10, 3.14, 200] 11 | 12 | var a []any = [10, 3.14, 200] 13 | a = [10, 3.14, 200] 14 | echo a 15 | echo anyslice() 16 | -------------------------------------------------------------------------------- /demo/stringtrans/transform.xgo: -------------------------------------------------------------------------------- 1 | t := "u_int32_t" 2 | echo t.split("_").capitalize.join("") 3 | echo t.split("_").repeat(3).join(" ") 4 | -------------------------------------------------------------------------------- /demo/tpl-calc-dump/calc_dump.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "os" 3 | "xgo/tpl" 4 | ) 5 | 6 | cl := tpl` 7 | expr = termExpr % ("+" | "-") 8 | 9 | termExpr = unaryExpr % ("*" | "/") 10 | 11 | unaryExpr = operand | "-" unaryExpr 12 | 13 | operand = INT | FLOAT | "(" expr ")" 14 | `! 15 | 16 | print "> " 17 | for line in os.Stdin { 18 | e, err := cl.parseExpr(line, nil) 19 | if err != nil { 20 | print "${err}\n> " 21 | } else { 22 | tpl.dump e 23 | print "> " 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /demo/tpl-calc/calc.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "os" 3 | "xgo/tpl" 4 | ) 5 | 6 | cl := tpl` 7 | 8 | expr = operand % ("*" | "/") % ("+" | "-") => { 9 | return tpl.binaryOp(true, self, (op, x, y) => { 10 | switch op.Tok { 11 | case '+': return x.(float64) + y.(float64) 12 | case '-': return x.(float64) - y.(float64) 13 | case '*': return x.(float64) * y.(float64) 14 | case '/': return x.(float64) / y.(float64) 15 | } 16 | panic("unexpected") 17 | }) 18 | } 19 | 20 | operand = basicLit | unaryExpr 21 | 22 | unaryExpr = "-" operand => { 23 | return -(self[1].(float64)) 24 | } 25 | 26 | basicLit = INT | FLOAT => { 27 | return self.(*tpl.Token).Lit.float! 28 | } 29 | `! 30 | 31 | print "> " 32 | for line in os.Stdin { 33 | e, err := cl.parseExpr(line, nil) 34 | if err != nil { 35 | print err, "\n> " 36 | } else { 37 | print e, "\n> " 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /demo/tpl-gen-ast/gen_calc_ast.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "encoding/json" 3 | "os" 4 | "xgo/tpl" 5 | "xgo/tpl/token" 6 | ) 7 | 8 | type Expr any 9 | 10 | type UnaryExpr struct { 11 | OpPos token.Pos 12 | Op token.Token 13 | X Expr 14 | } 15 | 16 | type BinaryExpr struct { 17 | X Expr 18 | OpPos token.Pos 19 | Op token.Token 20 | Y Expr 21 | } 22 | 23 | type BasicLit struct { 24 | ValuePos token.Pos 25 | Kind token.Token 26 | Value string 27 | } 28 | 29 | cl := tpl` 30 | 31 | expr = operand % ("*" | "/") % ("+" | "-") => { 32 | return tpl.binaryOp(true, self, (op, x, y) => { 33 | return &BinaryExpr{ 34 | X: x, 35 | OpPos: op.Pos, 36 | Op: op.Tok, 37 | Y: y, 38 | } 39 | }) 40 | } 41 | 42 | operand = basicLit | unaryExpr 43 | 44 | unaryExpr = "-" operand => { 45 | op := self[0].(*tpl.Token) 46 | return &UnaryExpr{ 47 | OpPos: op.Pos, 48 | Op: op.Tok, 49 | X: self[1], 50 | } 51 | } 52 | 53 | basicLit = INT | FLOAT => { 54 | op := self.(*tpl.Token) 55 | return &BasicLit{ 56 | ValuePos: op.Pos, 57 | Kind: op.Tok, 58 | Value: op.Lit, 59 | } 60 | } 61 | `! 62 | 63 | print "> " 64 | for line in os.Stdin { 65 | e, err := cl.parseExpr(line, nil) 66 | if err != nil { 67 | print err, "\n> " 68 | } else { 69 | print string(json.marshalIndent(e, "", " ")!), "\n> " 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /demo/tpl-intlist/ints.xgo: -------------------------------------------------------------------------------- 1 | import "xgo/tpl" 2 | 3 | cl := tpl` 4 | expr = INT % "," => { 5 | return tpl.ListOp[int](self, v => { 6 | return v.(*tpl.Token).Lit.int! 7 | }) 8 | } 9 | `! 10 | 11 | echo cl.parseExpr("1, 2, 3", nil)! 12 | -------------------------------------------------------------------------------- /demo/tpl-natural-lang/nlang.xgo: -------------------------------------------------------------------------------- 1 | import "os" 2 | 3 | cl := tpl` 4 | expr = subject verb object 5 | subject = "I" | "You" | "He" | "She" | "It" | "Dog" | "Cat" 6 | object = "me" | "you" | "him" | "her" | "it" | "fish" | "apple" | "banana" | "dog" | "cat" 7 | verb = "attack" | "love" | "eat" | "hate" 8 | `! 9 | 10 | print "> " 11 | for line in os.Stdin { 12 | e, err := cl.parseExpr(line, nil) 13 | if err != nil { 14 | print "${err}\n> " 15 | } else { 16 | print e, "\n > " 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /demo/tpl-parser-demo/demo.xgo: -------------------------------------------------------------------------------- 1 | cl := tpl`expr = *INT`! 2 | echo cl.parseExpr("1 2 3", nil)! 3 | 4 | cl = tpl`expr = INT % ","`! 5 | echo cl.parseExpr("1, 2, 3", nil)! 6 | 7 | cl = tpl`expr = INT % ("+" | "-")`! 8 | echo cl.parseExpr("1 + 2 - 3", nil)! 9 | -------------------------------------------------------------------------------- /demo/tpl-pseudo/gauss.pseudo: -------------------------------------------------------------------------------- 1 | DECLARE n, i, sum : INTEGER 2 | OUTPUT "please input n:" 3 | INPUT n 4 | i <- 1 5 | sum <- 0 6 | WHILE i <= n DO 7 | sum <- sum + i 8 | i <- i + 1 9 | ENDWHILE 10 | OUTPUT sum 11 | -------------------------------------------------------------------------------- /demo/typeasparamsfunc/col.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | ) 7 | 8 | func Gopx_Col[T any](name string) { 9 | fmt.Printf("%v: %s\n", reflect.TypeOf((*T)(nil)).Elem(), name) 10 | } 11 | -------------------------------------------------------------------------------- /demo/typeasparamsfunc/typeAsParamsFunc.xgo: -------------------------------------------------------------------------------- 1 | col string, "name" 2 | col int, "age" 3 | -------------------------------------------------------------------------------- /demo/typeasparamsmethod/col.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | ) 7 | 8 | type basetype interface { 9 | int | string 10 | } 11 | 12 | type Table struct { 13 | } 14 | 15 | func Gopt_Table_Gopx_Col__0[T basetype](p *Table, name string) { 16 | fmt.Printf("Gopt_Table_Gopx_Col__0 %v: %s\n", reflect.TypeOf((*T)(nil)).Elem(), name) 17 | } 18 | 19 | func Gopt_Table_Gopx_Col__1[Array any](p *Table, name string) { 20 | fmt.Printf("Gopt_Table_Gopx_Col__1 %v: %s\n", reflect.TypeOf((*Array)(nil)).Elem(), name) 21 | } 22 | -------------------------------------------------------------------------------- /demo/typeasparamsmethod/typeAsParamsMethod.xgo: -------------------------------------------------------------------------------- 1 | var tbl Table 2 | 3 | tbl.col int, "age" 4 | tbl.col bool, "male" 5 | -------------------------------------------------------------------------------- /demo/typeparamscast/foo.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type M = map[string]any 4 | 5 | type basetype interface { 6 | string | int | bool | float64 7 | } 8 | 9 | type Var__0[T basetype] struct { 10 | Val T 11 | } 12 | 13 | type Var__1[T map[string]any] struct { 14 | Val T 15 | } 16 | 17 | func Gopx_Var_Cast__0[T basetype]() *Var__0[T] { 18 | return new(Var__0[T]) 19 | } 20 | 21 | func Gopx_Var_Cast__1[T map[string]any]() *Var__1[T] { 22 | return new(Var__1[T]) 23 | } 24 | -------------------------------------------------------------------------------- /demo/typeparamscast/typecast.xgo: -------------------------------------------------------------------------------- 1 | println Var(int) 2 | -------------------------------------------------------------------------------- /demo/unit-test/foo.xgo: -------------------------------------------------------------------------------- 1 | func foo(v int) int { 2 | return v * 2 3 | } 4 | -------------------------------------------------------------------------------- /demo/unit-test/foo_test.gox: -------------------------------------------------------------------------------- 1 | if v := foo(50); v != 100 { 2 | t.error "foo(50) ret: ${v}" 3 | } 4 | 5 | t.run "foo -10", t => { 6 | if foo(-10) != -20 { 7 | t.fatal "foo(-10) != -20" 8 | } 9 | } 10 | 11 | t.run "foo 0", t => { 12 | if foo(0) != 0 { 13 | t.fatal "foo(0) != 0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /demo/unitliteral/unitlit.xgo: -------------------------------------------------------------------------------- 1 | import "time" 2 | 3 | echo "Let's wait for a second" 4 | time.sleep 1s 5 | echo "Hello, world" 6 | -------------------------------------------------------------------------------- /demo/xgo-calc/calc.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "math" 3 | "os" 4 | "xgo/ast" 5 | "xgo/parser" 6 | "xgo/token" 7 | ) 8 | 9 | func calc(e ast.Expr) float64 { 10 | switch e := e.(type) { 11 | case *ast.BasicLit: 12 | return e.Value.float! 13 | case *ast.BinaryExpr: 14 | switch e.Op { 15 | case token.ADD: 16 | return calc(e.X) + calc(e.Y) 17 | case token.SUB: 18 | return calc(e.X) - calc(e.Y) 19 | case token.MUL: 20 | return calc(e.X) * calc(e.Y) 21 | case token.QUO: 22 | return calc(e.X) + calc(e.Y) 23 | } 24 | case *ast.CallExpr: 25 | switch e.Fun.(*ast.Ident).Name { 26 | case "sin": 27 | return math.Sin(calc(e.Args[0])) 28 | case "cos": 29 | return math.Cos(calc(e.Args[0])) 30 | case "pow": 31 | return math.Pow(calc(e.Args[0]), calc(e.Args[1])) 32 | } 33 | case *ast.ParenExpr: 34 | return calc(e.X) 35 | case *ast.UnaryExpr: 36 | switch e.Op { 37 | case token.SUB: 38 | return -calc(e.X) 39 | } 40 | } 41 | panic("unknown expression") 42 | } 43 | 44 | print "> " 45 | for line in os.Stdin { 46 | e, err := parser.parseExpr(line) 47 | if err != nil { 48 | print "error: ${err}\n> " 49 | } else { 50 | print "${calc(e)}\n> " 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /demo/xgo-parser/parser.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "xgo/ast" 3 | "xgo/parser" 4 | ) 5 | 6 | e := parser.parseExpr("10 + 3.2")!.(*ast.BinaryExpr) 7 | echo e.X, e.Op, e.Y 8 | -------------------------------------------------------------------------------- /demo/xgo-sample/a.xgo: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func a() { 4 | println("a") 5 | } 6 | 7 | func Ab() { 8 | println("ab") 9 | } 10 | -------------------------------------------------------------------------------- /demo/xgo-sample/b.xgo: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | ab "github.com/goplus/xgo/demo/xgo-sample/cpkag/b" 5 | ) 6 | 7 | a() 8 | Ab() 9 | ab.Ab() 10 | -------------------------------------------------------------------------------- /demo/xgo-sample/cpkag/b/ab.go: -------------------------------------------------------------------------------- 1 | package ab 2 | 3 | func Ab() { 4 | println("ab") 5 | } 6 | -------------------------------------------------------------------------------- /demo/xgo-scanner/rpncalc/rpncalc.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "os" 3 | "xgo/scanner" 4 | "xgo/token" 5 | ) 6 | 7 | func calc(expr string) string { 8 | var vals []float64 9 | s := scanner.new(expr, nil, 0) 10 | for { 11 | pos, tok, lit := s.scan() 12 | switch tok { 13 | case token.INT, token.FLOAT: 14 | vals <- lit.float! 15 | case token.ADD, token.SUB, token.MUL, token.QUO: 16 | x := len(vals) - 1 17 | switch tok { 18 | case token.ADD: 19 | vals[x-1] += vals[x] 20 | case token.SUB: 21 | vals[x-1] -= vals[x] 22 | case token.MUL: 23 | vals[x-1] *= vals[x] 24 | case token.QUO: 25 | vals[x-1] /= vals[x] 26 | } 27 | vals = vals[:x] 28 | case token.EOF, token.SEMICOLON: 29 | return vals[0].string 30 | default: 31 | return "${pos}: invalid token ${tok}" 32 | } 33 | } 34 | } 35 | 36 | print "> " 37 | for line in os.Stdin { 38 | print "${calc(line)}\n> " 39 | } 40 | -------------------------------------------------------------------------------- /demo/xgo-scanner/scanner.xgo: -------------------------------------------------------------------------------- 1 | import "xgo/scanner" 2 | 3 | s := scanner.new("10 + 3.2", nil, 0) 4 | _, _, lit1 := s.scan() 5 | _, tok2, _ := s.scan() 6 | _, _, lit3 := s.scan() 7 | echo lit1, tok2, lit3 8 | -------------------------------------------------------------------------------- /demo/xgo-typeof/typeof.xgo: -------------------------------------------------------------------------------- 1 | echo type([1]), type("Hi") 2 | -------------------------------------------------------------------------------- /doc/_testdata/gopoFn/in.go: -------------------------------------------------------------------------------- 1 | package foo 2 | 3 | const GopPackage = true 4 | 5 | const Gopo_Add = "AddInt,,AddString" 6 | 7 | // Add doc 8 | func Add__1(a, b float64) float64 { 9 | return 0 10 | } 11 | 12 | // AddInt doc 13 | func AddInt(a, b int) int { 14 | return 0 15 | } 16 | 17 | // AddString doc 18 | func AddString(a, b string) string { 19 | return "" 20 | } 21 | -------------------------------------------------------------------------------- /doc/_testdata/gopoFn/out.expect: -------------------------------------------------------------------------------- 1 | == Func Add == 2 | Doc: AddInt doc 3 | 4 | func Add(a, b int) int 5 | == Func Add == 6 | Doc: Add doc 7 | 8 | func Add(a, b float64) float64 9 | == Func Add == 10 | Doc: AddString doc 11 | 12 | func Add(a, b string) string 13 | == Func AddInt == 14 | Doc: AddInt doc 15 | 16 | func AddInt(a, b int) int 17 | == Func AddString == 18 | Doc: AddString doc 19 | 20 | func AddString(a, b string) string 21 | -------------------------------------------------------------------------------- /doc/_testdata/gopoMethod/in.go: -------------------------------------------------------------------------------- 1 | package foo 2 | 3 | const GopPackage = true 4 | 5 | const Gopo__T__Gop_Add = ".AddInt,AddString" 6 | 7 | type T int 8 | 9 | // AddInt doc 10 | func (p T) AddInt(b int) *T { 11 | return nil 12 | } 13 | 14 | // AddString doc 15 | func AddString(this *T, b string) *T { 16 | return nil 17 | } 18 | -------------------------------------------------------------------------------- /doc/_testdata/gopoMethod/out.expect: -------------------------------------------------------------------------------- 1 | == Type T == 2 | - Func AddString - 3 | Doc: AddString doc 4 | 5 | func AddString(this *T, b string) *T 6 | - Method AddInt - 7 | Recv: T 8 | Doc: AddInt doc 9 | 10 | func (p T) AddInt(b int) *T 11 | - Method Gop_Add - 12 | Recv: T 13 | Doc: AddInt doc 14 | 15 | func (p T) Gop_Add(b int) *T 16 | - Method Gop_Add - 17 | Recv: *T 18 | Doc: AddString doc 19 | 20 | func (this *T) Gop_Add(b string) *T 21 | -------------------------------------------------------------------------------- /doc/_testdata/overloadFn/in.go: -------------------------------------------------------------------------------- 1 | package foo 2 | 3 | const GopPackage = true 4 | 5 | // Bar doc 6 | func Bar__0() { 7 | } 8 | -------------------------------------------------------------------------------- /doc/_testdata/overloadFn/out.expect: -------------------------------------------------------------------------------- 1 | == Func Bar == 2 | Doc: Bar doc 3 | 4 | func Bar() 5 | -------------------------------------------------------------------------------- /doc/_testdata/overloadMethod/in.go: -------------------------------------------------------------------------------- 1 | package foo 2 | 3 | const GopPackage = true 4 | 5 | type T int 6 | 7 | // Bar doc 1 8 | func (p *T) Bar__0() { 9 | } 10 | 11 | // Bar doc 2 12 | func (t T) Bar__1() { 13 | } 14 | -------------------------------------------------------------------------------- /doc/_testdata/overloadMethod/out.expect: -------------------------------------------------------------------------------- 1 | == Type T == 2 | - Method Bar - 3 | Recv: *T 4 | Doc: Bar doc 1 5 | 6 | func (p *T) Bar() 7 | - Method Bar - 8 | Recv: T 9 | Doc: Bar doc 2 10 | 11 | func (t T) Bar() 12 | -------------------------------------------------------------------------------- /doc/code-coverage.md: -------------------------------------------------------------------------------- 1 | XGo Unit Test: Code Coverage 2 | ===== 3 | 4 | TODO 5 | -------------------------------------------------------------------------------- /doc/images/dtl/image-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/goplus/xgo/0a0667c8ad573e65ffe6348ddb93df976578cd16/doc/images/dtl/image-1.png -------------------------------------------------------------------------------- /doc/images/dtl/image-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/goplus/xgo/0a0667c8ad573e65ffe6348ddb93df976578cd16/doc/images/dtl/image-2.png -------------------------------------------------------------------------------- /env/build.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package env 18 | 19 | // The value of variables come form 20 | // `go build -ldflags '-X "buildDate=xxxxx"` 21 | var ( 22 | buildDate string 23 | ) 24 | 25 | // BuildDate returns build date of the `xgo` command. 26 | func BuildDate() string { 27 | return buildDate 28 | } 29 | -------------------------------------------------------------------------------- /env/goenv.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package env 18 | 19 | import ( 20 | "os" 21 | ) 22 | 23 | // ----------------------------------------------------------------------------- 24 | 25 | func HOME() string { 26 | return os.Getenv(envHOME) 27 | } 28 | 29 | // ----------------------------------------------------------------------------- 30 | -------------------------------------------------------------------------------- /env/gop_nonwindows.go: -------------------------------------------------------------------------------- 1 | //go:build !windows 2 | // +build !windows 3 | 4 | /* 5 | * Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | package env 21 | 22 | func isXgoCmd(fname string) bool { 23 | return fname == "xgo" || fname == "gop" 24 | } 25 | -------------------------------------------------------------------------------- /env/sys_others.go: -------------------------------------------------------------------------------- 1 | //go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris 2 | // +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris 3 | 4 | /* 5 | * Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | package env 21 | 22 | const ( 23 | envHOME = "HOME" 24 | ) 25 | -------------------------------------------------------------------------------- /env/sys_plan9.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package env 18 | 19 | const ( 20 | envHOME = "home" 21 | ) 22 | -------------------------------------------------------------------------------- /env/sys_windows.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package env 18 | 19 | import "strings" 20 | 21 | const ( 22 | envHOME = "USERPROFILE" 23 | ) 24 | 25 | func isXgoCmd(fname string) bool { 26 | fname = strings.TrimSuffix(fname, ".exe") 27 | return fname == "xgo" || fname == "gop" 28 | } 29 | -------------------------------------------------------------------------------- /format/formatutil/_testdata/format/basic/in.data: -------------------------------------------------------------------------------- 1 | goto "a" 2 | 3 | // this is a comment 4 | 5 | // x comment 1 6 | // x comment 2 7 | // x comment 3 8 | func x() { 9 | } 10 | 11 | // call 12 | func(a func()) {}(nil) 13 | 14 | /* 15 | y comment 16 | */ 17 | func y() { 18 | } 19 | 20 | echo "Hi" 21 | -------------------------------------------------------------------------------- /format/formatutil/_testdata/format/basic/out.expect: -------------------------------------------------------------------------------- 1 | // this is a comment 2 | 3 | // x comment 1 4 | // x comment 2 5 | // x comment 3 6 | func x() { 7 | } 8 | 9 | /* 10 | y comment 11 | */ 12 | func y() { 13 | } 14 | 15 | goto "a" 16 | 17 | // call 18 | func(a func()) {}(nil) 19 | 20 | echo "Hi" 21 | -------------------------------------------------------------------------------- /format/formatutil/_testdata/format/nondecl/in.data: -------------------------------------------------------------------------------- 1 | const a = 0 2 | -------------------------------------------------------------------------------- /format/formatutil/_testdata/format/nondecl/out.expect: -------------------------------------------------------------------------------- 1 | const a = 0 2 | -------------------------------------------------------------------------------- /format/formatutil/_testdata/rearrange/noeol/in.data: -------------------------------------------------------------------------------- 1 | var a int -------------------------------------------------------------------------------- /format/formatutil/_testdata/rearrange/noeol/out.expect: -------------------------------------------------------------------------------- 1 | var a int -------------------------------------------------------------------------------- /format/formatutil/_testdata/rearrange/nondecl/in.data: -------------------------------------------------------------------------------- 1 | // this is a comment 2 | 3 | // x comment 1 4 | // x comment 2 5 | // x comment 3 6 | func x() { 7 | } 8 | 9 | /* 10 | y comment 11 | */ 12 | var y int 13 | -------------------------------------------------------------------------------- /format/formatutil/_testdata/rearrange/nondecl/out.expect: -------------------------------------------------------------------------------- 1 | // this is a comment 2 | 3 | // x comment 1 4 | // x comment 2 5 | // x comment 3 6 | func x() { 7 | } 8 | 9 | /* 10 | y comment 11 | */ 12 | var y int 13 | -------------------------------------------------------------------------------- /format/formatutil/_testdata/splitstmts/basic/in.data: -------------------------------------------------------------------------------- 1 | goto "a" 2 | 3 | // this is a comment 4 | 5 | // x comment 1 6 | // x comment 2 7 | // x comment 3 8 | func x() { 9 | } 10 | 11 | // call 12 | func(a func()) {}(nil) 13 | 14 | /* 15 | y comment 16 | */ 17 | func y() { 18 | } 19 | 20 | echo "Hi" 21 | -------------------------------------------------------------------------------- /format/formatutil/_testdata/splitstmts/basic/out.expect: -------------------------------------------------------------------------------- 1 | goto 2 | FUNC 3 | FNCALL 4 | FUNC 5 | IDENT 6 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/goplus/xgo 2 | 3 | go 1.18 4 | 5 | require ( 6 | github.com/fsnotify/fsnotify v1.9.0 7 | github.com/goplus/cobra v1.9.12 //gop:class 8 | github.com/goplus/gogen v1.18.1 9 | github.com/goplus/lib v0.2.0 10 | github.com/goplus/mod v0.17.0 11 | github.com/qiniu/x v1.15.0 12 | ) 13 | 14 | require ( 15 | golang.org/x/mod v0.20.0 // indirect 16 | golang.org/x/sys v0.21.0 // indirect 17 | ) 18 | 19 | retract v1.1.12 20 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= 2 | github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= 3 | github.com/goplus/cobra v1.9.12 h1:0F9EdEbeGyITGz+mqoHoJ5KpUw97p1CkxV74IexHw5s= 4 | github.com/goplus/cobra v1.9.12/go.mod h1:p4LhfNJDKEpiGjGiNn0crUXL5dUPA5DX2ztYpEJR34E= 5 | github.com/goplus/gogen v1.18.1 h1:ys18IiQwQ4QeQ3Mmk4vzql0Yfr6r+hkkQdjaL/M0Isc= 6 | github.com/goplus/gogen v1.18.1/go.mod h1:owX2e1EyU5WD+Nm6oH2m/GXjLdlBYcwkLO4wN8HHXZI= 7 | github.com/goplus/lib v0.2.0 h1:AjqkN1XK5H23wZMMlpaUYAMCDAdSBQ2NMFrLtSh7W4g= 8 | github.com/goplus/lib v0.2.0/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0= 9 | github.com/goplus/mod v0.17.0 h1:2rwBjNT/5a9fsZMkjBFYORxXzXldh/Oe/SAsZqCvP/I= 10 | github.com/goplus/mod v0.17.0/go.mod h1:5aGvHagL++m6XlBpqPfkeGqpkHl2biuWXyQcVxmpm3I= 11 | github.com/qiniu/x v1.15.0 h1:Tal7jtNNgBkFneJXG8qsTadVywC5zBQCfw9b12Igsr4= 12 | github.com/qiniu/x v1.15.0/go.mod h1:AiovSOCaRijaf3fj+0CBOpR1457pn24b0Vdb1JpwhII= 13 | golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0= 14 | golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= 15 | golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= 16 | golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 17 | -------------------------------------------------------------------------------- /make.bash: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | 3 | # 4 | # Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | set -ex 20 | 21 | go run cmd/make.go --install --autoproxy 22 | -------------------------------------------------------------------------------- /make.bat: -------------------------------------------------------------------------------- 1 | go run cmd/make.go --install --autoproxy 2 | -------------------------------------------------------------------------------- /parser/_instance/instance1/cmd.xgo: -------------------------------------------------------------------------------- 1 | type T1 P1[int] 2 | 3 | type T2 P2[int, string] 4 | 5 | type T3 *P1[P2[int, P1[int]]] 6 | 7 | type S1 struct { 8 | v1 P1[int] 9 | v2 P2[int, string] 10 | } 11 | 12 | type S2 struct { 13 | P1[int] 14 | } 15 | 16 | type A1 []P1[int] 17 | 18 | type A2 [2]P2[int, string] 19 | 20 | type M1 map[P1[int]]P2[int, string] 21 | 22 | type C1 chan P1[int] 23 | -------------------------------------------------------------------------------- /parser/_instance/instance2/cmd.xgo: -------------------------------------------------------------------------------- 1 | func fn1(P1[int], P2[int, string]) 2 | 3 | func fn2(p1 P1[int], p2 P2[int, string]) 4 | 5 | func fn3(p1 P1[int], p2 ...P2[int, string]) 6 | 7 | func fn4(p1 []P1[int], p3 []P2[int, string]) *P1[P2[int, P1[int]]] 8 | 9 | func fn5(p1 []P1[int], p2 [2]P2[int, string]) 10 | 11 | func fn6(p1 chan P1[int], p2 map[P1[int]]P2[int, string]) 12 | 13 | func fn7(p1 struct { 14 | v1 P1[int] 15 | v2 P2[int, string] 16 | }) 17 | -------------------------------------------------------------------------------- /parser/_instance/instance3/cmd.xgo: -------------------------------------------------------------------------------- 1 | println(P1[int]{}, P2[int, string]{}) 2 | println((*P1[int])(nil), (*P2[int, string])(nil)) 3 | 4 | println P1[int]{}, P2[int, string]{} 5 | println (*P1[int])(nil), (*P2[int, string])(nil) 6 | 7 | func(x P1[int], y *P2[int, string]) *int { 8 | return nil 9 | }(P1[int]{1}, &P2[int, string]{}) 10 | 11 | foo(=> P1[int]{1}) 12 | foo(=> { 13 | println(&P2[int, string]{}) 14 | }) 15 | 16 | fn1[int]([]P1[int]{P1[int]{0}, P1[int]{1}}, &P2[int, string]{}) 17 | fn2[int, string](&P1[int]{}, &P2[int, string]{}) 18 | 19 | foo1[int](=> P1[int]{1}) 20 | foo2[int, string](=> { 21 | println(&P2[int, string]{}) 22 | }) 23 | -------------------------------------------------------------------------------- /parser/_instance/instance4/cmd.xgo: -------------------------------------------------------------------------------- 1 | fn[int]() 2 | fn[[]int]() 3 | fn[struct { 4 | X int 5 | Y int 6 | }]() 7 | fn[map[int]string]() 8 | fn[chan int]() 9 | fn[chan struct{}]() 10 | fn[interface{}]() 11 | fn[interface{ Method() }]() 12 | fn[[]struct { 13 | X int 14 | Y int 15 | }, map[int]string]() 16 | -------------------------------------------------------------------------------- /parser/_instance/instance5/cmd.xgo: -------------------------------------------------------------------------------- 1 | var a [2]int 2 | 3 | if 0 < a[0] { 4 | println a 5 | println M[int]{1} 6 | println T[int]{a: 1, b: 2} 7 | } 8 | 9 | for _, i := range []int{7, 42} { 10 | println i 11 | } 12 | -------------------------------------------------------------------------------- /parser/_nofmt/cmdlinestyle1/cmd.xgo: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | changeYpos -0.7, 8 5 | changeYpos -0.7 6 | changeYpos - 0.7 7 | changeYpos-0.7 8 | x[1] 9 | x []... 10 | } 11 | -------------------------------------------------------------------------------- /parser/_nofmt/cmdlinestyle2/cmd2.xgo: -------------------------------------------------------------------------------- 1 | add(100, 200) (0).Test() 2 | -------------------------------------------------------------------------------- /parser/_nofmt/cmdlinestyle2/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file cmd2.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.SelectorExpr: 21 | X: 22 | ast.CallExpr: 23 | Fun: 24 | ast.CallExpr: 25 | Fun: 26 | ast.Ident: 27 | Name: add 28 | Args: 29 | ast.BasicLit: 30 | Kind: INT 31 | Value: 100 32 | ast.BasicLit: 33 | Kind: INT 34 | Value: 200 35 | Args: 36 | ast.BasicLit: 37 | Kind: INT 38 | Value: 0 39 | Sel: 40 | ast.Ident: 41 | Name: Test 42 | -------------------------------------------------------------------------------- /parser/_nofmt/cmdlinestyle3/cmd.xgo: -------------------------------------------------------------------------------- 1 | println (1+2i, 2) 2 | println (1, a...) 3 | println (a...) 4 | -------------------------------------------------------------------------------- /parser/_nofmt/exists/exists.xgo: -------------------------------------------------------------------------------- 1 | a := [1, 3, 5, 7, 8, 19] 2 | hasEven := {for x <- a if x%2 == 0} 3 | -------------------------------------------------------------------------------- /parser/_nofmt/forloop/forloop.xgo: -------------------------------------------------------------------------------- 1 | n := 0 2 | for range [1, 3, 5, 7, 11] { 3 | n++ 4 | } 5 | println("n:", n) 6 | 7 | for x := range [1] { 8 | } 9 | 10 | sum := 0 11 | for _, x := range [1, 3, 5, 7, 11] { 12 | if x > 3 { 13 | sum += x 14 | } 15 | } 16 | println("sum(1,3,5,7,11):", sum) 17 | 18 | sum = 0 19 | for i := 1; i < 100; i++ { 20 | sum += i 21 | } 22 | println("sum(1-100):", sum) 23 | 24 | for x <- [1] { 25 | println(x) 26 | } 27 | 28 | for i, x <- [1] if i%2 == 0 { 29 | println(i, x) 30 | } 31 | -------------------------------------------------------------------------------- /parser/_nofmt/listcompr/listcompr.xgo: -------------------------------------------------------------------------------- 1 | y := [x*x for x <- [1]] 2 | println(y) 3 | 4 | y = [x*x for x <- [1, 3, 5, 7, 11] if x > 3] 5 | println(y) 6 | 7 | z := [i+v for i, v <- [1, 3, 5, 7, 11] if t := i % 2; t == 1] 8 | println(z) 9 | 10 | println([k+","+s for k, s <- {"Hello": "xsw", "Hi": "XGo"}]) 11 | 12 | arr := [1, 2, 3, 4, 5, 6] 13 | x := [[a, b] for a <- arr if a < b for b <- arr if b > 2] 14 | println("x:", x) 15 | -------------------------------------------------------------------------------- /parser/_nofmt/matrix1/matrix.xgo: -------------------------------------------------------------------------------- 1 | echo [ 2 | 1, 2, 3 3 | 4, 5, 6 ] 4 | -------------------------------------------------------------------------------- /parser/_nofmt/matrix1/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file matrix.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: echo 22 | Args: 23 | ast.MatrixLit: 24 | Elts: 25 | ast.BasicLit: 26 | Kind: INT 27 | Value: 1 28 | ast.BasicLit: 29 | Kind: INT 30 | Value: 2 31 | ast.BasicLit: 32 | Kind: INT 33 | Value: 3 34 | ast.BasicLit: 35 | Kind: INT 36 | Value: 4 37 | ast.BasicLit: 38 | Kind: INT 39 | Value: 5 40 | ast.BasicLit: 41 | Kind: INT 42 | Value: 6 43 | NElt: 2 44 | -------------------------------------------------------------------------------- /parser/_nofmt/printvariadic/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file printv.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: println 22 | Args: 23 | ast.Ident: 24 | Name: x 25 | ast.ExprStmt: 26 | X: 27 | ast.CallExpr: 28 | Fun: 29 | ast.Ident: 30 | Name: println 31 | Args: 32 | ast.Ident: 33 | Name: y 34 | -------------------------------------------------------------------------------- /parser/_nofmt/printvariadic/printv.xgo: -------------------------------------------------------------------------------- 1 | println( 2 | x... 3 | ) 4 | println( 5 | y..., 6 | ) 7 | -------------------------------------------------------------------------------- /parser/_nofmt/rangeexpr1/rangeexpr.xgo: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | for i := range :10 { 5 | println(i) 6 | } 7 | 8 | for i <- 1:10 { 9 | println(i) 10 | } 11 | 12 | for i := range :10:2 { 13 | println(i) 14 | } 15 | 16 | for range :10 { 17 | println("Range expression") 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parser/_nofmt/selectdata/select.xgo: -------------------------------------------------------------------------------- 1 | a := [1, 3, 5, 7, 8, 19] 2 | y := {x for x <- a if x%2 == 0} 3 | -------------------------------------------------------------------------------- /parser/_testdata/append1/append.xgo: -------------------------------------------------------------------------------- 1 | a <- 1, 2, 3 2 | -------------------------------------------------------------------------------- /parser/_testdata/append1/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file append.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.SendStmt: 17 | Chan: 18 | ast.Ident: 19 | Name: a 20 | Values: 21 | ast.BasicLit: 22 | Kind: INT 23 | Value: 1 24 | ast.BasicLit: 25 | Kind: INT 26 | Value: 2 27 | ast.BasicLit: 28 | Kind: INT 29 | Value: 3 30 | -------------------------------------------------------------------------------- /parser/_testdata/append2/append.xgo: -------------------------------------------------------------------------------- 1 | a <- b... 2 | -------------------------------------------------------------------------------- /parser/_testdata/append2/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file append.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.SendStmt: 17 | Chan: 18 | ast.Ident: 19 | Name: a 20 | Values: 21 | ast.Ident: 22 | Name: b 23 | -------------------------------------------------------------------------------- /parser/_testdata/arrowop/arrowop.xgo: -------------------------------------------------------------------------------- 1 | echo 1+a -> b 2 | echo a <> b+1 3 | echo a -> b 4 | echo a <> b, "Hi" 5 | -------------------------------------------------------------------------------- /parser/_testdata/autoprop/goto.xgo: -------------------------------------------------------------------------------- 1 | L: 2 | goto(1, 2) + break(3, 4) + a.goto(6) 3 | goto L 4 | -------------------------------------------------------------------------------- /parser/_testdata/build/build.xgo: -------------------------------------------------------------------------------- 1 | type cstring string 2 | 3 | title := "Hello,world!2020-05-27" 4 | s := (*cstring)(&title) 5 | println(title[0 : len(title)-len("2006-01-02")]) 6 | -------------------------------------------------------------------------------- /parser/_testdata/c2gohello/hello.xgo: -------------------------------------------------------------------------------- 1 | import "C" 2 | 3 | C.printf c"Hello, world!\n" 4 | -------------------------------------------------------------------------------- /parser/_testdata/c2gohello/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file hello.xgo 4 | noEntrypoint 5 | ast.GenDecl: 6 | Tok: import 7 | Specs: 8 | ast.ImportSpec: 9 | Path: 10 | ast.BasicLit: 11 | Kind: STRING 12 | Value: "C" 13 | ast.FuncDecl: 14 | Name: 15 | ast.Ident: 16 | Name: main 17 | Type: 18 | ast.FuncType: 19 | Params: 20 | ast.FieldList: 21 | Body: 22 | ast.BlockStmt: 23 | List: 24 | ast.ExprStmt: 25 | X: 26 | ast.CallExpr: 27 | Fun: 28 | ast.SelectorExpr: 29 | X: 30 | ast.Ident: 31 | Name: C 32 | Sel: 33 | ast.Ident: 34 | Name: printf 35 | Args: 36 | ast.BasicLit: 37 | Kind: CSTRING 38 | Value: "Hello, world!\n" 39 | -------------------------------------------------------------------------------- /parser/_testdata/cmdlinestyle1/cmd.xgo: -------------------------------------------------------------------------------- 1 | println (1+2i)*2 2 | -------------------------------------------------------------------------------- /parser/_testdata/cmdlinestyle1/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file cmd.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: println 22 | Args: 23 | ast.BinaryExpr: 24 | X: 25 | ast.ParenExpr: 26 | X: 27 | ast.BinaryExpr: 28 | X: 29 | ast.BasicLit: 30 | Kind: INT 31 | Value: 1 32 | Op: + 33 | Y: 34 | ast.BasicLit: 35 | Kind: IMAG 36 | Value: 2i 37 | Op: * 38 | Y: 39 | ast.BasicLit: 40 | Kind: INT 41 | Value: 2 42 | -------------------------------------------------------------------------------- /parser/_testdata/cmdlinestyle2/cmd2.xgo: -------------------------------------------------------------------------------- 1 | x {} 2 | x{} 3 | -------------------------------------------------------------------------------- /parser/_testdata/cmdlinestyle2/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file cmd2.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: x 22 | Args: 23 | ast.CompositeLit: 24 | ast.ExprStmt: 25 | X: 26 | ast.CompositeLit: 27 | Type: 28 | ast.Ident: 29 | Name: x 30 | -------------------------------------------------------------------------------- /parser/_testdata/cmdlinestyle3/cmd3.xgo: -------------------------------------------------------------------------------- 1 | println &x 2 | println !x 3 | -------------------------------------------------------------------------------- /parser/_testdata/cmdlinestyle3/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file cmd3.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: println 22 | Args: 23 | ast.UnaryExpr: 24 | Op: & 25 | X: 26 | ast.Ident: 27 | Name: x 28 | ast.ExprStmt: 29 | X: 30 | ast.CallExpr: 31 | Fun: 32 | ast.Ident: 33 | Name: println 34 | Args: 35 | ast.UnaryExpr: 36 | Op: ! 37 | X: 38 | ast.Ident: 39 | Name: x 40 | -------------------------------------------------------------------------------- /parser/_testdata/cmdlinestyle4/cmd4.xgo: -------------------------------------------------------------------------------- 1 | func call(fn func(x int)) { fn(100) } 2 | 3 | call(func(x int) { println }) 4 | call(func(x int) { println x }) 5 | call(func(x int) { println {"x": 100, "y": 200}, x }) 6 | -------------------------------------------------------------------------------- /parser/_testdata/complit/complit.xgo: -------------------------------------------------------------------------------- 1 | a := [][]int{} 2 | println(a) 3 | -------------------------------------------------------------------------------- /parser/_testdata/complit/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file complit.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.AssignStmt: 17 | Lhs: 18 | ast.Ident: 19 | Name: a 20 | Tok: := 21 | Rhs: 22 | ast.CompositeLit: 23 | Type: 24 | ast.ArrayType: 25 | Elt: 26 | ast.ArrayType: 27 | Elt: 28 | ast.Ident: 29 | Name: int 30 | ast.ExprStmt: 31 | X: 32 | ast.CallExpr: 33 | Fun: 34 | ast.Ident: 35 | Name: println 36 | Args: 37 | ast.Ident: 38 | Name: a 39 | -------------------------------------------------------------------------------- /parser/_testdata/domainhuh/huh.xgo: -------------------------------------------------------------------------------- 1 | form := huh`> &ret, 10 2 |
3 | 4 | 10 | 11 |
12 | ` 13 | -------------------------------------------------------------------------------- /parser/_testdata/domaintext/tpl.xgo: -------------------------------------------------------------------------------- 1 | cl := tpl` 2 | expr = termExpr % ("+" | "-") 3 | 4 | termExpr = unaryExpr % ("*" | "/") 5 | 6 | unaryExpr = operand | "-" unaryExpr 7 | 8 | operand = INT | FLOAT | "(" expr ")" 9 | `! 10 | -------------------------------------------------------------------------------- /parser/_testdata/domaintpl/tpl.xgo: -------------------------------------------------------------------------------- 1 | cl := tpl` 2 | file = stmts => { 3 | return &ast.File{ 4 | Stmts: this.([]ast.Stmt), 5 | } 6 | } 7 | 8 | stmts = *(stmt ";") => { 9 | return [n.([]any)[0].(ast.Stmt) for n in this] 10 | } 11 | `! 12 | -------------------------------------------------------------------------------- /parser/_testdata/embedded1/embtype.xgo: -------------------------------------------------------------------------------- 1 | type T struct { 2 | abc.E 3 | } 4 | -------------------------------------------------------------------------------- /parser/_testdata/embedded1/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file embtype.xgo 4 | ast.GenDecl: 5 | Tok: type 6 | Specs: 7 | ast.TypeSpec: 8 | Name: 9 | ast.Ident: 10 | Name: T 11 | Type: 12 | ast.StructType: 13 | Fields: 14 | ast.FieldList: 15 | List: 16 | ast.Field: 17 | Type: 18 | ast.SelectorExpr: 19 | X: 20 | ast.Ident: 21 | Name: abc 22 | Sel: 23 | ast.Ident: 24 | Name: E 25 | -------------------------------------------------------------------------------- /parser/_testdata/envop1/envop.xgo: -------------------------------------------------------------------------------- 1 | echo ${name}, $id 2 | -------------------------------------------------------------------------------- /parser/_testdata/envop1/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file envop.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: echo 22 | Args: 23 | ast.EnvExpr: 24 | Name: 25 | ast.Ident: 26 | Name: name 27 | ast.EnvExpr: 28 | Name: 29 | ast.Ident: 30 | Name: id 31 | -------------------------------------------------------------------------------- /parser/_testdata/envop2/envop.xgo: -------------------------------------------------------------------------------- 1 | ${name} 2 | echo {"id": $id} 3 | -------------------------------------------------------------------------------- /parser/_testdata/envop2/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file envop.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.EnvExpr: 19 | Name: 20 | ast.Ident: 21 | Name: name 22 | ast.ExprStmt: 23 | X: 24 | ast.CallExpr: 25 | Fun: 26 | ast.Ident: 27 | Name: echo 28 | Args: 29 | ast.CompositeLit: 30 | Elts: 31 | ast.KeyValueExpr: 32 | Key: 33 | ast.BasicLit: 34 | Kind: STRING 35 | Value: "id" 36 | Value: 37 | ast.EnvExpr: 38 | Name: 39 | ast.Ident: 40 | Name: id 41 | -------------------------------------------------------------------------------- /parser/_testdata/errwrap1/errwrap.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "strconv" 3 | ) 4 | 5 | func add(x, y string) (int, error) { 6 | return strconv.Atoi(x)? + strconv.Atoi(y)?, nil 7 | } 8 | 9 | func addSafe(x, y string) int { 10 | return strconv.Atoi(x)?:0 + strconv.Atoi(y)?:0 11 | } 12 | 13 | println(`add("100", "23"):`, add("100", "23")!) 14 | 15 | sum, err := add("10", "abc") 16 | println(`add("10", "abc"):`, sum, err) 17 | 18 | println(`addSafe("10", "abc"):`, addSafe("10", "abc")) 19 | -------------------------------------------------------------------------------- /parser/_testdata/errwrap2/errwrap2.xgo: -------------------------------------------------------------------------------- 1 | func neg(x string) (int, error) { 2 | return -atoi(x)?, nil 3 | } 4 | -------------------------------------------------------------------------------- /parser/_testdata/errwrap2/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file errwrap2.xgo 4 | ast.FuncDecl: 5 | Name: 6 | ast.Ident: 7 | Name: neg 8 | Type: 9 | ast.FuncType: 10 | Params: 11 | ast.FieldList: 12 | List: 13 | ast.Field: 14 | Names: 15 | ast.Ident: 16 | Name: x 17 | Type: 18 | ast.Ident: 19 | Name: string 20 | Results: 21 | ast.FieldList: 22 | List: 23 | ast.Field: 24 | Type: 25 | ast.Ident: 26 | Name: int 27 | ast.Field: 28 | Type: 29 | ast.Ident: 30 | Name: error 31 | Body: 32 | ast.BlockStmt: 33 | List: 34 | ast.ReturnStmt: 35 | Results: 36 | ast.UnaryExpr: 37 | Op: - 38 | X: 39 | ast.ErrWrapExpr: 40 | X: 41 | ast.CallExpr: 42 | Fun: 43 | ast.Ident: 44 | Name: atoi 45 | Args: 46 | ast.Ident: 47 | Name: x 48 | Tok: ? 49 | ast.Ident: 50 | Name: nil 51 | -------------------------------------------------------------------------------- /parser/_testdata/errwrap3/errwrap3.xgo: -------------------------------------------------------------------------------- 1 | mkdir! "foo" 2 | println foo()!.fields 3 | -------------------------------------------------------------------------------- /parser/_testdata/errwrap3/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file errwrap3.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.ErrWrapExpr: 21 | X: 22 | ast.Ident: 23 | Name: mkdir 24 | Tok: ! 25 | Args: 26 | ast.BasicLit: 27 | Kind: STRING 28 | Value: "foo" 29 | ast.ExprStmt: 30 | X: 31 | ast.CallExpr: 32 | Fun: 33 | ast.Ident: 34 | Name: println 35 | Args: 36 | ast.SelectorExpr: 37 | X: 38 | ast.ErrWrapExpr: 39 | X: 40 | ast.CallExpr: 41 | Fun: 42 | ast.Ident: 43 | Name: foo 44 | Tok: ! 45 | Sel: 46 | ast.Ident: 47 | Name: fields 48 | -------------------------------------------------------------------------------- /parser/_testdata/exists/exists.xgo: -------------------------------------------------------------------------------- 1 | a := [1, 3, 5, 7, 8, 19] 2 | hasEven := {for x in a if x%2 == 0} 3 | -------------------------------------------------------------------------------- /parser/_testdata/fnbody/fnbody.xgo: -------------------------------------------------------------------------------- 1 | a := 1 2 | { 3 | type T = int 4 | var b = 2 5 | var c T = 3 6 | } 7 | -------------------------------------------------------------------------------- /parser/_testdata/fncall/fncall.xgo: -------------------------------------------------------------------------------- 1 | fn(1)(x) 2 | -------------------------------------------------------------------------------- /parser/_testdata/fncall/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file fncall.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.CallExpr: 21 | Fun: 22 | ast.Ident: 23 | Name: fn 24 | Args: 25 | ast.BasicLit: 26 | Kind: INT 27 | Value: 1 28 | Args: 29 | ast.Ident: 30 | Name: x 31 | -------------------------------------------------------------------------------- /parser/_testdata/forloop/forloop.xgo: -------------------------------------------------------------------------------- 1 | n := 0 2 | for range [1, 3, 5, 7, 11] { 3 | n++ 4 | } 5 | println("n:", n) 6 | 7 | for x := range [1] { 8 | } 9 | 10 | sum := 0 11 | for _, x := range [1, 3, 5, 7, 11] { 12 | if x > 3 { 13 | sum += x 14 | } 15 | } 16 | println("sum(1,3,5,7,11):", sum) 17 | 18 | sum = 0 19 | for i := 1; i < 100; i++ { 20 | sum += i 21 | } 22 | println("sum(1-100):", sum) 23 | 24 | for x in [1] { 25 | println(x) 26 | } 27 | 28 | for i, x in [1] if i%2 == 0 { 29 | println(i, x) 30 | } 31 | -------------------------------------------------------------------------------- /parser/_testdata/funcdecl1/fndecl.xgo: -------------------------------------------------------------------------------- 1 | func() (int, int) { 2 | return 1, 1 3 | }() 4 | -------------------------------------------------------------------------------- /parser/_testdata/funcdecl2/fndecl.xgo: -------------------------------------------------------------------------------- 1 | func() { 2 | }() 3 | -------------------------------------------------------------------------------- /parser/_testdata/funcdecl2/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file fndecl.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.FuncLit: 21 | Type: 22 | ast.FuncType: 23 | Params: 24 | ast.FieldList: 25 | Body: 26 | ast.BlockStmt: 27 | -------------------------------------------------------------------------------- /parser/_testdata/funcdecl3/fndecl.xgo: -------------------------------------------------------------------------------- 1 | func() int { 2 | return 1 3 | }() 4 | -------------------------------------------------------------------------------- /parser/_testdata/funcdecl3/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file fndecl.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.FuncLit: 21 | Type: 22 | ast.FuncType: 23 | Params: 24 | ast.FieldList: 25 | Results: 26 | ast.FieldList: 27 | List: 28 | ast.Field: 29 | Type: 30 | ast.Ident: 31 | Name: int 32 | Body: 33 | ast.BlockStmt: 34 | List: 35 | ast.ReturnStmt: 36 | Results: 37 | ast.BasicLit: 38 | Kind: INT 39 | Value: 1 40 | -------------------------------------------------------------------------------- /parser/_testdata/funcdoc/funcdoc.xgo: -------------------------------------------------------------------------------- 1 | package foo 2 | 3 | //go:noinline 4 | //go:uintptrescapes 5 | func test(s string, p, q uintptr, rest ...uintptr) int { 6 | } 7 | -------------------------------------------------------------------------------- /parser/_testdata/funclit/funclit.xgo: -------------------------------------------------------------------------------- 1 | func(x, y int) *int { 2 | return nil 3 | }(100, 200) 4 | 5 | println "hello" 6 | -------------------------------------------------------------------------------- /parser/_testdata/functype/dummy/dummy.md: -------------------------------------------------------------------------------- 1 | dummy file 2 | -------------------------------------------------------------------------------- /parser/_testdata/functype/functype.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | type T struct { 18 | *T 19 | A int `json:"a"` 20 | } 21 | 22 | func bar(v chan bool) (int, <-chan error) { 23 | v <- true 24 | <-v 25 | return 0, (<-chan error)(nil) 26 | } 27 | 28 | func foo(f func([]byte, *string, ...T) chan<- int) (v int, err error) { 29 | return 30 | } 31 | -------------------------------------------------------------------------------- /parser/_testdata/gmxtest/foo.gmx: -------------------------------------------------------------------------------- 1 | a := 1 2 | -------------------------------------------------------------------------------- /parser/_testdata/gmxtest/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file foo.gmx 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.AssignStmt: 17 | Lhs: 18 | ast.Ident: 19 | Name: a 20 | Tok: := 21 | Rhs: 22 | ast.BasicLit: 23 | Kind: INT 24 | Value: 1 25 | -------------------------------------------------------------------------------- /parser/_testdata/goto1/goto.xgo: -------------------------------------------------------------------------------- 1 | goto "a" 2 | goto 1, 2 3 | goto (1+2)*3 4 | -------------------------------------------------------------------------------- /parser/_testdata/goto2/goto.xgo: -------------------------------------------------------------------------------- 1 | goto x+y 2 | goto x, y 3 | -------------------------------------------------------------------------------- /parser/_testdata/goto2/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file goto.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: goto 22 | Args: 23 | ast.BinaryExpr: 24 | X: 25 | ast.Ident: 26 | Name: x 27 | Op: + 28 | Y: 29 | ast.Ident: 30 | Name: y 31 | ast.ExprStmt: 32 | X: 33 | ast.CallExpr: 34 | Fun: 35 | ast.Ident: 36 | Name: goto 37 | Args: 38 | ast.Ident: 39 | Name: x 40 | ast.Ident: 41 | Name: y 42 | -------------------------------------------------------------------------------- /parser/_testdata/goxtest1/bar.gox: -------------------------------------------------------------------------------- 1 | var ( 2 | A `json:"a"` 3 | *B 4 | x, y string 5 | C.A `json:"ca"` 6 | *C.B `json:"b"` 7 | v int `json:"v"` 8 | ) 9 | 10 | type A struct{} 11 | 12 | type B struct{} 13 | -------------------------------------------------------------------------------- /parser/_testdata/goxtest2/bar.gox: -------------------------------------------------------------------------------- 1 | var ( 2 | x.App 3 | ) 4 | -------------------------------------------------------------------------------- /parser/_testdata/goxtest2/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file bar.gox 4 | ast.GenDecl: 5 | Tok: var 6 | Specs: 7 | ast.ValueSpec: 8 | Type: 9 | ast.SelectorExpr: 10 | X: 11 | ast.Ident: 12 | Name: x 13 | Sel: 14 | ast.Ident: 15 | Name: App 16 | -------------------------------------------------------------------------------- /parser/_testdata/lambda1/lambda.xgo: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | foo(=> "Hi") 5 | foo(x => x * x) 6 | foo((x, y) => x + y) 7 | foo((x) => (x, x * 2)) 8 | foo(() => "Hi") 9 | } 10 | -------------------------------------------------------------------------------- /parser/_testdata/lambda2/lambda2.xgo: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | foo(=> { 5 | println("Hi") 6 | }) 7 | foo(x => { 8 | println(x) 9 | }) 10 | foo((x, y) => { 11 | println(x, y) 12 | }) 13 | } 14 | -------------------------------------------------------------------------------- /parser/_testdata/lambda3/lambda3.xgo: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | foo => { 5 | println "Hi" 6 | } 7 | foo x => { 8 | println x 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /parser/_testdata/lambda4/lambda4.xgo: -------------------------------------------------------------------------------- 1 | type Foo struct { 2 | Plot func(x float64) (float64, float64) 3 | } 4 | 5 | &Foo{ 6 | Plot: x => (x * 2, x * x), 7 | } 8 | 9 | &Foo{ 10 | Plot: x => { 11 | return x * 2, x * x 12 | }, 13 | } 14 | -------------------------------------------------------------------------------- /parser/_testdata/listcompr/listcompr.xgo: -------------------------------------------------------------------------------- 1 | y := [x*x for x in [1]] 2 | println(y) 3 | 4 | y = [x*x for x in [1, 3, 5, 7, 11] if x > 3] 5 | println(y) 6 | 7 | z := [i+v for i, v in [1, 3, 5, 7, 11] if t := i % 2; t == 1] 8 | println(z) 9 | 10 | println([k+","+s for k, s in {"Hello": "xsw", "Hi": "XGo"}]) 11 | 12 | arr := [1, 2, 3, 4, 5, 6] 13 | x := [[a, b] for a in arr if a < b for b in arr if b > 2] 14 | println("x:", x) 15 | -------------------------------------------------------------------------------- /parser/_testdata/mapfunc/map.xgo: -------------------------------------------------------------------------------- 1 | map strs, toUpper 2 | map ["hello", "world"], toUpper 3 | map[string]int{"Hi": 1} 4 | println map(strs, strings.ToUpper) 5 | -------------------------------------------------------------------------------- /parser/_testdata/matrix1/matrix.xgo: -------------------------------------------------------------------------------- 1 | echo [ 2 | 1, 2, 3 3 | 4, 5, 6 4 | ] 5 | -------------------------------------------------------------------------------- /parser/_testdata/matrix1/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file matrix.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: echo 22 | Args: 23 | ast.MatrixLit: 24 | Elts: 25 | ast.BasicLit: 26 | Kind: INT 27 | Value: 1 28 | ast.BasicLit: 29 | Kind: INT 30 | Value: 2 31 | ast.BasicLit: 32 | Kind: INT 33 | Value: 3 34 | ast.BasicLit: 35 | Kind: INT 36 | Value: 4 37 | ast.BasicLit: 38 | Kind: INT 39 | Value: 5 40 | ast.BasicLit: 41 | Kind: INT 42 | Value: 6 43 | NElt: 2 44 | -------------------------------------------------------------------------------- /parser/_testdata/matrix2/matrix.xgo: -------------------------------------------------------------------------------- 1 | echo [ 2 | 1, 2, 3 3 | row... 4 | 7, 8, 9 5 | ] 6 | -------------------------------------------------------------------------------- /parser/_testdata/overload1/overload.xgo: -------------------------------------------------------------------------------- 1 | func foo = ( 2 | func(a, b float64) float64 { 3 | return a + b 4 | } 5 | func(a, b string) string { 6 | return a + b 7 | } 8 | ) 9 | 10 | func bar = ( 11 | addComplex 12 | (T).add 13 | ) 14 | -------------------------------------------------------------------------------- /parser/_testdata/overload2/overload2.xgo: -------------------------------------------------------------------------------- 1 | func (T).* = ( 2 | mul1 3 | mul2 4 | ) 5 | 6 | func (T).add = ( 7 | add1 8 | func(a, b T) T { 9 | return a + b 10 | } 11 | ) 12 | -------------------------------------------------------------------------------- /parser/_testdata/overloadop/op_overload.xgo: -------------------------------------------------------------------------------- 1 | type foo struct { 2 | } 3 | 4 | func (a *foo) * (b *foo) *foo 5 | 6 | func (a *foo) + (b *foo) *foo { 7 | println("a + b") 8 | return &foo{} 9 | } 10 | 11 | func (a foo) / (b foo) foo { 12 | println("a / b") 13 | return foo{} 14 | } 15 | 16 | func -(a foo) { 17 | println("-a") 18 | } 19 | 20 | func ++(a foo) { 21 | println("a++") 22 | } 23 | -------------------------------------------------------------------------------- /parser/_testdata/printvariadic/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file printv.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: println 22 | Args: 23 | ast.Ident: 24 | Name: x 25 | ast.ExprStmt: 26 | X: 27 | ast.CallExpr: 28 | Fun: 29 | ast.Ident: 30 | Name: println 31 | Args: 32 | ast.Ident: 33 | Name: y 34 | -------------------------------------------------------------------------------- /parser/_testdata/printvariadic/printv.xgo: -------------------------------------------------------------------------------- 1 | println x... 2 | println y... 3 | -------------------------------------------------------------------------------- /parser/_testdata/pystr/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file pystr.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: print 22 | Args: 23 | ast.BasicLit: 24 | Kind: PYSTRING 25 | Value: "Hello" 26 | -------------------------------------------------------------------------------- /parser/_testdata/pystr/pystr.xgo: -------------------------------------------------------------------------------- 1 | print py"Hello" 2 | -------------------------------------------------------------------------------- /parser/_testdata/rangeexpr1/rangeexpr.xgo: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | for i := range :10 { 5 | println(i) 6 | } 7 | 8 | for i in 1:10 { 9 | println(i) 10 | } 11 | 12 | for i := range :10:2 { 13 | println(i) 14 | } 15 | 16 | for range :10 { 17 | println("Range expression") 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /parser/_testdata/rangeexpr2/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file rangeexpr.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.RangeStmt: 17 | Tok: ILLEGAL 18 | X: 19 | ast.RangeExpr: 20 | Last: 21 | ast.BasicLit: 22 | Kind: INT 23 | Value: 10 24 | Body: 25 | ast.BlockStmt: 26 | -------------------------------------------------------------------------------- /parser/_testdata/rangeexpr2/rangeexpr.xgo: -------------------------------------------------------------------------------- 1 | for :10 { 2 | } 3 | -------------------------------------------------------------------------------- /parser/_testdata/rangeexpr3/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file rangeexpr.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.RangeStmt: 17 | Tok: ILLEGAL 18 | X: 19 | ast.RangeExpr: 20 | First: 21 | ast.BasicLit: 22 | Kind: INT 23 | Value: 1 24 | Last: 25 | ast.BasicLit: 26 | Kind: INT 27 | Value: 10 28 | Body: 29 | ast.BlockStmt: 30 | -------------------------------------------------------------------------------- /parser/_testdata/rangeexpr3/rangeexpr.xgo: -------------------------------------------------------------------------------- 1 | for 1:10 { 2 | } 3 | -------------------------------------------------------------------------------- /parser/_testdata/rational/rational.xgo: -------------------------------------------------------------------------------- 1 | a := 1r << 65 // bigint, large than int64 2 | b := 4/5r // bigrat 3 | c := b - 1/3r + 3*1/2r 4 | println(a, b, c) 5 | -------------------------------------------------------------------------------- /parser/_testdata/selectdata/select.xgo: -------------------------------------------------------------------------------- 1 | a := [1, 3, 5, 7, 8, 19] 2 | y := {x for x in a if x%2 == 0} 3 | -------------------------------------------------------------------------------- /parser/_testdata/slice1/slice.xgo: -------------------------------------------------------------------------------- 1 | println([1][0]) 2 | println([1][0:1]) 3 | println([1, 2][0:1:5]) 4 | 5 | a := [1] 6 | println(a[0]) 7 | -------------------------------------------------------------------------------- /parser/_testdata/slice2/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file slice2.xgo 4 | ast.FuncDecl: 5 | Name: 6 | ast.Ident: 7 | Name: main 8 | Type: 9 | ast.FuncType: 10 | Params: 11 | ast.FieldList: 12 | Body: 13 | ast.BlockStmt: 14 | List: 15 | ast.AssignStmt: 16 | Lhs: 17 | ast.Ident: 18 | Name: a 19 | Tok: := 20 | Rhs: 21 | ast.SliceLit: 22 | Elts: 23 | ast.BasicLit: 24 | Kind: INT 25 | Value: 1 26 | ast.BasicLit: 27 | Kind: INT 28 | Value: 2 29 | ast.BasicLit: 30 | Kind: INT 31 | Value: 3 32 | ast.ExprStmt: 33 | X: 34 | ast.CallExpr: 35 | Fun: 36 | ast.Ident: 37 | Name: println 38 | Args: 39 | ast.Ident: 40 | Name: a 41 | -------------------------------------------------------------------------------- /parser/_testdata/slice2/slice2.xgo: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | a := [ 5 | 1, 6 | 2, 7 | 3, 8 | ] 9 | println(a) 10 | } 11 | -------------------------------------------------------------------------------- /parser/_testdata/spxtest/foo.spx: -------------------------------------------------------------------------------- 1 | a := 1 2 | -------------------------------------------------------------------------------- /parser/_testdata/spxtest/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file foo.spx 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.AssignStmt: 17 | Lhs: 18 | ast.Ident: 19 | Name: a 20 | Tok: := 21 | Rhs: 22 | ast.BasicLit: 23 | Kind: INT 24 | Value: 1 25 | -------------------------------------------------------------------------------- /parser/_testdata/staticmthd1/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file static_method.xgo 4 | ast.FuncDecl: 5 | Recv: 6 | ast.FieldList: 7 | List: 8 | ast.Field: 9 | Type: 10 | ast.Ident: 11 | Name: T 12 | Name: 13 | ast.Ident: 14 | Name: foo 15 | Type: 16 | ast.FuncType: 17 | Params: 18 | ast.FieldList: 19 | List: 20 | ast.Field: 21 | Names: 22 | ast.Ident: 23 | Name: a 24 | ast.Ident: 25 | Name: b 26 | Type: 27 | ast.Ident: 28 | Name: int 29 | Results: 30 | ast.FieldList: 31 | List: 32 | ast.Field: 33 | Type: 34 | ast.Ident: 35 | Name: string 36 | Body: 37 | ast.BlockStmt: 38 | -------------------------------------------------------------------------------- /parser/_testdata/staticmthd1/static_method.xgo: -------------------------------------------------------------------------------- 1 | func T.foo(a, b int) string { 2 | } 3 | -------------------------------------------------------------------------------- /parser/_testdata/staticmthd2/a.gox: -------------------------------------------------------------------------------- 1 | func .New() *T { 2 | } 3 | -------------------------------------------------------------------------------- /parser/_testdata/staticmthd2/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file a.gox 4 | ast.FuncDecl: 5 | Recv: 6 | ast.FieldList: 7 | Name: 8 | ast.Ident: 9 | Name: New 10 | Type: 11 | ast.FuncType: 12 | Params: 13 | ast.FieldList: 14 | Results: 15 | ast.FieldList: 16 | List: 17 | ast.Field: 18 | Type: 19 | ast.StarExpr: 20 | X: 21 | ast.Ident: 22 | Name: T 23 | Body: 24 | ast.BlockStmt: 25 | -------------------------------------------------------------------------------- /parser/_testdata/stdtype/stdtype.xgo: -------------------------------------------------------------------------------- 1 | package bar 2 | 3 | import "io" 4 | 5 | x := []float64{1, 3.4, 5} 6 | y := map[string]float64{"Hello": 1, "xsw": 3.4} 7 | println("x:", x, "y:", y) 8 | 9 | a := [...]float64{1, 3.4, 5} 10 | b := [...]float64{1, 3: 3.4, 5} 11 | c := []float64{2: 1.2, 3, 6: 4.5} 12 | println("a:", a, "b:", b, "c:", c) 13 | -------------------------------------------------------------------------------- /parser/_testdata/stringex1/string_lit.xgo: -------------------------------------------------------------------------------- 1 | println "$" 2 | println "$$" 3 | println "a$$b$" 4 | println "a$$b$$" 5 | println "a$b$%" 6 | -------------------------------------------------------------------------------- /parser/_testdata/stringex2/string_lit.xgo: -------------------------------------------------------------------------------- 1 | println "${" 2 | println "${ b }" 3 | println "a${" 4 | println "a${b}c" 5 | -------------------------------------------------------------------------------- /parser/_testdata/stringex3/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file string_lit.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: println 22 | Args: 23 | ast.BasicLit: 24 | Kind: STRING 25 | Value: "file:${args[0]}?${query}" 26 | Extra: 27 | file: 28 | ast.IndexExpr: 29 | X: 30 | ast.Ident: 31 | Name: args 32 | Index: 33 | ast.BasicLit: 34 | Kind: INT 35 | Value: 0 36 | ? 37 | ast.Ident: 38 | Name: query 39 | -------------------------------------------------------------------------------- /parser/_testdata/stringex3/string_lit.xgo: -------------------------------------------------------------------------------- 1 | println "file:${args[0]}?${query}" 2 | -------------------------------------------------------------------------------- /parser/_testdata/typeof/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file typeof.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: echo 22 | Args: 23 | ast.CallExpr: 24 | Fun: 25 | ast.Ident: 26 | Name: type 27 | Args: 28 | ast.BasicLit: 29 | Kind: INT 30 | Value: 1 31 | ast.CallExpr: 32 | Fun: 33 | ast.Ident: 34 | Name: type 35 | Args: 36 | ast.BasicLit: 37 | Kind: STRING 38 | Value: "Hi" 39 | -------------------------------------------------------------------------------- /parser/_testdata/typeof/typeof.xgo: -------------------------------------------------------------------------------- 1 | echo type(1), type("Hi") 2 | -------------------------------------------------------------------------------- /parser/_testdata/typeswitch/typeswitch.xgo: -------------------------------------------------------------------------------- 1 | func add(v interface{}) { 2 | switch a := v.(type) { 3 | case int, interface{ Foo() }: 4 | case *string: 5 | default: 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /parser/_testdata/unit/parser.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | file step.xgo 4 | noEntrypoint 5 | ast.FuncDecl: 6 | Name: 7 | ast.Ident: 8 | Name: main 9 | Type: 10 | ast.FuncType: 11 | Params: 12 | ast.FieldList: 13 | Body: 14 | ast.BlockStmt: 15 | List: 16 | ast.ExprStmt: 17 | X: 18 | ast.CallExpr: 19 | Fun: 20 | ast.Ident: 21 | Name: wait 22 | Args: 23 | ast.NumberUnitLit: 24 | Kind: INT 25 | Value: 1 26 | Unit: µs 27 | -------------------------------------------------------------------------------- /parser/_testdata/unit/step.xgo: -------------------------------------------------------------------------------- 1 | wait 1µs 2 | -------------------------------------------------------------------------------- /parser/_testexpr/lambda/in.xgo: -------------------------------------------------------------------------------- 1 | => { 2 | return this 3 | } 4 | -------------------------------------------------------------------------------- /parser/_testexpr/lambda/out.expect: -------------------------------------------------------------------------------- 1 | ast.LambdaExpr2: 2 | Body: 3 | ast.BlockStmt: 4 | List: 5 | ast.ReturnStmt: 6 | Results: 7 | ast.Ident: 8 | Name: this 9 | -------------------------------------------------------------------------------- /printer/_testdata/02-Var-and-operator/var_and_op.xgo: -------------------------------------------------------------------------------- 1 | x := 123.1 - 3i 2 | y, z := "Hello, ", 123 3 | println(y+"complex:", x+1, "int:", z) 4 | -------------------------------------------------------------------------------- /printer/_testdata/03-Import-go-package/import.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "fmt" 3 | "strings" 4 | ) 5 | 6 | x := strings.NewReplacer("?", "!").Replace("hello, world???") 7 | fmt.Println("x:", x) 8 | -------------------------------------------------------------------------------- /printer/_testdata/04-Func/func.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "fmt" 3 | "strings" 4 | ) 5 | 6 | func foo(x string) string { 7 | return strings.NewReplacer("?", "!").Replace(x) 8 | } 9 | 10 | func printf(format string, args ...interface{}) (n int, err error) { 11 | n, err = fmt.Printf(format, args...) 12 | return 13 | } 14 | 15 | func bar(foo func(string, ...interface{}) (int, error)) { 16 | foo("Hello, %v!\n", "XGo") 17 | } 18 | 19 | bar(printf) 20 | fmt.Println(foo("Hello, world???")) 21 | fmt.Println(printf("Hello, %v\n", "XGo")) 22 | -------------------------------------------------------------------------------- /printer/_testdata/05-Closure/closure.xgo: -------------------------------------------------------------------------------- 1 | import "fmt" 2 | 3 | var x = "Hello, world!" 4 | 5 | foo := func(prompt string) (n int, err error) { 6 | n, err = fmt.Println(prompt + x) 7 | return 8 | } 9 | 10 | fmt.Println(foo("x: ")) 11 | 12 | printf := func(format string, args ...interface{}) (n int, err error) { 13 | n, err = fmt.Printf(format, args...) 14 | return 15 | } 16 | 17 | bar := func(foo func(string, ...interface{}) (int, error)) { 18 | foo("Hello, %v!\n", "XGo") 19 | } 20 | 21 | bar(printf) 22 | fmt.Println(printf("Hello, %v\n", "XGo")) 23 | -------------------------------------------------------------------------------- /printer/_testdata/06-String-Map-Array-Slice/datastruct.xgo: -------------------------------------------------------------------------------- 1 | x := []float64{1, 3.4, 5} 2 | y := map[string]float64{"Hello": 1, "xsw": 3.4} 3 | 4 | a := [...]float64{1, 3.4, 5} 5 | b := [...]float64{1, 3: 3.4, 5} 6 | c := []float64{2: 1.2, 3, 6: 4.5} 7 | 8 | x[1], y["xsw"] = 1.7, 2.8 9 | println("x:", x, "y:", y) 10 | println(`x[1]:`, x[1], `y["xsw"]:`, y["xsw"], `a[1]:`, a[1]) 11 | 12 | i := uint16(4) 13 | b[uint32(4)], c[i] = 123, 1.7 14 | println("a:", a, "b:", b, "c:", c) 15 | 16 | arr := [...]float64{1, 2} 17 | title := "Hello,world!" + "2020-05-27" 18 | println(title[:len(title)-len("2006-01-02")], len(arr), arr[1:]) 19 | -------------------------------------------------------------------------------- /printer/_testdata/07-MapLit/maplit.xgo: -------------------------------------------------------------------------------- 1 | x := {"Hello": 1, "xsw": 3.4} // map[string]float64 2 | println("x:", x) 3 | 4 | y := {"Hello": 1, "xsw": "XGo"} // map[string]any 5 | println("y:", y) 6 | 7 | println({"Hello": 1, "xsw": 3}) // map[string]int 8 | println({1: 1.4, 3: "XGo"}) // map[int]any 9 | 10 | println("empty map:", {}) // map[string]any 11 | -------------------------------------------------------------------------------- /printer/_testdata/08-SliceLit/slicelit.xgo: -------------------------------------------------------------------------------- 1 | x := [1, 3.4] // []float64 2 | println("x:", x) 3 | 4 | y := [1] // []int 5 | println("y:", y) 6 | 7 | z := [1+2i, "xsw"] // []interface{} 8 | println("z:", z) 9 | 10 | println([1, 3.4, 3+4i]) // []complex128 11 | println([5+6i]) // []complex128 12 | println(["xsw", 3]) // []interface{} 13 | 14 | println("empty slice:", []) // []interface{} 15 | -------------------------------------------------------------------------------- /printer/_testdata/09-IfElse-SwitchCase/flow.xgo: -------------------------------------------------------------------------------- 1 | x := 0 2 | if t := false; t { 3 | x = 3 4 | } else { 5 | x = 5 6 | } 7 | println("x:", x) 8 | 9 | x = 0 10 | switch s := "Hello"; s { 11 | default: 12 | x = 7 13 | case "world", "hi": 14 | x = 5 15 | case "xsw": 16 | x = 3 17 | } 18 | println("x:", x) 19 | 20 | v := "Hello" 21 | switch { 22 | case v == "xsw": 23 | x = 3 24 | case v == "Hello", v == "world": 25 | x = 9 26 | default: 27 | x = 7 28 | } 29 | println("x:", x) 30 | 31 | v = "Hello" 32 | switch { 33 | case v == "xsw": 34 | x = 3 35 | case v == "hi", v == "world": 36 | x = 9 37 | default: 38 | x = 11 39 | } 40 | println("x:", x) 41 | 42 | switch v { 43 | case "Hello": 44 | println(v) 45 | fallthrough 46 | case "hi": 47 | println(v) 48 | fallthrough 49 | default: 50 | println(v) 51 | } 52 | 53 | z := 3 54 | switch { 55 | case z < 10: 56 | println(z) 57 | fallthrough 58 | case z == 10: 59 | println(z) 60 | fallthrough 61 | case z > 10: 62 | println(z) 63 | fallthrough 64 | default: 65 | println(z) 66 | } 67 | -------------------------------------------------------------------------------- /printer/_testdata/10-List-comprehension/list_comprehens.xgo: -------------------------------------------------------------------------------- 1 | y := [x*x for x in [1, 3, 5, 7, 11]] 2 | println(y) 3 | 4 | y = [x*x for x in [1, 3, 5, 7, 11] if x > 3] 5 | println(y) 6 | 7 | z := [i+v for i, v in [1, 3, 5, 7, 11] if i%2 == 1] 8 | println(z) 9 | 10 | println([k+","+s for k, s in {"Hello": "xsw", "Hi": "XGo"}]) 11 | 12 | arr := [1, 2, 3, 4, 5, 6] 13 | x := [[a, b] for a in arr if a < b for b in arr if b > 2] 14 | println("x:", x) 15 | -------------------------------------------------------------------------------- /printer/_testdata/11-Map-comprehension/map_comprehens.xgo: -------------------------------------------------------------------------------- 1 | y := {x: i for i, x in [1, 3, 5, 7, 11]} 2 | println(y) 3 | 4 | y = {x: i for i, x in [1, 3, 5, 7, 11] if i%2 == 1} 5 | println(y) 6 | 7 | z := {v: k for k, v in {1: "Hello", 3: "Hi", 5: "xsw", 7: "XGo"} if k > 3} 8 | println(z) 9 | -------------------------------------------------------------------------------- /printer/_testdata/12-Select-comprehension/select.xgo: -------------------------------------------------------------------------------- 1 | a := [2, 3, 5, 7, 9, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59] 2 | where := {i for i, v in a if v == 19} 3 | println("19 is at index", where) 4 | 5 | if at, ok := {i for i, v in a if v == 37}; ok { 6 | println("37 is found! index =", at) 7 | } 8 | 9 | println("first element of a is:", {v for v in a}) 10 | -------------------------------------------------------------------------------- /printer/_testdata/12-Select-comprehension2/findscore.xgo: -------------------------------------------------------------------------------- 1 | type student struct { 2 | name string 3 | score int 4 | } 5 | 6 | func findScore(a []student, name string) (score int, ok bool) { 7 | return {x.score for x in a if x.name == name} 8 | } 9 | 10 | a := [student{"ken", 95}, student{"john", 90}, student{"tom", 58}] 11 | println(findScore(a, "tom")) 12 | -------------------------------------------------------------------------------- /printer/_testdata/13-Exists-comprehension/exists.xgo: -------------------------------------------------------------------------------- 1 | type student struct { 2 | name string 3 | score int 4 | } 5 | 6 | a := [student{"du", 84}, student{"wang", 70}, student{"ken", 100}] 7 | 8 | hasFullMark := {for x in a if x.score == 100} 9 | println("is any student full mark:", hasFullMark) 10 | 11 | hasFailed := {for x in a if x.score < 60} 12 | println("is any student failed:", hasFailed) 13 | -------------------------------------------------------------------------------- /printer/_testdata/14-Using-goplus-in-Go/foo/foo.xgo: -------------------------------------------------------------------------------- 1 | package foo 2 | 3 | func ReverseMap(m map[string]int) map[int]string { 4 | return {v: k for k, v in m} 5 | } 6 | -------------------------------------------------------------------------------- /printer/_testdata/14-Using-goplus-in-Go/foo/foo_test.xgo: -------------------------------------------------------------------------------- 1 | package foo 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestReverseMap(t *testing.T) { 8 | out := ReverseMap({"a": 1}) 9 | if len(out) != 1 || out[1] != "a" { 10 | t.Fatal("ReverseMap failed:", out) 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /printer/_testdata/14-Using-goplus-in-Go/foo/footest_test.xgo: -------------------------------------------------------------------------------- 1 | package foo_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/goplus/xgo/tutorial/14-Using-goplus-in-Go/foo" 7 | ) 8 | 9 | func TestReverseMap(t *testing.T) { 10 | out := foo.ReverseMap({"b": 2}) 11 | if len(out) != 1 || out[2] != "b" { 12 | t.Fatal("ReverseMap failed:", out) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /printer/_testdata/15-ErrWrap/err_wrap.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "strconv" 3 | ) 4 | 5 | func add(x, y string) (int, error) { 6 | return strconv.Atoi(x)? + strconv.Atoi(y)?, nil 7 | } 8 | 9 | func addSafe(x, y string) int { 10 | return strconv.Atoi(x)?:0 + strconv.Atoi(y)?:0 11 | } 12 | 13 | println(`add("100", "23"):`, add("100", "23")!) 14 | 15 | sum, err := add("10", "abc") 16 | println(`add("10", "abc"):`, sum, err) 17 | 18 | println(`addSafe("10", "abc"):`, addSafe("10", "abc")) 19 | -------------------------------------------------------------------------------- /printer/_testdata/16-Fib/fib.xgo: -------------------------------------------------------------------------------- 1 | func fib(n int) int { 2 | if n == 0 { 3 | return 0 4 | } 5 | if n == 1 { 6 | return 1 7 | } 8 | return fib(n-1) + fib(n-2) 9 | } 10 | 11 | println `fib(27):`, fib(27) 12 | -------------------------------------------------------------------------------- /printer/_testdata/17-Fibtc/fibtc.xgo: -------------------------------------------------------------------------------- 1 | func fibtc(n, a, b int) int { 2 | if n == 0 { 3 | return a 4 | } 5 | if n == 1 { 6 | return b 7 | } 8 | return fibtc(n-1, b, a+b) 9 | } 10 | 11 | println `fibtc(27):`, fibtc(27, 0, 1) 12 | -------------------------------------------------------------------------------- /printer/_testdata/18-Rational/rational.xgo: -------------------------------------------------------------------------------- 1 | import "math/big" 2 | 3 | var a bigint = 1r << 65 // bigint, large than int64 4 | var b bigrat = 4/5r // bigrat 5 | c := b - 1/3r + 3*1/2r // bigrat 6 | println(a, b, c) 7 | 8 | var x *big.Int = 1r << 65 // (1r << 65) is untyped bigint, and can be assigned to *big.Int 9 | var y *big.Rat = 4/5r 10 | println(x, y) 11 | 12 | a = new(big.Int).Abs(-265r) 13 | println("abs(-265r):", a) 14 | -------------------------------------------------------------------------------- /printer/_testdata/21-Break-continue-goto/flow.xgo: -------------------------------------------------------------------------------- 1 | println("start") 2 | 3 | goto L 4 | println("before") 5 | L: 6 | println("over") 7 | i := 0 8 | L2: 9 | if i < 3 { 10 | println(i) 11 | i++ 12 | goto L2 13 | } 14 | println("over") 15 | 16 | sum := 0 17 | arr := [1, 3, 5, 7, 11, 13, 17] 18 | for i = 0; i < len(arr); i++ { 19 | if arr[i] < 3 { 20 | continue 21 | } 22 | if arr[i] > 11 { 23 | break 24 | } 25 | sum += arr[i] 26 | } 27 | println("sum(3,5,7,11):", sum == 26, sum) 28 | sum = 0 29 | L3: 30 | for i = 0; i < len(arr); i++ { 31 | if arr[i] < 3 { 32 | continue L3 33 | } 34 | if arr[i] > 11 { 35 | break L3 36 | } 37 | sum += arr[i] 38 | } 39 | println("sum(3,5,7,11):", sum == 26, sum) 40 | 41 | z := 3 42 | v := "Hello" 43 | switch z { 44 | case 3: 45 | if v == "Hello" { 46 | println("break") 47 | break 48 | } 49 | println("break fail") 50 | default: 51 | println(z) 52 | } 53 | L4: 54 | switch z { 55 | case 3: 56 | if v == "Hello" { 57 | println("break") 58 | break L4 59 | } 60 | println("break fail") 61 | default: 62 | println(z) 63 | } 64 | -------------------------------------------------------------------------------- /printer/_testdata/23-Defer/defer.xgo: -------------------------------------------------------------------------------- 1 | func f() (x int) { 2 | defer func() { 3 | x = 3 4 | }() 5 | return 1 6 | } 7 | 8 | func g() (x int) { 9 | defer func() { 10 | x = 3 11 | }() 12 | x = 1 13 | return 14 | } 15 | 16 | func h() (x int) { 17 | for i in [3, 2, 1] { 18 | v := i 19 | defer func() { 20 | x = v 21 | }() 22 | } 23 | return 24 | } 25 | 26 | println("f-x:", f()) 27 | println("g-x:", g()) 28 | println("h-x:", h()) 29 | 30 | defer println(println("Hello, defer")) 31 | println("Hello, XGo") 32 | -------------------------------------------------------------------------------- /printer/_testdata/24-Goroutine/goroutine.xgo: -------------------------------------------------------------------------------- 1 | import "time" 2 | 3 | go func() { 4 | println("Hello, goroutine!") 5 | }() 6 | 7 | time.Sleep(1e8) 8 | -------------------------------------------------------------------------------- /printer/_testdata/25-Struct/struct.xgo: -------------------------------------------------------------------------------- 1 | a := struct { 2 | A int `json:"a"` // comment a 3 | B string `b` // comment b 4 | }{1, "Hello"} 5 | 6 | println(a) 7 | 8 | b := &struct { 9 | A int // a 10 | B string 11 | }{1, "Hello"} 12 | 13 | c := &struct { 14 | a int 15 | b string 16 | }{1, "Hello"} 17 | 18 | println(b) 19 | 20 | a.A, a.B = 1, "Hi" 21 | println(a) 22 | 23 | b.A, b.B = 2, "Hi2" 24 | println(b) 25 | 26 | c.a, c.b = 3, "Hi3" 27 | println(c) 28 | -------------------------------------------------------------------------------- /printer/_testdata/26-Method/method.xgo: -------------------------------------------------------------------------------- 1 | type Person struct { 2 | Name string 3 | Age int 4 | Friends []string 5 | } 6 | 7 | func (p *Person) SetName(name string) { 8 | p.Name = name 9 | println(p.Name) 10 | } 11 | 12 | func (p *Person) SetAge(age int) { 13 | age = age + 5 14 | p.Age = age 15 | println(p.Age) 16 | } 17 | 18 | func (p *Person) AddFriends(args ...string) { 19 | p.Friends = append(p.Friends, args...) 20 | } 21 | 22 | type M int 23 | 24 | func (m M) Foo() { 25 | println("foo", m) 26 | } 27 | 28 | p := Person{ 29 | Name: "bar", 30 | Age: 30, 31 | } 32 | 33 | p.Name, p.Age = "bar2", 31 34 | 35 | p.SetName("foo") 36 | p.SetAge(32) 37 | p.AddFriends("a", "b", "c") 38 | 39 | a := int32(0) 40 | m := M(a) 41 | m.Foo() 42 | 43 | println(p.Name) 44 | println(p.Age) 45 | println(p.Friends) 46 | println(m) 47 | -------------------------------------------------------------------------------- /printer/_testdata/27-Func-Set/func.xgo: -------------------------------------------------------------------------------- 1 | func A(a *int, c *struct { 2 | b *int 3 | m map[string]*int 4 | s []*int 5 | }) { 6 | *a = 5 7 | *c.b = 3 8 | *c.m["foo"] = 7 9 | *c.s[0] = 9 10 | } 11 | 12 | func Index() int { 13 | return 0 14 | } 15 | 16 | a1 := 6 17 | a2 := 6 18 | a3 := 6 19 | c := struct { 20 | b *int 21 | m map[string]*int 22 | s []*int 23 | }{ 24 | b: &a1, 25 | m: map[string]*int{ 26 | "foo": &a2, 27 | }, 28 | s: []*int{&a3}, 29 | } 30 | A(&a1, &c) 31 | *c.m["foo"] = 8 32 | *c.s[0] = 10 33 | *c.s[Index()] = 11 34 | println(a1, *c.b, *c.m["foo"], *c.s[0]) 35 | -------------------------------------------------------------------------------- /printer/_testdata/28-Chan/chan.xgo: -------------------------------------------------------------------------------- 1 | c := make(chan int, 10) 2 | c <- 3 3 | close(c) 4 | 5 | d := <-c 6 | e, ok := <-c 7 | 8 | println(d) 9 | println(e, ok) 10 | -------------------------------------------------------------------------------- /printer/_testdata/29-CompareToNil/ref.xgo: -------------------------------------------------------------------------------- 1 | func foo() []int { 2 | return nil 3 | } 4 | 5 | func foo1() map[int]int { 6 | return make(map[int]int, 10) 7 | } 8 | 9 | func foo2() chan int { 10 | return make(chan int, 10) 11 | } 12 | 13 | func foo3() *int { 14 | return nil 15 | } 16 | 17 | println(foo() == nil) 18 | println(nil == foo()) 19 | println(foo() != nil) 20 | println(nil != foo()) 21 | 22 | println(foo1() == nil) 23 | println(nil == foo1()) 24 | println(foo1() != nil) 25 | println(nil != foo1()) 26 | 27 | println(foo2() == nil) 28 | println(nil == foo2()) 29 | println(foo2() != nil) 30 | println(nil != foo2()) 31 | 32 | println(foo3() == nil) 33 | println(nil == foo3()) 34 | println(foo3() != nil) 35 | println(nil != foo3()) 36 | -------------------------------------------------------------------------------- /printer/_testdata/30-Recover/recover.xgo: -------------------------------------------------------------------------------- 1 | defer func() { 2 | var err interface{} 3 | if err = recover(); err != nil { 4 | println(err) 5 | } 6 | }() 7 | 8 | panic("hello recover") 9 | -------------------------------------------------------------------------------- /printer/_testdata/31-Builtin-Typecast/builtin_and_typecast.xgo: -------------------------------------------------------------------------------- 1 | n := 2 2 | a := make([]int, uint64(n)) 3 | a = append(a, 1, 2, 3) 4 | println(a) 5 | println("len:", len(a)) 6 | 7 | b := make([]int, 0, uint16(4)) 8 | c := [1, 2, 3] 9 | b = append(b, c...) 10 | println(b) 11 | println("len:", len(b), "cap:", cap(b)) 12 | -------------------------------------------------------------------------------- /printer/_testdata/32-Import-gop-package/import_gop_pkg.xgo: -------------------------------------------------------------------------------- 1 | import "github.com/goplus/xgo/tutorial/14-Using-goplus-in-Go/foo" 2 | 3 | rmap := foo.ReverseMap({"Hi": 1, "Hello": 2}) 4 | println(rmap) 5 | -------------------------------------------------------------------------------- /printer/_testdata/33-Interface/shape.xgo: -------------------------------------------------------------------------------- 1 | import "math" 2 | 3 | type Shape interface { 4 | Area() float64 5 | } 6 | 7 | type Rect struct { 8 | x, y, w, h float64 9 | } 10 | 11 | func (p *Rect) Area() float64 { 12 | return p.w * p.h 13 | } 14 | 15 | type Circle struct { 16 | x, y, r float64 17 | } 18 | 19 | func (p *Circle) Area() float64 { 20 | return math.Pi * p.r * p.r 21 | } 22 | 23 | func Area(shapes ...Shape) float64 { 24 | s := 0.0 25 | for shape in shapes { 26 | s += shape.Area() 27 | } 28 | return s 29 | } 30 | 31 | rect := &Rect{0, 0, 2, 5} 32 | circle := &Circle{0, 0, 3} 33 | println("area:", Area(circle, rect)) 34 | -------------------------------------------------------------------------------- /printer/_testdata/34-Type-assert/type_assert.xgo: -------------------------------------------------------------------------------- 1 | func foo(v interface{}) string { 2 | if _, ok := v.(bool); ok { 3 | return "bool" 4 | } 5 | switch v.(type) { 6 | case int: 7 | return "int" 8 | case string: 9 | return "string" 10 | default: 11 | return "unknown" 12 | } 13 | } 14 | 15 | func add(v, delta interface{}) interface{} { 16 | switch a := v.(type) { 17 | case int: 18 | return a + delta.(int) 19 | case float64: 20 | return a + delta.(float64) 21 | case string: 22 | return a + delta.(string) 23 | } 24 | return nil 25 | } 26 | 27 | println(foo(1), foo("Hi")) 28 | println(add(4, 3), add("n", "iu")) 29 | -------------------------------------------------------------------------------- /printer/_testdata/35-Chan-select/select.xgo: -------------------------------------------------------------------------------- 1 | var done = make(chan bool, 1) 2 | var exited = make(chan bool, 1) 3 | 4 | func consume(xchg chan int) { 5 | for { 6 | select { 7 | case c := <-xchg: 8 | println(c) 9 | case <-done: 10 | println("done!") 11 | exited <- true 12 | return 13 | } 14 | } 15 | } 16 | 17 | func product(xchg chan int, from chan int) { 18 | for x in from { 19 | xchg <- x 20 | } 21 | done <- true 22 | } 23 | 24 | from := make(chan int, 10) 25 | xchg := make(chan int, 1) 26 | go consume(xchg) 27 | go product(xchg, from) 28 | for i := 1; i <= 10; i++ { 29 | from <- i 30 | } 31 | close(from) 32 | <-exited 33 | -------------------------------------------------------------------------------- /printer/_testdata/36-Auto-Property/autoprop.xgo: -------------------------------------------------------------------------------- 1 | import ( 2 | "gop/ast/goptest" 3 | ) 4 | 5 | script := ` 6 | import ( 7 | gio "io" 8 | ) 9 | 10 | func New() (*Bar, error) { 11 | return nil, gio.EOF 12 | } 13 | 14 | bar, err := New() 15 | if err != nil { 16 | log.Println(err) 17 | } 18 | ` 19 | 20 | doc := goptest.New(script)! 21 | 22 | println(doc.any.funcDecl.name) 23 | println(doc.any.importSpec.name) 24 | -------------------------------------------------------------------------------- /printer/_testdata/37-Cmdline/cmdline.xgo: -------------------------------------------------------------------------------- 1 | import "os" 2 | 3 | println("args:", os.Args[1:]) 4 | -------------------------------------------------------------------------------- /printer/_testdata/38-Overload-operator/overload_op.xgo: -------------------------------------------------------------------------------- 1 | import "math/big" 2 | 3 | type MyBigInt struct { 4 | *big.Int 5 | } 6 | 7 | func Int(v *big.Int) MyBigInt { 8 | return MyBigInt{v} 9 | } 10 | 11 | func (a MyBigInt) + (b MyBigInt) MyBigInt { 12 | return MyBigInt{new(big.Int).Add(a.Int, b.Int)} 13 | } 14 | 15 | func (a MyBigInt) += (b MyBigInt) { 16 | a.Int.Add(a.Int, b.Int) 17 | } 18 | 19 | func -(a MyBigInt) MyBigInt { 20 | return MyBigInt{new(big.Int).Neg(a.Int)} 21 | } 22 | 23 | a := Int(1r) 24 | a += Int(2r) 25 | println(a + Int(3r)) 26 | println(-a) 27 | -------------------------------------------------------------------------------- /printer/_testdata/39-Lambda-expression/lambda.xgo: -------------------------------------------------------------------------------- 1 | func Map(c []float64, t func(float64) float64) []float64 { 2 | return [t(x) for x in c] 3 | } 4 | 5 | println(Map([1.2, 3.5, 6], x => x * x)) 6 | -------------------------------------------------------------------------------- /printer/_testdata/40-Deduce-struct-type/deduce.xgo: -------------------------------------------------------------------------------- 1 | type Config struct { 2 | Dir string 3 | Level int 4 | } 5 | 6 | type Result struct { 7 | Text string 8 | } 9 | 10 | func foo(conf *Config) *Result { 11 | println("conf:", *conf) 12 | return {Text: "Hello, XGo"} 13 | } 14 | 15 | ret := foo({Dir: "/foo/bar", Level: 1}) 16 | println(ret) 17 | -------------------------------------------------------------------------------- /printer/_testdata/41-UDT-RangeForEach/udt_range.xgo: -------------------------------------------------------------------------------- 1 | type foo struct { 2 | } 3 | 4 | func (p *foo) Gop_Enum(proc func(key int, val string)) { 5 | proc(3, "Hi") 6 | proc(7, "XGo") 7 | } 8 | 9 | for k, v in new(foo) { 10 | println(k, v) 11 | } 12 | 13 | println({v: k for k, v in new(foo)}) 14 | -------------------------------------------------------------------------------- /printer/_testdata/42-UDT-RangeIterator/udt_range_iter.xgo: -------------------------------------------------------------------------------- 1 | type fooIter struct { 2 | data *foo 3 | idx int 4 | } 5 | 6 | func (p *fooIter) Next() (key int, val string, ok bool) { 7 | if p.idx < len(p.data.key) { 8 | key, val, ok = p.data.key[p.idx], p.data.val[p.idx], true 9 | p.idx++ 10 | } 11 | return 12 | } 13 | 14 | type foo struct { 15 | key []int 16 | val []string 17 | } 18 | 19 | func newFoo() *foo { 20 | return &foo{key: [3, 7], val: ["Hi", "XGo"]} 21 | } 22 | 23 | func (p *foo) Gop_Enum() *fooIter { 24 | return &fooIter{data: p} 25 | } 26 | 27 | obj := newFoo() 28 | for k, v in obj { 29 | println(k, v) 30 | } 31 | 32 | println({v: k for k, v in obj}) 33 | -------------------------------------------------------------------------------- /printer/_testdata/43-RangeExpr/rangeexpr.xgo: -------------------------------------------------------------------------------- 1 | println "---------------------------" 2 | 3 | for i in :10 { 4 | println(i) 5 | } 6 | 7 | println "---------------------------" 8 | 9 | for i in 9:-1:-1 { 10 | println(i) 11 | } 12 | 13 | println "---------------------------" 14 | 15 | for i := range :10:2 { 16 | println(i) 17 | } 18 | 19 | println "---------------------------" 20 | 21 | for i := range 1:10:3 { 22 | println(i) 23 | } 24 | 25 | println "---------------------------" 26 | 27 | for range :10 { 28 | println("Range expression") 29 | } 30 | 31 | println "---------------------------" 32 | -------------------------------------------------------------------------------- /token/internal/tokenutil/lines_go121.go: -------------------------------------------------------------------------------- 1 | //go:build go1.21 2 | // +build go1.21 3 | 4 | /* 5 | * Copyright (c) 2023 The XGo Authors (xgo.dev). All rights reserved. 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | package tokenutil 21 | 22 | import ( 23 | "go/token" 24 | ) 25 | 26 | // Lines returns the effective line offset table of the form described by SetLines. 27 | // Callers must not mutate the result. 28 | func Lines(f *token.File) []int { 29 | return f.Lines() 30 | } 31 | -------------------------------------------------------------------------------- /token/internal/tokenutil/lines_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package tokenutil 18 | 19 | import ( 20 | "go/token" 21 | "reflect" 22 | "testing" 23 | ) 24 | 25 | func TestLines(t *testing.T) { 26 | fset := token.NewFileSet() 27 | f := fset.AddFile("foo.go", 100, 100) 28 | lines := []int{0, 10, 50} 29 | f.SetLines(lines) 30 | ret := Lines(f) 31 | if !reflect.DeepEqual(ret, lines) { 32 | t.Fatal("TestLines failed:", ret) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tpl/encoding/csv/csv.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package csv 18 | 19 | import ( 20 | "encoding/csv" 21 | "strings" 22 | ) 23 | 24 | // New creates a new csv object from a string 25 | func New(text string) (records [][]string, err error) { 26 | return csv.NewReader(strings.NewReader(text)).ReadAll() 27 | } 28 | -------------------------------------------------------------------------------- /tpl/encoding/json/json.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package json 18 | 19 | import ( 20 | "encoding/json" 21 | "strings" 22 | ) 23 | 24 | // New creates a new json object from a string 25 | func New(text string) (ret any, err error) { 26 | err = json.NewDecoder(strings.NewReader(text)).Decode(&ret) 27 | return 28 | } 29 | -------------------------------------------------------------------------------- /tpl/encoding/regexp/regexp.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package regexp 18 | 19 | import ( 20 | "regexp" 21 | ) 22 | 23 | // New creates a new regexp object from a string 24 | func New(text string) (*regexp.Regexp, error) { 25 | return regexp.Compile(text) 26 | } 27 | -------------------------------------------------------------------------------- /tpl/encoding/regexposix/regexp.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package regexposix 18 | 19 | import ( 20 | "regexp" 21 | ) 22 | 23 | // New creates a new POSIX regexp object from a string 24 | func New(text string) (*regexp.Regexp, error) { 25 | return regexp.CompilePOSIX(text) 26 | } 27 | -------------------------------------------------------------------------------- /tpl/encoding/xml/xml.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package xml 18 | 19 | import ( 20 | "encoding/xml" 21 | "strings" 22 | ) 23 | 24 | // New creates a new xml object from a string 25 | func New(text string) (ret any, err error) { 26 | err = xml.NewDecoder(strings.NewReader(text)).Decode(&ret) 27 | return 28 | } 29 | -------------------------------------------------------------------------------- /tpl/parser/_testdata/adjoin/in.xgo: -------------------------------------------------------------------------------- 1 | doc = IDENT ++ RAWSTRING | IDENT SPACE "{" "}" 2 | -------------------------------------------------------------------------------- /tpl/parser/_testdata/adjoin/out.expect: -------------------------------------------------------------------------------- 1 | ast.Rule: 2 | Name: 3 | ast.Ident: 4 | Name: doc 5 | Expr: 6 | ast.Choice: 7 | Options: 8 | ast.BinaryExpr: 9 | X: 10 | ast.Ident: 11 | Name: IDENT 12 | Op: ++ 13 | Y: 14 | ast.Ident: 15 | Name: RAWSTRING 16 | ast.Sequence: 17 | Items: 18 | ast.Ident: 19 | Name: IDENT 20 | ast.Ident: 21 | Name: SPACE 22 | ast.BasicLit: 23 | Kind: STRING 24 | Value: "{" 25 | ast.BasicLit: 26 | Kind: STRING 27 | Value: "}" 28 | -------------------------------------------------------------------------------- /tpl/parser/_testdata/simple1/in.xgo: -------------------------------------------------------------------------------- 1 | expr = termExpr | expr ("+" | "-") expr 2 | 3 | termExpr = unaryExpr | termExpr ("*" | "/") termExpr 4 | 5 | unaryExpr = operand | "-" unaryExpr 6 | 7 | operand = INT | FLOAT | "(" expr ")" 8 | -------------------------------------------------------------------------------- /tpl/parser/_testdata/simple2/in.xgo: -------------------------------------------------------------------------------- 1 | expr = termExpr % ("+" | "-") 2 | 3 | termExpr = unaryExpr % ("*" | "/") 4 | 5 | unaryExpr = operand | "-" unaryExpr 6 | 7 | operand = INT | FLOAT | "(" expr ")" 8 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/cstr/go.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT c 2 | 2 . 3 | 3 IDENT printf 4 | 10 IDENT c 5 | 11 STRING "Hello" 6 | 18 ; 7 | 8 | 19 IDENT std 9 | 22 . 10 | 23 IDENT print 11 | 29 IDENT py 12 | 31 STRING "Hello" 13 | 38 ; 14 | 15 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/cstr/gop.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT c 2 | 2 . 3 | 3 IDENT printf 4 | 10 CSTRING "Hello" 5 | 18 ; 6 | 7 | 19 IDENT std 8 | 22 . 9 | 23 IDENT print 10 | 29 PYSTRING "Hello" 11 | 38 ; 12 | 13 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/cstr/in.xgo: -------------------------------------------------------------------------------- 1 | c.printf c"Hello" 2 | std.print py"Hello" 3 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/cstr/tpl.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT c 2 | 2 . 3 | 3 IDENT printf 4 | 10 IDENT c 5 | 11 STRING "Hello" 6 | 18 ; 7 | 8 | 19 IDENT std 9 | 22 . 10 | 23 IDENT print 11 | 29 IDENT py 12 | 31 STRING "Hello" 13 | 38 ; 14 | 15 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/num/go.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT echo 2 | 6 INT 1_002 3 | 11 ; 4 | 5 | 12 IDENT echo 6 | 17 INT 2 7 | 18 + 8 | 19 IMAG 3i 9 | 21 ; 10 | 11 | 22 IDENT echo 12 | 27 FLOAT 2. 13 | 29 IDENT string 14 | 35 ; 15 | 16 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/num/gop.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT echo 2 | 6 INT 1_002 3 | 11 ; 4 | 5 | 12 IDENT echo 6 | 17 INT 2 7 | 18 + 8 | 19 IMAG 3i 9 | 21 ; 10 | 11 | 22 IDENT echo 12 | 27 FLOAT 2. 13 | 29 UNIT string 14 | 35 ; 15 | 16 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/num/in.xgo: -------------------------------------------------------------------------------- 1 | echo 1_002 2 | echo 2+3i 3 | echo 2.string 4 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/num/tpl.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT echo 2 | 6 INT 1_002 3 | 11 ; 4 | 5 | 12 IDENT echo 6 | 17 INT 2 7 | 18 + 8 | 19 IMAG 3i 9 | 21 ; 10 | 11 | 22 IDENT echo 12 | 27 FLOAT 2. 13 | 29 UNIT string 14 | 35 ; 15 | 16 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/pow/go.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT echo 2 | 6 INT 2 3 | 7 * 4 | 8 * 5 | 9 INT 3 6 | 10 ; 7 | 8 | 11 IDENT echo 9 | 16 STRING `abc` 10 | 21 [ 11 | 22 INT 0 12 | 23 ] 13 | 24 ; 14 | 15 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/pow/gop.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT echo 2 | 6 INT 2 3 | 7 * 4 | 8 * 5 | 9 INT 3 6 | 10 ; 7 | 8 | 11 IDENT echo 9 | 16 STRING `abc` 10 | 21 [ 11 | 22 INT 0 12 | 23 ] 13 | 24 ; 14 | 15 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/pow/in.xgo: -------------------------------------------------------------------------------- 1 | echo 2**3 2 | echo `abc`[0] 3 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/pow/tpl.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT echo 2 | 6 INT 2 3 | 7 ** 4 | 9 INT 3 5 | 10 ; 6 | 7 | 11 IDENT echo 8 | 16 STRING `abc` 9 | 21 [ 10 | 22 INT 0 11 | 23 ] 12 | 24 ; 13 | 14 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/rat/go.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT echo 2 | 6 INT 1 3 | 7 / 4 | 8 INT 3 5 | 9 IDENT r 6 | 10 ; 7 | 8 | 11 IDENT echo 9 | 16 FLOAT 1.23 10 | 20 IDENT r 11 | 21 ; 12 | 13 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/rat/gop.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT echo 2 | 6 INT 1 3 | 7 / 4 | 8 RAT 3r 5 | 10 ; 6 | 7 | 11 IDENT echo 8 | 16 RAT 1.23r 9 | 21 ; 10 | 11 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/rat/in.xgo: -------------------------------------------------------------------------------- 1 | echo 1/3r 2 | echo 1.23r 3 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/rat/tpl.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT echo 2 | 6 INT 1 3 | 7 / 4 | 8 RAT 3r 5 | 10 ; 6 | 7 | 11 IDENT echo 8 | 16 RAT 1.23r 9 | 21 ; 10 | 11 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/unit/go.expect: -------------------------------------------------------------------------------- 1 | 1 import import 2 | 8 STRING "time" 3 | 14 ; 4 | 5 | 16 IDENT x 6 | 18 := 7 | 21 STRING "Let's wait for a second" 8 | 46 ; 9 | 10 | 47 IDENT echo 11 | 52 IDENT x 12 | 53 ; 13 | 14 | 55 IDENT wait 15 | 60 INT 0x10 16 | 64 IDENT s 17 | 65 ; 18 | 19 | 66 IDENT wait 20 | 71 FLOAT .1 21 | 73 IDENT s 22 | 74 ; 23 | 24 | 75 IDENT wait 25 | 80 FLOAT 1. 26 | 82 IDENT s 27 | 83 ; 28 | 29 | 84 IDENT wait 30 | 89 FLOAT 1.e7 31 | 93 IDENT s 32 | 94 ; 33 | 34 | 95 IDENT wait 35 | 100 FLOAT 1.2 36 | 103 IDENT s 37 | 104 ; 38 | 39 | 106 IDENT time 40 | 110 . 41 | 111 IDENT sleep 42 | 117 IMAG 100i 43 | 121 IDENT m 44 | 122 ; 45 | 46 | 124 IDENT spend 47 | 130 FLOAT 10.0 48 | 134 IDENT rmb 49 | 137 ; 50 | 51 | 138 IDENT spend 52 | 144 FLOAT 10. 53 | 147 IDENT rmb 54 | 150 ; 55 | 56 | 152 IDENT echo 57 | 157 STRING "Hello, world" 58 | 171 ... 59 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/unit/gop.expect: -------------------------------------------------------------------------------- 1 | 1 import import 2 | 8 STRING "time" 3 | 14 ; 4 | 5 | 16 IDENT x 6 | 18 := 7 | 21 STRING "Let's wait for a second" 8 | 46 ; 9 | 10 | 47 IDENT echo 11 | 52 IDENT x 12 | 53 ; 13 | 14 | 55 IDENT wait 15 | 60 INT 0x10 16 | 64 UNIT s 17 | 65 ; 18 | 19 | 66 IDENT wait 20 | 71 FLOAT .1 21 | 73 UNIT s 22 | 74 ; 23 | 24 | 75 IDENT wait 25 | 80 FLOAT 1. 26 | 82 UNIT s 27 | 83 ; 28 | 29 | 84 IDENT wait 30 | 89 FLOAT 1.e7 31 | 93 UNIT s 32 | 94 ; 33 | 34 | 95 IDENT wait 35 | 100 FLOAT 1.2 36 | 103 UNIT s 37 | 104 ; 38 | 39 | 106 IDENT time 40 | 110 . 41 | 111 IDENT sleep 42 | 117 INT 100 43 | 120 UNIT im 44 | 122 ; 45 | 46 | 124 IDENT spend 47 | 130 FLOAT 10.0 48 | 134 UNIT rmb 49 | 137 ; 50 | 51 | 138 IDENT spend 52 | 144 FLOAT 10. 53 | 147 UNIT rmb 54 | 150 ; 55 | 56 | 152 IDENT echo 57 | 157 STRING "Hello, world" 58 | 171 ... 59 | 174 ; 60 | 61 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/unit/in.xgo: -------------------------------------------------------------------------------- 1 | import "time" 2 | 3 | x := "Let's wait for a second" 4 | echo x 5 | 6 | wait 0x10s 7 | wait .1s 8 | wait 1.s 9 | wait 1.e7s 10 | wait 1.2s 11 | 12 | time.sleep 100im 13 | 14 | spend 10.0rmb 15 | spend 10.rmb 16 | 17 | echo "Hello, world"... 18 | -------------------------------------------------------------------------------- /tpl/scanner/_testdata/unit/tpl.expect: -------------------------------------------------------------------------------- 1 | 1 IDENT import 2 | 8 STRING "time" 3 | 14 ; 4 | 5 | 16 IDENT x 6 | 18 := 7 | 21 STRING "Let's wait for a second" 8 | 46 ; 9 | 10 | 47 IDENT echo 11 | 52 IDENT x 12 | 53 ; 13 | 14 | 55 IDENT wait 15 | 60 INT 0x10 16 | 64 UNIT s 17 | 65 ; 18 | 19 | 66 IDENT wait 20 | 71 FLOAT .1 21 | 73 UNIT s 22 | 74 ; 23 | 24 | 75 IDENT wait 25 | 80 FLOAT 1. 26 | 82 UNIT s 27 | 83 ; 28 | 29 | 84 IDENT wait 30 | 89 FLOAT 1.e7 31 | 93 UNIT s 32 | 94 ; 33 | 34 | 95 IDENT wait 35 | 100 FLOAT 1.2 36 | 103 UNIT s 37 | 104 ; 38 | 39 | 106 IDENT time 40 | 110 . 41 | 111 IDENT sleep 42 | 117 INT 100 43 | 120 UNIT im 44 | 122 ; 45 | 46 | 124 IDENT spend 47 | 130 FLOAT 10.0 48 | 134 UNIT rmb 49 | 137 ; 50 | 51 | 138 IDENT spend 52 | 144 FLOAT 10. 53 | 147 UNIT rmb 54 | 150 ; 55 | 56 | 152 IDENT echo 57 | 157 STRING "Hello, world" 58 | 171 ... 59 | 174 ; 60 | 61 | -------------------------------------------------------------------------------- /tpl/types/types.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package types 18 | 19 | import ( 20 | "github.com/goplus/xgo/tpl/token" 21 | ) 22 | 23 | // A Token is a lexical unit returned by Scan. 24 | type Token struct { 25 | Tok token.Token 26 | Pos token.Pos 27 | Lit string 28 | } 29 | 30 | // String returns the string representation of a token. 31 | func (p *Token) String() string { 32 | n := len(p.Lit) 33 | if n == 0 { 34 | return p.Tok.String() 35 | } 36 | return p.Lit 37 | } 38 | 39 | // End returns end position of this token. 40 | func (p *Token) End() token.Pos { 41 | n := len(p.Lit) 42 | if n == 0 { 43 | n = p.Tok.Len() 44 | } 45 | return p.Pos + token.Pos(n) 46 | } 47 | -------------------------------------------------------------------------------- /tpl/variant/time/time.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2025 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package time 18 | 19 | import ( 20 | "time" 21 | 22 | "github.com/goplus/xgo/tpl/variant" 23 | ) 24 | 25 | // ----------------------------------------------------------------------------- 26 | 27 | func now(args ...any) any { 28 | if len(args) > 0 { 29 | panic("function call: arity mismatch") 30 | } 31 | return time.Now() 32 | } 33 | 34 | func init() { 35 | mod := variant.NewModule("time") 36 | mod.Insert("now", now) 37 | } 38 | 39 | // ----------------------------------------------------------------------------- 40 | -------------------------------------------------------------------------------- /x/build/_testdata/hello/hello.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("XGo") 7 | } 8 | -------------------------------------------------------------------------------- /x/build/_testdata/hello/main.xgo: -------------------------------------------------------------------------------- 1 | println "XGo" 2 | -------------------------------------------------------------------------------- /x/build/_testdata/multi/Rect.gox: -------------------------------------------------------------------------------- 1 | var ( 2 | BaseClass 3 | Width, Height float64 4 | *AggClass 5 | ) 6 | 7 | type BaseClass struct { 8 | x int 9 | y int 10 | } 11 | 12 | type AggClass struct{} 13 | 14 | func Area() float64 { 15 | return Width * Height 16 | } 17 | -------------------------------------------------------------------------------- /x/build/_testdata/multi/main.xgo: -------------------------------------------------------------------------------- 1 | rc := &Rect{Width: 100, Height: 200} 2 | println rc 3 | -------------------------------------------------------------------------------- /x/build/_testdata/multi/multi.expect: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type BaseClass struct { 6 | x int 7 | y int 8 | } 9 | type AggClass struct { 10 | } 11 | type Rect struct { 12 | BaseClass 13 | Width float64 14 | Height float64 15 | *AggClass 16 | } 17 | 18 | func (this *Rect) Area() float64 { 19 | return this.Width * this.Height 20 | } 21 | func main() { 22 | rc := &Rect{Width: 100, Height: 200} 23 | fmt.Println(rc) 24 | } 25 | -------------------------------------------------------------------------------- /x/build/_testdata/pkg/pkg.expect: -------------------------------------------------------------------------------- 1 | package pkg 2 | 3 | import "fmt" 4 | 5 | func Hello() { 6 | fmt.Println("XGo") 7 | } 8 | -------------------------------------------------------------------------------- /x/build/_testdata/pkg/pkg.xgo: -------------------------------------------------------------------------------- 1 | package pkg 2 | 3 | func Hello() { 4 | println "XGo" 5 | } 6 | -------------------------------------------------------------------------------- /x/format/_testdata/gopsyntax/format.expect: -------------------------------------------------------------------------------- 1 | a := [1, 2] 2 | plot x => x * x 3 | onStart x => { 4 | println x 5 | } 6 | for i in :10 { 7 | } 8 | println [x*x for x in :10] 9 | print f()?:10 10 | -------------------------------------------------------------------------------- /x/format/_testdata/gopsyntax/index.xgo: -------------------------------------------------------------------------------- 1 | a := [1, 2] 2 | plot x => x * x 3 | onStart x => { 4 | println x 5 | } 6 | for i <- :10 { 7 | } 8 | println [x*x for x <- :10] 9 | print f()?:10 10 | -------------------------------------------------------------------------------- /x/format/_testdata/syntax/format.expect: -------------------------------------------------------------------------------- 1 | func f(...T) { 2 | } 3 | 4 | a++ 5 | arr := [...]int{1: 3} 6 | m := make(map[string]chan interface{}) 7 | for range arr[1:] { 8 | foo: 9 | *(p) = 1 10 | echo f(arr...) 11 | m["a"] <- true 12 | } 13 | for i := 0; i < 10; i++ { 14 | var t *T 15 | if false { 16 | } else { 17 | } 18 | select { 19 | case <-m["a"]: 20 | } 21 | switch { 22 | case i == 10: 23 | } 24 | switch t.(type) { 25 | case int: 26 | defer f() 27 | go f().println("Hi") 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /x/format/_testdata/syntax/index.xgo: -------------------------------------------------------------------------------- 1 | import "fmt" 2 | 3 | func f(...T) { 4 | } 5 | 6 | a++ 7 | arr := [...]int{1: 3} 8 | m := make(map[string]chan interface{}) 9 | for range arr[1:] { 10 | foo: 11 | *(p) = 1 12 | fmt.Println(f(arr...)) 13 | m["a"] <- true 14 | } 15 | for i := 0; i < 10; i++ { 16 | var t *T 17 | if false { 18 | } else { 19 | } 20 | select { 21 | case <-m["a"]: 22 | } 23 | switch { 24 | case i == 10: 25 | } 26 | switch t.(type) { 27 | case int: 28 | defer f() 29 | go f().Println("Hi") 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /x/jsonrpc2/jsonrpc2test/jsonrpc2_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package jsonrpc2test_test 6 | 7 | import ( 8 | "context" 9 | "testing" 10 | 11 | "github.com/goplus/xgo/x/jsonrpc2" 12 | "github.com/goplus/xgo/x/jsonrpc2/jsonrpc2test" 13 | "github.com/goplus/xgo/x/jsonrpc2/jsonrpc2test/cases" 14 | ) 15 | 16 | func TestNetPipe(t *testing.T) { 17 | jsonrpc2.SetDebug(jsonrpc2.DbgFlagCall) 18 | ctx := context.Background() 19 | listener := jsonrpc2test.NetPipeListener() 20 | cases.Test(t, ctx, listener, jsonrpc2.HeaderFramer(), true) 21 | } 22 | -------------------------------------------------------------------------------- /x/jsonrpc2/jsonrpc2test/pipe_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package jsonrpc2test 18 | 19 | import ( 20 | "context" 21 | "io" 22 | "net" 23 | "testing" 24 | ) 25 | 26 | func TestNetPipeDone(t *testing.T) { 27 | np := &netPiper{ 28 | done: make(chan struct{}, 1), 29 | dialed: make(chan io.ReadWriteCloser), 30 | } 31 | np.done <- struct{}{} 32 | if f, err := np.Accept(context.Background()); f != nil || err != net.ErrClosed { 33 | t.Fatal("np.Accept:", f, err) 34 | } 35 | np.done <- struct{}{} 36 | if f, err := np.Dial(context.Background()); f != nil || err != net.ErrClosed { 37 | t.Fatal("np.Dial:", f, err) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /x/typesutil/builtin_test.go: -------------------------------------------------------------------------------- 1 | package typesutil 2 | 3 | import ( 4 | "errors" 5 | "testing" 6 | 7 | "github.com/goplus/gogen" 8 | ) 9 | 10 | func TestConvErr(t *testing.T) { 11 | e := errors.New("foo") 12 | if ret, ok := convErr(nil, &gogen.ImportError{Err: e}); !ok || ret.Msg != "foo" { 13 | t.Fatal("convErr:", ret, ok) 14 | } 15 | if _, ok := convErr(nil, e); ok { 16 | t.Fatal("convErr: ok?") 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /x/xgoenv/env.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022 The XGo Authors (xgo.dev). All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package xgoenv 18 | 19 | import ( 20 | modenv "github.com/goplus/mod/env" 21 | "github.com/goplus/xgo/env" 22 | ) 23 | 24 | func Get() *modenv.XGo { 25 | return &modenv.XGo{ 26 | Version: env.Version(), 27 | BuildDate: env.BuildDate(), 28 | Root: env.XGOROOT(), 29 | } 30 | } 31 | --------------------------------------------------------------------------------