Michael Sippel 3 months ago
parent c29477ceb9
commit 67036488bc
Signed by: senvas
GPG Key ID: F96CF119C34B64A6

@ -6,5 +6,5 @@ members = [
# "math/str2int",
# "math/int2str",
# "math/radix_transform",
# "math/fib"
"math/fib"
]

@ -11,34 +11,7 @@ use crate::{
tree::{nav::TreeNavResult}
};
//use r3vi::view::singleton::*;
pub trait ObjCommander {
fn send_cmd_obj(&mut self, cmd_obj: Arc<RwLock<ReprTree>>) -> TreeNavResult;
}
/*
//impl<Cmd: 'static, T: Commander<Cmd>> ObjCommander for T {
impl<C: Commander> ObjCommander for C
where C::Cmd: 'static
{
fn send_cmd_obj(&mut self, _cmd_obj: Arc<RwLock<ReprTree>>) -> TreeNavResult{
/*
self.send_cmd(
&cmd_obj.read().unwrap()
.get_port::<dyn SingletonView<Item = C::Cmd>>().unwrap()
.get_view().unwrap()
.get()
);
*/
}
}
impl<T: Clone + Send + Sync> Commander for r3vi::buffer::vec::VecBuffer<T> {
type Cmd = r3vi::buffer::vec::VecDiff<T>;
fn send_cmd(&mut self, cmd: &Self::Cmd) -> TreeNavResult {
self.apply_diff(cmd.clone());
TreeNavResult::
}
}
*/

