mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 07:41:12 +00:00
Don't have builtins return Defs
This commit is contained in:
parent
093579780f
commit
e726d838ea
2 changed files with 34 additions and 29 deletions
|
@ -1,6 +1,5 @@
|
||||||
use crate::def::Def;
|
|
||||||
use crate::expr::{Expr, Recursive};
|
use crate::expr::{Expr, Recursive};
|
||||||
use roc_collections::all::{MutMap, SendMap};
|
use roc_collections::all::MutMap;
|
||||||
use roc_module::ident::TagName;
|
use roc_module::ident::TagName;
|
||||||
use roc_module::operator::CalledVia;
|
use roc_module::operator::CalledVia;
|
||||||
use roc_module::symbol::Symbol;
|
use roc_module::symbol::Symbol;
|
||||||
|
@ -24,7 +23,7 @@ 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) -> MutMap<Symbol, Def> {
|
pub fn builtin_defs(var_store: &VarStore) -> MutMap<Symbol, Expr> {
|
||||||
mut_map! {
|
mut_map! {
|
||||||
Symbol::LIST_GET => list_get(var_store),
|
Symbol::LIST_GET => list_get(var_store),
|
||||||
Symbol::LIST_FIRST => list_first(var_store),
|
Symbol::LIST_FIRST => list_first(var_store),
|
||||||
|
@ -43,7 +42,7 @@ pub fn builtin_defs(var_store: &VarStore) -> MutMap<Symbol, Def> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Float.isZero : Float -> Bool
|
/// Float.isZero : Float -> Bool
|
||||||
fn float_is_zero(var_store: &VarStore) -> Def {
|
fn float_is_zero(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -62,7 +61,7 @@ fn float_is_zero(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Float.isNegative : Float -> Bool
|
/// Float.isNegative : Float -> Bool
|
||||||
fn float_is_negative(var_store: &VarStore) -> Def {
|
fn float_is_negative(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -81,7 +80,7 @@ fn float_is_negative(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Float.isPositive : Float -> Bool
|
/// Float.isPositive : Float -> Bool
|
||||||
fn float_is_positive(var_store: &VarStore) -> Def {
|
fn float_is_positive(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -100,7 +99,7 @@ fn float_is_positive(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Int.isNegative : Int -> Bool
|
/// Int.isNegative : Int -> Bool
|
||||||
fn int_is_negative(var_store: &VarStore) -> Def {
|
fn int_is_negative(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -116,7 +115,7 @@ fn int_is_negative(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Int.isPositive : Int -> Bool
|
/// Int.isPositive : Int -> Bool
|
||||||
fn int_is_positive(var_store: &VarStore) -> Def {
|
fn int_is_positive(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -132,7 +131,7 @@ fn int_is_positive(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Int.isZero : Int -> Bool
|
/// Int.isZero : Int -> Bool
|
||||||
fn int_is_zero(var_store: &VarStore) -> Def {
|
fn int_is_zero(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -148,7 +147,7 @@ fn int_is_zero(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Int.isOdd : Int -> Bool
|
/// Int.isOdd : Int -> Bool
|
||||||
fn int_is_odd(var_store: &VarStore) -> Def {
|
fn int_is_odd(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -171,7 +170,7 @@ fn int_is_odd(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Int.isEven : Int -> Bool
|
/// Int.isEven : Int -> Bool
|
||||||
fn int_is_even(var_store: &VarStore) -> Def {
|
fn int_is_even(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -194,7 +193,7 @@ fn int_is_even(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List.get : List elem, Int -> Result elem [ OutOfBounds ]*
|
/// List.get : List elem, Int -> Result elem [ OutOfBounds ]*
|
||||||
fn list_get(var_store: &VarStore) -> Def {
|
fn list_get(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -265,7 +264,7 @@ fn list_get(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Int.rem : Int, Int -> Int
|
/// Int.rem : Int, Int -> Int
|
||||||
fn int_rem(var_store: &VarStore) -> Def {
|
fn int_rem(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -312,7 +311,7 @@ fn int_rem(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Int.abs : Int -> Int
|
/// Int.abs : Int -> Int
|
||||||
fn int_abs(var_store: &VarStore) -> Def {
|
fn int_abs(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -349,7 +348,7 @@ fn int_abs(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Int.div : Int, Int -> Result Int [ DivByZero ]*
|
/// Int.div : Int, Int -> Result Int [ DivByZero ]*
|
||||||
fn int_div(var_store: &VarStore) -> Def {
|
fn int_div(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -408,7 +407,7 @@ fn int_div(var_store: &VarStore) -> Def {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// List.first : List elem -> Result elem [ ListWasEmpty ]*
|
/// List.first : List elem -> Result elem [ ListWasEmpty ]*
|
||||||
fn list_first(var_store: &VarStore) -> Def {
|
fn list_first(var_store: &VarStore) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
|
|
||||||
defn(
|
defn(
|
||||||
|
@ -500,7 +499,7 @@ fn call(symbol: Symbol, args: Vec<Expr>, var_store: &VarStore) -> Expr {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn defn(fn_name: Symbol, args: Vec<Symbol>, var_store: &VarStore, body: Expr) -> Def {
|
fn defn(fn_name: Symbol, args: Vec<Symbol>, var_store: &VarStore, body: Expr) -> Expr {
|
||||||
use crate::expr::Expr::*;
|
use crate::expr::Expr::*;
|
||||||
use crate::pattern::Pattern::*;
|
use crate::pattern::Pattern::*;
|
||||||
|
|
||||||
|
@ -509,19 +508,11 @@ fn defn(fn_name: Symbol, args: Vec<Symbol>, var_store: &VarStore, body: Expr) ->
|
||||||
.map(|symbol| (var_store.fresh(), no_region(Identifier(symbol))))
|
.map(|symbol| (var_store.fresh(), no_region(Identifier(symbol))))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let expr = Closure(
|
Closure(
|
||||||
var_store.fresh(),
|
var_store.fresh(),
|
||||||
fn_name,
|
fn_name,
|
||||||
Recursive::NotRecursive,
|
Recursive::NotRecursive,
|
||||||
closure_args,
|
closure_args,
|
||||||
Box::new((no_region(body), var_store.fresh())),
|
Box::new((no_region(body), var_store.fresh())),
|
||||||
);
|
)
|
||||||
|
|
||||||
Def {
|
|
||||||
loc_pattern: no_region(Identifier(fn_name)),
|
|
||||||
loc_expr: no_region(expr),
|
|
||||||
expr_var: var_store.fresh(),
|
|
||||||
pattern_vars: SendMap::default(),
|
|
||||||
annotation: None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,12 @@ pub mod eval;
|
||||||
use self::bumpalo::Bump;
|
use self::bumpalo::Bump;
|
||||||
use roc_builtins::unique::uniq_stdlib;
|
use roc_builtins::unique::uniq_stdlib;
|
||||||
use roc_can::constraint::Constraint;
|
use roc_can::constraint::Constraint;
|
||||||
|
use roc_can::def::Def;
|
||||||
use roc_can::env::Env;
|
use roc_can::env::Env;
|
||||||
use roc_can::expected::Expected;
|
use roc_can::expected::Expected;
|
||||||
use roc_can::expr::{canonicalize_expr, Expr, Output};
|
use roc_can::expr::{canonicalize_expr, Expr, Output};
|
||||||
use roc_can::operator;
|
use roc_can::operator;
|
||||||
|
use roc_can::pattern::Pattern;
|
||||||
use roc_can::scope::Scope;
|
use roc_can::scope::Scope;
|
||||||
use roc_collections::all::{ImMap, ImSet, MutMap, SendMap, SendSet};
|
use roc_collections::all::{ImMap, ImSet, MutMap, SendMap, SendSet};
|
||||||
use roc_constrain::expr::constrain_expr;
|
use roc_constrain::expr::constrain_expr;
|
||||||
|
@ -245,11 +247,23 @@ pub fn can_expr_with(arena: &Bump, home: ModuleId, expr_str: &str) -> CanExprOut
|
||||||
// since we aren't using modules here.
|
// since we aren't using modules here.
|
||||||
let builtin_defs = roc_can::builtins::builtin_defs(&var_store);
|
let builtin_defs = roc_can::builtins::builtin_defs(&var_store);
|
||||||
|
|
||||||
for (symbol, def) in builtin_defs {
|
for (symbol, expr) in builtin_defs {
|
||||||
if output.references.lookups.contains(&symbol) || output.references.calls.contains(&symbol)
|
if output.references.lookups.contains(&symbol) || output.references.calls.contains(&symbol)
|
||||||
{
|
{
|
||||||
with_builtins = Expr::LetNonRec(
|
with_builtins = Expr::LetNonRec(
|
||||||
Box::new(def),
|
Box::new(Def {
|
||||||
|
loc_pattern: Located {
|
||||||
|
region: Region::zero(),
|
||||||
|
value: Pattern::Identifier(symbol),
|
||||||
|
},
|
||||||
|
loc_expr: Located {
|
||||||
|
region: Region::zero(),
|
||||||
|
value: expr,
|
||||||
|
},
|
||||||
|
expr_var: var_store.fresh(),
|
||||||
|
pattern_vars: SendMap::default(),
|
||||||
|
annotation: None,
|
||||||
|
}),
|
||||||
Box::new(Located {
|
Box::new(Located {
|
||||||
region: Region::zero(),
|
region: Region::zero(),
|
||||||
value: with_builtins,
|
value: with_builtins,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue