Skip to content

Commit 3065bcd

Browse files
authored
Merge pull request #196 from JSAbrahams/v0.2.2
V0.2.2
2 parents 0741180 + 1d87edc commit 3065bcd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1084
-692
lines changed

Cargo.lock

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mamba"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
authors = ["Joel Abrahams <abrahamsjo@gmail.com>"]
55
description = "A transpiler which translates Mamba to Python 3 files"
66
edition = "2018"
@@ -23,3 +23,4 @@ clap = {version = "2.33", features = ["yaml"]}
2323
leg = "0.1.3"
2424
pathdiff = "0.1.0"
2525
glob = "0.3.0"
26+
itertools = "0.8.2"

src/core/construct.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ pub enum Core {
7777
num: String,
7878
exp: String
7979
},
80+
DocStr {
81+
_str: String
82+
},
8083
Str {
8184
_str: String
8285
},

src/core/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ fn to_py(core: &Core, ind: usize) -> String {
5353
format!("{}[{}]", lit, comma_delimited(generics, ind))
5454
},
5555
Core::IdType { lit, ty } => format!("{}: {}", lit, to_py(ty, ind)),
56+
Core::DocStr { _str } => format!("\"\"\"{}\"\"\"", _str),
5657
Core::Str { _str } => format!("\"{}\"", _str),
5758
Core::FStr { _str } => format!("f\"{}\"", _str),
5859
Core::Int { int } => int.clone(),

src/desugar/call.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub fn desugar_call(ast: &AST, imp: &mut Imports, state: &State) -> DesugarResul
1717
function: Box::from(desugar_node(name, imp, state)?),
1818
args: desugar_vec(args, imp, state)?
1919
},
20+
Node::ConstructorCall { name, args } => Core::FunctionCall {
21+
function: Box::from(desugar_node(name, imp, &state.is_constructor(true))?),
22+
args: desugar_vec(args, imp, state)?
23+
},
2024
other => panic!("Expected call flow but was: {:?}.", other)
2125
})
2226
}

src/desugar/class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn desugar_class(ast: &AST, imp: &mut Imports, state: &State) -> DesugarResu
108108
_ => false
109109
});
110110
stmts.append(&mut non_variables);
111-
final_definitions = stmts.clone();
111+
final_definitions = stmts;
112112

