changes to work with combination of Def2 and Expr2

This commit is contained in:
Anton-4 2021-08-28 18:54:09 +02:00
parent 41519fca96
commit 0135e7b9b1
20 changed files with 255 additions and 190 deletions

View file

@ -81,6 +81,8 @@ impl Symbol {
) )
}); });
dbg!(ident_ids);
ident_ids ident_ids
.get_name(self.ident_id()) .get_name(self.ident_id())
.unwrap_or_else(|| { .unwrap_or_else(|| {

View file

@ -1,3 +1,4 @@
use crate::lang::parse::ASTNodeId;
use crate::ui::ui_error::UIResult; use crate::ui::ui_error::UIResult;
use crate::{editor::slow_pool::MarkNodeId, ui::text::text_pos::TextPos}; use crate::{editor::slow_pool::MarkNodeId, ui::text::text_pos::TextPos};
use colored::*; use colored::*;
@ -11,6 +12,16 @@ use snafu::{Backtrace, ErrorCompat, NoneError, ResultExt, Snafu};
#[derive(Debug, Snafu)] #[derive(Debug, Snafu)]
#[snafu(visibility(pub))] #[snafu(visibility(pub))]
pub enum EdError { pub enum EdError {
#[snafu(display(
"ASTNodeIdWithoutExprId: The expr_id_opt in ASTNode({:?}) was `None` but I was expexting `Some(ExprId)` .",
ast_node_id
))]
ASTNodeIdWithoutExprId {
ast_node_id: ASTNodeId,
backtrace: Backtrace,
},
#[snafu(display( #[snafu(display(
"CaretNotFound: No carets were found in the expected node with id {}", "CaretNotFound: No carets were found in the expected node with id {}",
node_id node_id

View file

@ -6,7 +6,7 @@ use crate::editor::slow_pool::MarkNodeId;
use crate::editor::slow_pool::SlowPool; use crate::editor::slow_pool::SlowPool;
use crate::editor::util::first_last_index_of; use crate::editor::util::first_last_index_of;
use crate::editor::util::index_of; use crate::editor::util::index_of;
use crate::lang::ast::ExprId; use crate::lang::parse::ASTNodeId;
use crate::ui::text::selection::Selection; use crate::ui::text::selection::Selection;
use crate::ui::text::text_pos::TextPos; use crate::ui::text::text_pos::TextPos;
use crate::ui::ui_error::{OutOfBounds, UIResult}; use crate::ui::ui_error::{OutOfBounds, UIResult};
@ -153,10 +153,10 @@ impl GridNodeMap {
&self, &self,
caret_pos: TextPos, caret_pos: TextPos,
ed_model: &EdModel, ed_model: &EdModel,
) -> EdResult<(TextPos, TextPos, ExprId, MarkNodeId)> { ) -> EdResult<(TextPos, TextPos, ASTNodeId, MarkNodeId)> {
let line = slice_get(caret_pos.line, &self.lines)?; let line = slice_get(caret_pos.line, &self.lines)?;
let node_id = slice_get(caret_pos.column, line)?; let node_id = slice_get(caret_pos.column, line)?;
let node = ed_model.markup_node_pool.get(*node_id); let node = ed_model.mark_node_pool.get(*node_id);
if node.is_nested() { if node.is_nested() {
let (start_pos, end_pos) = self.get_nested_start_end_pos(*node_id, ed_model)?; let (start_pos, end_pos) = self.get_nested_start_end_pos(*node_id, ed_model)?;
@ -167,7 +167,7 @@ impl GridNodeMap {
let curr_node_id = slice_get(first_node_index, line)?; let curr_node_id = slice_get(first_node_index, line)?;
let curr_ast_node_id = ed_model let curr_ast_node_id = ed_model
.markup_node_pool .mark_node_pool
.get(*curr_node_id) .get(*curr_node_id)
.get_ast_node_id(); .get_ast_node_id();
@ -180,11 +180,11 @@ impl GridNodeMap {
for i in (0..first_node_index).rev() { for i in (0..first_node_index).rev() {
let prev_pos_node_id = slice_get(i, line)?; let prev_pos_node_id = slice_get(i, line)?;
let prev_ast_node_id = ed_model let prev_ast_node_id = ed_model
.markup_node_pool .mark_node_pool
.get(*prev_pos_node_id) .get(*prev_pos_node_id)
.get_ast_node_id(); .get_ast_node_id();
if prev_ast_node_id == curr_ast_node_id { if prev_ast_node_id.equals(&curr_ast_node_id) {
if pos_extra_subtract > 0 { if pos_extra_subtract > 0 {
expr_start_index -= pos_extra_subtract + 1; expr_start_index -= pos_extra_subtract + 1;
pos_extra_subtract = 0; pos_extra_subtract = 0;
@ -202,11 +202,11 @@ impl GridNodeMap {
for i in last_node_index..line.len() { for i in last_node_index..line.len() {
let next_pos_node_id = slice_get(i, line)?; let next_pos_node_id = slice_get(i, line)?;
let next_ast_node_id = ed_model let next_ast_node_id = ed_model
.markup_node_pool .mark_node_pool
.get(*next_pos_node_id) .get(*next_pos_node_id)
.get_ast_node_id(); .get_ast_node_id();
if next_ast_node_id == curr_ast_node_id { if next_ast_node_id.equals(&curr_ast_node_id) {
if pos_extra_add > 0 { if pos_extra_add > 0 {
expr_end_index += pos_extra_add + 1; expr_end_index += pos_extra_add + 1;
pos_extra_add = 0; pos_extra_add = 0;
@ -219,7 +219,7 @@ impl GridNodeMap {
} }
let correct_mark_node_id = let correct_mark_node_id =
GridNodeMap::get_top_node_with_expr_id(*curr_node_id, &ed_model.markup_node_pool); GridNodeMap::get_top_node_with_expr_id(*curr_node_id, &ed_model.mark_node_pool);
Ok(( Ok((
TextPos { TextPos {
@ -240,14 +240,14 @@ impl GridNodeMap {
// `{` is not the entire Expr2 // `{` is not the entire Expr2
fn get_top_node_with_expr_id( fn get_top_node_with_expr_id(
curr_node_id: MarkNodeId, curr_node_id: MarkNodeId,
markup_node_pool: &SlowPool, mark_node_pool: &SlowPool,
) -> MarkNodeId { ) -> MarkNodeId {
let curr_node = markup_node_pool.get(curr_node_id); let curr_node = mark_node_pool.get(curr_node_id);
if let Some(parent_id) = curr_node.get_parent_id_opt() { if let Some(parent_id) = curr_node.get_parent_id_opt() {
let parent = markup_node_pool.get(parent_id); let parent = mark_node_pool.get(parent_id);
if parent.get_ast_node_id() == curr_node.get_ast_node_id() { if parent.get_ast_node_id().equals(&curr_node.get_ast_node_id()) {
parent_id parent_id
} else { } else {
curr_node_id curr_node_id
@ -262,7 +262,7 @@ impl GridNodeMap {
nested_node_id: MarkNodeId, nested_node_id: MarkNodeId,
ed_model: &EdModel, ed_model: &EdModel,
) -> EdResult<(TextPos, TextPos)> { ) -> EdResult<(TextPos, TextPos)> {
let parent_mark_node = ed_model.markup_node_pool.get(nested_node_id); let parent_mark_node = ed_model.mark_node_pool.get(nested_node_id);
let all_child_ids = parent_mark_node.get_children_ids(); let all_child_ids = parent_mark_node.get_children_ids();
let first_child_id = all_child_ids let first_child_id = all_child_ids

View file

@ -1,7 +1,4 @@
use crate::{ use crate::{editor::{slow_pool::MarkNodeId, syntax_highlight::HighlightStyle}, lang::{ast::{ExprId}, pool::NodeId}};
editor::{slow_pool::MarkNodeId, syntax_highlight::HighlightStyle},
lang::ast::ExprId,
};
use super::{attribute::Attributes, nodes, nodes::MarkupNode}; use super::{attribute::Attributes, nodes, nodes::MarkupNode};
@ -27,7 +24,7 @@ pub fn new_comma_mn(ast_node_id: ExprId, parent_id_opt: Option<MarkNodeId>) -> M
} }
} }
pub fn new_blank_mn(ast_node_id: ExprId, parent_id_opt: Option<MarkNodeId>) -> MarkupNode { pub fn new_blank_mn<T>(ast_node_id: NodeId<T>, parent_id_opt: Option<MarkNodeId>) -> MarkupNode {
MarkupNode::Blank { MarkupNode::Blank {
ast_node_id, ast_node_id,
syn_high_style: HighlightStyle::Blank, syn_high_style: HighlightStyle::Blank,
@ -37,7 +34,7 @@ pub fn new_blank_mn(ast_node_id: ExprId, parent_id_opt: Option<MarkNodeId>) -> M
} }
} }
pub fn new_blank_mn_w_nl(ast_node_id: ExprId, parent_id_opt: Option<MarkNodeId>) -> MarkupNode { pub fn new_blank_mn_w_nl<T>(ast_node_id: NodeId<T>, parent_id_opt: Option<MarkNodeId>) -> MarkupNode {
MarkupNode::Blank { MarkupNode::Blank {
ast_node_id, ast_node_id,
syn_high_style: HighlightStyle::Blank, syn_high_style: HighlightStyle::Blank,

View file

@ -18,6 +18,7 @@ use crate::lang::ast::ExprId;
use crate::lang::ast::RecordField; use crate::lang::ast::RecordField;
use crate::lang::ast::ValueDef; use crate::lang::ast::ValueDef;
use crate::lang::ast::expr2_to_string; use crate::lang::ast::expr2_to_string;
use crate::lang::parse::ASTNodeId;
use crate::lang::parse::{AppHeader, AST}; use crate::lang::parse::{AppHeader, AST};
use crate::lang::pattern::get_identifier_string; use crate::lang::pattern::get_identifier_string;
use crate::lang::{ast::Expr2, expr::Env, pool::PoolStr}; use crate::lang::{ast::Expr2, expr::Env, pool::PoolStr};
@ -29,21 +30,21 @@ use std::fmt;
#[derive(Debug)] #[derive(Debug)]
pub enum MarkupNode { pub enum MarkupNode {
Nested { Nested {
ast_node_id: ExprId, ast_node_id: ASTNodeId,
children_ids: Vec<MarkNodeId>, children_ids: Vec<MarkNodeId>,
parent_id_opt: Option<MarkNodeId>, parent_id_opt: Option<MarkNodeId>,
newline_at_end: bool, newline_at_end: bool,
}, },
Text { Text {
content: String, content: String,
ast_node_id: ExprId, ast_node_id: ASTNodeId,
syn_high_style: HighlightStyle, syn_high_style: HighlightStyle,
attributes: Attributes, attributes: Attributes,
parent_id_opt: Option<MarkNodeId>, parent_id_opt: Option<MarkNodeId>,
newline_at_end: bool, newline_at_end: bool,
}, },
Blank { Blank {
ast_node_id: ExprId, ast_node_id: ASTNodeId,
attributes: Attributes, attributes: Attributes,
syn_high_style: HighlightStyle, // TODO remove HighlightStyle, this is always HighlightStyle::Blank syn_high_style: HighlightStyle, // TODO remove HighlightStyle, this is always HighlightStyle::Blank
parent_id_opt: Option<MarkNodeId>, parent_id_opt: Option<MarkNodeId>,
@ -52,7 +53,7 @@ pub enum MarkupNode {
} }
impl MarkupNode { impl MarkupNode {
pub fn get_ast_node_id(&self) -> ExprId { pub fn get_ast_node_id(&self) -> ASTNodeId {
match self { match self {
MarkupNode::Nested { ast_node_id, .. } => *ast_node_id, MarkupNode::Nested { ast_node_id, .. } => *ast_node_id,
MarkupNode::Text { ast_node_id, .. } => *ast_node_id, MarkupNode::Text { ast_node_id, .. } => *ast_node_id,
@ -76,9 +77,9 @@ impl MarkupNode {
} }
} }
pub fn get_sibling_ids(&self, markup_node_pool: &SlowPool) -> Vec<MarkNodeId> { pub fn get_sibling_ids(&self, mark_node_pool: &SlowPool) -> Vec<MarkNodeId> {
if let Some(parent_id) = self.get_parent_id_opt() { if let Some(parent_id) = self.get_parent_id_opt() {
let parent_node = markup_node_pool.get(parent_id); let parent_node = mark_node_pool.get(parent_id);
parent_node.get_children_ids() parent_node.get_children_ids()
} else { } else {
@ -90,7 +91,7 @@ impl MarkupNode {
pub fn get_child_indices( pub fn get_child_indices(
&self, &self,
child_id: MarkNodeId, child_id: MarkNodeId,
markup_node_pool: &SlowPool, mark_node_pool: &SlowPool,
) -> EdResult<(usize, usize)> { ) -> EdResult<(usize, usize)> {
match self { match self {
MarkupNode::Nested { children_ids, .. } => { MarkupNode::Nested { children_ids, .. } => {
@ -103,7 +104,7 @@ impl MarkupNode {
mark_child_index_opt = Some(indx); mark_child_index_opt = Some(indx);
} }
let child_mark_node = markup_node_pool.get(mark_child_id); let child_mark_node = mark_node_pool.get(mark_child_id);
// a node that points to the same ast_node as the parent is a ',', '[', ']' // a node that points to the same ast_node as the parent is a ',', '[', ']'
// those are not "real" ast children // those are not "real" ast children
if child_mark_node.get_ast_node_id() != self_ast_id { if child_mark_node.get_ast_node_id() != self_ast_id {
@ -259,7 +260,7 @@ fn new_markup_node(
text: String, text: String,
node_id: ExprId, node_id: ExprId,
highlight_style: HighlightStyle, highlight_style: HighlightStyle,
markup_node_pool: &mut SlowPool, mark_node_pool: &mut SlowPool,
) -> MarkNodeId { ) -> MarkNodeId {
let node = MarkupNode::Text { let node = MarkupNode::Text {
content: text, content: text,
@ -270,7 +271,7 @@ fn new_markup_node(
newline_at_end: false, newline_at_end: false,
}; };
markup_node_pool.add(node) mark_node_pool.add(node)
} }
// make Markup Nodes: generate String representation, assign Highlighting Style // make Markup Nodes: generate String representation, assign Highlighting Style
@ -279,7 +280,7 @@ pub fn expr2_to_markup<'a, 'b>(
env: &mut Env<'b>, env: &mut Env<'b>,
expr2: &Expr2, expr2: &Expr2,
expr2_node_id: ExprId, expr2_node_id: ExprId,
markup_node_pool: &mut SlowPool, mark_node_pool: &mut SlowPool,
interns: &Interns, interns: &Interns,
) -> EdResult<MarkNodeId> { ) -> EdResult<MarkNodeId> {
dbg!(expr2_to_string(expr2_node_id, env.pool)); dbg!(expr2_to_string(expr2_node_id, env.pool));
@ -295,24 +296,24 @@ pub fn expr2_to_markup<'a, 'b>(
num_str, num_str,
expr2_node_id, expr2_node_id,
HighlightStyle::Number, HighlightStyle::Number,
markup_node_pool, mark_node_pool,
) )
} }
Expr2::Str(text) => new_markup_node( Expr2::Str(text) => new_markup_node(
"\"".to_owned() + text.as_str(env.pool) + "\"", "\"".to_owned() + text.as_str(env.pool) + "\"",
expr2_node_id, expr2_node_id,
HighlightStyle::String, HighlightStyle::String,
markup_node_pool, mark_node_pool,
), ),
Expr2::GlobalTag { name, .. } => new_markup_node( Expr2::GlobalTag { name, .. } => new_markup_node(
get_string(env, name), get_string(env, name),
expr2_node_id, expr2_node_id,
HighlightStyle::Type, HighlightStyle::Type,
markup_node_pool, mark_node_pool,
), ),
Expr2::Call { expr: expr_id, .. } => { Expr2::Call { expr: expr_id, .. } => {
let expr = env.pool.get(*expr_id); let expr = env.pool.get(*expr_id);
expr2_to_markup(arena, env, expr, *expr_id, markup_node_pool, interns)? expr2_to_markup(arena, env, expr, *expr_id, mark_node_pool, interns)?
} }
Expr2::Var(symbol) => { Expr2::Var(symbol) => {
//TODO make bump_format with arena //TODO make bump_format with arena
@ -321,12 +322,12 @@ pub fn expr2_to_markup<'a, 'b>(
text, text,
expr2_node_id, expr2_node_id,
HighlightStyle::Variable, HighlightStyle::Variable,
markup_node_pool, mark_node_pool,
) )
} }
Expr2::List { elems, .. } => { Expr2::List { elems, .. } => {
let mut children_ids = let mut children_ids =
vec![markup_node_pool.add(new_left_square_mn(expr2_node_id, None))]; vec![mark_node_pool.add(new_left_square_mn(expr2_node_id, None))];
let indexed_node_ids: Vec<(usize, ExprId)> = let indexed_node_ids: Vec<(usize, ExprId)> =
elems.iter(env.pool).copied().enumerate().collect(); elems.iter(env.pool).copied().enumerate().collect();
@ -339,15 +340,15 @@ pub fn expr2_to_markup<'a, 'b>(
env, env,
sub_expr2, sub_expr2,
*node_id, *node_id,
markup_node_pool, mark_node_pool,
interns, interns,
)?); )?);
if idx + 1 < elems.len() { if idx + 1 < elems.len() {
children_ids.push(markup_node_pool.add(new_comma_mn(expr2_node_id, None))); children_ids.push(mark_node_pool.add(new_comma_mn(expr2_node_id, None)));
} }
} }
children_ids.push(markup_node_pool.add(new_right_square_mn(expr2_node_id, None))); children_ids.push(mark_node_pool.add(new_right_square_mn(expr2_node_id, None)));
let list_node = MarkupNode::Nested { let list_node = MarkupNode::Nested {
ast_node_id: expr2_node_id, ast_node_id: expr2_node_id,
@ -356,12 +357,12 @@ pub fn expr2_to_markup<'a, 'b>(
newline_at_end: false, newline_at_end: false,
}; };
markup_node_pool.add(list_node) mark_node_pool.add(list_node)
} }
Expr2::EmptyRecord => { Expr2::EmptyRecord => {
let children_ids = vec![ let children_ids = vec![
markup_node_pool.add(new_left_accolade_mn(expr2_node_id, None)), mark_node_pool.add(new_left_accolade_mn(expr2_node_id, None)),
markup_node_pool.add(new_right_accolade_mn(expr2_node_id, None)), mark_node_pool.add(new_right_accolade_mn(expr2_node_id, None)),
]; ];
let record_node = MarkupNode::Nested { let record_node = MarkupNode::Nested {
@ -371,11 +372,11 @@ pub fn expr2_to_markup<'a, 'b>(
newline_at_end: false, newline_at_end: false,
}; };
markup_node_pool.add(record_node) mark_node_pool.add(record_node)
} }
Expr2::Record { fields, .. } => { Expr2::Record { fields, .. } => {
let mut children_ids = let mut children_ids =
vec![markup_node_pool.add(new_left_accolade_mn(expr2_node_id, None))]; vec![mark_node_pool.add(new_left_accolade_mn(expr2_node_id, None))];
for (idx, field_node_id) in fields.iter_node_ids().enumerate() { for (idx, field_node_id) in fields.iter_node_ids().enumerate() {
let record_field = env.pool.get(field_node_id); let record_field = env.pool.get(field_node_id);
@ -386,14 +387,14 @@ pub fn expr2_to_markup<'a, 'b>(
field_name.as_str(env.pool).to_owned(), field_name.as_str(env.pool).to_owned(),
expr2_node_id, expr2_node_id,
HighlightStyle::RecordField, HighlightStyle::RecordField,
markup_node_pool, mark_node_pool,
)); ));
match record_field { match record_field {
RecordField::InvalidLabelOnly(_, _) => (), RecordField::InvalidLabelOnly(_, _) => (),
RecordField::LabelOnly(_, _, _) => (), RecordField::LabelOnly(_, _, _) => (),
RecordField::LabeledValue(_, _, sub_expr2_node_id) => { RecordField::LabeledValue(_, _, sub_expr2_node_id) => {
children_ids.push(markup_node_pool.add(new_colon_mn(expr2_node_id, None))); children_ids.push(mark_node_pool.add(new_colon_mn(expr2_node_id, None)));
let sub_expr2 = env.pool.get(*sub_expr2_node_id); let sub_expr2 = env.pool.get(*sub_expr2_node_id);
children_ids.push(expr2_to_markup( children_ids.push(expr2_to_markup(
@ -401,18 +402,18 @@ pub fn expr2_to_markup<'a, 'b>(
env, env,
sub_expr2, sub_expr2,
*sub_expr2_node_id, *sub_expr2_node_id,
markup_node_pool, mark_node_pool,
interns, interns,
)?); )?);
} }
} }
if idx + 1 < fields.len() { if idx + 1 < fields.len() {
children_ids.push(markup_node_pool.add(new_comma_mn(expr2_node_id, None))); children_ids.push(mark_node_pool.add(new_comma_mn(expr2_node_id, None)));
} }
} }
children_ids.push(markup_node_pool.add(new_right_accolade_mn(expr2_node_id, None))); children_ids.push(mark_node_pool.add(new_right_accolade_mn(expr2_node_id, None)));
let record_node = MarkupNode::Nested { let record_node = MarkupNode::Nested {
ast_node_id: expr2_node_id, ast_node_id: expr2_node_id,
@ -421,9 +422,9 @@ pub fn expr2_to_markup<'a, 'b>(
newline_at_end: false, newline_at_end: false,
}; };
markup_node_pool.add(record_node) mark_node_pool.add(record_node)
} }
Expr2::Blank => markup_node_pool.add(new_blank_mn(expr2_node_id, None)), Expr2::Blank => mark_node_pool.add(new_blank_mn(expr2_node_id, None)),
Expr2::LetValue { Expr2::LetValue {
def_id, def_id,
body_id, body_id,
@ -436,7 +437,7 @@ pub fn expr2_to_markup<'a, 'b>(
let pattern_id = env.pool.get(*def_id).get_pattern_id(); let pattern_id = env.pool.get(*def_id).get_pattern_id();
let pattern2 = env.pool.get(pattern_id); let pattern2 = env.pool.get(pattern_id);
//dbg!(pattern2); dbg!(pattern2);
let val_name = get_identifier_string(pattern2, interns)?; let val_name = get_identifier_string(pattern2, interns)?;
let val_name_mn = MarkupNode::Text { let val_name_mn = MarkupNode::Text {
@ -448,9 +449,9 @@ pub fn expr2_to_markup<'a, 'b>(
newline_at_end: false, newline_at_end: false,
}; };
let val_name_mn_id = markup_node_pool.add(val_name_mn); let val_name_mn_id = mark_node_pool.add(val_name_mn);
let equals_mn_id = markup_node_pool.add(new_equals_mn(expr2_node_id, None)); let equals_mn_id = mark_node_pool.add(new_equals_mn(expr2_node_id, None));
let value_def = env.pool.get(*def_id); let value_def = env.pool.get(*def_id);
@ -465,11 +466,11 @@ pub fn expr2_to_markup<'a, 'b>(
env, env,
env.pool.get(*expr_id), env.pool.get(*expr_id),
*expr_id, *expr_id,
markup_node_pool, mark_node_pool,
interns, interns,
)?; )?;
let body_mn = markup_node_pool.get_mut(body_mn_id); let body_mn = mark_node_pool.get_mut(body_mn_id);
body_mn.add_newline_at_end(); body_mn.add_newline_at_end();
let full_let_node = MarkupNode::Nested { let full_let_node = MarkupNode::Nested {
@ -479,7 +480,7 @@ pub fn expr2_to_markup<'a, 'b>(
newline_at_end: true, newline_at_end: true,
}; };
markup_node_pool.add(full_let_node) mark_node_pool.add(full_let_node)
} }
other => { other => {
unimplemented!( unimplemented!(
@ -493,7 +494,7 @@ pub fn expr2_to_markup<'a, 'b>(
"RunTimeError".to_string(), "RunTimeError".to_string(),
expr2_node_id, expr2_node_id,
HighlightStyle::Blank, HighlightStyle::Blank,
markup_node_pool, mark_node_pool,
), ),
rest => todo!("implement expr2_to_markup for {:?}", rest), rest => todo!("implement expr2_to_markup for {:?}", rest),
}; };
@ -501,8 +502,8 @@ pub fn expr2_to_markup<'a, 'b>(
Ok(mark_node_id) Ok(mark_node_id)
} }
pub fn set_parent_for_all(markup_node_id: MarkNodeId, markup_node_pool: &mut SlowPool) { pub fn set_parent_for_all(markup_node_id: MarkNodeId, mark_node_pool: &mut SlowPool) {
let node = markup_node_pool.get(markup_node_id); let node = mark_node_pool.get(markup_node_id);
if let MarkupNode::Nested { if let MarkupNode::Nested {
ast_node_id: _, ast_node_id: _,
@ -515,7 +516,7 @@ pub fn set_parent_for_all(markup_node_id: MarkNodeId, markup_node_pool: &mut Slo
let children_ids_clone = children_ids.clone(); let children_ids_clone = children_ids.clone();
for child_id in children_ids_clone { for child_id in children_ids_clone {
set_parent_for_all_helper(child_id, markup_node_id, markup_node_pool); set_parent_for_all_helper(child_id, markup_node_id, mark_node_pool);
} }
} }
} }
@ -523,9 +524,9 @@ pub fn set_parent_for_all(markup_node_id: MarkNodeId, markup_node_pool: &mut Slo
pub fn set_parent_for_all_helper( pub fn set_parent_for_all_helper(
markup_node_id: MarkNodeId, markup_node_id: MarkNodeId,
parent_node_id: MarkNodeId, parent_node_id: MarkNodeId,
markup_node_pool: &mut SlowPool, mark_node_pool: &mut SlowPool,
) { ) {
let node = markup_node_pool.get_mut(markup_node_id); let node = mark_node_pool.get_mut(markup_node_id);
match node { match node {
MarkupNode::Nested { MarkupNode::Nested {
@ -539,7 +540,7 @@ pub fn set_parent_for_all_helper(
let children_ids_clone = children_ids.clone(); let children_ids_clone = children_ids.clone();
for child_id in children_ids_clone { for child_id in children_ids_clone {
set_parent_for_all_helper(child_id, markup_node_id, markup_node_pool); set_parent_for_all_helper(child_id, markup_node_id, mark_node_pool);
} }
} }
MarkupNode::Text { parent_id_opt, .. } => *parent_id_opt = Some(parent_node_id), MarkupNode::Text { parent_id_opt, .. } => *parent_id_opt = Some(parent_node_id),
@ -680,7 +681,7 @@ pub fn header_to_markup(app_header: &AppHeader, mark_node_pool: &mut SlowPool) -
let provides_val_node_id = header_val_mn( let provides_val_node_id = header_val_mn(
// TODO iter over provides like with imports // TODO iter over provides like with imports
app_header.provides.first().unwrap().to_owned(), app_header.provides.first().unwrap_or(&String::new()).to_owned(),
ast_node_id, ast_node_id,
HighlightStyle::Provides, HighlightStyle::Provides,
mark_node_pool, mark_node_pool,

View file

@ -7,7 +7,7 @@ use crate::editor::{
ed_error::{EdResult, EmptyCodeString, MissingParent, NoNodeAtCaretPosition}, ed_error::{EdResult, EmptyCodeString, MissingParent, NoNodeAtCaretPosition},
}; };
use crate::graphics::primitives::rect::Rect; use crate::graphics::primitives::rect::Rect;
use crate::lang::ast::ExprId; use crate::lang::ast::{DefId, ExprId};
use crate::lang::expr::Env; use crate::lang::expr::Env;
use crate::lang::parse::AST; use crate::lang::parse::AST;
use crate::lang::pool::PoolStr; use crate::lang::pool::PoolStr;
@ -28,7 +28,7 @@ pub struct EdModel<'a> {
// allows us to map window coordinates to MarkNodeId's // allows us to map window coordinates to MarkNodeId's
pub grid_node_map: GridNodeMap, pub grid_node_map: GridNodeMap,
pub markup_ids: Vec<MarkNodeId>, // one root node for every expression pub markup_ids: Vec<MarkNodeId>, // one root node for every expression
pub markup_node_pool: SlowPool, pub mark_node_pool: SlowPool,
// contains single char dimensions, used to calculate line height, column width... // contains single char dimensions, used to calculate line height, column width...
pub glyph_dim_rect_opt: Option<Rect>, pub glyph_dim_rect_opt: Option<Rect>,
pub has_focus: bool, pub has_focus: bool,
@ -57,7 +57,7 @@ pub fn init_model<'a>(
) -> EdResult<EdModel<'a>> { ) -> EdResult<EdModel<'a>> {
let mut module = EdModule::new(code_str, env, code_arena)?; let mut module = EdModule::new(code_str, env, code_arena)?;
let mut markup_node_pool = SlowPool::new(); let mut mark_node_pool = SlowPool::new();
let markup_ids = if code_str.is_empty() { let markup_ids = if code_str.is_empty() {
EmptyCodeString {}.fail() EmptyCodeString {}.fail()
@ -66,13 +66,13 @@ pub fn init_model<'a>(
code_arena, code_arena,
&mut module.env, &mut module.env,
&module.ast, &module.ast,
&mut markup_node_pool, &mut mark_node_pool,
&loaded_module.interns, &loaded_module.interns,
) )
}?; }?;
let code_lines = EdModel::build_code_lines_from_markup(&markup_ids, &markup_node_pool)?; let code_lines = EdModel::build_code_lines_from_markup(&markup_ids, &mark_node_pool)?;
let grid_node_map = EdModel::build_node_map_from_markup(&markup_ids, &markup_node_pool)?; let grid_node_map = EdModel::build_node_map_from_markup(&markup_ids, &mark_node_pool)?;
let caret = match caret_pos { let caret = match caret_pos {
CaretPos::Start => CaretWSelect::default(), CaretPos::Start => CaretWSelect::default(),
@ -86,7 +86,7 @@ pub fn init_model<'a>(
code_lines, code_lines,
grid_node_map, grid_node_map,
markup_ids, markup_ids,
markup_node_pool, mark_node_pool,
glyph_dim_rect_opt: None, glyph_dim_rect_opt: None,
has_focus: true, has_focus: true,
caret_w_select_vec: NonEmpty::new((caret, None)), caret_w_select_vec: NonEmpty::new((caret, None)),
@ -135,11 +135,11 @@ impl<'a> EdModel<'a> {
pub fn get_curr_child_indices(&self) -> EdResult<(usize, usize)> { pub fn get_curr_child_indices(&self) -> EdResult<(usize, usize)> {
if self.node_exists_at_caret() { if self.node_exists_at_caret() {
let curr_mark_node_id = self.get_curr_mark_node_id()?; let curr_mark_node_id = self.get_curr_mark_node_id()?;
let curr_mark_node = self.markup_node_pool.get(curr_mark_node_id); let curr_mark_node = self.mark_node_pool.get(curr_mark_node_id);
if let Some(parent_id) = curr_mark_node.get_parent_id_opt() { if let Some(parent_id) = curr_mark_node.get_parent_id_opt() {
let parent = self.markup_node_pool.get(parent_id); let parent = self.mark_node_pool.get(parent_id);
parent.get_child_indices(curr_mark_node_id, &self.markup_node_pool) parent.get_child_indices(curr_mark_node_id, &self.mark_node_pool)
} else { } else {
MissingParent { MissingParent {
node_id: curr_mark_node_id, node_id: curr_mark_node_id,

View file

@ -26,8 +26,10 @@ use crate::editor::mvc::string_update::update_string;
use crate::editor::slow_pool::MarkNodeId; use crate::editor::slow_pool::MarkNodeId;
use crate::editor::slow_pool::SlowPool; use crate::editor::slow_pool::SlowPool;
use crate::editor::syntax_highlight::HighlightStyle; use crate::editor::syntax_highlight::HighlightStyle;
use crate::lang::ast::Def2;
use crate::lang::ast::{Expr2, ExprId}; use crate::lang::ast::{Expr2, ExprId};
use crate::lang::constrain::constrain_expr; use crate::lang::constrain::constrain_expr;
use crate::lang::parse::ASTNodeId;
use crate::lang::pool::Pool; use crate::lang::pool::Pool;
use crate::lang::pool::PoolStr; use crate::lang::pool::PoolStr;
use crate::lang::types::Type2; use crate::lang::types::Type2;
@ -116,7 +118,7 @@ impl<'a> EdModel<'a> {
pub fn build_node_map_from_markup( pub fn build_node_map_from_markup(
markup_ids: &[MarkNodeId], markup_ids: &[MarkNodeId],
markup_node_pool: &SlowPool, mark_node_pool: &SlowPool,
) -> EdResult<GridNodeMap> { ) -> EdResult<GridNodeMap> {
let mut grid_node_map = GridNodeMap::new(); let mut grid_node_map = GridNodeMap::new();
let mut line_ctr = 0; let mut line_ctr = 0;
@ -126,28 +128,28 @@ impl<'a> EdModel<'a> {
*mark_id, *mark_id,
&mut grid_node_map, &mut grid_node_map,
&mut line_ctr, &mut line_ctr,
markup_node_pool, mark_node_pool,
)?; )?;
} }
Ok(grid_node_map) Ok(grid_node_map)
} }
pub fn add_mark_node(&mut self, node: MarkupNode) -> MarkNodeId { pub fn add_mark_node<T>(&mut self, node: MarkupNode) -> MarkNodeId {
self.markup_node_pool.add(node) self.mark_node_pool.add(node)
} }
fn build_grid_node_map( fn build_grid_node_map(
node_id: MarkNodeId, node_id: MarkNodeId,
grid_node_map: &mut GridNodeMap, grid_node_map: &mut GridNodeMap,
line_ctr: &mut usize, line_ctr: &mut usize,
markup_node_pool: &SlowPool, mark_node_pool: &SlowPool,
) -> EdResult<()> { ) -> EdResult<()> {
let node = markup_node_pool.get(node_id); let node = mark_node_pool.get(node_id);
if node.is_nested() { if node.is_nested() {
for child_id in node.get_children_ids() { for child_id in node.get_children_ids() {
EdModel::build_grid_node_map(child_id, grid_node_map, line_ctr, markup_node_pool)?; EdModel::build_grid_node_map(child_id, grid_node_map, line_ctr, mark_node_pool)?;
} }
} else { } else {
let node_content_str = node.get_content(); let node_content_str = node.get_content();
@ -165,12 +167,12 @@ impl<'a> EdModel<'a> {
pub fn build_code_lines_from_markup( pub fn build_code_lines_from_markup(
markup_node_ids: &[MarkNodeId], markup_node_ids: &[MarkNodeId],
markup_node_pool: &SlowPool, mark_node_pool: &SlowPool,
) -> EdResult<CodeLines> { ) -> EdResult<CodeLines> {
let mut all_code_string = String::new(); let mut all_code_string = String::new();
for mark_node_id in markup_node_ids.iter() { for mark_node_id in markup_node_ids.iter() {
EdModel::build_markup_string(*mark_node_id, &mut all_code_string, markup_node_pool)?; EdModel::build_markup_string(*mark_node_id, &mut all_code_string, mark_node_pool)?;
} }
let code_lines = CodeLines::from_str(&all_code_string); let code_lines = CodeLines::from_str(&all_code_string);
@ -181,13 +183,13 @@ impl<'a> EdModel<'a> {
fn build_markup_string( fn build_markup_string(
node_id: MarkNodeId, node_id: MarkNodeId,
all_code_string: &mut String, all_code_string: &mut String,
markup_node_pool: &SlowPool, mark_node_pool: &SlowPool,
) -> EdResult<()> { ) -> EdResult<()> {
let node = markup_node_pool.get(node_id); let node = mark_node_pool.get(node_id);
if node.is_nested() { if node.is_nested() {
for child_id in node.get_children_ids() { for child_id in node.get_children_ids() {
EdModel::build_markup_string(child_id, all_code_string, markup_node_pool)?; EdModel::build_markup_string(child_id, all_code_string, mark_node_pool)?;
} }
} else { } else {
let node_content_str = node.get_content(); let node_content_str = node.get_content();
@ -224,7 +226,7 @@ impl<'a> EdModel<'a> {
let mut col_nr = index; let mut col_nr = index;
for &node_id in node_ids { for &node_id in node_ids {
let node_content_str = self.markup_node_pool.get(node_id).get_content(); let node_content_str = self.mark_node_pool.get(node_id).get_content();
self.grid_node_map.insert_between_line( self.grid_node_map.insert_between_line(
line_nr, line_nr,
@ -284,10 +286,10 @@ impl<'a> EdModel<'a> {
pub fn select_expr(&mut self) -> EdResult<()> { pub fn select_expr(&mut self) -> EdResult<()> {
// include parent in selection if an `Expr2` was already selected // include parent in selection if an `Expr2` was already selected
if let Some(selected_expr) = &self.selected_expr_opt { if let Some(selected_expr) = &self.selected_expr_opt {
let expr2_level_mark_node = self.markup_node_pool.get(selected_expr.mark_node_id); let expr2_level_mark_node = self.mark_node_pool.get(selected_expr.mark_node_id);
if let Some(parent_id) = expr2_level_mark_node.get_parent_id_opt() { if let Some(parent_id) = expr2_level_mark_node.get_parent_id_opt() {
let parent_mark_node = self.markup_node_pool.get(parent_id); let parent_mark_node = self.mark_node_pool.get(parent_id);
let ast_node_id = parent_mark_node.get_ast_node_id(); let ast_node_id = parent_mark_node.get_ast_node_id();
let (expr_start_pos, expr_end_pos) = self let (expr_start_pos, expr_end_pos) = self
@ -433,7 +435,7 @@ impl<'a> EdModel<'a> {
fn replace_selected_expr_with_blank(&mut self) -> EdResult<()> { fn replace_selected_expr_with_blank(&mut self) -> EdResult<()> {
let expr_mark_node_id_opt = if let Some(sel_expr) = &self.selected_expr_opt { let expr_mark_node_id_opt = if let Some(sel_expr) = &self.selected_expr_opt {
let expr2_level_mark_node = self.markup_node_pool.get(sel_expr.mark_node_id); let expr2_level_mark_node = self.mark_node_pool.get(sel_expr.mark_node_id);
let newline_at_end = expr2_level_mark_node.has_newline_at_end(); let newline_at_end = expr2_level_mark_node.has_newline_at_end();
let blank_replacement = MarkupNode::Blank { let blank_replacement = MarkupNode::Blank {
@ -444,7 +446,7 @@ impl<'a> EdModel<'a> {
newline_at_end, newline_at_end,
}; };
self.markup_node_pool self.mark_node_pool
.replace_node(sel_expr.mark_node_id, blank_replacement); .replace_node(sel_expr.mark_node_id, blank_replacement);
let active_selection = self.get_selection().context(MissingSelection {})?; let active_selection = self.get_selection().context(MissingSelection {})?;
@ -616,15 +618,15 @@ pub struct NodeContext<'a> {
pub curr_mark_node_id: MarkNodeId, pub curr_mark_node_id: MarkNodeId,
pub curr_mark_node: &'a MarkupNode, pub curr_mark_node: &'a MarkupNode,
pub parent_id_opt: Option<MarkNodeId>, pub parent_id_opt: Option<MarkNodeId>,
pub ast_node_id: ExprId, pub ast_node_id: ASTNodeId,
} }
pub fn get_node_context<'a>(ed_model: &'a EdModel) -> EdResult<NodeContext<'a>> { pub fn get_node_context<'a, T>(ed_model: &'a EdModel) -> EdResult<NodeContext<'a>> {
let old_caret_pos = ed_model.get_caret(); let old_caret_pos = ed_model.get_caret();
let curr_mark_node_id = ed_model let curr_mark_node_id = ed_model
.grid_node_map .grid_node_map
.get_id_at_row_col(ed_model.get_caret())?; .get_id_at_row_col(ed_model.get_caret())?;
let curr_mark_node = ed_model.markup_node_pool.get(curr_mark_node_id); let curr_mark_node = ed_model.mark_node_pool.get(curr_mark_node_id);
let parent_id_opt = curr_mark_node.get_parent_id_opt(); let parent_id_opt = curr_mark_node.get_parent_id_opt();
let ast_node_id = curr_mark_node.get_ast_node_id(); let ast_node_id = curr_mark_node.get_ast_node_id();
@ -662,7 +664,7 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
let outcome = let outcome =
if ed_model.node_exists_at_caret() { if ed_model.node_exists_at_caret() {
let curr_mark_node_id = ed_model.get_curr_mark_node_id()?; let curr_mark_node_id = ed_model.get_curr_mark_node_id()?;
let curr_mark_node = ed_model.markup_node_pool.get(curr_mark_node_id); let curr_mark_node = ed_model.mark_node_pool.get(curr_mark_node_id);
let prev_mark_node_id_opt = ed_model.get_prev_mark_node_id()?; let prev_mark_node_id_opt = ed_model.get_prev_mark_node_id()?;
let ast_node_id = curr_mark_node.get_ast_node_id(); let ast_node_id = curr_mark_node.get_ast_node_id();
@ -743,7 +745,7 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
_ => { _ => {
let prev_ast_node_id = let prev_ast_node_id =
ed_model ed_model
.markup_node_pool .mark_node_pool
.get(prev_mark_node_id) .get(prev_mark_node_id)
.get_ast_node_id(); .get_ast_node_id();
@ -763,7 +765,7 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
)? )?
} }
Expr2::Record{ record_var:_, fields } => { Expr2::Record{ record_var:_, fields } => {
let prev_mark_node = ed_model.markup_node_pool.get(prev_mark_node_id); let prev_mark_node = ed_model.mark_node_pool.get(prev_mark_node_id);
if (curr_mark_node.get_content() == nodes::RIGHT_ACCOLADE || curr_mark_node.get_content() == nodes::COLON) && if (curr_mark_node.get_content() == nodes::RIGHT_ACCOLADE || curr_mark_node.get_content() == nodes::COLON) &&
prev_mark_node.is_all_alphanumeric() { prev_mark_node.is_all_alphanumeric() {
@ -787,7 +789,7 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
} }
} }
Expr2::List{ elem_var: _, elems: _} => { Expr2::List{ elem_var: _, elems: _} => {
let prev_mark_node = ed_model.markup_node_pool.get(prev_mark_node_id); let prev_mark_node = ed_model.mark_node_pool.get(prev_mark_node_id);
if prev_mark_node.get_content() == nodes::LEFT_SQUARE_BR && curr_mark_node.get_content() == nodes::RIGHT_SQUARE_BR { if prev_mark_node.get_content() == nodes::LEFT_SQUARE_BR && curr_mark_node.get_content() == nodes::RIGHT_SQUARE_BR {
// based on if, we are at the start of the list // based on if, we are at the start of the list
@ -806,7 +808,7 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
_ => { _ => {
match ast_node_ref { match ast_node_ref {
Expr2::EmptyRecord => { Expr2::EmptyRecord => {
let sibling_ids = curr_mark_node.get_sibling_ids(&ed_model.markup_node_pool); let sibling_ids = curr_mark_node.get_sibling_ids(&ed_model.mark_node_pool);
update_empty_record( update_empty_record(
&ch.to_string(), &ch.to_string(),
@ -825,7 +827,7 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
let mark_parent_id_opt = curr_mark_node.get_parent_id_opt(); let mark_parent_id_opt = curr_mark_node.get_parent_id_opt();
if let Some(mark_parent_id) = mark_parent_id_opt { if let Some(mark_parent_id) = mark_parent_id_opt {
let parent_ast_id = ed_model.markup_node_pool.get(mark_parent_id).get_ast_node_id(); let parent_ast_id = ed_model.mark_node_pool.get(mark_parent_id).get_ast_node_id();
update_record_colon(ed_model, parent_ast_id)? update_record_colon(ed_model, parent_ast_id)?
} else { } else {
@ -838,7 +840,7 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
let mark_parent_id_opt = curr_mark_node.get_parent_id_opt(); let mark_parent_id_opt = curr_mark_node.get_parent_id_opt();
if let Some(mark_parent_id) = mark_parent_id_opt { if let Some(mark_parent_id) = mark_parent_id_opt {
let parent_ast_id = ed_model.markup_node_pool.get(mark_parent_id).get_ast_node_id(); let parent_ast_id = ed_model.mark_node_pool.get(mark_parent_id).get_ast_node_id();
let parent_expr2 = ed_model.module.env.pool.get(parent_ast_id); let parent_expr2 = ed_model.module.env.pool.get(parent_ast_id);
match parent_expr2 { match parent_expr2 {
@ -864,7 +866,7 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
} }
} }
} else if "\"{[".contains(*ch) { } else if "\"{[".contains(*ch) {
let prev_mark_node = ed_model.markup_node_pool.get(prev_mark_node_id); let prev_mark_node = ed_model.mark_node_pool.get(prev_mark_node_id);
if prev_mark_node.get_content() == nodes::LEFT_SQUARE_BR && curr_mark_node.get_content() == nodes::RIGHT_SQUARE_BR { if prev_mark_node.get_content() == nodes::LEFT_SQUARE_BR && curr_mark_node.get_content() == nodes::RIGHT_SQUARE_BR {
let (new_child_index, new_ast_child_index) = ed_model.get_curr_child_indices()?; let (new_child_index, new_ast_child_index) = ed_model.get_curr_child_indices()?;
@ -943,7 +945,7 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
} else { } else {
let prev_mark_node_id_opt = ed_model.get_prev_mark_node_id()?; let prev_mark_node_id_opt = ed_model.get_prev_mark_node_id()?;
if let Some(prev_mark_node_id) = prev_mark_node_id_opt { if let Some(prev_mark_node_id) = prev_mark_node_id_opt {
let prev_mark_node = ed_model.markup_node_pool.get(prev_mark_node_id); let prev_mark_node = ed_model.mark_node_pool.get(prev_mark_node_id);
let prev_ast_node = ed_model.module.env.pool.get(prev_mark_node.get_ast_node_id()); let prev_ast_node = ed_model.module.env.pool.get(prev_mark_node.get_ast_node_id());
@ -963,11 +965,11 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
if caret_pos.line > 0 { if caret_pos.line > 0 {
// TODO avoid code replication with '\r' // TODO avoid code replication with '\r'
// insert blank first, this simplifies flow // insert blank first, this simplifies flow
let new_blank = Expr2::Blank; let new_blank = Def2::Blank;
let new_blank_id = ed_model.module.env.pool.add(new_blank); let new_blank_id = ed_model.module.env.pool.add(new_blank);
// TODO this should insert at caret line_nr, not push at end // TODO this should insert at caret line_nr, not push at end
ed_model.module.ast.expression_ids.push(new_blank_id); ed_model.module.ast.def_ids.push(new_blank_id);
let blank_mn_id = ed_model let blank_mn_id = ed_model
.add_mark_node(new_blank_mn(new_blank_id, None)); .add_mark_node(new_blank_mn(new_blank_id, None));

View file

@ -83,7 +83,7 @@ pub fn model_to_wgpu<'a>(
txt_coords, txt_coords,
config, config,
glyph_dim_rect, glyph_dim_rect,
&ed_model.markup_node_pool, &ed_model.mark_node_pool,
)?; )?;
all_rendered.extend(rendered_code_graphics); all_rendered.extend(rendered_code_graphics);

View file

@ -51,7 +51,7 @@ pub fn start_new_int(ed_model: &mut EdModel, digit_char: &char) -> EdResult<Inpu
if is_blank_node { if is_blank_node {
ed_model ed_model
.markup_node_pool .mark_node_pool
.replace_node(curr_mark_node_id, int_node); .replace_node(curr_mark_node_id, int_node);
// remove data corresponding to Blank node // remove data corresponding to Blank node
@ -87,7 +87,7 @@ pub fn update_int(
.grid_node_map .grid_node_map
.get_offset_to_node_id(old_caret_pos, int_mark_node_id)?; .get_offset_to_node_id(old_caret_pos, int_mark_node_id)?;
let int_mark_node = ed_model.markup_node_pool.get_mut(int_mark_node_id); let int_mark_node = ed_model.mark_node_pool.get_mut(int_mark_node_id);
let int_ast_node_id = int_mark_node.get_ast_node_id(); let int_ast_node_id = int_mark_node.get_ast_node_id();
let content_str_mut = int_mark_node.get_content_mut()?; let content_str_mut = int_mark_node.get_content_mut()?;

View file

@ -85,7 +85,7 @@ pub fn start_new_let_value(ed_model: &mut EdModel, new_char: &char) -> EdResult<
if is_blank_node { if is_blank_node {
ed_model ed_model
.markup_node_pool .mark_node_pool
.replace_node(curr_mark_node_id, val_mark_node); .replace_node(curr_mark_node_id, val_mark_node);
// remove data corresponding to Blank node // remove data corresponding to Blank node
@ -118,7 +118,7 @@ pub fn update_let_value(
let old_caret_pos = ed_model.get_caret(); let old_caret_pos = ed_model.get_caret();
// update markup // update markup
let val_name_mn_mut = ed_model.markup_node_pool.get_mut(val_name_mn_id); let val_name_mn_mut = ed_model.mark_node_pool.get_mut(val_name_mn_id);
let content_str_mut = val_name_mn_mut.get_content_mut()?; let content_str_mut = val_name_mn_mut.get_content_mut()?;
let old_val_name = content_str_mut.clone(); let old_val_name = content_str_mut.clone();

View file

@ -49,7 +49,7 @@ pub fn start_new_list(ed_model: &mut EdModel) -> EdResult<InputOutcome> {
if is_blank_node { if is_blank_node {
ed_model ed_model
.markup_node_pool .mark_node_pool
.replace_node(curr_mark_node_id, nested_node); .replace_node(curr_mark_node_id, nested_node);
// remove data corresponding to Blank node // remove data corresponding to Blank node
@ -94,7 +94,7 @@ pub fn add_blank_child(
let trip_result: EdResult<(ExprId, ExprId, MarkNodeId)> = if let Some(parent_id) = parent_id_opt let trip_result: EdResult<(ExprId, ExprId, MarkNodeId)> = if let Some(parent_id) = parent_id_opt
{ {
let parent = ed_model.markup_node_pool.get(parent_id); let parent = ed_model.mark_node_pool.get(parent_id);
let list_ast_node_id = parent.get_ast_node_id(); let list_ast_node_id = parent.get_ast_node_id();
let list_ast_node = ed_model.module.env.pool.get(list_ast_node_id); let list_ast_node = ed_model.module.env.pool.get(list_ast_node_id);
@ -162,7 +162,7 @@ pub fn add_blank_child(
ed_model, ed_model,
)?; )?;
let parent = ed_model.markup_node_pool.get_mut(parent_id); let parent = ed_model.mark_node_pool.get_mut(parent_id);
for (indx, child) in new_mark_children.iter().enumerate() { for (indx, child) in new_mark_children.iter().enumerate() {
parent.add_child_at_index(new_child_index + indx, *child)?; parent.add_child_at_index(new_child_index + indx, *child)?;

View file

@ -35,7 +35,7 @@ pub fn update_invalid_lookup(
.set(ast_node_id, Expr2::InvalidLookup(new_pool_str)); .set(ast_node_id, Expr2::InvalidLookup(new_pool_str));
// update MarkupNode // update MarkupNode
let curr_mark_node_mut = ed_model.markup_node_pool.get_mut(curr_mark_node_id); let curr_mark_node_mut = ed_model.mark_node_pool.get_mut(curr_mark_node_id);
let content_str_mut = curr_mark_node_mut.get_content_mut()?; let content_str_mut = curr_mark_node_mut.get_content_mut()?;
content_str_mut.insert_str(caret_offset, input_str); content_str_mut.insert_str(caret_offset, input_str);

View file

@ -51,7 +51,7 @@ pub fn start_new_record(ed_model: &mut EdModel) -> EdResult<InputOutcome> {
if is_blank_node { if is_blank_node {
ed_model ed_model
.markup_node_pool .mark_node_pool
.replace_node(curr_mark_node_id, nested_node); .replace_node(curr_mark_node_id, nested_node);
// remove data corresponding to Blank node // remove data corresponding to Blank node
@ -83,7 +83,7 @@ pub fn update_empty_record(
if input_chars.all(|ch| ch.is_ascii_alphabetic()) if input_chars.all(|ch| ch.is_ascii_alphabetic())
&& input_chars.all(|ch| ch.is_ascii_lowercase()) && input_chars.all(|ch| ch.is_ascii_lowercase())
{ {
let prev_mark_node = ed_model.markup_node_pool.get(prev_mark_node_id); let prev_mark_node = ed_model.mark_node_pool.get(prev_mark_node_id);
let NodeContext { let NodeContext {
old_caret_pos, old_caret_pos,
@ -123,7 +123,7 @@ pub fn update_empty_record(
let record_field_node_id = ed_model.add_mark_node(record_field_node); let record_field_node_id = ed_model.add_mark_node(record_field_node);
if let Some(parent_id) = parent_id_opt { if let Some(parent_id) = parent_id_opt {
let parent = ed_model.markup_node_pool.get_mut(parent_id); let parent = ed_model.mark_node_pool.get_mut(parent_id);
let new_child_index = index_of(curr_mark_node_id, &sibling_ids)?; let new_child_index = index_of(curr_mark_node_id, &sibling_ids)?;
@ -171,7 +171,7 @@ pub fn update_record_colon(
let prev_mark_node_id_opt = ed_model.get_prev_mark_node_id()?; let prev_mark_node_id_opt = ed_model.get_prev_mark_node_id()?;
if let Some(prev_mark_node_id) = prev_mark_node_id_opt { if let Some(prev_mark_node_id) = prev_mark_node_id_opt {
let prev_mark_node = ed_model.markup_node_pool.get(prev_mark_node_id); let prev_mark_node = ed_model.mark_node_pool.get(prev_mark_node_id);
let prev_ast_node = ed_model let prev_ast_node = ed_model
.module .module
@ -183,7 +183,7 @@ pub fn update_record_colon(
if matches!(prev_ast_node, Expr2::Record { .. }) if matches!(prev_ast_node, Expr2::Record { .. })
&& matches!(curr_ast_node, Expr2::Record { .. }) && matches!(curr_ast_node, Expr2::Record { .. })
{ {
let sibling_ids = curr_mark_node.get_sibling_ids(&ed_model.markup_node_pool); let sibling_ids = curr_mark_node.get_sibling_ids(&ed_model.mark_node_pool);
let new_child_index = index_of(curr_mark_node_id, &sibling_ids)?; let new_child_index = index_of(curr_mark_node_id, &sibling_ids)?;
@ -197,7 +197,7 @@ pub fn update_record_colon(
if ed_model.node_exists_at_caret() { if ed_model.node_exists_at_caret() {
let next_mark_node_id = let next_mark_node_id =
ed_model.grid_node_map.get_id_at_row_col(old_caret_pos)?; ed_model.grid_node_map.get_id_at_row_col(old_caret_pos)?;
let next_mark_node = ed_model.markup_node_pool.get(next_mark_node_id); let next_mark_node = ed_model.mark_node_pool.get(next_mark_node_id);
if next_mark_node.get_content() == nodes::RIGHT_ACCOLADE { if next_mark_node.get_content() == nodes::RIGHT_ACCOLADE {
// update AST node // update AST node
let new_field_val = Expr2::Blank; let new_field_val = Expr2::Blank;
@ -229,7 +229,7 @@ pub fn update_record_colon(
let record_colon_node_id = let record_colon_node_id =
ed_model.add_mark_node(record_colon_node); ed_model.add_mark_node(record_colon_node);
ed_model ed_model
.markup_node_pool .mark_node_pool
.get_mut(parent_id) .get_mut(parent_id)
.add_child_at_index(new_child_index, record_colon_node_id)?; .add_child_at_index(new_child_index, record_colon_node_id)?;
@ -237,7 +237,7 @@ pub fn update_record_colon(
.add_mark_node(new_blank_mn(new_field_val_id, Some(parent_id))); .add_mark_node(new_blank_mn(new_field_val_id, Some(parent_id)));
ed_model ed_model
.markup_node_pool .mark_node_pool
.get_mut(parent_id) .get_mut(parent_id)
.add_child_at_index( .add_child_at_index(
new_child_index + 1, new_child_index + 1,
@ -286,7 +286,7 @@ pub fn update_record_field(
ed_model: &mut EdModel, ed_model: &mut EdModel,
) -> EdResult<InputOutcome> { ) -> EdResult<InputOutcome> {
// update MarkupNode // update MarkupNode
let curr_mark_node_mut = ed_model.markup_node_pool.get_mut(curr_mark_node_id); let curr_mark_node_mut = ed_model.mark_node_pool.get_mut(curr_mark_node_id);
let content_str_mut = curr_mark_node_mut.get_content_mut()?; let content_str_mut = curr_mark_node_mut.get_content_mut()?;
let node_caret_offset = ed_model let node_caret_offset = ed_model
.grid_node_map .grid_node_map

View file

@ -28,7 +28,7 @@ pub fn update_small_string(
let new_input = &new_char.to_string(); let new_input = &new_char.to_string();
// update markup // update markup
let curr_mark_node_mut = ed_model.markup_node_pool.get_mut(curr_mark_node_id); let curr_mark_node_mut = ed_model.mark_node_pool.get_mut(curr_mark_node_id);
let content_str_mut = curr_mark_node_mut.get_content_mut()?; let content_str_mut = curr_mark_node_mut.get_content_mut()?;
let node_caret_offset = ed_model let node_caret_offset = ed_model
.grid_node_map .grid_node_map
@ -84,7 +84,7 @@ pub fn update_string(new_char: char, ed_model: &mut EdModel) -> EdResult<InputOu
} = get_node_context(ed_model)?; } = get_node_context(ed_model)?;
// update markup // update markup
let curr_mark_node_mut = ed_model.markup_node_pool.get_mut(curr_mark_node_id); let curr_mark_node_mut = ed_model.mark_node_pool.get_mut(curr_mark_node_id);
let content_str_mut = curr_mark_node_mut.get_content_mut()?; let content_str_mut = curr_mark_node_mut.get_content_mut()?;
let node_caret_offset = ed_model let node_caret_offset = ed_model
.grid_node_map .grid_node_map
@ -143,7 +143,7 @@ pub fn start_new_string(ed_model: &mut EdModel) -> EdResult<InputOutcome> {
}; };
ed_model ed_model
.markup_node_pool .mark_node_pool
.replace_node(curr_mark_node_id, new_string_node); .replace_node(curr_mark_node_id, new_string_node);
// remove data corresponding to Blank node // remove data corresponding to Blank node

View file

@ -5,6 +5,7 @@ use crate::editor::slow_pool::SlowPool;
use crate::editor::{ed_error::EdResult, theme::EdTheme, util::map_get}; use crate::editor::{ed_error::EdResult, theme::EdTheme, util::map_get};
use crate::graphics::primitives::rect::Rect; use crate::graphics::primitives::rect::Rect;
use crate::graphics::primitives::text as gr_text; use crate::graphics::primitives::text as gr_text;
use crate::lang::ast::{DefId, ExprId};
use cgmath::Vector2; use cgmath::Vector2;
use winit::dpi::PhysicalSize; use winit::dpi::PhysicalSize;
@ -16,7 +17,7 @@ pub fn build_code_graphics<'a>(
txt_coords: Vector2<f32>, txt_coords: Vector2<f32>,
config: &Config, config: &Config,
glyph_dim_rect: Rect, glyph_dim_rect: Rect,
markup_node_pool: &'a SlowPool, mark_node_pool: &'a SlowPool,
) -> EdResult<RenderedWgpu> { ) -> EdResult<RenderedWgpu> {
let area_bounds = (size.width as f32, size.height as f32); let area_bounds = (size.width as f32, size.height as f32);
let layout = wgpu_glyph::Layout::default().h_align(wgpu_glyph::HorizontalAlign::Left); let layout = wgpu_glyph::Layout::default().h_align(wgpu_glyph::HorizontalAlign::Left);
@ -27,7 +28,7 @@ pub fn build_code_graphics<'a>(
let mut txt_row_col = (0, 0); let mut txt_row_col = (0, 0);
for markup_id in markup_ids.iter() { for markup_id in markup_ids.iter() {
let mark_node = markup_node_pool.get(*markup_id); let mark_node = mark_node_pool.get(*markup_id);
let (mut glyph_text_vec, mut rects) = markup_to_wgpu( let (mut glyph_text_vec, mut rects) = markup_to_wgpu(
mark_node, mark_node,
@ -38,7 +39,7 @@ pub fn build_code_graphics<'a>(
glyph_dim_rect, glyph_dim_rect,
}, },
&mut txt_row_col, &mut txt_row_col,
markup_node_pool, mark_node_pool,
)?; )?;
all_glyph_text_vec.append(&mut glyph_text_vec); all_glyph_text_vec.append(&mut glyph_text_vec);
@ -69,7 +70,7 @@ fn markup_to_wgpu<'a>(
markup_node: &'a MarkupNode, markup_node: &'a MarkupNode,
code_style: &CodeStyle, code_style: &CodeStyle,
txt_row_col: &mut (usize, usize), txt_row_col: &mut (usize, usize),
markup_node_pool: &'a SlowPool, mark_node_pool: &'a SlowPool,
) -> EdResult<(Vec<glyph_brush::OwnedText>, Vec<Rect>)> { ) -> EdResult<(Vec<glyph_brush::OwnedText>, Vec<Rect>)> {
let mut wgpu_texts: Vec<glyph_brush::OwnedText> = Vec::new(); let mut wgpu_texts: Vec<glyph_brush::OwnedText> = Vec::new();
let mut rects: Vec<Rect> = Vec::new(); let mut rects: Vec<Rect> = Vec::new();
@ -80,7 +81,7 @@ fn markup_to_wgpu<'a>(
&mut rects, &mut rects,
code_style, code_style,
txt_row_col, txt_row_col,
markup_node_pool, mark_node_pool,
)?; )?;
Ok((wgpu_texts, rects)) Ok((wgpu_texts, rects))
@ -92,7 +93,7 @@ fn markup_to_wgpu_helper<'a>(
rects: &mut Vec<Rect>, rects: &mut Vec<Rect>,
code_style: &CodeStyle, code_style: &CodeStyle,
txt_row_col: &mut (usize, usize), txt_row_col: &mut (usize, usize),
markup_node_pool: &'a SlowPool, mark_node_pool: &'a SlowPool,
) -> EdResult<()> { ) -> EdResult<()> {
match markup_node { match markup_node {
MarkupNode::Nested { MarkupNode::Nested {
@ -102,14 +103,14 @@ fn markup_to_wgpu_helper<'a>(
newline_at_end, newline_at_end,
} => { } => {
for child_id in children_ids.iter() { for child_id in children_ids.iter() {
let child = markup_node_pool.get(*child_id); let child = mark_node_pool.get(*child_id);
markup_to_wgpu_helper( markup_to_wgpu_helper(
child, child,
wgpu_texts, wgpu_texts,
rects, rects,
code_style, code_style,
txt_row_col, txt_row_col,
markup_node_pool, mark_node_pool,
)?; )?;
} }

View file

@ -4,7 +4,7 @@ use crate::editor::mvc::ed_model::EdModel;
use crate::graphics::colors; use crate::graphics::colors;
use crate::graphics::colors::from_hsb; use crate::graphics::colors::from_hsb;
use crate::graphics::primitives::text as gr_text; use crate::graphics::primitives::text as gr_text;
use crate::lang::ast::expr2_to_string; use crate::lang::ast::{def2_to_string};
use cgmath::Vector2; use cgmath::Vector2;
use winit::dpi::PhysicalSize; use winit::dpi::PhysicalSize;
@ -38,7 +38,7 @@ pub fn build_debug_graphics(
for mark_id in ed_model.markup_ids[1..].iter() { for mark_id in ed_model.markup_ids[1..].iter() {
// 1.. -> skip header // 1.. -> skip header
mark_node_trees_string.push_str(&tree_as_string(*mark_id, &ed_model.markup_node_pool)); mark_node_trees_string.push_str(&tree_as_string(*mark_id, &ed_model.mark_node_pool));
mark_node_trees_string.push('\n'); mark_node_trees_string.push('\n');
} }
@ -47,14 +47,14 @@ pub fn build_debug_graphics(
.with_color(colors::to_slice(from_hsb(266, 31, 96))) .with_color(colors::to_slice(from_hsb(266, 31, 96)))
.with_scale(config.code_font_size); .with_scale(config.code_font_size);
let mark_node_pool_text = glyph_brush::OwnedText::new(format!("{}", ed_model.markup_node_pool)) let mark_node_pool_text = glyph_brush::OwnedText::new(format!("{}", ed_model.mark_node_pool))
.with_color(colors::to_slice(from_hsb(110, 45, 82))) .with_color(colors::to_slice(from_hsb(110, 45, 82)))
.with_scale(config.code_font_size); .with_scale(config.code_font_size);
let mut ast_node_text_str = "AST:\n".to_owned(); let mut ast_node_text_str = "AST:\n".to_owned();
for expr_id in ed_model.module.ast.expression_ids.iter() { for def_id in ed_model.module.ast.def_ids.iter() {
ast_node_text_str.push_str(&expr2_to_string(*expr_id, ed_model.module.env.pool)) ast_node_text_str.push_str(&def2_to_string(*def_id, ed_model.module.env.pool))
} }
let ast_node_text = glyph_brush::OwnedText::new(ast_node_text_str) let ast_node_text = glyph_brush::OwnedText::new(ast_node_text_str)

View file

@ -15,8 +15,9 @@ app "test-app"
imports [] imports []
provides [ main ] to base provides [ main ] to base
main = "Hello, world!" main = "Hello Main"
mars = "Yo Mars!"
yolo = "Hello, world!"
"#; "#;

View file

@ -224,6 +224,17 @@ pub enum Expr2 {
RuntimeError(/* TODO make a version of RuntimeError that fits in 15B */), RuntimeError(/* TODO make a version of RuntimeError that fits in 15B */),
} }
// A top level definition, not inside a function. For example: `main = "Hello, world!"`
#[derive(Debug)]
pub enum Def2 {
// ValueDef example: `main = "Hello, world!"`. identifier -> `main`, expr -> "Hello, world!"
ValueDef {
identifier_id: NodeId<Pattern2>,
expr_id: NodeId<Expr2>,
},
Blank
}
#[derive(Debug)] #[derive(Debug)]
pub enum ValueDef { pub enum ValueDef {
WithAnnotation { WithAnnotation {
@ -431,6 +442,8 @@ pub struct WhenBranch {
// TODO make the inner types private? // TODO make the inner types private?
pub type ExprId = NodeId<Expr2>; pub type ExprId = NodeId<Expr2>;
pub type DefId = NodeId<Def2>;
use RecordField::*; use RecordField::*;
impl RecordField { impl RecordField {
pub fn get_record_field_var(&self) -> &Variable { pub fn get_record_field_var(&self) -> &Variable {
@ -593,6 +606,25 @@ fn expr2_to_string_helper(
out_string.push('\n'); out_string.push('\n');
} }
pub fn def2_to_string(node_id: DefId, pool: &Pool) -> String {
let mut full_string = String::new();
let def2 = pool.get(node_id);
match def2 {
Def2::ValueDef { identifier_id, expr_id } => {
full_string.push_str(&format!(
"Def2::ValueDef(identifier_id: >>{:?}), expr_id: >>{:?})",
pool.get(*identifier_id), expr2_to_string(*expr_id, pool)
));
},
Def2::Blank => {
full_string.push_str(&format!("Def2::Blank"));
}
}
full_string
}
fn var_to_string(some_var: &Variable, indent_level: usize) -> String { fn var_to_string(some_var: &Variable, indent_level: usize) -> String {
format!("{}Var({:?})\n", get_spacing(indent_level + 1), some_var) format!("{}Var({:?})\n", get_spacing(indent_level + 1), some_var)
} }

View file

@ -5,10 +5,7 @@ use bumpalo::{collections::Vec as BumpVec, Bump};
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::FromIterator; use std::iter::FromIterator;
use crate::lang::ast::{ use crate::lang::ast::{ClosureExtra, Def2, Expr2, ExprId, FloatVal, IntStyle, IntVal, RecordField, ValueDef, WhenBranch, expr2_to_string, value_def_to_string};
expr2_to_string, ClosureExtra, Expr2, ExprId, FloatVal, IntStyle, IntVal, RecordField,
ValueDef, WhenBranch,
};
use crate::lang::def::{ use crate::lang::def::{
canonicalize_defs, sort_can_defs, CanDefs, Declaration, Def, PendingDef, References, canonicalize_defs, sort_can_defs, CanDefs, Declaration, Def, PendingDef, References,
}; };
@ -289,15 +286,15 @@ pub fn to_expr_id<'a>(
(env.add(expr, region), output) (env.add(expr, region), output)
} }
pub fn str_to_expr2_w_defs<'a>( pub fn str_to_def2<'a>(
arena: &'a Bump, arena: &'a Bump,
input: &'a str, input: &'a str,
env: &mut Env<'a>, env: &mut Env<'a>,
scope: &mut Scope, scope: &mut Scope,
region: Region, region: Region,
) -> Result<Vec<Expr2>, SyntaxError<'a>> { ) -> Result<Vec<Def2>, SyntaxError<'a>> {
match roc_parse::test_helpers::parse_defs_with(arena, input.trim()) { match roc_parse::test_helpers::parse_defs_with(arena, input.trim()) {
Ok(vec_loc_def) => Ok(to_expr2_from_defs( Ok(vec_loc_def) => Ok(defs_to_defs2(
arena, arena,
env, env,
scope, scope,
@ -990,38 +987,39 @@ pub fn to_expr2<'a>(
} }
} }
pub fn to_expr2_from_defs<'a>( pub fn defs_to_defs2<'a>(
arena: &'a Bump, arena: &'a Bump,
env: &mut Env<'a>, env: &mut Env<'a>,
scope: &mut Scope, scope: &mut Scope,
parsed_defs: &'a BumpVec<roc_region::all::Loc<roc_parse::ast::Def<'a>>>, parsed_defs: &'a BumpVec<roc_region::all::Loc<roc_parse::ast::Def<'a>>>,
region: Region, region: Region,
) -> Vec<Expr2> { ) -> Vec<Def2> {
use roc_parse::ast::Expr::*; use roc_parse::ast::Expr::*;
parsed_defs parsed_defs
.iter() .iter()
.map(|loc| to_expr2_from_def(arena, env, scope, &loc.value, region)) .map(|loc| to_def2_from_def(arena, env, scope, &loc.value, region))
.collect() .collect()
} }
pub fn to_expr2_from_def<'a>( pub fn to_def2_from_def<'a>(
arena: &'a Bump, arena: &'a Bump,
env: &mut Env<'a>, env: &mut Env<'a>,
scope: &mut Scope, scope: &mut Scope,
parsed_def: &'a roc_parse::ast::Def<'a>, parsed_def: &'a roc_parse::ast::Def<'a>,
region: Region, region: Region,
) -> Expr2 { ) -> Def2 {
use roc_parse::ast::Def::*; use roc_parse::ast::Def::*;
match parsed_def { match parsed_def {
SpaceBefore(inner_def, _) => to_expr2_from_def(arena, env, scope, inner_def, region), SpaceBefore(inner_def, _) => to_def2_from_def(arena, env, scope, inner_def, region),
SpaceAfter(inner_def, _) => to_expr2_from_def(arena, env, scope, inner_def, region), SpaceAfter(inner_def, _) => to_def2_from_def(arena, env, scope, inner_def, region),
Body(&loc_pattern, &loc_expr) => { Body(&loc_pattern, &loc_expr) => {
// TODO loc_pattern use identifier // TODO loc_pattern use identifier
let body_expr2 = loc_expr_to_expr2(arena, loc_expr, env, scope, region).0; let expr2 = loc_expr_to_expr2(arena, loc_expr, env, scope, region).0;
dbg!(&expr2);
let body_expr_id = env.pool.add(body_expr2); let expr_id = env.pool.add(expr2);
dbg!(expr_id);
use roc_parse::ast::Pattern::*; use roc_parse::ast::Pattern::*;
@ -1037,24 +1035,9 @@ pub fn to_expr2_from_def<'a>(
let pattern_id = env.pool.add(pattern2); let pattern_id = env.pool.add(pattern2);
// TODO support with annotation // TODO support with annotation
let value_def = ValueDef::NoAnnotation { Def2::ValueDef {
pattern_id, identifier_id: pattern_id,
expr_id: body_expr_id, expr_id,
expr_var: env.var_store.fresh(),
};
let value_def_id = env.pool.add(value_def);
let ident_id = env.ident_ids.add(str_ref.into());
dbg!(ident_id);
let var_symbol = Symbol::new(env.home, ident_id);
let body = Expr2::Var(var_symbol);
let body_id = env.pool.add(body);
Expr2::LetValue {
def_id: value_def_id,
body_var: env.var_store.fresh(),
body_id,
} }
} }
other => { other => {

View file

@ -1,19 +1,54 @@
use crate::lang::scope::Scope; use std::fmt::Debug;
use crate::{editor::ed_error::EdResult, editor::ed_error::ASTNodeIdWithoutExprId,lang::scope::Scope};
use bumpalo::Bump; use bumpalo::Bump;
use roc_parse::parser::SyntaxError; use roc_parse::parser::SyntaxError;
use roc_region::all::Region; use roc_region::all::Region;
use super::{ use super::{ast::{DefId, Expr2, ExprId}, expr::{str_to_def2, Env}, pool::Pool};
ast::{Expr2, ExprId},
expr::{str_to_expr2_w_defs, Env},
};
// WORK IN PROGRESS FILE // WORK IN PROGRESS FILE
#[derive(Debug)] #[derive(Debug)]
pub struct AST { pub struct AST {
pub header: AppHeader, pub header: AppHeader,
pub expression_ids: Vec<ExprId>, pub def_ids: Vec<DefId>,
}
#[derive(Debug)]
pub struct ASTNodeId {
pub def_id_opt: Option<DefId>,
pub expr_id_opt: Option<ExprId>,
}
impl ASTNodeId {
pub fn to_expr_id(&self) -> EdResult<ExprId>{
if let Some(expr_id) = self.expr_id_opt {
Ok(expr_id)
} else {
ASTNodeIdWithoutExprId {
ast_node_id: self
}.fail()
}
}
pub fn equals(&self, other: &ASTNodeId) -> bool {
if let Some(def_id_self) = self.def_id_opt {
if let Some(def_id_other) = other.def_id_opt {
def_id_self == def_id_other
} else {
false
}
} else if let Some(expr_id_self) = self.expr_id_opt {
if let Some(expr_id_other) = other.expr_id_opt {
expr_id_self == expr_id_other
} else {
false
}
} else {
unreachable!()
}
}
} }
#[derive(Debug)] #[derive(Debug)]
@ -41,21 +76,21 @@ impl AST {
let mut scope = Scope::new(env.home, env.pool, env.var_store); let mut scope = Scope::new(env.home, env.pool, env.var_store);
let region = Region::new(0, 0, 0, 0); let region = Region::new(0, 0, 0, 0);
let mut expression_ids = Vec::<ExprId>::new(); let mut def_ids = Vec::<DefId>::new();
let expr2_vec = str_to_expr2_w_defs(ast_arena, tail_str, env, &mut scope, region)?; let def2_vec = str_to_def2(ast_arena, tail_str, env, &mut scope, region)?;
for expr2 in expr2_vec { for def2 in def2_vec {
let expr_id = env.pool.add(expr2); let def_id = env.pool.add(def2);
expression_ids.push(expr_id); def_ids.push(def_id);
} }
let ast_node_id = env.pool.add(Expr2::Blank); let ast_node_id = env.pool.add(Expr2::Blank);
Ok(AST { Ok(AST {
header: AppHeader::parse_from_string(header_str, ast_node_id), header: AppHeader::parse_from_string(header_str, ast_node_id),
expression_ids, def_ids,
}) })
} }
} }