More function parsing

This commit is contained in:
MrLetsplay 2024-03-12 22:00:03 +01:00
parent 3917dc6adc
commit f8ed70336d
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
2 changed files with 78 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import lang.stdlib;
(u8, u8) a(u8 a, u8 b) {
return a, b
return a, b;
}
void main() {

View File

@ -108,8 +108,14 @@ type Block struct {
Statements []Statement
}
type ParsedParameter struct {
Name string
Type Type
}
type ParsedFunction struct {
Parameters []Type
Name string
Parameters []ParsedParameter
ReturnType Type
Body Block
}
@ -141,6 +147,19 @@ func expectSeparator(tokens []LexToken, separators ...Separator) (Separator, []L
return separator.Value.(Separator), tokens[1:], nil
}
func expectIdentifier(tokens []LexToken) (string, []LexToken, error) {
if len(tokens) == 0 {
return "", nil, errors.New("expected identifier")
}
token := tokens[0]
if token.Type != Type_Separator && token.Type != Type_Identifier {
return "", nil, errors.New("expected identifier")
}
return token.Value.(string), tokens[1:], nil
}
func parseImport(tokens []LexToken) (*Import, []LexToken, error) {
var err error
@ -216,7 +235,8 @@ func parseType(tokens []LexToken) (*Type, []LexToken, error) {
func parseFunction(tokens []LexToken) (*ParsedFunction, []LexToken, error) {
var err error
var parameters []Type
var name string
var parameters []ParsedParameter
var returnType *Type
var body Block
@ -225,9 +245,62 @@ func parseFunction(tokens []LexToken) (*ParsedFunction, []LexToken, error) {
return nil, nil, err
}
// TODO: function name, parameters, body
if len(tokens) < 2 {
return nil, nil, errors.New("incomplete function declaration")
}
return &ParsedFunction{Parameters: parameters, ReturnType: *returnType, Body: body}, tokens, nil
name, tokens, err = expectIdentifier(tokens)
if err != nil {
return nil, nil, err
}
_, tokens, err = expectSeparator(tokens, Separator_OpenParen)
if err != nil {
return nil, nil, err
}
for {
if len(tokens) == 0 {
return nil, nil, errors.New("incomplete function declaration")
}
token := tokens[0]
if token.Type == Type_Separator && token.Value.(Separator) == Separator_CloseParen {
tokens = tokens[1:]
break
}
if len(parameters) > 0 {
if token.Type != Type_Separator && token.Value.(Separator) != Separator_Comma {
return nil, nil, errors.New("expected ,")
}
tokens = tokens[1:]
}
var paramType *Type
paramType, tokens, err = parseType(tokens)
if err != nil {
return nil, nil, err
}
var paramName string
paramName, tokens, err = expectIdentifier(tokens)
if err != nil {
return nil, nil, err
}
parameters = append(parameters, ParsedParameter{Name: paramName, Type: *paramType})
}
_, tokens, err = expectSeparator(tokens, Separator_OpenCurly)
if err != nil {
return nil, nil, err
}
// TODO: body, closing curly
return &ParsedFunction{Name: name, Parameters: parameters, ReturnType: *returnType, Body: body}, tokens, nil
}
func parser(tokens []LexToken) (*ParsedFile, error) {