From 141c881cf913a0785a6eb05c88896771cec4491e Mon Sep 17 00:00:00 2001 From: rvcas Date: Tue, 19 Jan 2021 21:14:00 -0500 Subject: [PATCH 1/6] feat: start to render some AST nodes --- editor/src/lang/expr.rs | 21 +++++++++++++++++++++ editor/src/lang/mod.rs | 2 +- editor/src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/editor/src/lang/expr.rs b/editor/src/lang/expr.rs index d68ea5e724..55f2e06958 100644 --- a/editor/src/lang/expr.rs +++ b/editor/src/lang/expr.rs @@ -16,6 +16,10 @@ use roc_module::low_level::LowLevel; use roc_module::operator::CalledVia; use roc_module::symbol::{IdentIds, ModuleId, ModuleIds, Symbol}; use roc_parse::ast::StrLiteral; +use roc_parse::ast::{self, Attempting}; +use roc_parse::blankspace::space0_before; +use roc_parse::expr::expr; +use roc_parse::parser::{loc, Fail, Parser, State}; use roc_problem::can::{Problem, RuntimeError}; use roc_region::all::{Located, Region}; use roc_types::subs::{VarStore, Variable}; @@ -222,6 +226,23 @@ pub fn to_expr_id<'a>( (env.add(expr, region), output) } +pub fn str_to_expr2<'a>( + arena: &'a Bump, + input: &'a str, + env: &mut Env<'a>, + scope: &mut Scope, + region: Region, +) -> Result<(Expr2, self::Output), Fail> { + let state = State::new(input.trim().as_bytes(), Attempting::Module); + let parser = space0_before(loc(expr(0)), 0); + let answer = parser.parse(&arena, state); + + answer + .map(|(loc_expr, _)| loc_expr) + .map(|loc_expr| to_expr2(env, scope, &loc_expr.value, region)) + .map_err(|(fail, _)| fail) +} + pub fn to_expr2<'a>( env: &mut Env<'a>, scope: &mut Scope, diff --git a/editor/src/lang/mod.rs b/editor/src/lang/mod.rs index a4025ace23..61dadbcbd5 100644 --- a/editor/src/lang/mod.rs +++ b/editor/src/lang/mod.rs @@ -3,7 +3,7 @@ mod def; mod expr; mod module; mod pattern; -mod pool; +pub mod pool; pub mod roc_file; mod scope; mod types; diff --git a/editor/src/lib.rs b/editor/src/lib.rs index cfdf2c6eb2..1ef6f18eab 100644 --- a/editor/src/lib.rs +++ b/editor/src/lib.rs @@ -266,6 +266,8 @@ fn run_event_loop(file_path_opt: Option<&Path>) -> Result<(), Box> { ); } else { queue_no_file_text(&size, NOTHING_OPENED, CODE_TXT_XY.into(), &mut glyph_brush); + let ast = crate::lang::expr::str_to_expr2(); + render_node(&size, ast, CODE_TXT_XY.into(), &mut glyph_brush); } match draw_all_rects( @@ -364,6 +366,39 @@ fn begin_render_pass<'a>( }) } +fn render_node( + size: &PhysicalSize, + ast: Expr2, + position: Vector2, + glyph_brush: &mut GlyphBrush<()>, +) { + use Expr2::*; + + let area_bounds = (size.width as f32, size.height as f32).into(); + + match ast { + SmallInt { + number, + .. + // text, + // style, pretending always decimal for now + // var, + } => { + let code_text = Text { + position, + area_bounds, + color: CODE_COLOR.into(), + text: number.into(), + size: CODE_FONT_SIZE, + ..Default::default() + }; + + queue_code_text_draw(&code_text, glyph_brush); + } + _ => + }; +} + // returns bounding boxes for every glyph fn queue_editor_text( size: &PhysicalSize, From ef149bedd080bb0282ffaa2f1fb92fe33edef440 Mon Sep 17 00:00:00 2001 From: rvcas Date: Thu, 21 Jan 2021 22:12:26 -0500 Subject: [PATCH 2/6] feat: construct pool, env, and scope --- editor/src/lang/mod.rs | 4 ++-- editor/src/lib.rs | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/editor/src/lang/mod.rs b/editor/src/lang/mod.rs index 61dadbcbd5..a2de563db0 100644 --- a/editor/src/lang/mod.rs +++ b/editor/src/lang/mod.rs @@ -1,9 +1,9 @@ pub mod ast; mod def; -mod expr; +pub mod expr; mod module; mod pattern; pub mod pool; pub mod roc_file; -mod scope; +pub mod scope; mod types; diff --git a/editor/src/lib.rs b/editor/src/lib.rs index 1ef6f18eab..4bddda7a60 100644 --- a/editor/src/lib.rs +++ b/editor/src/lib.rs @@ -31,7 +31,12 @@ use crate::vec_result::get_res; use bumpalo::Bump; use cgmath::Vector2; use ed_model::Position; +use lang::{ast::Expr2, pool::Pool, scope::Scope}; use pipelines::RectResources; +use roc_collections::all::MutMap; +use roc_module::symbol::{IdentIds, ModuleId, ModuleIds}; +use roc_region::all::Region; +use roc_types::subs::VarStore; use std::error::Error; use std::io; use std::path::Path; @@ -266,7 +271,33 @@ fn run_event_loop(file_path_opt: Option<&Path>) -> Result<(), Box> { ); } else { queue_no_file_text(&size, NOTHING_OPENED, CODE_TXT_XY.into(), &mut glyph_brush); - let ast = crate::lang::expr::str_to_expr2(); + + let mut pool = Pool::with_capacity(12); + let mut var_store = VarStore::default(); + let dep_idents = MutMap::default(); + let mut module_ids = ModuleIds::default(); + let exposed_ident_ids = IdentIds::default(); + + let home = module_ids.get_or_insert(&"Home".into()); + + let mut env = crate::lang::expr::Env::new( + home, + &arena, + &mut pool, + &mut var_store, + dep_idents, + &module_ids, + exposed_ident_ids, + ); + + let mut scope = Scope::new(home, env.pool, env.var_store); + + let region = Region::new(0, 0, 0, 0); + + let (ast, _) = + crate::lang::expr::str_to_expr2(&arena, "1", &mut env, &mut scope, region) + .unwrap(); + render_node(&size, ast, CODE_TXT_XY.into(), &mut glyph_brush); } @@ -388,14 +419,14 @@ fn render_node( position, area_bounds, color: CODE_COLOR.into(), - text: number.into(), + text: String::from("1"), size: CODE_FONT_SIZE, ..Default::default() }; queue_code_text_draw(&code_text, glyph_brush); } - _ => + rest => panic!("implement {:?} render", rest) }; } From c83f01db5a48d871a78fb0d9e5ff09aa37c991da Mon Sep 17 00:00:00 2001 From: rvcas Date: Fri, 22 Jan 2021 20:29:08 -0500 Subject: [PATCH 3/6] fix(str_to_expr2): borrow error on loc_expr --- editor/src/lang/expr.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/editor/src/lang/expr.rs b/editor/src/lang/expr.rs index 55f2e06958..f9dba9e7bd 100644 --- a/editor/src/lang/expr.rs +++ b/editor/src/lang/expr.rs @@ -235,11 +235,11 @@ pub fn str_to_expr2<'a>( ) -> Result<(Expr2, self::Output), Fail> { let state = State::new(input.trim().as_bytes(), Attempting::Module); let parser = space0_before(loc(expr(0)), 0); - let answer = parser.parse(&arena, state); + let parse_res = parser.parse(&arena, state); - answer - .map(|(loc_expr, _)| loc_expr) - .map(|loc_expr| to_expr2(env, scope, &loc_expr.value, region)) + parse_res + .map(|(loc_expr, _)| arena.alloc(loc_expr.value)) + .map(|loc_expr_val_ref| to_expr2(env, scope, loc_expr_val_ref, region)) .map_err(|(fail, _)| fail) } From 41f3a222ea0684d6aa1a8871e9f2eb4217c85988 Mon Sep 17 00:00:00 2001 From: rvcas Date: Sun, 24 Jan 2021 11:38:04 -0500 Subject: [PATCH 4/6] feat(editor): render a SmallInt --- editor/src/lang/scope.rs | 73 ++++++++++++++++++--------------- editor/src/lib.rs | 10 ++--- editor/src/resources/strings.rs | 2 +- 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/editor/src/lang/scope.rs b/editor/src/lang/scope.rs index d394fb461b..41090f6307 100644 --- a/editor/src/lang/scope.rs +++ b/editor/src/lang/scope.rs @@ -8,9 +8,16 @@ use roc_module::ident::{Ident, Lowercase}; use roc_module::symbol::{IdentIds, ModuleId, Symbol}; use roc_problem::can::RuntimeError; use roc_region::all::{Located, Region}; -use roc_types::subs::{VarStore, Variable}; +use roc_types::{ + solved_types::{FreeVars, SolvedType}, + subs::{VarStore, Variable}, +}; -fn solved_type_to_type_id() -> TypeId { +fn solved_type_to_type_id( + _solved_type: &SolvedType, + _free_vars: &mut FreeVars, + _var_store: &mut VarStore, +) -> TypeId { todo!() } @@ -33,45 +40,45 @@ pub struct Scope { } impl Scope { - pub fn new(home: ModuleId, pool: &mut Pool, _var_store: &mut VarStore) -> Scope { + pub fn new(home: ModuleId, _pool: &mut Pool, _var_store: &mut VarStore) -> Scope { use roc_types::solved_types::{BuiltinAlias, FreeVars}; - let solved_aliases = roc_types::builtin_aliases::aliases(); - let mut aliases = MutMap::default(); + let _solved_aliases = roc_types::builtin_aliases::aliases(); + let aliases = MutMap::default(); - for (symbol, builtin_alias) in solved_aliases { - // let BuiltinAlias { region, vars, typ } = builtin_alias; - let BuiltinAlias { vars, .. } = builtin_alias; + // for (symbol, builtin_alias) in solved_aliases { + // // let BuiltinAlias { region, vars, typ } = builtin_alias; + // let BuiltinAlias { vars, typ, .. } = builtin_alias; - let free_vars = FreeVars::default(); - let typ = solved_type_to_type_id(); - // roc_types::solved_types::to_type(&typ, &mut free_vars, var_store); + // let mut free_vars = FreeVars::default(); + // let typ = solved_type_to_type_id(&typ, &mut free_vars, var_store); + // // roc_types::solved_types::to_type(&typ, &mut free_vars, var_store); - // make sure to sort these variables to make them line up with the type arguments - let mut type_variables: Vec<_> = free_vars.unnamed_vars.into_iter().collect(); - type_variables.sort(); + // // make sure to sort these variables to make them line up with the type arguments + // let mut type_variables: Vec<_> = free_vars.unnamed_vars.into_iter().collect(); + // type_variables.sort(); - debug_assert_eq!(vars.len(), type_variables.len()); - let variables = PoolVec::with_capacity(vars.len() as u32, pool); + // debug_assert_eq!(vars.len(), type_variables.len()); + // let variables = PoolVec::with_capacity(vars.len() as u32, pool); - let it = variables - .iter_node_ids() - .zip(vars.iter()) - .zip(type_variables); - for ((node_id, loc_name), (_, var)) in it { - // TODO region is ignored, but "fake" anyway. How to resolve? - let name = PoolStr::new(loc_name.value.as_str(), pool); - pool[node_id] = (name, var); - } + // let it = variables + // .iter_node_ids() + // .zip(vars.iter()) + // .zip(type_variables); + // for ((node_id, loc_name), (_, var)) in it { + // // TODO region is ignored, but "fake" anyway. How to resolve? + // let name = PoolStr::new(loc_name.value.as_str(), pool); + // pool[node_id] = (name, var); + // } - let alias = Alias { - actual: typ, - /// We know that builtin aliases have no hiddden variables (e.g. in closures) - hidden_variables: PoolVec::empty(pool), - targs: variables, - }; + // let alias = Alias { + // actual: typ, + // /// We know that builtin aliases have no hiddden variables (e.g. in closures) + // hidden_variables: PoolVec::empty(pool), + // targs: variables, + // }; - aliases.insert(symbol, alias); - } + // aliases.insert(symbol, alias); + // } let idents = Symbol::default_in_scope(); let idents: MutMap<_, _> = idents.into_iter().collect(); diff --git a/editor/src/lib.rs b/editor/src/lib.rs index 4bddda7a60..a8e1c4310c 100644 --- a/editor/src/lib.rs +++ b/editor/src/lib.rs @@ -26,7 +26,7 @@ use crate::graphics::style::CODE_TXT_XY; use crate::mvc::app_model::AppModel; use crate::mvc::ed_model::EdModel; use crate::mvc::{ed_model, ed_view, update}; -use crate::resources::strings::NOTHING_OPENED; +// use crate::resources::strings::NOTHING_OPENED; use crate::vec_result::get_res; use bumpalo::Bump; use cgmath::Vector2; @@ -34,7 +34,7 @@ use ed_model::Position; use lang::{ast::Expr2, pool::Pool, scope::Scope}; use pipelines::RectResources; use roc_collections::all::MutMap; -use roc_module::symbol::{IdentIds, ModuleId, ModuleIds}; +use roc_module::symbol::{IdentIds, ModuleIds}; use roc_region::all::Region; use roc_types::subs::VarStore; use std::error::Error; @@ -270,7 +270,7 @@ fn run_event_loop(file_path_opt: Option<&Path>) -> Result<(), Box> { &mut glyph_brush, ); } else { - queue_no_file_text(&size, NOTHING_OPENED, CODE_TXT_XY.into(), &mut glyph_brush); + // queue_no_file_text(&size, NOTHING_OPENED, CODE_TXT_XY.into(), &mut glyph_brush); let mut pool = Pool::with_capacity(12); let mut var_store = VarStore::default(); @@ -419,7 +419,7 @@ fn render_node( position, area_bounds, color: CODE_COLOR.into(), - text: String::from("1"), + text: format!("{:?}", number), size: CODE_FONT_SIZE, ..Default::default() }; @@ -463,7 +463,7 @@ fn queue_editor_text( queue_code_text_draw(&code_text, glyph_brush); } -fn queue_no_file_text( +fn _queue_no_file_text( size: &PhysicalSize, text: &str, text_coords: Vector2, diff --git a/editor/src/resources/strings.rs b/editor/src/resources/strings.rs index 720fb4c89b..3b324062a9 100644 --- a/editor/src/resources/strings.rs +++ b/editor/src/resources/strings.rs @@ -1 +1 @@ -pub const NOTHING_OPENED: &str = "Execute `cargo run edit ` to open a file."; +// pub const NOTHING_OPENED: &str = "Execute `cargo run edit ` to open a file."; From 6abeea00d33c0de5acdd4ab4800a3d62902f8993 Mon Sep 17 00:00:00 2001 From: rvcas Date: Wed, 27 Jan 2021 08:19:22 -0500 Subject: [PATCH 5/6] feat(editor): pass ast by reference to render --- editor/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/editor/src/lib.rs b/editor/src/lib.rs index 8333c912ba..4a1bf71be4 100644 --- a/editor/src/lib.rs +++ b/editor/src/lib.rs @@ -298,7 +298,7 @@ fn run_event_loop(file_path_opt: Option<&Path>) -> Result<(), Box> { crate::lang::expr::str_to_expr2(&arena, "1", &mut env, &mut scope, region) .unwrap(); - render_node(&size, ast, CODE_TXT_XY.into(), &mut glyph_brush); + render_node(&size, &ast, CODE_TXT_XY.into(), &mut glyph_brush); } match draw_all_rects( @@ -399,7 +399,7 @@ fn begin_render_pass<'a>( fn render_node( size: &PhysicalSize, - ast: Expr2, + ast: &Expr2, position: Vector2, glyph_brush: &mut GlyphBrush<()>, ) { From ebdf5a495d1b8886bc90d0eba30eede1388b265e Mon Sep 17 00:00:00 2001 From: Brendan Hansknecht Date: Wed, 27 Jan 2021 12:45:01 -0800 Subject: [PATCH 6/6] Update nix version to pull in rustc 1.49.0 --- shell.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell.nix b/shell.nix index 57ed6ecfe7..14376baf6e 100644 --- a/shell.nix +++ b/shell.nix @@ -8,10 +8,10 @@ in with { # Look here for information about how pin version of nixpkgs # → https://nixos.wiki/wiki/FAQ/Pinning_Nixpkgs pkgs = import (builtins.fetchGit { - name = "nixpkgs-2021-01-05"; + name = "nixpkgs-2021-01-07"; url = "https://github.com/nixos/nixpkgs/"; ref = "refs/heads/nixpkgs-unstable"; - rev = "f53c431645da8e6760268092aa10f736b5cfb117"; + rev = "4a580eb51bce94cae356e841f3f5303d31afbaf1"; }) { }; isMacOS = currentOS == "darwin";