Skip to content

Commit

Permalink
Refactor, to allow typing
Browse files Browse the repository at this point in the history
  • Loading branch information
sozysozbot committed Jul 31, 2020
1 parent b10c23d commit c2ee4e4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
17 changes: 12 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2018"
[dependencies]
peek-nth = "0.2.0"
clap = "2.33.1"
serde_json = "1.0.56"
bimap = "0.4.0"
position = "0.0.3"
serde_json = "1.0.57"
bimap = "0.5.2"
position = "0.0.3"
bimap_plus_map = "0.1.1"
1 change: 0 additions & 1 deletion src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::parse;

type Line = (usize, String);

#[derive(Clone)]
struct Env {
ans_counter: usize,
rand_counter: usize,
Expand Down
40 changes: 20 additions & 20 deletions src/identbimap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::parse;
use bimap::BiMap;
use std::collections::HashMap;
use std::collections::HashSet;
use bimap_plus_map::BiMapPlusMap;

type Table = HashMap<String, String>;

Expand All @@ -20,38 +19,36 @@ fn to_pinyin(ident: parse::Identifier, conversion_table: &Table) -> String {
type Hanzi = parse::Identifier;
type Ascii = String;

#[derive(Clone)]
pub struct IdentBiMap {
bimap: BiMap<Hanzi, Ascii>,
mutable_idents: HashSet<Hanzi>,
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
Mutable
}

pub struct IdentBiMap(BiMapPlusMap<Hanzi, Ascii, Option<Type>>);

impl IdentBiMap {
pub fn translate_from_hanzi(&self, id: &parse::Identifier) -> Ascii {
self.bimap.get_by_left(id).unwrap().to_string()
self.0.bimap_get_by_left(id).unwrap().to_string()
}

pub fn is_mutable(&self, id: &parse::Identifier) -> bool {
self.mutable_idents.contains(id)
let typ = self.0.hashmap_get_by_left(id).unwrap();
*typ == Some(Type::Mutable)
}

pub fn new(parsed: &[parse::Statement], conversion_table: &Table) -> Self {
let mut ans = IdentBiMap {
bimap: BiMap::new(),
mutable_idents: HashSet::new(),
};
let mut ans = IdentBiMap(BiMapPlusMap::new());
for st in parsed {
ans.insert_stmt(&st, &conversion_table);
}

eprintln!("bimap: {:?}", ans.bimap);
eprintln!("mutable_idents: {:?}", ans.mutable_idents);
eprintln!("{:?}", ans.0);
ans
}

fn insert_ident(&mut self, ident: &parse::Identifier, conversion_table: &Table) {
// if already known, no need to do anything
if self.bimap.get_by_left(&ident).is_some() {
if self.0.bimap_get_by_left(&ident).is_some() {
return;
}

Expand All @@ -60,10 +57,10 @@ impl IdentBiMap {
let mut candidate: Ascii = to_pinyin(ident.clone(), &conversion_table);

loop {
if self.bimap.get_by_right(&candidate).is_some() {
if self.0.bimap_get_by_right(&candidate).is_some() {
candidate.push('_');
} else {
self.bimap.insert(ident.clone(), candidate);
self.0.insert(ident.clone(), candidate, None);
break;
}
}
Expand Down Expand Up @@ -182,7 +179,8 @@ impl IdentBiMap {
} => {
self.insert_data_or_qi2(&parse::OrQi2::from(what_to_fill), &conversion_table);
if let parse::OrQi2::NotQi2(ident) = what_to_fill {
self.mutable_idents.insert(ident.clone());
let ascii = self.0.bimap_get_by_left(&ident).unwrap().clone();
self.0.insert(ident.clone(), ascii.clone(), Some(Type::Mutable));
}
self.insert_dats(&elems, &conversion_table);
}
Expand Down Expand Up @@ -211,15 +209,17 @@ impl IdentBiMap {
rvalue,
} => {
self.insert_ident(&ident, &conversion_table);
self.mutable_idents.insert(ident.clone());
let ascii = self.0.bimap_get_by_left(&ident).unwrap().clone();
self.0.insert(ident.clone(), ascii.clone(), Some(Type::Mutable));
self.insert_rvalue(rvalue, &conversion_table)
}
Assignment {
lvalue: parse::Lvalue::IndexByIdent(ident, index),
rvalue,
} => {
self.insert_ident(&ident, &conversion_table);
self.mutable_idents.insert(ident.clone());
let ascii = self.0.bimap_get_by_left(&ident).unwrap().clone();
self.0.insert(ident.clone(), ascii.clone(), Some(Type::Mutable));
self.insert_ident(&index, &conversion_table);
self.insert_rvalue(rvalue, &conversion_table)
}
Expand Down

0 comments on commit c2ee4e4

Please sign in to comment.