@ -114,7 +114,7 @@ impl DigitEditor {
pub fn get_type(&self) -> TypeTerm {
TypeTerm::Type {
id: self.ctx.read().unwrap().get_typeid("Digit").unwrap(),
id: self.ctx.read().unwrap().get_fun_typeid("Digit").unwrap(),
args: vec![
TypeTerm::Num(self.radix as i64).into()
]
@ -143,7 +143,7 @@ impl PosIntEditor {
let editor = PTYListEditor::new(
ctx.clone(),
TypeTerm::Type {
id: ctx.read().unwrap().get_typeid("Digit").unwrap(),
id: ctx.read().unwrap().get_fun_typeid("Digit").unwrap(),
args: vec![
TypeTerm::Num(radix as i64).into()
]
@ -167,11 +167,11 @@ impl PosIntEditor {
node = node.set_data(ReprTree::ascend(
&data,
TypeTerm::Type {
id: ctx.read().unwrap().get_typeid("PosInt").unwrap(),
id: ctx.read().unwrap().get_fun_typeid("PosInt").unwrap(),
args: vec![
TypeTerm::Num(radix as i64).into(),
TypeTerm::Type {
id: ctx.read().unwrap().get_typeid("BigEndian").unwrap(),
id: ctx.read().unwrap().get_fun_typeid("BigEndian").unwrap(),
args: vec![]
}.into()
]

@ -124,7 +124,7 @@ impl ListEditor {
pub fn get_seq_type(&self) -> TypeTerm {
TypeTerm::Type {
id: self.ctx.read().unwrap().get_typeid("List").unwrap(),
id: self.ctx.read().unwrap().get_fun_typeid("List").unwrap(),
args: vec![ self.get_item_type().into() ]
}
}
@ -255,7 +255,7 @@ impl ListEditor {
let prev_node = self.data.get(prev_idx);
if let Some(prev_editor) = prev_node.editor.clone() {
let prev_editor = prev_editor.downcast::<RwLock<ListEditor>>().unwrap();
let prev_editor = prev_editor.get_view().unwrap().get().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
let prev_editor = prev_editor.write().unwrap();
if prev_editor.get_data_port().get_view().unwrap().iter()

@ -24,6 +24,14 @@ pub struct PTYListEditor {
depth: usize
}
pub struct PTYListView {
}
pub struct PTYListController {
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl PTYListEditor {
@ -141,7 +149,7 @@ impl PTYListEditor {
if let Some(head_editor) = item.editor.clone() {
let head = head_editor.downcast::<RwLock<ListEditor>>().unwrap();
let head = head_editor.get_view().unwrap().get().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
let mut head = head.write().unwrap();
if head.data.len() > 0 {
@ -193,11 +201,11 @@ impl PTYListEditor {
let prev_editor = e.data.get_mut(idx as usize-1);
let prev_editor = prev_editor.editor.clone();
if let Some(prev_editor) = prev_editor {
if let Ok(prev_editor) = prev_editor.downcast::<RwLock<ListEditor>>() {
if let Ok(prev_editor) = prev_editor.get_view().unwrap().get().unwrap().downcast::<RwLock<ListEditor>>() {
let mut prev_editor = prev_editor.write().unwrap();
let cur_editor = item.editor.clone().unwrap();
let cur_editor = cur_editor.downcast::<RwLock<ListEditor>>().unwrap();
let cur_editor = cur_editor.get_view().unwrap().get().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
let cur_editor = cur_editor.write().unwrap();
prev_editor.join(&cur_editor);
@ -219,10 +227,10 @@ impl PTYListEditor {
{
let next_editor = e.data.get_mut(next_idx).editor.clone();
if let Some(next_editor) = next_editor {
if let Ok(next_editor) = next_editor.downcast::<RwLock<ListEditor>>() {
if let Ok(next_editor) = next_editor.get_view().unwrap().get().unwrap().downcast::<RwLock<ListEditor>>() {
let mut next_editor = next_editor.write().unwrap();
let cur_editor = item.editor.clone().unwrap();
let cur_editor = cur_editor.downcast::<RwLock<ListEditor>>().unwrap();
let cur_editor = cur_editor.get_view().unwrap().get().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
let mut cur_editor = cur_editor.write().unwrap();
cur_editor.join(&next_editor);
@ -247,8 +255,24 @@ impl ObjCommander for PTYListEditor {
let co = cmd_obj.read().unwrap();
let cmd_type = co.get_type().clone();
let term_event_type = ctx.type_term_from_str("( TerminalEvent )").unwrap();
let nested_node_type = ctx.type_term_from_str("( NestedNode )").unwrap();
let char_type = ctx.type_term_from_str("( Char )").unwrap();
if cmd_type == nested_node_type {
if let Some(node_view) = co.get_view::<dyn SingletonView<Item = NestedNode>>() {
if let Some(idx) = cur.idx {
match cur.mode {
ListCursorMode::Select => {
*e.data.get_mut(idx as usize) = node_view.get();
}
ListCursorMode::Insert => {
e.data.insert(idx as usize, node_view.get());
}
}
}
}
}
if cmd_type == term_event_type {
if let Some(te_view) = co.get_view::<dyn SingletonView<Item = TerminalEvent>>() {
drop(co);

@ -65,7 +65,7 @@ pub fn read_ansi_from<R: Read + Unpin>(
loop {
match ansi_reader.read(&mut buf) {
Ok(0) => break,
// Ok(0) => break,
Ok(n) => {
for byte in &buf[..n] {
statemachine.advance(&mut performer, *byte);

@ -24,7 +24,7 @@ pub struct NestedNode {
pub ctx: Option<Arc<RwLock<Context>>>,
/// abstract editor
pub editor: Option<Arc<dyn Any + Send + Sync>>,
pub editor: Option<OuterViewPort<dyn SingletonView<Item = Option<Arc<dyn Any + Send + Sync>>>>>,
/// abstract data view
pub data: Option<Arc<RwLock<ReprTree>>>,
@ -195,7 +195,7 @@ impl NestedNode {
}
pub fn set_editor(mut self, editor: Arc<dyn Any + Send + Sync>) -> Self {
self.editor = Some(editor);
self.editor = Some(SingletonBuffer::new(Some(editor)).get_port());
self
}
@ -267,7 +267,7 @@ impl NestedNode {
*/
pub fn get_edit<T: Send + Sync + 'static>(&self) -> Option<Arc<RwLock<T>>> {
if let Some(edit) = self.editor.clone() {
if let Ok(edit) = edit.downcast::<RwLock<T>>() {
if let Ok(edit) = edit.get_view().unwrap().get().unwrap().downcast::<RwLock<T>>() {
Some(edit)
} else {
None

@ -1,6 +1,6 @@
use {
crate::{
type_system::{TypeDict, TypeTerm, TypeID, ReprTree},
type_system::{TypeDict, TypeTerm, TypeID, ReprTree, TypeLadder},
tree::NestedNode
},
std::{
@ -11,8 +11,6 @@ use {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct TypeLadder(Vec<TypeTerm>);
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub enum MorphismMode {
/// Isomorphism
@ -50,12 +48,12 @@ impl From<MorphismType> for MorphismTypePattern {
fn from(value: MorphismType) -> MorphismTypePattern {
MorphismTypePattern {
src_tyid: match value.src_type {
Some(TypeTerm::Type { id, args: _ }) => Some(id),
Some(TypeTerm::Type { id, args: _ }) => Some(TypeID::Fun(id)),
_ => None,
},
dst_tyid: match value.dst_type {
TypeTerm::Type { id, args: _ } => id,
TypeTerm::Type { id, args: _ } => TypeID::Fun(id),
_ => unreachable!()
}
}
@ -66,7 +64,7 @@ impl From<MorphismType> for MorphismTypePattern {
pub struct Context {
/// assigns a name to every type
type_dict: Arc<RwLock<TypeDict>>,
pub type_dict: Arc<RwLock<TypeDict>>,
/// named vertices of the graph
nodes: HashMap< String, NestedNode >,
@ -135,7 +133,7 @@ impl Context {
pub fn is_list_type(&self, t: &TypeTerm) -> bool {
match t {
TypeTerm::Type { id, args: _ } => {
self.list_types.contains(id)
self.list_types.contains(&TypeID::Fun(*id))
}
_ => false
}
@ -145,6 +143,20 @@ impl Context {
self.type_dict.read().unwrap().get_typeid(&tn.into())
}
pub fn get_fun_typeid(&self, tn: &str) -> Option<u64> {
match self.get_typeid(tn) {
Some(TypeID::Fun(x)) => Some(x),
_ => None
}
}
pub fn get_var_typeid(&self, tn: &str) -> Option<u64> {
match self.get_typeid(tn) {
Some(TypeID::Var(x)) => Some(x),
_ => None
}
}
pub fn type_term_from_str(&self, tn: &str) -> Option<TypeTerm> {
self.type_dict.read().unwrap().type_term_from_str(&tn)
}

@ -1,19 +1,24 @@
use {
crate::{
utils::Bimap,
type_system::TypeTerm
type_system::{TypeTerm, TypeLadder}
}
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub type TypeID = u64;
#[derive(Eq, PartialEq, Hash, Clone, Debug)]
pub enum TypeID {
Fun(u64),
Var(u64)
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct TypeDict {
typenames: Bimap<String, u64>,
type_id_counter: TypeID,
typenames: Bimap<String, TypeID>,
type_lit_counter: u64,
type_var_counter: u64,
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
@ -22,18 +27,26 @@ impl TypeDict {
pub fn new() -> Self {
TypeDict {
typenames: Bimap::new(),
type_id_counter: 0,
type_lit_counter: 0,
type_var_counter: 0,
}
}
pub fn add_varname(&mut self, tn: String) -> TypeID {
let tyid = TypeID::Var(self.type_var_counter);
self.type_var_counter += 1;
self.typenames.insert(tn, tyid.clone());
tyid
}
pub fn add_typename(&mut self, tn: String) -> TypeID {
let tyid = self.type_id_counter;
self.type_id_counter += 1;
self.typenames.insert(tn, tyid);
let tyid = TypeID::Fun(self.type_lit_counter);
self.type_lit_counter += 1;
self.typenames.insert(tn, tyid.clone());
tyid
}
pub fn get_typename(&self, tid: &u64) -> Option<String> {
pub fn get_typename(&self, tid: &TypeID) -> Option<String> {
self.typenames.my.get(tid).cloned()
}
@ -48,6 +61,10 @@ impl TypeDict {
pub fn type_term_to_str(&self, term: &TypeTerm) -> String {
term.to_str(&self.typenames.my)
}
pub fn type_ladder_to_str(&self, ladder: &TypeLadder) -> String {
ladder.to_str1(&self.typenames.my)
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>

@ -1,119 +1,249 @@
use {
r3vi::{
buffer::singleton::*,
view::{singleton::*, sequence::*, OuterViewPort},
projection::flatten_grid::*,
projection::flatten_singleton::*
},
crate::{
type_system::{Context},
type_system::{Context, TypeTerm, ReprTree},
terminal::{TerminalEvent},
editors::{sum::*},
tree::{TreeNav},
tree::NestedNode,
commander::Commander,
editors::{sum::*, list::{ListCursorMode, ListEditor, PTYListEditor}},
tree::{NestedNode, TreeNav, TreeNavResult, TreeCursor},
commander::ObjCommander,
PtySegment
},
termion::event::{Key},
std::{
sync::{Arc, RwLock}
}
},
cgmath::{Vector2, Point2}
};
#[derive(Clone)]
enum TypeTermVar {
enum State {
Any,
Symbol,
Char,
Num,
List
List,
Symbol,
Fun,
Var,
}
pub struct TypeTermEditor {
ctx: Arc<RwLock<Context>>,
ty: TypeTermVar,
sum_edit: Arc<RwLock<SumEditor>>
state: State,
cur_node: SingletonBuffer<NestedNode>
}
impl TypeTermEditor {
pub fn new(ctx: Arc<RwLock<Context>>, depth: usize) -> Self {
TypeTermEditor {
ctx: ctx.clone(),
ty: TypeTermVar::Any,
sum_edit: Arc::new(RwLock::new(SumEditor::new(
vec![
Context::make_node( &ctx, (&ctx, "( List TypeTerm )").into(), depth + 1).unwrap(),
Context::make_node( &ctx, (&ctx, "( PosInt 10 )").into(), depth + 1 ).unwrap(),
Context::make_node( &ctx, (&ctx, "( Symbol )").into(), depth + 1 ).unwrap()
])))
}
pub fn init_ctx(ctx: &mut Context) {
ctx.add_list_typename("TypeTerm".into());
ctx.add_node_ctor(
"TypeTerm", Arc::new(
|ctx: Arc<RwLock<Context>>, _ty: TypeTerm, depth: usize| {
Some(TypeTermEditor::new_node(ctx, depth))
}
)
);
}
pub fn into_node(self, depth: usize) -> NestedNode {
let ctx = self.ctx.clone();
let sum_edit = self.sum_edit.clone();
let view = sum_edit.read().unwrap().pty_view();
let editor = Arc::new(RwLock::new(self));
fn set_state(&mut self, new_state: State) {
let mut node = match new_state {
State::Char => {
let mut node = Context::make_node( &self.ctx, (&self.ctx, "( Char )").into(), 0 ).unwrap();
let mut grid = r3vi::buffer::index_hashmap::IndexBuffer::new();
grid.insert_iter(
vec![
(Point2::new(0,0), crate::terminal::make_label("'")),
(Point2::new(1,0), node.view.clone().unwrap()),
(Point2::new(2,0), crate::terminal::make_label("'")),
]
);
node.view = Some(
grid.get_port()
.flatten()
);
node
}
State::List => {
let mut node = Context::make_node( &self.ctx, (&self.ctx, "( List TypeTerm )").into(), 0 ).unwrap();
let depth = node.depth;
let editor = node.editor.clone().unwrap().get_view().unwrap().get().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
let pty_editor = PTYListEditor::from_editor(
editor,
Some(' '),
depth
);
node.view = Some(pty_editor.pty_view(
(
"(".into(),
" ".into(),
")".into()
)
));
node.cmd = Some(Arc::new(RwLock::new(pty_editor)));
node
}
State::Symbol => {
Context::make_node( &self.ctx, (&self.ctx, "( Symbol )").into(), 0 ).unwrap()
}
State::Num => {
Context::make_node( &self.ctx, (&self.ctx, "( PosInt 10 BigEndian )").into(), 0 ).unwrap()
}
_ => {
self.cur_node.get()
}
};
node.goto(TreeCursor::home());
self.cur_node.set(node);
self.state = new_state;
}
pub fn new_node(ctx: Arc<RwLock<Context>>, depth: usize) -> NestedNode {
let editor = TypeTermEditor {
ctx: ctx.clone(),
state: State::Any,
cur_node: SingletonBuffer::new(
Context::make_node( &ctx, (&ctx, "( Symbol )").into(), 0 ).unwrap()
)
};
let ed_view = editor.cur_node
.get_port()
.map(
|node|
match node.editor {
Some(e) => {
e
},
None => {
r3vi::buffer::singleton::SingletonBuffer::new(None).get_port()
}
}
)
.flatten();
let view = editor.cur_node
.get_port()
.map(
|node| {
match node.view.clone() {
Some(v) => {
v
}
None => {
r3vi::view::ViewPort::new().into_outer()
}
}
}
)
.to_grid()
.flatten();
let editor = Arc::new(RwLock::new(editor));
NestedNode::new(depth)
let mut node = NestedNode::new(depth)
.set_ctx(ctx)
.set_nav(sum_edit)
.set_cmd(editor.clone())
.set_view(view)
.set_view( view )
.set_nav(editor.clone())
.set_cmd(editor.clone());
node.editor = Some(ed_view);
//node.editor.unwrap().get_view().unwrap().get().unwrap()
node
}
}
impl Commander for TypeTermEditor {
type Cmd = TerminalEvent;
fn send_cmd(&mut self, event: &TerminalEvent) {
match event {
TerminalEvent::Input( termion::event::Event::Key(Key::Char(c)) ) => {
match self.ty {
TypeTermVar::Any => {
self.ty =
if *c == '(' {
let mut se = self.sum_edit.write().unwrap();
se.select(0);
se.dn();
TypeTermVar::List
} else if c.to_digit(10).is_some() {
let mut se = self.sum_edit.write().unwrap();
se.select(1);
se.dn();
se.send_cmd( event );
TypeTermVar::Num
} else {
let mut se = self.sum_edit.write().unwrap();
se.select(2);
se.dn();
se.send_cmd( event );
TypeTermVar::Symbol
};
},
impl TreeNav for TypeTermEditor {
fn get_cursor(&self) -> TreeCursor {
self.cur_node.get().get_cursor()
}
fn get_addr_view(&self) -> OuterViewPort<dyn SequenceView<Item = isize>> {
// fixme this is wrong
self.cur_node.get().get_addr_view()
}
fn get_mode_view(&self) -> OuterViewPort<dyn SingletonView<Item = ListCursorMode>> {
// this is wrong
self.cur_node.get().get_mode_view()
}
fn get_cursor_warp(&self) -> TreeCursor {
self.cur_node.get().get_cursor_warp()
}
fn get_max_depth(&self) -> usize {
self.cur_node.get().get_max_depth()
}
fn goby(&mut self, dir: Vector2<isize>) -> TreeNavResult {
self.cur_node.get_mut().goby(dir)
}
fn goto(&mut self, new_cur: TreeCursor) -> TreeNavResult {
self.cur_node.get_mut().goto(new_cur)
}
}
impl ObjCommander for TypeTermEditor {
fn send_cmd_obj(&mut self, co: Arc<RwLock<ReprTree>>) -> TreeNavResult {
let cmd_obj = co.clone();
let cmd_obj = cmd_obj.read().unwrap();
let cmd_type = cmd_obj.get_type().clone();
let char_type = (&self.ctx, "( Char )").into();
if cmd_type == char_type {
if let Some(cmd_view) = cmd_obj.get_view::<dyn SingletonView<Item = char>>() {
let c = cmd_view.get();
match self.state {
State::Any => {
match c {
'(' => {
self.set_state( State::List );
TreeNavResult::Continue
}
'0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' => {
self.set_state( State::Num );
self.cur_node.get_mut().send_cmd_obj( co );
TreeNavResult::Continue
}
'\'' => {
self.set_state( State::Char );
TreeNavResult::Continue
}
_ => {
self.set_state( State::Symbol );
self.cur_node.get_mut().goto(TreeCursor::home());
self.cur_node.get_mut().send_cmd_obj( co );
TreeNavResult::Continue
}
}
}
_ => {
/*
if *c == '(' {
let child = TypeTermEditor {
ctx: self.ctx.clone(),
ty: self.ty.clone(),
sum_edit: Arc::new(RwLock::new(SumEditor::new(
vec![
self.sum_edit.read().unwrap().editors[0].clone(),
self.sum_edit.read().unwrap().editors[1].clone(),
self.sum_edit.read().unwrap().editors[2].clone(),
])))
};
self.ty = TypeTermVar::List;
self.sum_edit.write().unwrap().select(0);
let l = self.sum_edit.read().unwrap().editors[0].clone();
let l = l.editor.clone().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
l.write().unwrap().insert(TypeTermEditor::new(self.ctx.clone(), 1).into_node());
} else {
*/
self.sum_edit.write().unwrap().send_cmd( event );
//}
self.cur_node.get_mut().send_cmd_obj( co );
TreeNavResult::Continue
}
}
},
event => {
self.sum_edit.write().unwrap().send_cmd( event );
} else {
TreeNavResult::Exit
}
} else {
self.cur_node.get_mut().send_cmd_obj( co )
}
}
}

@ -1,5 +1,5 @@
use {
crate::type_system::TypeTerm,
crate::type_system::{TypeTerm, TypeID},
std::collections::HashMap
};
@ -52,9 +52,15 @@ impl TypeLadder {
}
}
pub fn to_str1(&self, names: &HashMap<u64, String>) -> String {
pub fn to_str1(&self, names: &HashMap<TypeID, String>) -> String {
let mut s = String::new();
let mut first = true;
for t in self.0.iter() {
if !first {
s = s + "~";
}
first = false;
s = s + &t.to_str1(names);
}
s

@ -63,6 +63,8 @@ pub fn init_editor_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
);
ctx.write().unwrap().add_list_typename("Seq".into());
ctx.write().unwrap().add_list_typename("Sequence".into());
ctx.write().unwrap().add_list_typename("SepSeq".into());
ctx.write().unwrap().add_typename("NestedNode".into());
ctx.write().unwrap().add_list_typename("List".into());
ctx.write().unwrap().add_node_ctor(
@ -103,7 +105,7 @@ pub fn init_editor_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
Arc::new(
|mut node, _dst_type:_| {
let depth = node.depth;
let editor = node.editor.clone().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
let editor = node.editor.clone().unwrap().get_view().unwrap().get().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
let pty_editor = PTYListEditor::from_editor(
editor,
None,
@ -123,7 +125,7 @@ pub fn init_editor_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
let mut node = Context::make_node(
&ctx,
TypeTerm::Type {
id: ctx.read().unwrap().get_typeid("List").unwrap(),
id: ctx.read().unwrap().get_fun_typeid("List").unwrap(),
args: vec![
TypeTerm::new(ctx.read().unwrap().get_typeid("Char").unwrap()).into()
]
@ -147,7 +149,7 @@ pub fn init_editor_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
Arc::new(
|mut node, _dst_type:_| {
let depth = node.depth;
let editor = node.editor.clone().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
let editor = node.editor.clone().unwrap().get_view().unwrap().get().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
let pty_editor = PTYListEditor::from_editor(
editor,
None,
@ -171,7 +173,7 @@ pub fn init_editor_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
let mut node = Context::make_node(
&ctx,
TypeTerm::Type {
id: ctx.read().unwrap().get_typeid("List").unwrap(),
id: ctx.read().unwrap().get_fun_typeid("List").unwrap(),
args: vec![
TypeTerm::new(ctx.read().unwrap().get_typeid("Char").unwrap()).into()
]
@ -243,7 +245,7 @@ pub fn init_math_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
Arc::new(
|mut node, dst_type| {
let depth = node.depth;
let editor = node.editor.clone().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
let editor = node.editor.clone().unwrap().get_view().unwrap().get().unwrap().downcast::<RwLock<ListEditor>>().unwrap();
// todo: check src_type parameter to be ( Digit radix )
@ -263,9 +265,9 @@ pub fn init_math_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
// todo: set data view
node.view = Some(pty_editor.pty_view(
(
"{".into(),
", ".into(),
"}".into()
"0d".into(),
"".into(),
"".into()
)
));
node.cmd = Some(Arc::new(RwLock::new(pty_editor)));
@ -297,7 +299,7 @@ pub fn init_math_ctx(parent: Arc<RwLock<Context>>) -> Arc<RwLock<Context>> {
let mut node = Context::make_node(
&ctx,
TypeTerm::Type {
id: ctx.read().unwrap().get_typeid("List").unwrap(),
id: ctx.read().unwrap().get_fun_typeid("List").unwrap(),
args: vec![
TypeTerm::new(ctx.read().unwrap().get_typeid("Digit").unwrap())
.num_arg(radix)

@ -5,7 +5,7 @@ pub mod term;
pub mod ladder;
pub mod repr_tree;
pub mod make_editor;
//pub mod editor;
pub mod editor;
pub use {
dict::*,
@ -13,7 +13,6 @@ pub use {
repr_tree::*,
term::*,
context::{Context, MorphismMode, MorphismType, MorphismTypePattern},
// type_term_editor::TypeTermEditor,
make_editor::*
};

@ -1,7 +1,7 @@
use {
r3vi::view::{AnyOuterViewPort, OuterViewPort, View},
crate::{
type_system::{TypeTerm}
type_system::{TypeTerm, Context}
},
std::{
collections::HashMap,
@ -31,6 +31,14 @@ impl ReprTree {
&self.type_tag
}
pub fn from_char(ctx: &Arc<RwLock<Context>>, c: char) -> Arc<RwLock<Self>> {
let buf = r3vi::buffer::singleton::SingletonBuffer::<char>::new(c);
ReprTree::new_leaf(
(ctx, "( Char )"),
buf.get_port().into()
)
}
pub fn new_leaf(type_tag: impl Into<TypeTerm>, port: AnyOuterViewPort) -> Arc<RwLock<Self>> {
let mut tree = ReprTree::new(type_tag.into());
tree.insert_leaf(vec![].into_iter(), port);

@ -10,10 +10,10 @@ use {
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum TypeTerm {
Type {
id: TypeID,
id: u64,
args: Vec< TypeLadder >
},
Var(TypeID),
Var(u64),
Num(i64),
Char(char)
}
@ -22,7 +22,10 @@ pub enum TypeTerm {
impl TypeTerm {
pub fn new(id: TypeID) -> Self {
TypeTerm::Type { id, args: vec![] }
match id {
TypeID::Fun(id) => TypeTerm::Type { id, args: vec![] },
TypeID::Var(_) => {unreachable!();}
}
}
pub fn arg(&mut self, t: impl Into<TypeLadder>) -> &mut Self {
@ -36,8 +39,12 @@ impl TypeTerm {
pub fn num_arg(&mut self, v: i64) -> &mut Self {
self.arg(TypeTerm::Num(v))
}
pub fn from_str(s: &str, names: &HashMap<String, u64>) -> Option<Self> {
pub fn char_arg(&mut self, c: char) -> &mut Self {
self.arg(TypeTerm::Char(c))
}
pub fn from_str(s: &str, names: &HashMap<String, TypeID>) -> Option<Self> {
let mut term_stack = Vec::<Option<TypeTerm>>::new();
for token in s.split_whitespace() {
@ -67,13 +74,13 @@ impl TypeTerm {
f.num_arg(i64::from_str_radix(atom, 10).unwrap());
} else {
f.arg(TypeTerm::new(
*names.get(atom).expect(&format!("invalid atom {}", atom)),
names.get(atom).expect(&format!("invalid atom {}", atom)).clone(),
));
}
}
None => {
*f = Some(TypeTerm::new(
*names.get(atom).expect(&format!("invalid atom {}", atom)),
names.get(atom).expect(&format!("invalid atom {}", atom)).clone(),
));
}
}
@ -85,23 +92,23 @@ impl TypeTerm {
}
// only adds parenthesis where args.len > 0
pub fn to_str1(&self, names: &HashMap<u64, String>) -> String {
pub fn to_str1(&self, names: &HashMap<TypeID, String>) -> String {
match self {
TypeTerm::Type { id, args } => {
if args.len() > 0 {
format!(
"( {} {})",
names[id],
"({}{})",
names[&TypeID::Fun(*id)],
if args.len() > 0 {
args.iter().fold(String::new(), |str, term| {
format!("{}{} ", str, term.to_str1(names))
format!(" {}{}", str, term.to_str1(names))
})
} else {
String::new()
}
)
} else {
names[id].clone()
names[&TypeID::Fun(*id)].clone()
}
}
@ -112,11 +119,11 @@ impl TypeTerm {
}
// always adds an enclosing pair of parenthesis
pub fn to_str(&self, names: &HashMap<u64, String>) -> String {
pub fn to_str(&self, names: &HashMap<TypeID, String>) -> String {
match self {
TypeTerm::Type { id, args } => format!(
"( {} {})",
names[id],
names[&TypeID::Fun(*id)],
if args.len() > 0 {
args.iter().fold(String::new(), |str, term| {
format!("{}{} ", str, term.to_str1(names))

Loading…
Cancel
Save