Skip to content

Commit

Permalink
sema: add the Kind trait and implement to known Jule types
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jan 30, 2024
1 parent a84fe94 commit d7308d2
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
4 changes: 2 additions & 2 deletions std/jule/sema/enum.jule
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ pub struct Enum {
impl Kind for Enum {
// Implement: Kind
// Returns Enum's identifier.
fn to_str(self): str { ret self.ident }
pub fn to_str(self): str { ret self.ident }

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
let enm = unsafe { (*(&other)).enm() }
ret self == enm
}
Expand Down
4 changes: 2 additions & 2 deletions std/jule/sema/fn.jule
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,13 @@ pub struct FnIns {
impl Kind for FnIns {
// Implement: Kind
// Returns Fn's type kind as string.
fn to_str(self): str {
pub fn to_str(self): str {
const IDENT = false
ret self.get_kind_str(IDENT)
}

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
let f = unsafe { (*(&other)).fnc() }
if f == nil {
ret false
Expand Down
4 changes: 2 additions & 2 deletions std/jule/sema/struct.jule
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub struct StructIns {
impl Kind for StructIns {
// Implement: Kind
// Returns Struct's type kind as string.
fn to_str(self): str {
pub fn to_str(self): str {
let mut kind = ""
kind += self.decl.ident
if self.generics.len > 0 {
Expand All @@ -236,7 +236,7 @@ impl Kind for StructIns {
}

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
let s = unsafe { (*(&other)).strct() }
if s == nil {
ret false
Expand Down
4 changes: 2 additions & 2 deletions std/jule/sema/trait.jule
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ pub struct Trait {
impl Kind for Trait {
// Implement: Kind
// Returns Trait's identifier.
fn to_str(self): str { ret self.ident }
pub fn to_str(self): str { ret self.ident }

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
let trt = unsafe { (*(&other)).trt() }
ret self == trt
}
Expand Down
41 changes: 21 additions & 20 deletions std/jule/sema/type.jule
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ pub struct TypeAlias {
pub generics: []&TypeAlias // See developer reference (1).
}

trait Kind {
fn to_str(self): str
fn equals(&self, other: &TypeKind): bool
// Kind of type declaration.
pub trait Kind {
pub fn to_str(self): str
pub fn equals(&self, other: &TypeKind): bool
}

// Type's kind's type.
// Evaluated type declaration.
pub struct TypeKind {
pub cpp_ident: str
pub generic: bool
Expand All @@ -69,7 +70,7 @@ pub struct TypeKind {

impl Kind for TypeKind {
// Returns kind as string.
fn to_str(self): str {
pub fn to_str(self): str {
if self.is_nil() {
ret "nil"
}
Expand All @@ -90,7 +91,7 @@ impl Kind for TypeKind {
}

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
if self.is_nil() {
ret other.is_nil()
}
Expand Down Expand Up @@ -332,10 +333,10 @@ pub struct Prim {

impl Kind for Prim {
// Returns kind.
fn to_str(self): str { ret self.kind }
pub fn to_str(self): str { ret self.kind }

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
let prim = unsafe { (*(&other)).prim() }
if prim == nil {
ret false
Expand Down Expand Up @@ -401,10 +402,10 @@ pub struct Sptr {

impl Kind for Sptr {
// Returns smart pointer kind as string.
fn to_str(self): str { ret "&" + self.elem.to_str() }
pub fn to_str(self): str { ret "&" + self.elem.to_str() }

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
let sptr = unsafe { (*(&other)).sptr() }
if sptr == nil {
ret false
Expand All @@ -420,10 +421,10 @@ pub struct Slc {

impl Kind for Slc {
// Returns slice kind as string.
fn to_str(self): str { ret "[]" + self.elem.to_str() }
pub fn to_str(self): str { ret "[]" + self.elem.to_str() }

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
let slc = unsafe { (*(&other)).slc() }
if slc == nil {
ret false
Expand All @@ -439,7 +440,7 @@ pub struct Tuple {

impl Kind for Tuple {
// Returns tuple kind as string.
fn to_str(self): str {
pub fn to_str(self): str {
let mut s = "("
s += self.types[0].to_str()
for _, t in self.types[1:] {
Expand All @@ -451,7 +452,7 @@ impl Kind for Tuple {
}

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
let tup = unsafe { (*(&other)).tup() }
if tup == nil {
ret false
Expand Down Expand Up @@ -480,7 +481,7 @@ pub struct Map {

impl Kind for Map {
// Returns map kind as string.
fn to_str(self): str {
pub fn to_str(self): str {
let mut s = "["
s += self.key.to_str()
s += ":"
Expand All @@ -490,7 +491,7 @@ impl Kind for Map {
}

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
let map = unsafe { (*(&other)).map() }
if map == nil {
ret false
Expand All @@ -508,7 +509,7 @@ pub struct Arr {

impl Kind for Arr {
// Returns array kind as string.
fn to_str(self): str {
pub fn to_str(self): str {
let mut s = "["
s += itoa(self.n)
s += "]"
Expand All @@ -517,7 +518,7 @@ impl Kind for Arr {
}

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
let arr = unsafe { (*(&other)).arr() }
if arr == nil {
ret false
Expand All @@ -533,15 +534,15 @@ pub struct Ptr {

impl Kind for Ptr {
// Returns pointer kind as string.
fn to_str(self): str {
pub fn to_str(self): str {
if self.is_unsafe() {
ret "*unsafe"
}
ret "*" + self.elem.to_str()
}

// Reports whether types are same.
fn equals(&self, other: &TypeKind): bool {
pub fn equals(&self, other: &TypeKind): bool {
let ptr = unsafe { (*(&other)).ptr() }
if ptr == nil {
ret false
Expand Down
2 changes: 0 additions & 2 deletions std/jule/sema/type2.jule
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

// This file reserved for type compatibility checking.

use integ for std::jule::integrated

use std::conv::{fmt_float}
use std::math::{modf}
use std::jule::ast::{
Expand Down

0 comments on commit d7308d2

Please sign in to comment.