-
Notifications
You must be signed in to change notification settings - Fork 3
feat: support global variables #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
toaction
wants to merge
4
commits into
goplus:main
Choose a base branch
from
toaction:feat/variable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package pygen | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "go/token" | ||
| "github.com/goplus/gogen" | ||
| "github.com/goplus/llpyg/symbol" | ||
| ) | ||
|
|
||
| func (ctx *context) genVars(pkg *gogen.Package, syms []*symbol.Symbol) { | ||
|
toaction marked this conversation as resolved.
toaction marked this conversation as resolved.
|
||
| names := make(map[string]struct{}) | ||
| for _, sym := range syms { | ||
| if sym.Name == "" || sym.Name[0] == '_' { | ||
|
toaction marked this conversation as resolved.
|
||
| continue | ||
| } | ||
| name := ctx.genName(sym.Name, -1) | ||
| _, exist := names[name] | ||
| // avoid name conflict | ||
| for exist { | ||
|
toaction marked this conversation as resolved.
|
||
| name = name + "_" | ||
| _, exist = names[name] | ||
| } | ||
|
toaction marked this conversation as resolved.
|
||
| names[name] = struct{}{} | ||
| ctx.genVar(pkg, sym, name) | ||
| } | ||
| } | ||
|
|
||
| // current can not get variable comment, see https://github.com/goplus/llpyg/issues/43 | ||
| func (ctx *context) genVar(pkg *gogen.Package, sym *symbol.Symbol, goName string) { | ||
| def := pkg.NewVarDefs(pkg.Types.Scope()) | ||
| // linkname | ||
| docList := make([]*ast.Comment, 0, 2) | ||
| goLinkname := "//go:linkname " + goName + " py." + sym.Name | ||
| docList = append(docList, &ast.Comment{Text: goLinkname}) | ||
| def.SetComments(&ast.CommentGroup{List: docList}) | ||
|
|
||
| def.New(token.NoPos, ctx.objPtr, goName) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Numeric types | ||
| int_var = 42 | ||
| Int_var = 25 | ||
| float_var = 3.14 | ||
| complex_var = 3 + 4j | ||
|
|
||
| # Boolean type | ||
| bool_var = True | ||
|
|
||
| # String type | ||
| str_var = "Hello, World!" | ||
|
|
||
| # Sequence types | ||
| list_var = [1, 2, 3] | ||
| tuple_var = (1, 2, 3) | ||
| range_var = range(10) | ||
|
|
||
| # Mapping type | ||
| dict_var = {1: "one", 2: "two", 3: "three"} | ||
|
|
||
| # Set types | ||
| set_var = {1, 2, 3} | ||
| frozenset_var = frozenset({1, 2, 3}) | ||
|
|
||
| # Binary types | ||
| bytes_var = b"hello" | ||
| bytearray_var = bytearray(b"hello") | ||
| mv_var = memoryview(b"hello") | ||
|
|
||
| # Special type | ||
| none_var = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package demo | ||
|
|
||
| import ( | ||
| "github.com/goplus/lib/py" | ||
| _ "unsafe" | ||
| ) | ||
|
|
||
| const LLGoPackage = "py.demo" | ||
|
|
||
| //go:linkname IntVar py.int_var | ||
| var IntVar *py.Object | ||
|
|
||
| //go:linkname IntVar_ py.Int_var | ||
| var IntVar_ *py.Object | ||
|
|
||
| //go:linkname FloatVar py.float_var | ||
| var FloatVar *py.Object | ||
|
|
||
| //go:linkname ComplexVar py.complex_var | ||
| var ComplexVar *py.Object | ||
|
|
||
| //go:linkname BoolVar py.bool_var | ||
| var BoolVar *py.Object | ||
|
|
||
| //go:linkname StrVar py.str_var | ||
| var StrVar *py.Object | ||
|
|
||
| //go:linkname ListVar py.list_var | ||
| var ListVar *py.Object | ||
|
|
||
| //go:linkname TupleVar py.tuple_var | ||
| var TupleVar *py.Object | ||
|
|
||
| //go:linkname RangeVar py.range_var | ||
| var RangeVar *py.Object | ||
|
|
||
| //go:linkname DictVar py.dict_var | ||
| var DictVar *py.Object | ||
|
|
||
| //go:linkname SetVar py.set_var | ||
| var SetVar *py.Object | ||
|
|
||
| //go:linkname FrozensetVar py.frozenset_var | ||
| var FrozensetVar *py.Object | ||
|
|
||
| //go:linkname BytesVar py.bytes_var | ||
| var BytesVar *py.Object | ||
|
|
||
| //go:linkname BytearrayVar py.bytearray_var | ||
| var BytearrayVar *py.Object | ||
|
|
||
| //go:linkname MvVar py.mv_var | ||
| var MvVar *py.Object | ||
|
|
||
| //go:linkname NoneVar py.none_var | ||
| var NoneVar *py.Object |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.