mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 07:41:12 +00:00
Load main
in the CLI and use it for gen
This commit is contained in:
parent
9ca754b8fd
commit
1517581ced
6 changed files with 84 additions and 424 deletions
|
@ -1,396 +0,0 @@
|
||||||
use bumpalo::Bump;
|
|
||||||
use roc_builtins::unique::uniq_stdlib;
|
|
||||||
use roc_can::constraint::Constraint;
|
|
||||||
use roc_can::env::Env;
|
|
||||||
use roc_can::expected::Expected;
|
|
||||||
use roc_can::expr::{canonicalize_expr, Expr, Output};
|
|
||||||
use roc_can::operator;
|
|
||||||
use roc_can::scope::Scope;
|
|
||||||
use roc_collections::all::{ImMap, ImSet, MutMap, SendMap, SendSet};
|
|
||||||
use roc_constrain::expr::constrain_expr;
|
|
||||||
use roc_constrain::module::{constrain_imported_values, load_builtin_aliases, Import};
|
|
||||||
use roc_module::ident::Ident;
|
|
||||||
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds, Symbol};
|
|
||||||
use roc_parse::ast::{self, Attempting};
|
|
||||||
use roc_parse::blankspace::space0_before;
|
|
||||||
use roc_parse::parser::{loc, Fail, Parser, State};
|
|
||||||
use roc_problem::can::Problem;
|
|
||||||
use roc_region::all::{Located, Region};
|
|
||||||
use roc_solve::solve;
|
|
||||||
use roc_types::subs::{Content, Subs, VarStore, Variable};
|
|
||||||
use roc_types::types::Type;
|
|
||||||
use std::hash::Hash;
|
|
||||||
|
|
||||||
pub fn test_home() -> ModuleId {
|
|
||||||
ModuleIds::default().get_or_insert(&"Test".into())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn infer_expr(
|
|
||||||
subs: Subs,
|
|
||||||
problems: &mut Vec<solve::TypeError>,
|
|
||||||
constraint: &Constraint,
|
|
||||||
expr_var: Variable,
|
|
||||||
) -> (Content, Subs) {
|
|
||||||
let env = solve::Env {
|
|
||||||
aliases: MutMap::default(),
|
|
||||||
vars_by_symbol: SendMap::default(),
|
|
||||||
};
|
|
||||||
let (solved, _) = solve::run(&env, problems, subs, constraint);
|
|
||||||
|
|
||||||
let content = solved.inner().get_without_compacting(expr_var).content;
|
|
||||||
|
|
||||||
(content, solved.into_inner())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_with<'a>(arena: &'a Bump, input: &'a str) -> Result<ast::Expr<'a>, Fail> {
|
|
||||||
parse_loc_with(arena, input).map(|loc_expr| loc_expr.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_loc_with<'a>(arena: &'a Bump, input: &'a str) -> Result<Located<ast::Expr<'a>>, Fail> {
|
|
||||||
let state = State::new(&input, Attempting::Module);
|
|
||||||
let parser = space0_before(loc(roc_parse::expr::expr(0)), 0);
|
|
||||||
let answer = parser.parse(&arena, state);
|
|
||||||
|
|
||||||
answer
|
|
||||||
.map(|(loc_expr, _)| loc_expr)
|
|
||||||
.map_err(|(fail, _)| fail)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn can_expr(expr_str: &str) -> CanExprOut {
|
|
||||||
can_expr_with(&Bump::new(), test_home(), expr_str)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn uniq_expr(
|
|
||||||
expr_str: &str,
|
|
||||||
) -> (
|
|
||||||
Located<Expr>,
|
|
||||||
Output,
|
|
||||||
Vec<Problem>,
|
|
||||||
Subs,
|
|
||||||
Variable,
|
|
||||||
Constraint,
|
|
||||||
ModuleId,
|
|
||||||
Interns,
|
|
||||||
) {
|
|
||||||
let declared_idents: &ImMap<Ident, (Symbol, Region)> = &ImMap::default();
|
|
||||||
|
|
||||||
uniq_expr_with(&Bump::new(), expr_str, declared_idents)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn uniq_expr_with(
|
|
||||||
arena: &Bump,
|
|
||||||
expr_str: &str,
|
|
||||||
declared_idents: &ImMap<Ident, (Symbol, Region)>,
|
|
||||||
) -> (
|
|
||||||
Located<Expr>,
|
|
||||||
Output,
|
|
||||||
Vec<Problem>,
|
|
||||||
Subs,
|
|
||||||
Variable,
|
|
||||||
Constraint,
|
|
||||||
ModuleId,
|
|
||||||
Interns,
|
|
||||||
) {
|
|
||||||
let home = test_home();
|
|
||||||
let CanExprOut {
|
|
||||||
loc_expr,
|
|
||||||
output,
|
|
||||||
problems,
|
|
||||||
var_store: old_var_store,
|
|
||||||
var,
|
|
||||||
interns,
|
|
||||||
..
|
|
||||||
} = can_expr_with(arena, home, expr_str);
|
|
||||||
|
|
||||||
// double check
|
|
||||||
let var_store = VarStore::new(old_var_store.fresh());
|
|
||||||
|
|
||||||
let expected2 = Expected::NoExpectation(Type::Variable(var));
|
|
||||||
let constraint = roc_constrain::uniq::constrain_declaration(
|
|
||||||
home,
|
|
||||||
&var_store,
|
|
||||||
Region::zero(),
|
|
||||||
&loc_expr,
|
|
||||||
declared_idents,
|
|
||||||
expected2,
|
|
||||||
);
|
|
||||||
|
|
||||||
let stdlib = uniq_stdlib();
|
|
||||||
|
|
||||||
let types = stdlib.types;
|
|
||||||
let imports: Vec<_> = types
|
|
||||||
.iter()
|
|
||||||
.map(|(symbol, (solved_type, region))| Import {
|
|
||||||
loc_symbol: Located::at(*region, *symbol),
|
|
||||||
solved_type,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// load builtin values
|
|
||||||
|
|
||||||
// TODO what to do with those rigids?
|
|
||||||
let (_introduced_rigids, constraint) =
|
|
||||||
constrain_imported_values(imports, constraint, &var_store);
|
|
||||||
|
|
||||||
// load builtin types
|
|
||||||
let mut constraint = load_builtin_aliases(&stdlib.aliases, constraint, &var_store);
|
|
||||||
|
|
||||||
constraint.instantiate_aliases(&var_store);
|
|
||||||
|
|
||||||
let subs2 = Subs::new(var_store.into());
|
|
||||||
|
|
||||||
(
|
|
||||||
loc_expr, output, problems, subs2, var, constraint, home, interns,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct CanExprOut {
|
|
||||||
pub loc_expr: Located<Expr>,
|
|
||||||
pub output: Output,
|
|
||||||
pub problems: Vec<Problem>,
|
|
||||||
pub home: ModuleId,
|
|
||||||
pub interns: Interns,
|
|
||||||
pub var_store: VarStore,
|
|
||||||
pub var: Variable,
|
|
||||||
pub constraint: Constraint,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn can_expr_with(arena: &Bump, home: ModuleId, expr_str: &str) -> CanExprOut {
|
|
||||||
let loc_expr = parse_loc_with(&arena, expr_str).unwrap_or_else(|e| {
|
|
||||||
panic!(
|
|
||||||
"can_expr_with() got a parse error when attempting to canonicalize:\n\n{:?} {:?}",
|
|
||||||
expr_str, e
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
let var_store = VarStore::default();
|
|
||||||
let var = var_store.fresh();
|
|
||||||
let expected = Expected::NoExpectation(Type::Variable(var));
|
|
||||||
let module_ids = ModuleIds::default();
|
|
||||||
|
|
||||||
// Desugar operators (convert them to Apply calls, taking into account
|
|
||||||
// operator precedence and associativity rules), before doing other canonicalization.
|
|
||||||
//
|
|
||||||
// If we did this *during* canonicalization, then each time we
|
|
||||||
// visited a BinOp node we'd recursively try to apply this to each of its nested
|
|
||||||
// operators, and then again on *their* nested operators, ultimately applying the
|
|
||||||
// rules multiple times unnecessarily.
|
|
||||||
let loc_expr = operator::desugar_expr(arena, &loc_expr);
|
|
||||||
|
|
||||||
let mut scope = Scope::new(home);
|
|
||||||
let dep_idents = IdentIds::exposed_builtins(0);
|
|
||||||
let mut env = Env::new(home, dep_idents, &module_ids, IdentIds::default());
|
|
||||||
let (loc_expr, output) = canonicalize_expr(
|
|
||||||
&mut env,
|
|
||||||
&var_store,
|
|
||||||
&mut scope,
|
|
||||||
Region::zero(),
|
|
||||||
&loc_expr.value,
|
|
||||||
);
|
|
||||||
|
|
||||||
let constraint = constrain_expr(
|
|
||||||
&roc_constrain::expr::Env {
|
|
||||||
rigids: ImMap::default(),
|
|
||||||
home,
|
|
||||||
},
|
|
||||||
loc_expr.region,
|
|
||||||
&loc_expr.value,
|
|
||||||
expected,
|
|
||||||
);
|
|
||||||
|
|
||||||
let types = roc_builtins::std::types();
|
|
||||||
|
|
||||||
let imports: Vec<_> = types
|
|
||||||
.iter()
|
|
||||||
.map(|(symbol, (solved_type, region))| Import {
|
|
||||||
loc_symbol: Located::at(*region, *symbol),
|
|
||||||
solved_type,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
//load builtin values
|
|
||||||
let (_introduced_rigids, constraint) =
|
|
||||||
constrain_imported_values(imports, constraint, &var_store);
|
|
||||||
|
|
||||||
// TODO determine what to do with those rigids
|
|
||||||
// for var in introduced_rigids {
|
|
||||||
// output.ftv.insert(var, format!("internal_{:?}", var).into());
|
|
||||||
// }
|
|
||||||
|
|
||||||
//load builtin types
|
|
||||||
let mut constraint =
|
|
||||||
load_builtin_aliases(&roc_builtins::std::aliases(), constraint, &var_store);
|
|
||||||
|
|
||||||
constraint.instantiate_aliases(&var_store);
|
|
||||||
|
|
||||||
let mut all_ident_ids = MutMap::default();
|
|
||||||
|
|
||||||
// When pretty printing types, we may need the exposed builtins,
|
|
||||||
// so include them in the Interns we'll ultimately return.
|
|
||||||
for (module_id, ident_ids) in IdentIds::exposed_builtins(0) {
|
|
||||||
all_ident_ids.insert(module_id, ident_ids);
|
|
||||||
}
|
|
||||||
|
|
||||||
all_ident_ids.insert(home, env.ident_ids);
|
|
||||||
|
|
||||||
let interns = Interns {
|
|
||||||
module_ids: env.module_ids.clone(),
|
|
||||||
all_ident_ids,
|
|
||||||
};
|
|
||||||
|
|
||||||
CanExprOut {
|
|
||||||
loc_expr,
|
|
||||||
output,
|
|
||||||
problems: env.problems,
|
|
||||||
home: env.home,
|
|
||||||
var_store,
|
|
||||||
interns,
|
|
||||||
var,
|
|
||||||
constraint,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn mut_map_from_pairs<K, V, I>(pairs: I) -> MutMap<K, V>
|
|
||||||
where
|
|
||||||
I: IntoIterator<Item = (K, V)>,
|
|
||||||
K: Hash + Eq,
|
|
||||||
{
|
|
||||||
let mut answer = MutMap::default();
|
|
||||||
|
|
||||||
for (key, value) in pairs {
|
|
||||||
answer.insert(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
answer
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn im_map_from_pairs<K, V, I>(pairs: I) -> ImMap<K, V>
|
|
||||||
where
|
|
||||||
I: IntoIterator<Item = (K, V)>,
|
|
||||||
K: Hash + Eq + Clone,
|
|
||||||
V: Clone,
|
|
||||||
{
|
|
||||||
let mut answer = ImMap::default();
|
|
||||||
|
|
||||||
for (key, value) in pairs {
|
|
||||||
answer.insert(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
answer
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn send_set_from<V, I>(elems: I) -> SendSet<V>
|
|
||||||
where
|
|
||||||
I: IntoIterator<Item = V>,
|
|
||||||
V: Hash + Eq + Clone,
|
|
||||||
{
|
|
||||||
let mut answer = SendSet::default();
|
|
||||||
|
|
||||||
for elem in elems {
|
|
||||||
answer.insert(elem);
|
|
||||||
}
|
|
||||||
|
|
||||||
answer
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check constraints
|
|
||||||
//
|
|
||||||
// Keep track of the used (in types or expectations) variables, and the declared variables (in
|
|
||||||
// flex_vars or rigid_vars fields of LetConstraint. These roc_collections should match: no duplicates
|
|
||||||
// and no variables that are used but not declared are allowed.
|
|
||||||
//
|
|
||||||
// There is one exception: the initial variable (that stores the type of the whole expression) is
|
|
||||||
// never declared, but is used.
|
|
||||||
pub fn assert_correct_variable_usage(constraint: &Constraint) {
|
|
||||||
// variables declared in constraint (flex_vars or rigid_vars)
|
|
||||||
// and variables actually used in constraints
|
|
||||||
let (declared, used) = variable_usage(constraint);
|
|
||||||
|
|
||||||
let used: ImSet<Variable> = used.into();
|
|
||||||
let mut decl: ImSet<Variable> = declared.rigid_vars.clone().into();
|
|
||||||
|
|
||||||
for var in declared.flex_vars.clone() {
|
|
||||||
decl.insert(var);
|
|
||||||
}
|
|
||||||
|
|
||||||
let diff = used.clone().relative_complement(decl);
|
|
||||||
|
|
||||||
// NOTE: this checks whether we're using variables that are not declared. For recursive type
|
|
||||||
// definitions, their rigid types are declared twice, which is correct!
|
|
||||||
if !diff.is_empty() {
|
|
||||||
println!("VARIABLE USAGE PROBLEM");
|
|
||||||
|
|
||||||
println!("used: {:?}", &used);
|
|
||||||
println!("rigids: {:?}", &declared.rigid_vars);
|
|
||||||
println!("flexs: {:?}", &declared.flex_vars);
|
|
||||||
|
|
||||||
println!("difference: {:?}", &diff);
|
|
||||||
|
|
||||||
panic!("variable usage problem (see stdout for details)");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct SeenVariables {
|
|
||||||
pub rigid_vars: Vec<Variable>,
|
|
||||||
pub flex_vars: Vec<Variable>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn variable_usage(con: &Constraint) -> (SeenVariables, Vec<Variable>) {
|
|
||||||
let mut declared = SeenVariables::default();
|
|
||||||
let mut used = ImSet::default();
|
|
||||||
variable_usage_help(con, &mut declared, &mut used);
|
|
||||||
|
|
||||||
used.remove(unsafe { &Variable::unsafe_test_debug_variable(1) });
|
|
||||||
|
|
||||||
let mut used_vec: Vec<Variable> = used.into_iter().collect();
|
|
||||||
used_vec.sort();
|
|
||||||
|
|
||||||
declared.rigid_vars.sort();
|
|
||||||
declared.flex_vars.sort();
|
|
||||||
|
|
||||||
(declared, used_vec)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn variable_usage_help(con: &Constraint, declared: &mut SeenVariables, used: &mut ImSet<Variable>) {
|
|
||||||
use Constraint::*;
|
|
||||||
|
|
||||||
match con {
|
|
||||||
True | SaveTheEnvironment => (),
|
|
||||||
Eq(tipe, expectation, _, _) => {
|
|
||||||
for v in tipe.variables() {
|
|
||||||
used.insert(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
for v in expectation.get_type_ref().variables() {
|
|
||||||
used.insert(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Lookup(_, expectation, _) => {
|
|
||||||
for v in expectation.get_type_ref().variables() {
|
|
||||||
used.insert(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Pattern(_, _, tipe, pexpectation) => {
|
|
||||||
for v in tipe.variables() {
|
|
||||||
used.insert(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
for v in pexpectation.get_type_ref().variables() {
|
|
||||||
used.insert(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Let(letcon) => {
|
|
||||||
declared.rigid_vars.extend(letcon.rigid_vars.clone());
|
|
||||||
declared.flex_vars.extend(letcon.flex_vars.clone());
|
|
||||||
|
|
||||||
variable_usage_help(&letcon.defs_constraint, declared, used);
|
|
||||||
variable_usage_help(&letcon.ret_constraint, declared, used);
|
|
||||||
}
|
|
||||||
And(constraints) => {
|
|
||||||
for sub in constraints {
|
|
||||||
variable_usage_help(sub, declared, used);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
extern crate roc_gen;
|
extern crate roc_gen;
|
||||||
extern crate roc_reporting;
|
extern crate roc_reporting;
|
||||||
|
|
||||||
use crate::helpers::{infer_expr, uniq_expr_with};
|
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
use inkwell::context::Context;
|
use inkwell::context::Context;
|
||||||
use inkwell::module::Linkage;
|
use inkwell::module::Linkage;
|
||||||
|
@ -15,6 +14,7 @@ use roc_gen::llvm::build::{
|
||||||
};
|
};
|
||||||
use roc_gen::llvm::convert::basic_type_from_layout;
|
use roc_gen::llvm::convert::basic_type_from_layout;
|
||||||
use roc_load::file::{load, LoadedModule, LoadingProblem};
|
use roc_load::file::{load, LoadedModule, LoadingProblem};
|
||||||
|
use roc_module::symbol::Symbol;
|
||||||
use roc_mono::expr::{Expr, Procs};
|
use roc_mono::expr::{Expr, Procs};
|
||||||
use roc_mono::layout::Layout;
|
use roc_mono::layout::Layout;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
@ -29,8 +29,6 @@ use std::process::Command;
|
||||||
use target_lexicon::{Architecture, OperatingSystem, Triple, Vendor};
|
use target_lexicon::{Architecture, OperatingSystem, Triple, Vendor};
|
||||||
use tokio::runtime::Builder;
|
use tokio::runtime::Builder;
|
||||||
|
|
||||||
pub mod helpers;
|
|
||||||
|
|
||||||
fn main() -> io::Result<()> {
|
fn main() -> io::Result<()> {
|
||||||
let argv = std::env::args().collect::<Vec<String>>();
|
let argv = std::env::args().collect::<Vec<String>>();
|
||||||
|
|
||||||
|
@ -67,6 +65,7 @@ fn main() -> io::Result<()> {
|
||||||
|
|
||||||
async fn load_file(src_dir: PathBuf, filename: PathBuf) -> Result<(), LoadingProblem> {
|
async fn load_file(src_dir: PathBuf, filename: PathBuf) -> Result<(), LoadingProblem> {
|
||||||
let compilation_start = SystemTime::now();
|
let compilation_start = SystemTime::now();
|
||||||
|
let arena = Bump::new();
|
||||||
|
|
||||||
// Step 1: compile the app and generate the .o file
|
// Step 1: compile the app and generate the .o file
|
||||||
let subs_by_module = MutMap::default();
|
let subs_by_module = MutMap::default();
|
||||||
|
@ -80,7 +79,7 @@ async fn load_file(src_dir: PathBuf, filename: PathBuf) -> Result<(), LoadingPro
|
||||||
|
|
||||||
let dest_filename = filename.with_extension("o");
|
let dest_filename = filename.with_extension("o");
|
||||||
|
|
||||||
gen(loaded, filename, Triple::host(), &dest_filename);
|
gen(&arena, loaded, filename, Triple::host(), &dest_filename);
|
||||||
|
|
||||||
let compilation_end = compilation_start.elapsed().unwrap();
|
let compilation_end = compilation_start.elapsed().unwrap();
|
||||||
|
|
||||||
|
@ -103,14 +102,16 @@ async fn load_file(src_dir: PathBuf, filename: PathBuf) -> Result<(), LoadingPro
|
||||||
.expect("`ar` failed to run");
|
.expect("`ar` failed to run");
|
||||||
|
|
||||||
// Step 3: have rustc compile the host and link in the .a file
|
// Step 3: have rustc compile the host and link in the .a file
|
||||||
|
let binary_path = cwd.join("app");
|
||||||
|
|
||||||
Command::new("rustc")
|
Command::new("rustc")
|
||||||
.args(&["-L", ".", "host.rs", "-o", "app"])
|
.args(&["-L", ".", "--crate-type", "bin", "host.rs", "-o", binary_path.as_path().to_str().unwrap()])
|
||||||
.current_dir(cwd)
|
.current_dir(cwd)
|
||||||
.spawn()
|
.spawn()
|
||||||
.expect("rustc failed to run");
|
.expect("rustc failed to run");
|
||||||
|
|
||||||
// Step 4: Run the compiled app
|
// Step 4: Run the compiled app
|
||||||
Command::new(cwd.join("app")).spawn().unwrap_or_else(|err| {
|
Command::new(binary_path).spawn().unwrap_or_else(|err| {
|
||||||
panic!(
|
panic!(
|
||||||
"{} failed to run: {:?}",
|
"{} failed to run: {:?}",
|
||||||
cwd.join("app").to_str().unwrap(),
|
cwd.join("app").to_str().unwrap(),
|
||||||
|
@ -121,27 +122,19 @@ async fn load_file(src_dir: PathBuf, filename: PathBuf) -> Result<(), LoadingPro
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen(loaded: LoadedModule, filename: PathBuf, target: Triple, dest_filename: &Path) {
|
fn gen(arena: &Bump, loaded: LoadedModule, filename: PathBuf, target: Triple, dest_filename: &Path) {
|
||||||
use roc_reporting::report::{can_problem, RocDocAllocator, DEFAULT_PALETTE};
|
use roc_reporting::report::{can_problem, RocDocAllocator, DEFAULT_PALETTE};
|
||||||
use roc_reporting::type_error::type_problem;
|
use roc_reporting::type_error::type_problem;
|
||||||
|
|
||||||
// Build the expr
|
|
||||||
let arena = Bump::new();
|
|
||||||
|
|
||||||
let src = loaded.src;
|
let src = loaded.src;
|
||||||
let (loc_expr, _output, can_problems, subs, var, constraint, home, interns) =
|
let home = loaded.module_id;
|
||||||
uniq_expr_with(&arena, &src, &ImMap::default());
|
|
||||||
|
|
||||||
let mut type_problems = Vec::new();
|
|
||||||
let (content, mut subs) = infer_expr(subs, &mut type_problems, &constraint, var);
|
|
||||||
|
|
||||||
let src_lines: Vec<&str> = src.split('\n').collect();
|
let src_lines: Vec<&str> = src.split('\n').collect();
|
||||||
let palette = DEFAULT_PALETTE;
|
let palette = DEFAULT_PALETTE;
|
||||||
|
|
||||||
// Report parsing and canonicalization problems
|
// Report parsing and canonicalization problems
|
||||||
let alloc = RocDocAllocator::new(&src_lines, home, &interns);
|
let alloc = RocDocAllocator::new(&src_lines, home, &loaded.interns);
|
||||||
|
|
||||||
for problem in can_problems.into_iter() {
|
for problem in loaded.can_problems.into_iter() {
|
||||||
let report = can_problem(&alloc, filename.clone(), problem);
|
let report = can_problem(&alloc, filename.clone(), problem);
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
|
||||||
|
@ -150,7 +143,7 @@ fn gen(loaded: LoadedModule, filename: PathBuf, target: Triple, dest_filename: &
|
||||||
println!("\n{}\n", buf);
|
println!("\n{}\n", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
for problem in type_problems.into_iter() {
|
for problem in loaded.type_problems.into_iter() {
|
||||||
let report = type_problem(&alloc, filename.clone(), problem);
|
let report = type_problem(&alloc, filename.clone(), problem);
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
|
||||||
|
@ -159,6 +152,63 @@ fn gen(loaded: LoadedModule, filename: PathBuf, target: Triple, dest_filename: &
|
||||||
println!("\n{}\n", buf);
|
println!("\n{}\n", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Look up the types and expressions of the `provided` values
|
||||||
|
|
||||||
|
// TODO instead of hardcoding this to `main`, use the `provided` list and gen all of them.
|
||||||
|
let ident_ids = loaded.interns.all_ident_ids.get(&home).unwrap();
|
||||||
|
let main_ident_id = *ident_ids.get_id(&"main".into()).unwrap_or_else(|| {
|
||||||
|
todo!("TODO gracefully handle the case where `main` wasn't declared in the app")
|
||||||
|
});
|
||||||
|
let main_symbol = Symbol::new(home, main_ident_id);
|
||||||
|
let mut main_var = None;
|
||||||
|
let mut main_expr = None;
|
||||||
|
|
||||||
|
for (symbol, var) in loaded.exposed_vars_by_symbol {
|
||||||
|
if symbol == main_symbol {
|
||||||
|
main_var = Some(var);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We use a loop label here so we can break all the way out of a nested
|
||||||
|
// loop inside DeclareRec if we find the expr there.
|
||||||
|
//
|
||||||
|
// https://doc.rust-lang.org/1.30.0/book/first-edition/loops.html#loop-labels
|
||||||
|
'find_expr: for decl in loaded.declarations {
|
||||||
|
use roc_can::def::Declaration::*;
|
||||||
|
|
||||||
|
match decl {
|
||||||
|
Declare(def) => {
|
||||||
|
if def.pattern_vars.contains_key(&main_symbol) {
|
||||||
|
main_expr = Some(def.loc_expr);
|
||||||
|
|
||||||
|
break 'find_expr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DeclareRec(defs) => {
|
||||||
|
for def in defs {
|
||||||
|
if def.pattern_vars.contains_key(&main_symbol) {
|
||||||
|
main_expr = Some(def.loc_expr);
|
||||||
|
|
||||||
|
break 'find_expr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
InvalidCycle( _, _) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let loc_expr = main_expr.unwrap_or_else(|| panic!
|
||||||
|
("TODO gracefully handle the case where `main` was declared but not exposed")
|
||||||
|
);
|
||||||
|
let mut subs = loaded.solved.into_inner();
|
||||||
|
let content = match main_var {
|
||||||
|
Some(var) => subs.get_without_compacting(var).content,
|
||||||
|
None => todo!("TODO gracefully handle the case where `main` was declared but not exposed"),
|
||||||
|
};
|
||||||
|
|
||||||
// Generate the binary
|
// Generate the binary
|
||||||
|
|
||||||
let context = Context::create();
|
let context = Context::create();
|
||||||
|
@ -181,14 +231,14 @@ fn gen(loaded: LoadedModule, filename: PathBuf, target: Triple, dest_filename: &
|
||||||
|
|
||||||
let main_fn_type =
|
let main_fn_type =
|
||||||
basic_type_from_layout(&arena, &context, &layout, ptr_bytes).fn_type(&[], false);
|
basic_type_from_layout(&arena, &context, &layout, ptr_bytes).fn_type(&[], false);
|
||||||
let main_fn_name = "$Test.main";
|
let main_fn_name = "$main";
|
||||||
|
|
||||||
// Compile and add all the Procs before adding main
|
// Compile and add all the Procs before adding main
|
||||||
let mut env = roc_gen::llvm::build::Env {
|
let mut env = roc_gen::llvm::build::Env {
|
||||||
arena: &arena,
|
arena: &arena,
|
||||||
builder: &builder,
|
builder: &builder,
|
||||||
context: &context,
|
context: &context,
|
||||||
interns,
|
interns: loaded.interns,
|
||||||
module: arena.alloc(module),
|
module: arena.alloc(module),
|
||||||
ptr_bytes,
|
ptr_bytes,
|
||||||
};
|
};
|
||||||
|
|
|
@ -55,6 +55,7 @@ pub struct LoadedModule {
|
||||||
pub can_problems: Vec<roc_problem::can::Problem>,
|
pub can_problems: Vec<roc_problem::can::Problem>,
|
||||||
pub type_problems: Vec<solve::TypeError>,
|
pub type_problems: Vec<solve::TypeError>,
|
||||||
pub declarations: Vec<Declaration>,
|
pub declarations: Vec<Declaration>,
|
||||||
|
pub exposed_vars_by_symbol: Vec<(Symbol, Variable)>,
|
||||||
pub src: Box<str>,
|
pub src: Box<str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,6 +97,7 @@ enum Msg {
|
||||||
solved_types: MutMap<Symbol, SolvedType>,
|
solved_types: MutMap<Symbol, SolvedType>,
|
||||||
aliases: MutMap<Symbol, Alias>,
|
aliases: MutMap<Symbol, Alias>,
|
||||||
subs: Arc<Solved<Subs>>,
|
subs: Arc<Solved<Subs>>,
|
||||||
|
exposed_vars_by_symbol: Vec<(Symbol, Variable)>,
|
||||||
problems: Vec<solve::TypeError>,
|
problems: Vec<solve::TypeError>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -395,6 +397,7 @@ pub async fn load<'a>(
|
||||||
solved_types,
|
solved_types,
|
||||||
subs,
|
subs,
|
||||||
problems,
|
problems,
|
||||||
|
exposed_vars_by_symbol,
|
||||||
aliases,
|
aliases,
|
||||||
src,
|
src,
|
||||||
} => {
|
} => {
|
||||||
|
@ -431,6 +434,7 @@ pub async fn load<'a>(
|
||||||
can_problems,
|
can_problems,
|
||||||
type_problems,
|
type_problems,
|
||||||
declarations,
|
declarations,
|
||||||
|
exposed_vars_by_symbol,
|
||||||
src,
|
src,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
@ -928,10 +932,10 @@ fn solve_module(
|
||||||
// annotations which are decoupled from our Subs, because that's how
|
// annotations which are decoupled from our Subs, because that's how
|
||||||
// other modules will generate constraints for imported values
|
// other modules will generate constraints for imported values
|
||||||
// within the context of their own Subs.
|
// within the context of their own Subs.
|
||||||
for (symbol, var) in exposed_vars_by_symbol {
|
for (symbol, var) in exposed_vars_by_symbol.iter() {
|
||||||
let solved_type = SolvedType::new(&solved_subs, var);
|
let solved_type = SolvedType::new(&solved_subs, *var);
|
||||||
|
|
||||||
solved_types.insert(symbol, solved_type);
|
solved_types.insert(*symbol, solved_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
|
@ -942,6 +946,7 @@ fn solve_module(
|
||||||
src,
|
src,
|
||||||
module_id: home,
|
module_id: home,
|
||||||
subs: Arc::new(solved_subs),
|
subs: Arc::new(solved_subs),
|
||||||
|
exposed_vars_by_symbol,
|
||||||
solved_types,
|
solved_types,
|
||||||
problems,
|
problems,
|
||||||
aliases: env.aliases,
|
aliases: env.aliases,
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
app Hello provides [ text ] imports []
|
app Hello provides [ main ] imports []
|
||||||
|
|
||||||
text = "Hello, World!"
|
main = "Hello, World!"
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::os::raw::c_char;
|
||||||
|
|
||||||
#[link(name = "roc_app", kind = "static")]
|
#[link(name = "roc_app", kind = "static")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#[link_name = "$Test.main"]
|
#[link_name = "$main"]
|
||||||
fn str_from_roc() -> *const c_char;
|
fn str_from_roc() -> *const c_char;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
app Quicksort provides [ quicksort ] imports []
|
app Quicksort provides [ main ] imports []
|
||||||
|
|
||||||
|
main = quicksort [ 7, 19, 4, 21 ]
|
||||||
|
|
||||||
quicksort : List (Num a) -> List (Num a)
|
quicksort : List (Num a) -> List (Num a)
|
||||||
quicksort = \list ->
|
quicksort = \list ->
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue