let-value ast updating progress

This commit is contained in:
Anton-4 2021-08-06 19:26:25 +02:00
parent 5ab1dda836
commit 334f91392a
5 changed files with 89 additions and 28 deletions

View file

@ -66,6 +66,10 @@ impl Symbol {
}
pub fn ident_string(self, interns: &Interns) -> &InlinableString {
dbg!(&interns.all_ident_ids);
let stop = "here";
let ident_ids = interns
.all_ident_ids
.get(&self.module_id())
@ -542,6 +546,45 @@ impl IdentIds {
}
}
pub fn update_key(&mut self, old_ident_name: InlinableString, new_ident_name: InlinableString) -> Result<IdentId, String> {
let ident_id_ref_opt = self.by_ident.get(&old_ident_name);
match ident_id_ref_opt {
Some(ident_id_ref) => {
let ident_id = (*ident_id_ref).clone();
self.by_ident.remove(&old_ident_name);
self.by_ident.insert(new_ident_name.clone(), ident_id);
let by_id = &mut self.by_id;
let key_index_opt = by_id.iter().position(|x| *x == old_ident_name);
if let Some(key_index) = key_index_opt {
if let Some(vec_elt) = by_id.get_mut(key_index) {
*vec_elt = new_ident_name;
} else {
// we get the index from by_id so unless there is a bug in the rust std lib, this is unreachable
unreachable!()
}
Ok(ident_id)
} else {
Err(
format!(
"Tried to find position of key {:?} in IdentIds.by_id but I could not find the key. IdentIds.by_id: {:?}",
old_ident_name,
self.by_id
)
)
}
}
None => {
Err("Tried to update key in IdentIds but I could not find the key.".to_string())
}
}
}
/// Generates a unique, new name that's just a strigified integer
/// (e.g. "1" or "5"), using an internal counter. Since valid Roc variable
/// names cannot begin with a number, this has no chance of colliding

View file

@ -162,7 +162,7 @@ pub struct EdModule<'a> {
}
// for debugging
// use crate::lang::ast::expr2_to_string;
use crate::lang::ast::expr2_to_string;
impl<'a> EdModule<'a> {
pub fn new(code_str: &'a str, mut env: Env<'a>, ast_arena: &'a Bump) -> EdResult<EdModule<'a>> {
@ -178,7 +178,7 @@ impl<'a> EdModule<'a> {
let ast_root_id = env.pool.add(expr2);
// for debugging
// dbg!(expr2_to_string(ast_root_id, env.pool));
dbg!(expr2_to_string(ast_root_id, env.pool));
Ok(EdModule { env, ast_root_id })
}

View file

@ -738,8 +738,8 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
InputOutcome::Ignored
}
}
Expr2::LetValue{ def_id, .. } => {
update_let_value(prev_mark_node_id, *def_id, ed_model, ch)?
Expr2::LetValue{ def_id, body_id, body_var:_ } => {
update_let_value(prev_mark_node_id, *def_id, *body_id, ed_model, ch)?
}
_ => {
match ast_node_ref {

View file

@ -12,8 +12,7 @@ use crate::editor::mvc::ed_update::get_node_context;
use crate::editor::mvc::ed_update::NodeContext;
use crate::editor::slow_pool::MarkNodeId;
use crate::editor::syntax_highlight::HighlightStyle;
use crate::lang::ast::{ArrString};
use crate::lang::ast::{Expr2, ValueDef, update_str_expr};
use crate::lang::ast::{Expr2, ValueDef};
use crate::lang::pattern::Pattern2;
use crate::lang::pool::NodeId;
use crate::ui::text::lines::SelectableLines;
@ -33,26 +32,22 @@ pub fn start_new_let_value(ed_model: &mut EdModel, new_char: &char) -> EdResult<
let val_name_string = new_char.to_string();
// safe unwrap because our ArrString has a 30B capacity
let val_name_string_container = ArrString::try_from_str(val_name_string.clone()).unwrap();
let val_name_expr2_node =
Expr2::SmallStr(
val_name_string_container
);
let val_name_expr_id = ed_model.module.env.pool.add(val_name_expr2_node);
let body_placeholder = Expr2::Blank;
let body_id = ed_model.module.env.pool.add(body_placeholder);
let val_expr2_node = Expr2::Blank;
let val_expr_id = ed_model.module.env.pool.add(val_expr2_node);
let ident_string = InlinableString::from_iter(val_name_string.chars());
let ident_id = ed_model.module.env.ident_ids.add(ident_string);
let var_symbol = Symbol::new(ed_model.module.env.home, ident_id);
let body = Expr2::Var(var_symbol);
let body_id = ed_model.module.env.pool.add(body);
let pattern = Pattern2::Identifier(Symbol::new(ed_model.module.env.home, ident_id));
let pattern = Pattern2::Identifier(var_symbol);
let pattern_id = ed_model.module.env.pool.add(pattern);
let value_def =
ValueDef::NoAnnotation {
pattern_id,
expr_id: val_name_expr_id,
expr_id: val_expr_id,
expr_var: ed_model.module.env.var_store.fresh()
};
let def_id = ed_model.module.env.pool.add(value_def);
@ -109,13 +104,17 @@ pub fn start_new_let_value(ed_model: &mut EdModel, new_char: &char) -> EdResult<
}
}
pub fn update_let_value(val_name_mn_id: MarkNodeId, def_id: NodeId<ValueDef>, ed_model: &mut EdModel, new_char: &char) -> EdResult<InputOutcome> {
pub fn update_let_value(val_name_mn_id: MarkNodeId, def_id: NodeId<ValueDef>, body_id: NodeId<Expr2>, ed_model: &mut EdModel, new_char: &char) -> EdResult<InputOutcome> {
if new_char.is_ascii_alphanumeric() {
let old_caret_pos = ed_model.get_caret();
// update markup
let val_name_mn_mut = ed_model.markup_node_pool.get_mut(val_name_mn_id);
let content_str_mut = val_name_mn_mut.get_content_mut()?;
let old_val_name = content_str_mut.clone();
let old_val_ident_string = InlinableString::from_iter(old_val_name.chars());
let node_caret_offset = ed_model
.grid_node_map
.get_offset_to_node_id(old_caret_pos, val_name_mn_id)?;
@ -123,6 +122,20 @@ pub fn update_let_value(val_name_mn_id: MarkNodeId, def_id: NodeId<ValueDef>, ed
if node_caret_offset != 0 && node_caret_offset <= content_str_mut.len() {
content_str_mut.insert(node_caret_offset, *new_char);
// update ast
let value_def = ed_model.module.env.pool.get(def_id);
let value_ident_pattern_id = value_def.get_pattern_id();
let ident_string = InlinableString::from_iter(content_str_mut.chars());
// TODO no unwrap
let ident_id = ed_model.module.env.ident_ids.update_key(old_val_ident_string, ident_string).unwrap();
let new_var_symbol = Symbol::new(ed_model.module.env.home, ident_id);
ed_model.module.env.pool.set(value_ident_pattern_id, Pattern2::Identifier(new_var_symbol));
ed_model.module.env.pool.set(body_id, Expr2::Var(new_var_symbol));
// update GridNodeMap and CodeLines
ed_model.insert_between_line(
old_caret_pos.line,
@ -134,12 +147,6 @@ pub fn update_let_value(val_name_mn_id: MarkNodeId, def_id: NodeId<ValueDef>, ed
// update caret
ed_model.simple_move_carets_right(1);
// update ast
let value_def = ed_model.module.env.pool.get(def_id);
let value_name_expr_id = value_def.get_expr_id();
update_str_expr(value_name_expr_id, *new_char, node_caret_offset, ed_model.module.env.pool)?;
Ok(InputOutcome::Accepted)
} else {
Ok(InputOutcome::Ignored)

View file

@ -5,10 +5,7 @@ use bumpalo::{collections::Vec as BumpVec, Bump};
use inlinable_string::InlinableString;
use std::collections::HashMap;
use crate::lang::ast::{
expr2_to_string, ClosureExtra, Expr2, ExprId, FloatVal, IntStyle, IntVal, RecordField,
WhenBranch,
};
use crate::lang::ast::{ClosureExtra, Expr2, ExprId, FloatVal, IntStyle, IntVal, RecordField, ValueDef, WhenBranch, expr2_to_string};
use crate::lang::def::{
canonicalize_defs, sort_can_defs, CanDefs, Declaration, Def, PendingDef, References,
};
@ -299,6 +296,7 @@ pub fn str_to_expr2<'a>(
) -> Result<(Expr2, self::Output), SyntaxError<'a>> {
match roc_parse::test_helpers::parse_loc_with(arena, input.trim()) {
Ok(loc_expr) => {
dbg!(loc_expr);
let desugared_loc_expr = desugar_expr(arena, arena.alloc(loc_expr));
Ok(to_expr2(
@ -1414,7 +1412,20 @@ fn decl_to_let(pool: &mut Pool, var_store: &mut VarStore, decl: Declaration, ret
Declaration::Declare(def) => match def {
Def::AnnotationOnly { .. } => todo!(),
Def::Value(value_def) => {
// TODO remove me
match &value_def {
ValueDef::NoAnnotation{ pattern_id, expr_id, expr_var} => {
dbg!(pool.get(*pattern_id));
dbg!(pool.get(*expr_id));
dbg!(expr_var);
}
_ => panic!("REMOVE THIS BLOCK")
}
let def_id = pool.add(value_def);
dbg!(&ret);
let body_id = pool.add(ret);
Expr2::LetValue {