schala/schala-lang/language/src/builtin.rs

123 lines
3.4 KiB
Rust
Raw Normal View History

2018-02-23 19:06:37 -08:00
use std::rc::Rc;
use std::collections::HashMap;
2018-02-23 19:06:37 -08:00
2019-01-07 13:00:37 -08:00
use crate::tokenizing::TokenKind;
use crate::typechecking::{TypeConst, Type};
2018-02-23 19:06:37 -08:00
#[derive(Debug, PartialEq, Clone)]
pub struct BinOp {
2018-02-24 14:39:45 -08:00
sigil: Rc<String>
2018-02-23 19:06:37 -08:00
}
impl BinOp {
pub fn from_sigil(sigil: &str) -> BinOp {
BinOp { sigil: Rc::new(sigil.to_string()) }
2018-02-23 19:06:37 -08:00
}
2018-02-24 14:39:45 -08:00
pub fn sigil(&self) -> &Rc<String> {
&self.sigil
}
2018-11-16 23:17:34 -08:00
pub fn from_sigil_token(tok: &TokenKind) -> Option<BinOp> {
use self::TokenKind::*;
2018-08-19 20:33:50 -07:00
let s = match tok {
Operator(op) => op,
Period => ".",
Pipe => "|",
Slash => "/",
LAngleBracket => "<",
RAngleBracket => ">",
2018-08-19 20:33:50 -07:00
_ => return None
};
Some(BinOp::from_sigil(s))
}
2019-02-20 03:27:46 -08:00
pub fn get_type(&self) -> Result<Type, String> {
let s = self.sigil.as_str();
BINOPS.get(s).map(|x| x.0.clone()).ok_or(format!("Binop {} not found", s))
}
2019-02-20 03:27:46 -08:00
2018-02-23 19:06:37 -08:00
pub fn min_precedence() -> i32 {
i32::min_value()
}
2018-11-16 23:17:34 -08:00
pub fn get_precedence_from_token(op: &TokenKind) -> Option<i32> {
use self::TokenKind::*;
2018-08-19 20:33:50 -07:00
let s = match op {
Operator(op) => op,
Period => ".",
Pipe => "|",
Slash => "/",
LAngleBracket => "<",
RAngleBracket => ">",
2018-08-19 20:33:50 -07:00
_ => return None
};
let default = 10_000_000;
Some(BINOPS.get(s).map(|x| x.2.clone()).unwrap_or_else(|| {
default
}))
2018-02-23 19:06:37 -08:00
}
2018-10-19 11:02:10 -07:00
pub fn get_precedence(&self) -> i32 {
let s: &str = &self.sigil;
let default = 10_000_000;
BINOPS.get(s).map(|x| x.2.clone()).unwrap_or_else(|| {
default
})
}
2018-02-23 19:06:37 -08:00
}
#[derive(Debug, PartialEq, Clone)]
pub struct PrefixOp {
sigil: Rc<String>
}
2018-02-23 19:06:37 -08:00
impl PrefixOp {
pub fn from_sigil(sigil: &str) -> PrefixOp {
PrefixOp { sigil: Rc::new(sigil.to_string()) }
2018-02-23 19:06:37 -08:00
}
2018-02-24 14:39:45 -08:00
pub fn sigil(&self) -> &Rc<String> {
&self.sigil
}
2018-02-23 19:06:37 -08:00
pub fn is_prefix(op: &str) -> bool {
2018-02-24 17:43:26 -08:00
PREFIX_OPS.get(op).is_some()
2018-02-23 19:06:37 -08:00
}
pub fn get_type(&self) -> Result<Type, String> {
2018-02-26 02:21:21 -08:00
let s = self.sigil.as_str();
PREFIX_OPS.get(s).map(|x| x.0.clone()).ok_or(format!("Prefix op {} not found", s))
}
2018-02-23 19:06:37 -08:00
}
2018-02-24 17:43:26 -08:00
lazy_static! {
static ref PREFIX_OPS: HashMap<&'static str, (Type, ())> =
2018-02-24 17:43:26 -08:00
hashmap! {
"+" => (ty!(Nat -> Int), ()),
"-" => (ty!(Nat -> Int), ()),
"!" => (ty!(Bool -> Bool), ()),
2018-02-24 17:43:26 -08:00
};
}
/* the second tuple member is a placeholder for when I want to make evaluation rules tied to the
* binop definition */
//TODO some of these types are going to have to be adjusted
lazy_static! {
static ref BINOPS: HashMap<&'static str, (Type, (), i32)> =
hashmap! {
"+" => (ty!(Nat -> Nat -> Nat), (), 10),
"-" => (ty!(Nat -> Nat -> Nat), (), 10),
"*" => (ty!(Nat -> Nat -> Nat), (), 20),
"/" => (ty!(Nat -> Nat -> Float), (), 20),
"quot" => (ty!(Nat -> Nat -> Nat), (), 20),
"%" => (ty!(Nat -> Nat -> Nat), (), 20),
"++" => (ty!(StringT -> StringT -> StringT), (), 30),
"^" => (ty!(Nat -> Nat -> Nat), (), 20),
"&" => (ty!(Nat -> Nat -> Nat), (), 20),
"|" => (ty!(Nat -> Nat -> Nat), (), 20),
">" => (ty!(Nat -> Nat -> Bool), (), 20),
">=" => (ty!(Nat -> Nat -> Bool), (), 20),
"<" => (ty!(Nat -> Nat -> Bool), (), 20),
"<=" => (ty!(Nat -> Nat -> Bool), (), 20),
"==" => (ty!(Nat -> Nat -> Bool), (), 20),
"=" => (ty!(Unit), (), 20), //TODO not sure what the type of this should be b/c special fmr
"<=>" => (ty!(Nat -> Nat -> Ordering), (), 20), //TODO figure out how to treat Order
};
}