summaryrefslogtreecommitdiff
path: root/parser
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2026-01-09 19:10:27 +0100
committerMarc Vertes <mvertes@free.fr>2026-01-09 19:10:27 +0100
commit7520aa4474ea30985cf26631c6bbdebf38484a0d (patch)
treee1e344c2fdc04affc0095eab26a39eef1fc3ba91 /parser
parent6ae0a2530c9a57fc093d2159591d9cae8140d641 (diff)
feat: initial support for maps
Diffstat (limited to 'parser')
-rw-r--r--parser/README.md6
-rw-r--r--parser/expr.go14
-rw-r--r--parser/type.go18
3 files changed, 33 insertions, 5 deletions
diff --git a/parser/README.md b/parser/README.md
index ba2f15c..9f40e77 100644
--- a/parser/README.md
+++ b/parser/README.md
@@ -39,14 +39,14 @@ Go language support:
- [ ] complete numeric types
- [x] function types
- [ ] variadic functions
-- [ ] pointers
+- [x] pointers
- [x] structures
- [ ] embedded structures
- [ ] recursive structures
-- [ ] literal composite objects
+- [x] literal composite objects
- [ ] interfaces
- [x] arrays, slices
-- [ ] maps
+- [x] maps
- [ ] deterministic maps
- [ ] channel types
- [ ] channel operations
diff --git a/parser/expr.go b/parser/expr.go
index e00113f..0a87999 100644
--- a/parser/expr.go
+++ b/parser/expr.go
@@ -155,12 +155,22 @@ func (p *Parser) parseExpr(in Tokens, typeStr string) (out Tokens, err error) {
typeStr = typ.String()
p.Symbols.Add(symbol.UnsetAddr, typ.String(), vm.NewValue(typ), symbol.Type, typ, p.funcScope != "")
out = append(out, scanner.Token{Tok: lang.Ident, Pos: t.Pos, Str: typeStr})
- i++
+ i++ // FIXME: number of tokens to skip should be computed from type parsing.
+
+ case lang.Map:
+ typ, err := p.parseTypeExpr(in[i:])
+ if err != nil {
+ return out, err
+ }
+ typeStr = typ.String()
+ p.Symbols.Add(symbol.UnsetAddr, typ.String(), vm.NewValue(typ), symbol.Type, typ, p.funcScope != "")
+ out = append(out, scanner.Token{Tok: lang.Ident, Pos: t.Pos, Str: typeStr})
+ i += 2 // FIXME: number of tokens to skip should be computed from type parsing.
case lang.Comment:
default:
- log.Println("unxexpected token:", t)
+ log.Println("unexpected token:", t)
}
}
for len(ops) > 0 {
diff --git a/parser/type.go b/parser/type.go
index 70ed4d1..5e8df02 100644
--- a/parser/type.go
+++ b/parser/type.go
@@ -127,6 +127,24 @@ func (p *Parser) parseTypeExpr(in Tokens) (typ *vm.Type, err error) {
}
return vm.StructOf(fields), nil
+ case lang.Map:
+ if len(in) < 3 || in[1].Tok != lang.BracketBlock {
+ return nil, fmt.Errorf("%w: %s", ErrInvalidType, in[0].Str)
+ }
+ kin, err := p.Scan(in[1].Block(), false)
+ if err != nil {
+ return nil, err
+ }
+ ktyp, err := p.parseTypeExpr(kin) // Key type
+ if err != nil {
+ return nil, err
+ }
+ etyp, err := p.parseTypeExpr(in[2:]) // Element type
+ if err != nil {
+ return nil, err
+ }
+ return vm.MapOf(ktyp, etyp), nil
+
default:
return nil, fmt.Errorf("%w: %v", ErrNotImplemented, in[0].Name())
}