Added types

This commit is contained in:
2024-08-05 18:47:24 +02:00
parent cd1119add2
commit 83661c5e7a
33 changed files with 113 additions and 92 deletions

View File

@ -1,5 +1,26 @@
package errors
var (
EmptySwitch = &Base{"Empty switch"}
ExpectedFunctionName = &Base{"Expected function name"}
ExpectedFunctionParameters = &Base{"Expected function parameters"}
ExpectedFunctionDefinition = &Base{"Expected function definition"}
ExpectedIfBeforeElse = &Base{"Expected an 'if' block before 'else'"}
InvalidNumber = &Base{"Invalid number"}
InvalidExpression = &Base{"Invalid expression"}
InvalidRune = &Base{"Invalid rune"}
InvalidStatement = &Base{"Invalid statement"}
MissingBlockStart = &Base{"Missing '{'"}
MissingBlockEnd = &Base{"Missing '}'"}
MissingExpression = &Base{"Missing expression"}
MissingGroupStart = &Base{"Missing '('"}
MissingGroupEnd = &Base{"Missing ')'"}
MissingMainFunction = &Base{"Missing main function"}
MissingOperand = &Base{"Missing operand"}
MissingType = &Base{"Missing type"}
NotImplemented = &Base{"Not implemented"}
)
// Base is the base class for errors that have no parameters.
type Base struct {
Message string

View File

@ -1,14 +0,0 @@
package errors
var (
EmptySwitch = &Base{"Empty switch"}
ExpectedIfBeforeElse = &Base{"Expected an 'if' block before 'else'"}
InvalidNumber = &Base{"Invalid number"}
InvalidExpression = &Base{"Invalid expression"}
InvalidRune = &Base{"Invalid rune"}
InvalidStatement = &Base{"Invalid statement"}
MissingExpression = &Base{"Missing expression"}
MissingMainFunction = &Base{"Missing main function"}
MissingOperand = &Base{"Missing operand"}
NotImplemented = &Base{"Not implemented"}
)

View File

@ -1,11 +0,0 @@
package errors
var (
MissingBlockStart = &Base{"Missing '{'"}
MissingBlockEnd = &Base{"Missing '}'"}
MissingGroupStart = &Base{"Missing '('"}
MissingGroupEnd = &Base{"Missing ')'"}
ExpectedFunctionName = &Base{"Expected function name"}
ExpectedFunctionParameters = &Base{"Expected function parameters"}
ExpectedFunctionDefinition = &Base{"Expected function definition"}
)

View File

@ -1,6 +1,7 @@
package scanner
import (
"fmt"
"os"
"path/filepath"
@ -153,8 +154,20 @@ func (s *Scanner) scanFile(path string, pkg string) error {
return errors.New(errors.ExpectedFunctionParameters, file, tokens[i].Position)
}
// Return type
if i < len(tokens) && tokens[i].Kind == token.ReturnType {
for i < len(tokens) && tokens[i].Kind != token.BlockStart {
i++
}
}
// Function definition
for i < len(tokens) {
if tokens[i].Kind == token.ReturnType {
i++
continue
}
if tokens[i].Kind == token.BlockStart {
blockLevel++
i++
@ -207,17 +220,18 @@ func (s *Scanner) scanFile(path string, pkg string) error {
name := tokens[nameStart].Text(contents)
body := tokens[bodyStart:i]
function := core.NewFunction(pkg, name, file, body)
parameters := tokens[paramsStart:paramsEnd]
count := 0
err := expression.EachParameter(parameters, func(tokens token.List) error {
if len(tokens) != 1 {
return errors.New(errors.NotImplemented, file, tokens[0].Position)
if len(tokens) < 2 {
return errors.New(errors.MissingType, file, tokens[0].End())
}
name := tokens[0].Text(contents)
dataType := tokens[1].Text(contents)
fmt.Println(dataType)
register := x64.CallRegisters[count]
uses := token.Count(function.Body, contents, token.Identifier, name)

View File

@ -18,6 +18,7 @@ const (
BlockEnd // }
ArrayStart // [
ArrayEnd // ]
ReturnType // ->
_operators // <operators>
Add // +
Sub // -

View File

@ -30,9 +30,17 @@ func Tokenize(buffer []byte) List {
if len(tokens) == 0 || tokens[len(tokens)-1].IsOperator() || tokens[len(tokens)-1].IsExpressionStart() || tokens[len(tokens)-1].IsKeyword() {
tokens = append(tokens, Token{Kind: Negate, Position: i, Length: 1})
} else {
if i+1 < Position(len(buffer)) && buffer[i+1] == '=' {
tokens = append(tokens, Token{Kind: SubAssign, Position: i, Length: 2})
i++
if i+1 < Position(len(buffer)) {
switch buffer[i+1] {
case '=':
tokens = append(tokens, Token{Kind: SubAssign, Position: i, Length: 2})
i++
case '>':
tokens = append(tokens, Token{Kind: ReturnType, Position: i, Length: 2})
i++
default:
tokens = append(tokens, Token{Kind: Sub, Position: i, Length: 1})
}
} else {
tokens = append(tokens, Token{Kind: Sub, Position: i, Length: 1})
}
@ -212,10 +220,12 @@ func Tokenize(buffer []byte) List {
kind = Add
case "+=":
kind = AddAssign
case "-":
kind = Sub
case "-=":
kind = SubAssign
// case "-":
// kind = Sub
// case "-=":
// kind = SubAssign
// case "->":
// kind = ReturnType
case ".":
kind = Period
case "/":