add type aliases, rework compiler, remove optimization

This commit is contained in:
Neemek 2026-07-12 11:34:17 +02:00
parent 94b12f28ab
commit d54249cffe
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
12 changed files with 869 additions and 1597 deletions

View file

@ -7,21 +7,25 @@ import (
)
type Token struct {
Type TokenType
Type TokenKind
Start Pos
End Pos
Line Pos
Lexeme string
}
func (t Token) Bounds() (Pos, Pos) {
return t.Start, t.End
}
func (t Token) String() string {
return fmt.Sprintf("token %s, '%s' %d -> %d, line %d", t.Type.String(), t.Lexeme, t.Start, t.End, t.Line)
}
type TokenType uint64
type TokenKind uint64
const (
TokenPlus TokenType = iota
TokenPlus TokenKind = iota
TokenMinus
TokenStar
TokenSlash
@ -52,6 +56,7 @@ const (
TokenIf
TokenElse
TokenImport
TokenType
TokenComma
TokenDot
@ -77,7 +82,7 @@ const (
TokenError
)
func (t TokenType) String() string {
func (t TokenKind) String() string {
switch t {
case TokenPlus:
return "plus"
@ -171,11 +176,28 @@ func (t TokenType) String() string {
return "arrow"
case TokenNewLine:
return "newline"
case TokenType:
return "type"
}
panic("UNDEFINED TOKENTYPE STRING CONVERSION")
}
var Keywords = map[string]TokenKind{
"true": TokenTrue,
"false": TokenFalse,
"nil": TokenNil,
"if": TokenIf,
"else": TokenElse,
"import": TokenImport,
"var": TokenVar,
"fn": TokenFunc,
"return": TokenReturn,
"while": TokenWhile,
"breakpoint": TokenBreakpoint,
"type": TokenType,
}
type Lexer struct {
src []rune
start Pos
@ -340,32 +362,12 @@ func (l *Lexer) NextToken() (Token, error) {
l.advance()
}
switch string(l.src[l.start:l.current]) {
case "true":
return l.makeToken(TokenTrue), nil
case "false":
return l.makeToken(TokenFalse), nil
case "nil":
return l.makeToken(TokenNil), nil
case "if":
return l.makeToken(TokenIf), nil
case "else":
return l.makeToken(TokenElse), nil
case "var":
return l.makeToken(TokenVar), nil
case "fn":
return l.makeToken(TokenFunc), nil
case "while":
return l.makeToken(TokenWhile), nil
case "breakpoint":
return l.makeToken(TokenBreakpoint), nil
case "return":
return l.makeToken(TokenReturn), nil
case "import":
return l.makeToken(TokenImport), nil
default:
return l.makeToken(TokenName), nil
lexeme := string(l.src[l.start:l.current])
if k, ok := Keywords[lexeme]; ok {
return l.makeToken(k), nil
}
return l.makeToken(TokenName), nil
} else if c == '0' && l.peek() != '.' {
if l.peek() == 'x' {
l.advance()
@ -400,7 +402,7 @@ func (l *Lexer) NextToken() (Token, error) {
}
}
func NewToken(t TokenType, start Pos, end Pos, line Pos, lexeme string) Token {
func NewToken(t TokenKind, start Pos, end Pos, line Pos, lexeme string) Token {
return Token{
Type: t,
Start: start,
@ -425,7 +427,7 @@ func (l *Lexer) Tokenize() ([]Token, error) {
return tokens, err
}
func (l *Lexer) makeToken(t TokenType) Token {
func (l *Lexer) makeToken(t TokenKind) Token {
return NewToken(t, l.start, l.current, l.line, string(l.src[l.start:l.current]))
}