summaryrefslogtreecommitdiff
path: root/parser/type.go
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2024-04-23 14:36:57 +0200
committerGitHub <noreply@github.com>2024-04-23 14:36:57 +0200
commit0063922f5c8a07b78603b2da7f6a3c2094711c3d (patch)
tree613cd8f53e86f33678a2f96fa0e776edfa555101 /parser/type.go
parent1bff92c52b27b9a516599e172fe9852c3d99be38 (diff)
feat: initial and partial support of composite expressions (#9)
A new `Composite` token is created. Literal composite expressions are recognized and partially handled by the parser but not yet by the code generator. Other cosmetic changes are present.
Diffstat (limited to 'parser/type.go')
-rw-r--r--parser/type.go24
1 files changed, 18 insertions, 6 deletions
diff --git a/parser/type.go b/parser/type.go
index b9ab0f9..ba60e8f 100644
--- a/parser/type.go
+++ b/parser/type.go
@@ -26,12 +26,10 @@ var (
ErrTypeNotImplemented = errors.New("not implemented")
)
-// ParseTypeExpr parses a list of tokens defining a type expresssion and returns
-// the corresponding runtime type or an error.
-func (p *Parser) ParseTypeExpr(in Tokens) (typ *vm.Type, err error) {
+func (p *Parser) parseTypeExpr(in Tokens) (typ *vm.Type, err error) {
switch in[0].Tok {
case lang.BracketBlock:
- typ, err := p.ParseTypeExpr(in[1:])
+ typ, err := p.parseTypeExpr(in[1:])
if err != nil {
return nil, err
}
@@ -53,7 +51,7 @@ func (p *Parser) ParseTypeExpr(in Tokens) (typ *vm.Type, err error) {
return vm.SliceOf(typ), nil
case lang.Mul:
- typ, err := p.ParseTypeExpr(in[1:])
+ typ, err := p.parseTypeExpr(in[1:])
if err != nil {
return nil, err
}
@@ -157,7 +155,7 @@ func (p *Parser) parseParamTypes(in Tokens, flag typeFlag) (types []*vm.Type, va
continue
}
}
- typ, err := p.ParseTypeExpr(t)
+ typ, err := p.parseTypeExpr(t)
if err != nil {
return nil, nil, err
}
@@ -196,3 +194,17 @@ func (p *Parser) hasFirstParam(in Tokens) bool {
s, _, ok := p.getSym(in[0].Str, p.scope)
return !ok || s.kind != symType
}
+
+// typeStartIndex returns the index of the start of type expression in tokens, or -1.
+func (p *Parser) typeStartIndex(in Tokens) int {
+ index := len(in) - 1
+ for i := index; i >= 0; i-- {
+ switch in[i].Tok {
+ case lang.Ident, lang.Struct, lang.Map, lang.Func, lang.Interface, lang.Mul, lang.BraceBlock, lang.BracketBlock, lang.ParenBlock:
+ index = i
+ default:
+ return index
+ }
+ }
+ return -1
+}