2019-08-12 13:49:39 -07:00
|
|
|
use std::str::FromStr;
|
2021-10-16 20:21:08 -07:00
|
|
|
use std::convert::TryFrom;
|
2018-02-23 19:06:37 -08:00
|
|
|
|
2019-02-10 04:30:37 -08:00
|
|
|
use crate::typechecking::{TypeConst, Type};
|
2021-10-16 20:21:08 -07:00
|
|
|
use crate::ast::{BinOp, PrefixOp};
|
2018-05-20 20:47:24 -07:00
|
|
|
|
2021-10-16 20:21:08 -07:00
|
|
|
/// "Builtin" computational operations with some kind of semantics, mostly mathematical operations.
|
2019-08-13 04:17:17 -07:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2019-08-12 14:10:07 -07:00
|
|
|
pub enum Builtin {
|
2019-08-12 13:10:22 -07:00
|
|
|
Add,
|
2019-08-14 07:25:45 -07:00
|
|
|
Increment,
|
2019-08-12 13:10:22 -07:00
|
|
|
Subtract,
|
2019-08-14 07:25:45 -07:00
|
|
|
Negate,
|
2019-08-12 13:10:22 -07:00
|
|
|
Multiply,
|
|
|
|
Divide,
|
|
|
|
Quotient,
|
|
|
|
Modulo,
|
|
|
|
Exponentiation,
|
|
|
|
BitwiseAnd,
|
|
|
|
BitwiseOr,
|
|
|
|
BooleanAnd,
|
|
|
|
BooleanOr,
|
|
|
|
BooleanNot,
|
|
|
|
Equality,
|
|
|
|
LessThan,
|
2019-08-12 13:49:39 -07:00
|
|
|
LessThanOrEqual,
|
2019-08-12 13:10:22 -07:00
|
|
|
GreaterThan,
|
2019-08-12 13:49:39 -07:00
|
|
|
GreaterThanOrEqual,
|
2019-08-12 13:10:22 -07:00
|
|
|
Comparison,
|
|
|
|
FieldAccess,
|
|
|
|
IOPrint,
|
|
|
|
IOPrintLn,
|
|
|
|
IOGetLine,
|
2019-08-12 13:49:39 -07:00
|
|
|
Assignment,
|
|
|
|
Concatenate,
|
|
|
|
}
|
|
|
|
|
2019-08-13 04:17:17 -07:00
|
|
|
impl Builtin {
|
|
|
|
pub fn get_type(&self) -> Type {
|
2019-08-13 04:28:21 -07:00
|
|
|
use Builtin::*;
|
|
|
|
match self {
|
|
|
|
Add => ty!(Nat -> Nat -> Nat),
|
|
|
|
Subtract => ty!(Nat -> Nat -> Nat),
|
|
|
|
Multiply => ty!(Nat -> Nat -> Nat),
|
|
|
|
Divide => ty!(Nat -> Nat -> Float),
|
|
|
|
Quotient => ty!(Nat -> Nat -> Nat),
|
|
|
|
Modulo => ty!(Nat -> Nat -> Nat),
|
|
|
|
Exponentiation => ty!(Nat -> Nat -> Nat),
|
|
|
|
BitwiseAnd => ty!(Nat -> Nat -> Nat),
|
|
|
|
BitwiseOr => ty!(Nat -> Nat -> Nat),
|
|
|
|
BooleanAnd => ty!(Bool -> Bool -> Bool),
|
|
|
|
BooleanOr => ty!(Bool -> Bool -> Bool),
|
|
|
|
BooleanNot => ty!(Bool -> Bool),
|
|
|
|
Equality => ty!(Nat -> Nat -> Bool),
|
|
|
|
LessThan => ty!(Nat -> Nat -> Bool),
|
|
|
|
LessThanOrEqual => ty!(Nat -> Nat -> Bool),
|
|
|
|
GreaterThan => ty!(Nat -> Nat -> Bool),
|
|
|
|
GreaterThanOrEqual => ty!(Nat -> Nat -> Bool),
|
|
|
|
Comparison => ty!(Nat -> Nat -> Ordering),
|
|
|
|
FieldAccess => ty!(Unit),
|
|
|
|
IOPrint => ty!(Unit),
|
|
|
|
IOPrintLn => ty!(Unit) ,
|
|
|
|
IOGetLine => ty!(StringT),
|
|
|
|
Assignment => ty!(Unit),
|
|
|
|
Concatenate => ty!(StringT -> StringT -> StringT),
|
2019-08-14 07:25:45 -07:00
|
|
|
Increment => ty!(Nat -> Int),
|
|
|
|
Negate => ty!(Nat -> Int)
|
2019-08-13 04:28:21 -07:00
|
|
|
}
|
2019-08-13 04:17:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 20:21:08 -07:00
|
|
|
impl TryFrom<&BinOp> for Builtin {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn try_from(binop: &BinOp) -> Result<Self, Self::Error> {
|
|
|
|
FromStr::from_str(binop.sigil())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&PrefixOp> for Builtin {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn try_from(prefix_op: &PrefixOp) -> Result<Self, Self::Error> {
|
|
|
|
use Builtin::*;
|
|
|
|
|
|
|
|
match prefix_op.sigil() {
|
|
|
|
"+" => Ok(Increment),
|
|
|
|
"-" => Ok(Negate),
|
|
|
|
"!" => Ok(BooleanNot),
|
|
|
|
_ => Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:49:39 -07:00
|
|
|
impl FromStr for Builtin {
|
|
|
|
type Err = ();
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
use Builtin::*;
|
|
|
|
Ok(match s {
|
|
|
|
"+" => Add,
|
|
|
|
"-" => Subtract,
|
|
|
|
"*" => Multiply,
|
|
|
|
"/" => Divide,
|
|
|
|
"quot" => Quotient,
|
|
|
|
"%" => Modulo,
|
|
|
|
"++" => Concatenate,
|
|
|
|
"^" => Exponentiation,
|
|
|
|
"&" => BitwiseAnd,
|
|
|
|
"&&" => BooleanAnd,
|
|
|
|
"|" => BitwiseOr,
|
|
|
|
"||" => BooleanOr,
|
|
|
|
"!" => BooleanNot,
|
|
|
|
">" => GreaterThan,
|
|
|
|
">=" => GreaterThanOrEqual,
|
|
|
|
"<" => LessThan,
|
|
|
|
"<=" => LessThanOrEqual,
|
|
|
|
"==" => Equality,
|
|
|
|
"=" => Assignment,
|
|
|
|
"<=>" => Comparison,
|
|
|
|
"." => FieldAccess,
|
|
|
|
"print" => IOPrint,
|
|
|
|
"println" => IOPrintLn,
|
|
|
|
"getline" => IOGetLine,
|
|
|
|
_ => return Err(())
|
|
|
|
})
|
|
|
|
}
|
2019-08-12 13:10:22 -07:00
|
|
|
}
|