Store builtins as a Map

This commit is contained in:
Richard Feldman 2020-06-06 23:41:09 -04:00
parent a1d15f42bb
commit 9e02537ebb
3 changed files with 33 additions and 12 deletions

View file

@ -1,6 +1,6 @@
use crate::def::Def; use crate::def::Def;
use crate::expr::{Expr, Recursive}; use crate::expr::{Expr, Recursive};
use roc_collections::all::SendMap; use roc_collections::all::{MutMap, SendMap};
use roc_module::ident::TagName; use roc_module::ident::TagName;
use roc_module::low_level::LowLevel; use roc_module::low_level::LowLevel;
use roc_module::operator::CalledVia; use roc_module::operator::CalledVia;
@ -25,17 +25,17 @@ use roc_types::subs::{VarStore, Variable};
/// delegates to the compiler-internal List.getUnsafe function to do the actual /// delegates to the compiler-internal List.getUnsafe function to do the actual
/// lookup (if the bounds check passed). That internal function is hardcoded in code gen, /// lookup (if the bounds check passed). That internal function is hardcoded in code gen,
/// which works fine because it doesn't involve any open tag unions. /// which works fine because it doesn't involve any open tag unions.
pub fn builtin_defs(var_store: &VarStore) -> Vec<Def> { pub fn builtin_defs(var_store: &VarStore) -> MutMap<Symbol, Def> {
vec![ mut_map! {
list_len(var_store), Symbol::LIST_LEN => list_len(var_store),
list_get(var_store), Symbol::LIST_GET => list_get(var_store),
list_first(var_store), Symbol::LIST_FIRST => list_first(var_store),
int_div(var_store), Symbol::INT_DIV => int_div(var_store),
int_abs(var_store), Symbol::INT_ABS => int_abs(var_store),
int_rem(var_store), Symbol::INT_REM => int_rem(var_store),
int_is_odd(var_store), Symbol::INT_IS_ODD => int_is_odd(var_store),
int_is_even(var_store), Symbol::INT_IS_EVEN => int_is_even(var_store)
] }
} }
/// Int.isOdd : Int -> Bool /// Int.isOdd : Int -> Bool

View file

@ -24,3 +24,6 @@ pub mod pattern;
pub mod procedure; pub mod procedure;
pub mod scope; pub mod scope;
pub mod string; pub mod string;
#[macro_use]
extern crate roc_collections;

View file

@ -134,3 +134,21 @@ fn int_to_ordinal(number: usize) -> std::string::String {
format!("{}{}", number, ending) format!("{}{}", number, ending)
} }
#[macro_export]
macro_rules! mut_map {
(@single $($x:tt)*) => (());
(@count $($rest:expr),*) => (<[()]>::len(&[$(mut_map!(@single $rest)),*]));
($($key:expr => $value:expr,)+) => { mut_map!($($key => $value),+) };
($($key:expr => $value:expr),*) => {
{
let _cap = mut_map!(@count $($key),*);
let mut _map = ::std::collections::HashMap::with_capacity_and_hasher(_cap, $crate::all::default_hasher());
$(
let _ = _map.insert($key, $value);
)*
_map
}
};
}