Skip to content
This repository was archived by the owner on Apr 14, 2020. It is now read-only.

Commit deed5b1

Browse files
committed
Fixing enum namespacing, Iterators with associated types, lifetime bounds
1 parent 587092f commit deed5b1

17 files changed

+561
-529
lines changed

builtins.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use types::*;
2+
use types::Type::Generic;
23
use interner::intern;
34

45
///Returns an array of all the compiler primitves which exist (not including numeric primitives atm)
56
pub fn builtins() -> Vec<(&'static str, Type)> {
6-
let var = Generic(TypeVariable { id: intern("a"), kind: StarKind, age: 0 } );
7-
let var2 = Generic(TypeVariable { id: intern("b"), kind: StarKind, age: 0 } );
7+
let var = Type::Generic(TypeVariable { id: intern("a"), kind: Kind::Star, age: 0 } );
8+
let var2 = Type::Generic(TypeVariable { id: intern("b"), kind: Kind::Star, age: 0 } );
89
vec![("error", function_type_(list_type(char_type()), var.clone())),
910
("seq", function_type_(var.clone(), function_type_(var2.clone(), var2.clone()))),
1011
("readFile", function_type_(list_type(char_type()), io(list_type(char_type())))),

compiler.rs

Lines changed: 79 additions & 76 deletions
Large diffs are not rendered by default.

core.rs

Lines changed: 108 additions & 100 deletions
Large diffs are not rendered by default.

deriving.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use module::encode_binding_identifier;
22
use core::*;
3+
use core::Expr::*;
4+
use types::{bool_type, function_type_};
35
use renamer::NameSupply;
46
use interner::{intern, InternedStr};
57

@@ -102,7 +104,7 @@ impl DerivingGen {
102104
//Create a constraint for each type parameter
103105
fn make_constraints(mut result: Vec<Constraint<Name>>, class: InternedStr, typ: &Type) -> Vec<Constraint<Name>> {
104106
match typ {
105-
&TypeApplication(ref f, ref param) => {
107+
&Type::Application(ref f, ref param) => {
106108
result.push(Constraint { class: Name { name: class, uid: 0 }, variables: box [param.var().clone()] });
107109
make_constraints(result, class, *f)
108110
}
@@ -120,10 +122,10 @@ impl DerivingGen {
120122
let match_id = Id::new(self.name_supply.anonymous(), Type::new_op(intern("Ordering"), box []), box []);
121123
Case(box cmp, box [
122124
Alternative {
123-
pattern: ConstructorPattern(id("EQ", Type::new_op(intern("Ordering"), box [])), box []),
125+
pattern: Pattern::Constructor(id("EQ", Type::new_op(intern("Ordering"), box [])), box []),
124126
expression: def
125127
},
126-
Alternative { pattern: IdentifierPattern(match_id.clone()), expression: Identifier(match_id) }
128+
Alternative { pattern: Pattern::Identifier(match_id.clone()), expression: Identifier(match_id) }
127129
])
128130
}
129131

@@ -139,15 +141,15 @@ impl DerivingGen {
139141
.collect();
140142
let ctor_id = Id::new(constructor.name, iter.typ.clone(), constructor.typ.constraints.clone());
141143
let expr = f(self, args_l, args_r);
142-
let pattern_r = ConstructorPattern(ctor_id.clone(), args_r);
144+
let pattern_r = Pattern::Constructor(ctor_id.clone(), args_r);
143145
let inner = Case(box Identifier(id_r.clone()), box [
144146
Alternative { pattern: pattern_r, expression: expr },
145147
Alternative {
146-
pattern: WildCardPattern,
148+
pattern: Pattern::WildCard,
147149
expression: Identifier(Id::new(Name { uid: 0, name: intern("False") }, bool_type(), box []))
148150
}
149151
]);
150-
Alternative { pattern: ConstructorPattern(ctor_id, args_l), expression: inner }
152+
Alternative { pattern: Pattern::Constructor(ctor_id, args_l), expression: inner }
151153
}).collect();
152154
alts
153155
}
@@ -181,7 +183,8 @@ fn true_expr() -> Expr<Id<Name>> {
181183
struct ArgIterator<'a> {
182184
typ: &'a Type
183185
}
184-
impl <'a> Iterator<&'a Type> for ArgIterator<'a> {
186+
impl <'a> Iterator for ArgIterator<'a> {
187+
type Item = &'a Type;
185188
fn next(&mut self) -> Option<&'a Type> {
186189
use types::try_get_function;
187190
match try_get_function(self.typ) {
@@ -195,7 +198,7 @@ impl <'a> Iterator<&'a Type> for ArgIterator<'a> {
195198
}
196199
fn extract_applied_type<'a>(typ: &'a Type) -> &'a Type {
197200
match typ {
198-
&TypeApplication(ref lhs, _) => extract_applied_type(*lhs),
201+
&Type::Application(ref lhs, _) => extract_applied_type(*lhs),
199202
_ => typ
200203
}
201204
}

graph.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
///Graph module, contains a simple graph structure which is when typechecking to find
22
///functions which are mutually recursive
33
4+
use std::iter::repeat;
45
use std::cmp::min;
56

67
#[deriving(PartialEq, Show)]
@@ -66,8 +67,9 @@ impl <T> Graph<T> {
6667
pub fn strongly_connected_components<T>(graph: &Graph<T>) -> Vec<Vec<VertexIndex>> {
6768

6869
let mut tarjan = TarjanComponents { graph: graph, index: 1, stack: Vec::new(), connections: Vec::new(),
69-
valid: Vec::from_fn(graph.len(), |_| 0),
70-
lowlink: Vec::from_fn(graph.len(), |_| 0)};
70+
valid: repeat(0).take(graph.len()).collect(),
71+
lowlink: repeat(0).take(graph.len()).collect()
72+
};
7173

7274

7375
for vert in range(0, graph.len()) {
@@ -82,7 +84,7 @@ pub fn strongly_connected_components<T>(graph: &Graph<T>) -> Vec<Vec<VertexIndex
8284
.collect()
8385
}
8486

85-
struct TarjanComponents<'a, T>{
87+
struct TarjanComponents<'a, T: 'a>{
8688
index: uint,
8789
graph: &'a Graph<T>,
8890
valid: Vec<uint>,

infix.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ impl MutVisitor<Name> for PrecedenceVisitor {
99
fn visit_expr(&mut self, expr: &mut TypedExpr<Name>) {
1010
walk_expr_mut(self, expr);
1111
match expr.expr {
12-
OpApply(..) => {
13-
let mut temp = TypedExpr::new(Identifier(Name { uid: -1, name: intern("") }));
12+
Expr::OpApply(..) => {
13+
let mut temp = TypedExpr::new(Expr::Identifier(Name { uid: -1, name: intern("") }));
1414
::std::mem::swap(&mut temp, expr);
1515
temp = self.rewrite(box temp);
1616
::std::mem::swap(&mut temp, expr);
@@ -31,14 +31,14 @@ impl PrecedenceVisitor {
3131

3232
pub fn new() -> PrecedenceVisitor {
3333
let mut map = HashMap::new();
34-
map.insert(Name { uid: 0, name: intern(":") }, (5, RightAssoc));
34+
map.insert(Name { uid: 0, name: intern(":") }, (5, Assoc::Right));
3535
PrecedenceVisitor { precedence: map }
3636
}
3737

3838
fn get_precedence(&self, name: &Name) -> (int, Assoc) {
3939
self.precedence.find(name)
4040
.map(|x| *x)
41-
.unwrap_or_else(|| (9, LeftAssoc))
41+
.unwrap_or_else(|| (9, Assoc::Left))
4242
}
4343

4444
///Takes a operator expression the is in the form (1 + (2 * (3 - 4))) and rewrites it using the
@@ -51,14 +51,14 @@ impl PrecedenceVisitor {
5151
let rhs = expr_stack.pop().unwrap();
5252
let lhs = expr_stack.pop().unwrap();
5353
let loc = lhs.location;
54-
expr_stack.push(box TypedExpr::with_location(OpApply(lhs, op, rhs), loc));
54+
expr_stack.push(box TypedExpr::with_location(Expr::OpApply(lhs, op, rhs), loc));
5555
}
5656
let mut expr_stack = Vec::new();
5757
let mut op_stack = Vec::new();
5858
loop {
5959
let TypedExpr { typ: typ, location:location, expr: expr } = *input;
6060
match expr {
61-
OpApply(l, op, r) => {
61+
Expr::OpApply(l, op, r) => {
6262
expr_stack.push(l);
6363
input = r;
6464
loop {
@@ -72,10 +72,10 @@ impl PrecedenceVisitor {
7272
}
7373
else if op_prec == prev_prec {
7474
match (op_assoc, prev_assoc) {
75-
(LeftAssoc, LeftAssoc) => {
75+
(Assoc::Left, Assoc::Left) => {
7676
reduce(&mut expr_stack, &mut op_stack);
7777
}
78-
(RightAssoc, RightAssoc) => {
78+
(Assoc::Right, Assoc::Right) => {
7979
debug!("Shift op {}", op);
8080
op_stack.push(op);
8181
break
@@ -100,7 +100,7 @@ impl PrecedenceVisitor {
100100
assert!(expr_stack.len() >= 1);
101101
let lhs = expr_stack.pop().unwrap();
102102
let op = op_stack.pop().unwrap();
103-
result = TypedExpr::with_location(OpApply(lhs, op, box result), location);
103+
result = TypedExpr::with_location(Expr::OpApply(lhs, op, box result), location);
104104
}
105105
return result;
106106
}

interner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Interner {
4141

4242
///Returns a reference to the interner stored in TLD
4343
pub fn get_local_interner() -> Rc<RefCell<Interner>> {
44-
thread_local!(static INTERNER: Rc<RefCell<(Interner, Gc)>> = Rc::new(RefCell::new((Interner::new(), Gc::new()))));
44+
thread_local!(static INTERNER: Rc<RefCell<Interner>> = Rc::new(RefCell::new(Interner::new())));
4545
INTERNER.with(|interner| interner.clone())
4646
}
4747

lambda_lift.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22
use core::*;
3+
use core::Expr::*;
34
use types::function_type_;
45
use renamer::NameSupply;
56

@@ -15,8 +16,8 @@ struct FreeVariables {
1516

1617
fn each_pattern_variables(pattern: &Pattern<Id>, f: &mut FnMut(&Name)) {
1718
match *pattern {
18-
IdentifierPattern(ref ident) => (*f)(&ident.name),
19-
ConstructorPattern(_, ref patterns) => {
19+
Pattern::Identifier(ref ident) => (*f)(&ident.name),
20+
Pattern::Constructor(_, ref patterns) => {
2021
for p in patterns.iter() {
2122
(*f)(&p.name);
2223
}

lexer.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::rc::Rc;
55
use std::cell::RefCell;
66
use interner::*;
77

8+
use self::TokenEnum::*;
9+
810
#[deriving(Clone, PartialEq, Show)]
911
pub enum TokenEnum {
1012
EOF,
@@ -150,7 +152,7 @@ fn is_operator(first_char : char) -> bool {
150152
}
151153
}
152154

153-
pub struct Lexer<Stream> {
155+
pub struct Lexer<Stream: Iterator<Item=char>> {
154156
///The input which the lexer processes
155157
input : Peekable<char, Stream>,
156158
///The current location of the lexer
@@ -168,7 +170,7 @@ pub struct Lexer<Stream> {
168170
}
169171

170172

171-
impl <Stream : Iterator<char>> Lexer<Stream> {
173+
impl <Stream : Iterator<Item=char>> Lexer<Stream> {
172174

173175
///Constructs a new lexer with a default sized token buffer and the local string interner
174176
pub fn new(input : Stream) -> Lexer<Stream> {
@@ -297,7 +299,7 @@ impl <Stream : Iterator<char>> Lexer<Stream> {
297299
}
298300
///Scans a number, float or integer and returns the appropriate token
299301
fn scan_number(&mut self, c : char, location : Location) -> Token {
300-
let mut number = String::from_char(1, c);
302+
let mut number = c.to_string();
301303
number.push_str(self.scan_digits().as_slice());
302304
let mut token = NUMBER;
303305
match self.peek_char() {
@@ -313,7 +315,7 @@ impl <Stream : Iterator<char>> Lexer<Stream> {
313315
}
314316
///Scans an identifier or a keyword
315317
fn scan_identifier(&mut self, c: char, startLocation: Location) -> Token {
316-
let mut result = String::from_char(1, c);
318+
let mut result = c.to_string();
317319
loop {
318320
match self.peek_char() {
319321
Some(ch) => {
@@ -497,7 +499,7 @@ impl <Stream : Iterator<char>> Lexer<Stream> {
497499
//Decide how to tokenize depending on what the first char is
498500
//ie if its an operator then more operators will follow
499501
if is_operator(c) {
500-
let mut result = String::from_char(1, c);
502+
let mut result = c.to_string();
501503
loop {
502504
match self.peek_char() {
503505
Some(ch) => {
@@ -556,7 +558,8 @@ impl <Stream : Iterator<char>> Lexer<Stream> {
556558
match self.read_char() {
557559
Some(x) => {
558560
if self.read_char() == Some('\'') {
559-
return Token::new(&self.interner, CHAR, ::std::str::from_char(x).as_slice(), startLocation);
561+
//FIXME: Slow
562+
return Token::new(&self.interner, CHAR, &*x.to_string(), startLocation);
560563
}
561564
else {
562565
panic!("Multi char character")
@@ -577,7 +580,8 @@ impl <Stream : Iterator<char>> Lexer<Stream> {
577580
'\\'=> LAMBDA,
578581
_ => EOF
579582
};
580-
Token::new(&self.interner, tok, ::std::str::from_char(c).as_slice(), startLocation)
583+
//FIXME: Slow
584+
Token::new(&self.interner, tok, c.to_string().as_slice(), startLocation)
581585
}
582586
}
583587

0 commit comments

Comments
 (0)