97 lines
2.9 KiB
Nim
97 lines
2.9 KiB
Nim
# Copyright 2022 Mattia Giambirtone & All Contributors
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
## Peon's type system
|
|
import ast
|
|
export ast
|
|
|
|
|
|
type
|
|
TypeKind* = enum
|
|
## An enumeration of compile-time
|
|
## types
|
|
Int8, UInt8, Int16, UInt16, Int32,
|
|
UInt32, Int64, UInt64, Float32, Float64,
|
|
Char, Byte, String, Function, CustomType,
|
|
Nil, Nan, Bool, Inf
|
|
Type* = ref object
|
|
## A wrapper around
|
|
## compile-time types
|
|
node*: ASTNode
|
|
case kind*: TypeKind:
|
|
of Function:
|
|
returnType*: Type
|
|
else:
|
|
discard
|
|
|
|
|
|
proc `==`*(self, other: Type): bool =
|
|
## Compares two type objects
|
|
## for equality
|
|
if system.`==`(self, nil):
|
|
return system.`==`(other, nil)
|
|
elif system.`==`(other, nil):
|
|
return system.`==`(self, nil)
|
|
if self.kind != other.kind:
|
|
return false
|
|
case self.kind:
|
|
of {Int8, UInt8, Int16, UInt16, Int32,
|
|
UInt32, Int64, UInt64, Float32, Float64,
|
|
Char, Byte, String, Nil, Nan, Bool, Inf}:
|
|
return true
|
|
of Function:
|
|
discard # TODO
|
|
else:
|
|
discard
|
|
|
|
|
|
proc toIntrinsic*(name: string): Type =
|
|
## Converts a string to an intrinsic
|
|
## type if it is valid and returns nil
|
|
## otherwise
|
|
if name in ["int", "int64", "i64"]:
|
|
return Type(kind: Int64)
|
|
elif name in ["uint64", "u64"]:
|
|
return Type(kind: UInt64)
|
|
elif name in ["int32", "i32"]:
|
|
return Type(kind: Int32)
|
|
elif name in ["uint32", "u32"]:
|
|
return Type(kind: UInt32)
|
|
elif name in ["int16", "i16"]:
|
|
return Type(kind: Int16)
|
|
elif name in ["uint16", "u16"]:
|
|
return Type(kind: UInt16)
|
|
elif name in ["int8", "i8"]:
|
|
return Type(kind: Int8)
|
|
elif name in ["uint8", "u8"]:
|
|
return Type(kind: UInt8)
|
|
elif name in ["f64", "float", "float64"]:
|
|
return Type(kind: Float64)
|
|
elif name in ["f32", "float32"]:
|
|
return Type(kind: Float32)
|
|
elif name == "byte":
|
|
return Type(kind: Byte)
|
|
elif name == "char":
|
|
return Type(kind: Char)
|
|
elif name == "nan":
|
|
return Type(kind: Nan)
|
|
elif name == "nil":
|
|
return Type(kind: Nil)
|
|
elif name == "inf":
|
|
return Type(kind: Inf)
|
|
elif name == "bool":
|
|
return Type(kind: Bool)
|
|
else:
|
|
return nil
|