113113
Core::ClassDef {
114114
name: Box::from(desugar_node(id, imp, state)?),

src/desugar/definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn desugar_definition(ast: &AST, imp: &mut Imports, state: &State) -> Desuga
2121
Core::VarDef {
2222
private: *private,
2323
id: Box::from(id.clone()),
24-
right: match (id.clone(), expression) {
24+
right: match (id, expression) {
2525
(_, Some(expr)) => Box::from(desugar_node(&expr, imp, &state)?),
2626
(Core::Tuple { elements }, None) =>
2727
Box::from(Core::Tuple { elements: vec![Core::None; elements.len()] }),

src/desugar/node.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::desugar::state::State;
1111
use crate::desugar::ty::desugar_type;
1212
use crate::parser::ast::Node;
1313
use crate::parser::ast::AST;
14+
use crate::type_checker::context::ty::concrete::concrete_to_python;
1415

1516
// TODO return imports instead of modifying mutable reference
1617
pub fn desugar_node(ast: &AST, imp: &mut Imports, state: &State) -> DesugarResult {
@@ -48,6 +49,7 @@ pub fn desugar_node(ast: &AST, imp: &mut Imports, state: &State) -> DesugarResul
4849
num: num.clone(),
4950
exp: if exp.is_empty() { String::from("0") } else { exp.clone() }
5051
},
52+
Node::DocStr { lit } => Core::DocStr { _str: lit.clone() },
5153
Node::Str { lit, expressions } =>
5254
if expressions.is_empty() {
5355
Core::Str { _str: lit.clone() }
@@ -70,7 +72,9 @@ pub fn desugar_node(ast: &AST, imp: &mut Imports, state: &State) -> DesugarResul
7072

7173
Node::Undefined => Core::None,
7274
Node::IdType { .. } => desugar_type(ast, imp, state)?,
73-
Node::Id { lit } => Core::Id { lit: lit.clone() },
75+
Node::Id { lit } => Core::Id {
76+
lit: if state.is_constructor { concrete_to_python(lit) } else { lit.clone() }
77+
},
7478
Node::_Self => Core::Id { lit: String::from("self") },
7579
Node::Init => Core::Id { lit: String::from("init") },
7680
Node::Bool { lit } => Core::Bool { _bool: *lit },
@@ -215,7 +219,8 @@ pub fn desugar_node(ast: &AST, imp: &mut Imports, state: &State) -> DesugarResul
215219
}
216220
},
217221

218-
Node::FunctionCall { .. } | Node::PropertyCall { .. } => desugar_call(ast, imp, state)?,
222+
Node::FunctionCall { .. } | Node::PropertyCall { .. } | Node::ConstructorCall { .. } =>
223+
desugar_call(ast, imp, state)?,
219224

220225
Node::AnonFun { args, body } => Core::AnonFun {
221226
args: desugar_vec(args, imp, &state.expand_ty(false))?,
@@ -249,12 +254,9 @@ pub fn desugar_node(ast: &AST, imp: &mut Imports, state: &State) -> DesugarResul
249254
},
250255
Node::Script { statements } =>
251256
Core::Block { statements: desugar_vec(statements, imp, state)? },
252-
Node::File { modules, imports, .. } => {
253-
let mut imports = desugar_vec(imports, imp, state)?;
257+
Node::File { modules, .. } => {
254258
let mut modules = desugar_vec(modules, imp, state)?;
255-
imports.append(&mut imp.imports.clone().into_iter().collect());
256-
257-
let mut statements = imports;
259+
let mut statements = imp.imports.clone();
258260
statements.append(&mut modules);
259261
Core::Block { statements }
260262
}
@@ -290,7 +292,6 @@ pub fn desugar_node(ast: &AST, imp: &mut Imports, state: &State) -> DesugarResul
290292
Node::Step { .. } => panic!("Step cannot be top level."),
291293
Node::Raises { expr_or_stmt, .. } => desugar_node(expr_or_stmt, imp, state)?,
292294
Node::Raise { error } => Core::Raise { error: Box::from(desugar_node(error, imp, state)?) },
293-
Node::Retry { .. } => return Err(UnimplementedErr::new(ast, "retry")),
294295

295296
Node::Handle { expr_or_stmt, cases } => {
296297
let assign_to = if let Node::VariableDef { id_maybe_type, .. } = &expr_or_stmt.node {
@@ -351,7 +352,7 @@ pub fn desugar_node(ast: &AST, imp: &mut Imports, state: &State) -> DesugarResul
351352
let core = if let Some(assign_to) = assign_to {
352353
match core {
353354
Core::Block { .. } | Core::Return { .. } => core,
354-
expr => Core::Assign { left: Box::from(assign_to), right: Box::from(expr.clone()) }
355+
expr => Core::Assign { left: Box::from(assign_to), right: Box::from(expr) }
355356
}
356357
} else {
357358
core

src/desugar/state.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@ use crate::core::construct::Core;
33
// TODO remove expect_expr once type checker augments AST
44
#[derive(Clone, Debug)]
55
pub struct State {
6-
pub tup: usize,
6+
pub tup: usize,
77
pub interface: bool,
88
pub expand_ty: bool,
9+
pub is_constructor: bool,
910
pub assign_to: Option<Core>
1011
}
1112

1213
impl State {
13-
pub fn new() -> State { State { tup: 1, interface: false, expand_ty: true, assign_to: None } }
14+
pub fn new() -> State {
15+
State { tup: 1, interface: false, expand_ty: true, assign_to: None, is_constructor: false }
16+
}
1417

1518
pub fn in_tup(&self, tup: usize) -> State { State { tup, ..self.clone() } }
1619

1720
pub fn in_interface(&self, interface: bool) -> State { State { interface, ..self.clone() } }
1821

1922
pub fn expand_ty(&self, expand_ty: bool) -> State { State { expand_ty, ..self.clone() } }
2023

24+
pub fn is_constructor(&self, is_constructor: bool) -> State {
25+
State { is_constructor, ..self.clone() }
26+
}
27+
2128
pub fn assign_to(&self, assign_to: Option<&Core>) -> State {
2229
State { assign_to: assign_to.cloned(), ..self.clone() }
2330
}

src/desugar/ty.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,22 @@ pub fn desugar_type(ast: &AST, imp: &mut Imports, state: &State) -> DesugarResul
2727
ty: Box::from(desugar_node(ty, imp, state)?)
2828
}
2929
} else {
30-
Core::Id { lit: lit.clone() }
30+
Core::Id {
31+
lit: if state.is_constructor {
32+
concrete_to_python(lit)
33+
} else {
34+
lit.clone()
35+
}
36+
}
3137
}
3238
} else {
33-
Core::Id { lit: lit.clone() }
39+
Core::Id {
40+
lit: if state.is_constructor {
41+
concrete_to_python(lit)
42+
} else {
43+
lit.clone()
44+
}
45+
}
3446
},
3547
Node::_Self => Core::Id { lit: String::from("self") },
3648
_ => desugar_node(id, imp, state)?

0 commit comments

Comments
 (0)