From c64df4ae6ec442ca2fd6d25d6fc02784a4a7eb9c Mon Sep 17 00:00:00 2001 From: mertcandav Date: Sun, 21 Jul 2024 16:25:12 +0300 Subject: [PATCH] jule: data types are considered as built-in type aliases instead of keywords --- std/jule/ast/node.jule | 7 ---- std/jule/lex/lex.jule | 16 -------- std/jule/lex/token.jule | 17 --------- std/jule/parser/expr.jule | 7 +--- std/jule/parser/type.jule | 8 ---- std/jule/sema/builtin.jule | 75 +++++++++++++++++++++++++++++--------- std/jule/sema/type.jule | 41 +-------------------- 7 files changed, 59 insertions(+), 112 deletions(-) diff --git a/std/jule/ast/node.jule b/std/jule/ast/node.jule index c1842a5a2..a88f9c790 100644 --- a/std/jule/ast/node.jule +++ b/std/jule/ast/node.jule @@ -65,13 +65,6 @@ struct IdentTypeDecl { Generics: []&TypeDecl } -impl IdentTypeDecl { - // Reports whether identifier is primitive type. - fn IsPrim(self): bool { - ret self.Token.Id == TokenId.Prim - } -} - // Sub-identifier type. struct SubIdentTypeDecl { Idents: []&IdentTypeDecl diff --git a/std/jule/lex/lex.jule b/std/jule/lex/lex.jule index 2a38be5b5..df938b1d4 100644 --- a/std/jule/lex/lex.jule +++ b/std/jule/lex/lex.jule @@ -17,22 +17,6 @@ struct kindPair { } static keywords: [...]kindPair = [ - {TokenKind.I8, TokenId.Prim}, - {TokenKind.I16, TokenId.Prim}, - {TokenKind.I32, TokenId.Prim}, - {TokenKind.I64, TokenId.Prim}, - {TokenKind.U8, TokenId.Prim}, - {TokenKind.U16, TokenId.Prim}, - {TokenKind.U32, TokenId.Prim}, - {TokenKind.U64, TokenId.Prim}, - {TokenKind.F32, TokenId.Prim}, - {TokenKind.F64, TokenId.Prim}, - {TokenKind.Uint, TokenId.Prim}, - {TokenKind.Int, TokenId.Prim}, - {TokenKind.Uintptr, TokenId.Prim}, - {TokenKind.Bool, TokenId.Prim}, - {TokenKind.Str, TokenId.Prim}, - {TokenKind.Any, TokenId.Prim}, {TokenKind.True, TokenId.Lit}, {TokenKind.False, TokenId.Lit}, {TokenKind.Nil, TokenId.Lit}, diff --git a/std/jule/lex/token.jule b/std/jule/lex/token.jule index eeba4807b..1b765e120 100644 --- a/std/jule/lex/token.jule +++ b/std/jule/lex/token.jule @@ -116,7 +116,6 @@ enum Ident: str { // Token identities. enum TokenId: uint { Na, - Prim, Ident, Range, Ret, @@ -208,22 +207,6 @@ enum TokenKind: str { LBrace: "{", RBrace: "}", Hash: "#", - I8: "i8", - I16: "i16", - I32: "i32", - I64: "i64", - U8: "u8", - U16: "u16", - U32: "u32", - U64: "u64", - F32: "f32", - F64: "f64", - Uint: "uint", - Int: "int", - Uintptr: "uintptr", - Bool: "bool", - Str: "str", - Any: "any", True: "true", False: "false", Nil: "nil", diff --git a/std/jule/parser/expr.jule b/std/jule/parser/expr.jule index 216f58711..086f2be5c 100644 --- a/std/jule/parser/expr.jule +++ b/std/jule/parser/expr.jule @@ -85,8 +85,6 @@ impl exprBuilder { | TokenId.Self | TokenId.Error: ret buildIdentExpr(token) - | TokenId.Prim: - ret self.buildPrimitiveType(token) } self.pushErr(token, LogMsg.InvalidSyntax) ret nil @@ -775,7 +773,7 @@ impl exprBuilder { // Handle pointer to primitive type. if len(tokens) > 1 && token.Kind == TokenKind.Star { token = tokens[1] - if token.Id == TokenId.Prim || token.Id == TokenId.Unsafe { + if token.Id == TokenId.Unsafe { ret self.buildType(tokens) } } @@ -803,9 +801,6 @@ impl exprBuilder { match token.Id { | TokenId.Ident: ret self.buildSubIdent(tokens) - | TokenId.Prim: - // Catch slice, and array types. - ret self.buildType(tokens) | TokenId.Op: ret self.buildOpRight(tokens) | TokenId.Range: diff --git a/std/jule/parser/type.jule b/std/jule/parser/type.jule index 5e6218c19..64da200a8 100644 --- a/std/jule/parser/type.jule +++ b/std/jule/parser/type.jule @@ -51,12 +51,6 @@ impl typeBuilder { } } - unsafe fn buildPrim(mut self): &TypeDecl { - let mut t = buildPrimType(self.tokens[*self.i]) - *self.i++ - ret t - } - unsafe fn buildNamespace(mut self): &TypeDecl { let mut t = &TypeDecl{ Token: self.tokens[*self.i], @@ -384,8 +378,6 @@ impl typeBuilder { unsafe fn step(mut self): &TypeDecl { let token = self.tokens[*self.i] match token.Id { - | TokenId.Prim: - ret self.buildPrim() | TokenId.Ident: ret self.buildIdent() | TokenId.Cpp: diff --git a/std/jule/sema/builtin.jule b/std/jule/sema/builtin.jule index c4b2a9009..08641ea86 100644 --- a/std/jule/sema/builtin.jule +++ b/std/jule/sema/builtin.jule @@ -63,27 +63,66 @@ fn findBuiltinFn(ident: str): &FnIns { } } +fn primTypeAlias(k: PrimKind): &TypeAlias { + ret &TypeAlias{ + Public: true, + Kind: &TypeSymbol{ + Kind: &TypeKind{ + Kind: buildPrimType(k), + }, + }, + } +} + fn findBuiltinTypeAlias(ident: str): &TypeAlias { match ident { - | "byte": - static mut t = &TypeAlias{ - Public: true, - Kind: &TypeSymbol{ - Kind: &TypeKind{ - Kind: buildPrimType(PrimKind.U8), - }, - }, - } + | "any": + static mut t = primTypeAlias(PrimKind.Any) ret t - | "rune": - static mut t = &TypeAlias{ - Public: true, - Kind: &TypeSymbol{ - Kind: &TypeKind{ - Kind: buildPrimType(PrimKind.I32), - }, - }, - } + | "str": + static mut t = primTypeAlias(PrimKind.Str) + ret t + | "bool": + static mut t = primTypeAlias(PrimKind.Bool) + ret t + | "uintptr": + static mut t = primTypeAlias(PrimKind.Uintptr) + ret t + | "uint": + static mut t = primTypeAlias(PrimKind.Uint) + ret t + | "int": + static mut t = primTypeAlias(PrimKind.Int) + ret t + | "i8": + static mut t = primTypeAlias(PrimKind.I8) + ret t + | "i16": + static mut t = primTypeAlias(PrimKind.I16) + ret t + | "i64": + static mut t = primTypeAlias(PrimKind.I64) + ret t + | "u16": + static mut t = primTypeAlias(PrimKind.U16) + ret t + | "u32": + static mut t = primTypeAlias(PrimKind.U32) + ret t + | "u64": + static mut t = primTypeAlias(PrimKind.U64) + ret t + | "f32": + static mut t = primTypeAlias(PrimKind.F32) + ret t + | "f64": + static mut t = primTypeAlias(PrimKind.F64) + ret t + | "byte" | "u8": + static mut t = primTypeAlias(PrimKind.U8) + ret t + | "rune" | "i32": + static mut t = primTypeAlias(PrimKind.I32) ret t |: ret nil diff --git a/std/jule/sema/type.jule b/std/jule/sema/type.jule index a1951ca93..f9366bdff 100644 --- a/std/jule/sema/type.jule +++ b/std/jule/sema/type.jule @@ -834,20 +834,6 @@ impl typeChecker { self.disBuiltin = true } - fn buildPrim(mut self, &decl: &IdentTypeDecl): &Prim { - if !isPrim(decl.Ident) { - self.pushErr(self.errorToken, LogMsg.InvalidType) - ret nil - } - - if len(decl.Generics) > 0 { - self.pushErr(decl.Token, LogMsg.TypeNotSupportsGenerics, decl.Ident) - ret nil - } - - ret buildPrimType(decl.Ident) - } - fn pushReference[T](mut self, mut &t: T) { if self.refers == nil { ret @@ -1284,12 +1270,7 @@ impl typeChecker { } fn buildIdent(mut self, mut decl: &IdentTypeDecl): Kind { - match { - | isPrim(decl.Ident): - ret self.buildPrim(decl) - |: - ret self.getDef(decl) - } + ret self.getDef(decl) } fn buildSubIdent(mut self, mut decl: &SubIdentTypeDecl): Kind { @@ -1824,26 +1805,6 @@ fn getStructFromKind(mut k: &TypeKind): &Struct { } } -// Reports whether kind is primitive type. -fn isPrim(kind: str): bool { - ret kind == TokenKind.I8 || - kind == TokenKind.I16 || - kind == TokenKind.I32 || - kind == TokenKind.I64 || - kind == TokenKind.U8 || - kind == TokenKind.U16 || - kind == TokenKind.U32 || - kind == TokenKind.U64 || - kind == TokenKind.F32 || - kind == TokenKind.F64 || - kind == TokenKind.Int || - kind == TokenKind.Uint || - kind == TokenKind.Uintptr || - kind == TokenKind.Bool || - kind == TokenKind.Str || - kind == TokenKind.Any -} - fn applyImplicitCast(mut &dest: &TypeKind, mut &d: &Data) { if d.Kind.IsNil() { ret