Working imports

This commit is contained in:
2026-04-22 09:34:23 +02:00
parent 53b87eb02e
commit ea6aabd896
5 changed files with 42 additions and 27 deletions
+12 -2
View File
@@ -6,6 +6,7 @@ import (
"go/token" "go/token"
"go/types" "go/types"
"os" "os"
"unicode"
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
) )
@@ -78,7 +79,7 @@ func main() {
switch obj := obj.(type) { switch obj := obj.(type) {
case *types.Func: case *types.Func:
scopeData.Functions[name] = buildFunction(obj) scopeData.Functions[LowerFirst(name)] = buildFunction(obj)
case *types.TypeName: case *types.TypeName:
if strct, ok := buildStruct(obj); ok { if strct, ok := buildStruct(obj); ok {
@@ -99,6 +100,15 @@ func main() {
fmt.Println(string(out)) fmt.Println(string(out))
} }
func LowerFirst(s string) string {
if s == "" {
return ""
}
r := []rune(s)
r[0] = unicode.ToLower(r[0])
return string(r)
}
func buildFunction(fn *types.Func) FunctionSymbol { func buildFunction(fn *types.Func) FunctionSymbol {
sig := fn.Type().(*types.Signature) sig := fn.Type().(*types.Signature)
@@ -166,7 +176,7 @@ func buildStruct(tn *types.TypeName) (StructSymbol, bool) {
continue continue
} }
methods[m.Name()] = buildFunction(m) methods[LowerFirst(m.Name())] = buildFunction(m)
} }
return StructSymbol{ return StructSymbol{
+1 -1
View File
@@ -14,7 +14,7 @@ pub mod top_level;
pub fn walk_ast(top_scope: Arc<Scope>, tl: &mut Vec<parser::ast::TopLevel>) { pub fn walk_ast(top_scope: Arc<Scope>, tl: &mut Vec<parser::ast::TopLevel>) {
for tl in tl { for tl in tl {
match tl { match tl {
parser::ast::TopLevel::Import(package) => {} parser::ast::TopLevel::Import(_) => {}
parser::ast::TopLevel::Package(_) => {} parser::ast::TopLevel::Package(_) => {}
parser::ast::TopLevel::FunctionDecl { parser::ast::TopLevel::FunctionDecl {
+5
View File
@@ -120,6 +120,11 @@ impl LocalScope {
*id = field.name.clone(); *id = field.name.clone();
current_type = field.var_type.clone(); current_type = field.var_type.clone();
} }
TypeRef::Package(p) => {
let var_ref = p.variables.get(id)?;
*id = var_ref.name.clone();
current_type = var_ref.var_type.clone();
}
_ => unimplemented!(), _ => unimplemented!(),
}, },
Postfix::Call(_args) => match &*current_type { Postfix::Call(_args) => match &*current_type {
+23 -23
View File
@@ -3,7 +3,7 @@
"package_name": "fmt", "package_name": "fmt",
"structs": {}, "structs": {},
"functions": { "functions": {
"Append": { "append": {
"export": true, "export": true,
"name": "Append", "name": "Append",
"params": { "params": {
@@ -22,7 +22,7 @@
"[]byte" "[]byte"
] ]
}, },
"Appendf": { "appendf": {
"export": true, "export": true,
"name": "Appendf", "name": "Appendf",
"params": { "params": {
@@ -46,7 +46,7 @@
"[]byte" "[]byte"
] ]
}, },
"Appendln": { "appendln": {
"export": true, "export": true,
"name": "Appendln", "name": "Appendln",
"params": { "params": {
@@ -65,7 +65,7 @@
"[]byte" "[]byte"
] ]
}, },
"Errorf": { "errorf": {
"export": true, "export": true,
"name": "Errorf", "name": "Errorf",
"params": { "params": {
@@ -84,7 +84,7 @@
"error" "error"
] ]
}, },
"FormatString": { "formatString": {
"export": true, "export": true,
"name": "FormatString", "name": "FormatString",
"params": { "params": {
@@ -103,7 +103,7 @@
"string" "string"
] ]
}, },
"Fprint": { "fprint": {
"export": true, "export": true,
"name": "Fprint", "name": "Fprint",
"params": { "params": {
@@ -123,7 +123,7 @@
"error" "error"
] ]
}, },
"Fprintf": { "fprintf": {
"export": true, "export": true,
"name": "Fprintf", "name": "Fprintf",
"params": { "params": {
@@ -148,7 +148,7 @@
"error" "error"
] ]
}, },
"Fprintln": { "fprintln": {
"export": true, "export": true,
"name": "Fprintln", "name": "Fprintln",
"params": { "params": {
@@ -168,7 +168,7 @@
"error" "error"
] ]
}, },
"Fscan": { "fscan": {
"export": true, "export": true,
"name": "Fscan", "name": "Fscan",
"params": { "params": {
@@ -188,7 +188,7 @@
"error" "error"
] ]
}, },
"Fscanf": { "fscanf": {
"export": true, "export": true,
"name": "Fscanf", "name": "Fscanf",
"params": { "params": {
@@ -213,7 +213,7 @@
"error" "error"
] ]
}, },
"Fscanln": { "fscanln": {
"export": true, "export": true,
"name": "Fscanln", "name": "Fscanln",
"params": { "params": {
@@ -233,7 +233,7 @@
"error" "error"
] ]
}, },
"Print": { "print": {
"export": true, "export": true,
"name": "Print", "name": "Print",
"params": { "params": {
@@ -248,7 +248,7 @@
"error" "error"
] ]
}, },
"Printf": { "printf": {
"export": true, "export": true,
"name": "Printf", "name": "Printf",
"params": { "params": {
@@ -268,7 +268,7 @@
"error" "error"
] ]
}, },
"Println": { "println": {
"export": true, "export": true,
"name": "Println", "name": "Println",
"params": { "params": {
@@ -283,7 +283,7 @@
"error" "error"
] ]
}, },
"Scan": { "scan": {
"export": true, "export": true,
"name": "Scan", "name": "Scan",
"params": { "params": {
@@ -298,7 +298,7 @@
"error" "error"
] ]
}, },
"Scanf": { "scanf": {
"export": true, "export": true,
"name": "Scanf", "name": "Scanf",
"params": { "params": {
@@ -318,7 +318,7 @@
"error" "error"
] ]
}, },
"Scanln": { "scanln": {
"export": true, "export": true,
"name": "Scanln", "name": "Scanln",
"params": { "params": {
@@ -333,7 +333,7 @@
"error" "error"
] ]
}, },
"Sprint": { "sprint": {
"export": true, "export": true,
"name": "Sprint", "name": "Sprint",
"params": { "params": {
@@ -347,7 +347,7 @@
"string" "string"
] ]
}, },
"Sprintf": { "sprintf": {
"export": true, "export": true,
"name": "Sprintf", "name": "Sprintf",
"params": { "params": {
@@ -366,7 +366,7 @@
"string" "string"
] ]
}, },
"Sprintln": { "sprintln": {
"export": true, "export": true,
"name": "Sprintln", "name": "Sprintln",
"params": { "params": {
@@ -380,7 +380,7 @@
"string" "string"
] ]
}, },
"Sscan": { "sscan": {
"export": true, "export": true,
"name": "Sscan", "name": "Sscan",
"params": { "params": {
@@ -400,7 +400,7 @@
"error" "error"
] ]
}, },
"Sscanf": { "sscanf": {
"export": true, "export": true,
"name": "Sscanf", "name": "Sscanf",
"params": { "params": {
@@ -425,7 +425,7 @@
"error" "error"
] ]
}, },
"Sscanln": { "sscanln": {
"export": true, "export": true,
"name": "Sscanln", "name": "Sscanln",
"params": { "params": {
+1 -1
View File
@@ -3,5 +3,5 @@ package main;
import "fmt"; import "fmt";
function main() { function main() {
fmt.Println("hello"); fmt.println("hello");
} }