Added new modular standard library
This commit is contained in:
parent
d33a597f19
commit
11f725e176
|
@ -0,0 +1,106 @@
|
|||
# Builtin arithmetic operators for Peon
|
||||
# Note: Most of these do nothing on their own. All they do
|
||||
# is serve as placeholders for emitting specific VM
|
||||
# instructions. They're implemented this way because:
|
||||
# - They tie into the existing type system nicely
|
||||
# - It makes the implementation easier and more flexible
|
||||
|
||||
|
||||
operator `+`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](a, b: T): T {
|
||||
#pragma[magic: "Add", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `+`(a, b: float): float {
|
||||
#pragma[magic: "AddFloat64", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `+`(a, b: float32): float32 {
|
||||
#pragma[magic: "AddFloat32", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `-`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](a, b: T): T {
|
||||
#pragma[magic: "Subtract", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `-`*(a, b: float64): float64 {
|
||||
#pragma[magic: "SubtractFloat64", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `-`*(a, b: float32): float32 {
|
||||
#pragma[magic: "SubtractFloat32", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `-`*[T: int | int32 | int16 | int8](a: T): T {
|
||||
#pragma[magic: "Negate", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `-`*(a: float64): float64 {
|
||||
#pragma[magic: "NegateFloat64", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `-`*(a: float32): float32 {
|
||||
#pragma[magic: "NegateFloat32", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `-`*(a: inf): inf {
|
||||
#pragma[magic: "NegInf", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `*`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](a, b: T): T {
|
||||
#pragma[magic: "Multiply", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `*`*(a, b: float64): float64 {
|
||||
#pragma[magic: "MultiplyFloat64", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `*`*(a, b: float32): float32 {
|
||||
#pragma[magic: "MultiplyFloat32", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `/`*[T: int | int32 | int16 | int8](a, b: T): T {
|
||||
#pragma[magic: "SignedDivide", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `/`*[T: uint64 | uint32 | uint16 | uint8](a, b: T): T {
|
||||
#pragma[magic: "Divide", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `/`*(a, b: float64): float64 {
|
||||
#pragma[magic: "DivFloat64", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `/`*(a, b: float32): float32 {
|
||||
#pragma[magic: "DivFloat32", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `**`*[T: int | int32 | int16 | int8](a, b: T): T {
|
||||
#pragma[magic: "SignedPow", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `**`*[T: uint64 | uint32 | uint16 | uint8](a, b: T): T {
|
||||
#pragma[magic: "Pow", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `%`*(a, b: int64): int64 {
|
||||
#pragma[magic: "SignedMod", pure]
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
# Bitwise operations on primitive types
|
||||
|
||||
operator `&`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](a, b: T): T {
|
||||
#pragma[magic: "And", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `|`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](a, b: T): T {
|
||||
#pragma[magic: "Or", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `~`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](a: T): T {
|
||||
#pragma[magic: "Not", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `>>`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](a, b: T): T {
|
||||
#pragma[magic: "RShift", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `<<`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](a, b: T): T {
|
||||
#pragma[magic: "LShift", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `^`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8](a, b: T): T {
|
||||
#pragma[magic: "Xor", pure]
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
# Comparison operators
|
||||
|
||||
operator `>`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8 | float | float32](a, b: T): bool {
|
||||
#pragma[magic: "GreaterThan", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `<`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8 | float | float32](a, b: T): bool {
|
||||
#pragma[magic: "LessThan", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `==`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8 | float | float32](a, b: T): bool {
|
||||
#pragma[magic: "Equal", pure]
|
||||
|
||||
}
|
||||
|
||||
operator `!=`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8 | float | float32](a, b: T): bool {
|
||||
#pragma[magic: "NotEqual", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `>=`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8 | float | float32](a, b: T): bool {
|
||||
#pragma[magic: "GreaterOrEqual", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `<=`*[T: int | uint64 | int32 | uint32 | int16 | uint16 | int8 | uint8 | float | float32](a, b: T): bool {
|
||||
#pragma[magic: "LessOrEqual", pure]
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
# Logical operators
|
||||
|
||||
operator `and`*(a, b: bool): bool {
|
||||
#pragma[magic: "LogicalAnd", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `or`*(a, b: bool): bool {
|
||||
#pragma[magic: "LogicalOr", pure]
|
||||
}
|
||||
|
||||
|
||||
operator `not`*(a: bool): bool {
|
||||
#pragma[magic: "LogicalNot", pure]
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
# Assignment operators
|
||||
|
||||
operator `=`*[T: all](a: var T, b: T) { # TODO: This is just a placeholder right now
|
||||
#pragma[magic: "GenericAssign"]
|
||||
}
|
||||
|
||||
# Some useful builtins
|
||||
|
||||
fn clock*: float {
|
||||
#pragma[magic: "SysClock64", pure]
|
||||
}
|
||||
|
||||
|
||||
fn print*(x: int) {
|
||||
#pragma[magic: "PrintInt64"]
|
||||
}
|
||||
|
||||
|
||||
fn print*(x: uint64) {
|
||||
#pragma[magic: "PrintUInt64"]
|
||||
}
|
||||
|
||||
|
||||
fn print*(x: float) {
|
||||
#pragma[magic: "PrintFloat64"]
|
||||
}
|
||||
|
||||
|
||||
fn print*(x: string) {
|
||||
#pragma[magic: "PrintString"]
|
||||
}
|
||||
|
||||
|
||||
fn print*(x: bool) {
|
||||
#pragma[magic: "PrintBool"]
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
## The peon standard library
|
||||
|
||||
import builtins/arithmetics;
|
||||
import builtins/bitwise;
|
||||
import builtins/logical;
|
||||
import builtins/misc;
|
||||
import builtins/comparisons;
|
||||
|
||||
|
||||
export arithmetics;
|
||||
export bitwise;
|
||||
export logical;
|
||||
export misc;
|
Loading…
Reference in New Issue