use std::str::FromStr; use std::convert::TryFrom; use crate::typechecking::{TypeConst, Type}; use crate::ast::{BinOp, PrefixOp}; /// "Builtin" computational operations with some kind of semantics, mostly mathematical operations. #[derive(Debug, Clone, Copy, PartialEq)] pub enum Builtin { Add, Increment, Subtract, Negate, Multiply, Divide, Quotient, Modulo, Exponentiation, BitwiseAnd, BitwiseOr, BooleanAnd, BooleanOr, BooleanNot, Equality, LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual, Comparison, FieldAccess, IOPrint, IOPrintLn, IOGetLine, Assignment, Concatenate, } impl Builtin { pub fn get_type(&self) -> Type { 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), Increment => ty!(Nat -> Int), Negate => ty!(Nat -> Int) } } } impl TryFrom<&BinOp> for Builtin { type Error = (); fn try_from(binop: &BinOp) -> Result { FromStr::from_str(binop.sigil()) } } impl TryFrom<&PrefixOp> for Builtin { type Error = (); fn try_from(prefix_op: &PrefixOp) -> Result { use Builtin::*; match prefix_op.sigil() { "+" => Ok(Increment), "-" => Ok(Negate), "!" => Ok(BooleanNot), _ => Err(()) } } } impl FromStr for Builtin { type Err = (); fn from_str(s: &str) -> Result { 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(()) }) } }