From 0063922f5c8a07b78603b2da7f6a3c2094711c3d Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 23 Apr 2024 14:36:57 +0200 Subject: 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. --- parser/type.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'parser/type.go') 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 +} -- cgit v1.2.3