schala/schala-lang/src/builtin.rs

78 lines
2.6 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
2018-03-23 18:43:43 -07:00
use typechecking::{Type, TypeResult, TConst};
use self::Type::*; use self::TConst::*;
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
}
pub fn get_type(&self) -> TypeResult<Type> {
let s = self.sigil.as_str();
BINOPS.get(s).map(|x| x.0.clone()).ok_or(format!("Binop {} not found", s))
}
2018-02-23 19:06:37 -08:00
pub fn min_precedence() -> i32 {
i32::min_value()
}
pub fn get_precedence(op: &str) -> i32 {
2018-02-24 17:43:26 -08:00
let default = 10_000_000;
BINOPS.get(op).map(|x| x.2.clone()).unwrap_or(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
}
2018-02-26 02:21:21 -08:00
pub fn get_type(&self) -> TypeResult<Type> {
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, ())> =
hashmap! {
"+" => (Func(bx!(Const(Int)), bx!(Const(Int))), ()),
"-" => (Func(bx!(Const(Int)), bx!(Const(Int))), ()),
"!" => (Func(bx!(Const(Bool)), bx!(Const(Bool))), ()),
};
}
/* the second tuple member is a placeholder for when I want to make evaluation rules tied to the
* binop definition */
lazy_static! {
static ref BINOPS: HashMap<&'static str, (Type, (), i32)> =
hashmap! {
"+" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 10),
"-" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 10),
"*" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
2018-02-26 02:11:56 -08:00
"/" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Float))))), (), 20),
2018-03-17 19:12:58 -07:00
"//" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20), //TODO change this to `quot`
"%" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
"++" => (Func(bx!(Const(StringT)), bx!(Func(bx!(Const(StringT)), bx!(Const(StringT))))), (), 30),
2018-02-26 02:21:21 -08:00
"^" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
"&" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
"|" => (Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))), (), 20),
};
}