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

Commit 38b332a

Browse files
committed
Made the tests compile
The entire compiler is now compiling, tests are still failing though
1 parent 17e85e3 commit 38b332a

File tree

8 files changed

+105
-98
lines changed

8 files changed

+105
-98
lines changed

compiler.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ pub fn compile(contents: &str) -> Assembly {
10541054
compile_with_type_env(&mut type_env, &[], contents)
10551055
}
10561056
#[allow(dead_code)]
1057-
pub fn compile_with_type_env<'a>(type_env: &'a mut TypeEnvironment<'a>, assemblies: &[&'a Assembly], contents: &str) -> Assembly {
1057+
pub fn compile_with_type_env<'a>(type_env: &mut TypeEnvironment<'a>, assemblies: &[&'a Assembly], contents: &str) -> Assembly {
10581058
use parser::Parser;
10591059

10601060
let mut parser = Parser::new(contents.as_slice().chars());
@@ -1110,6 +1110,7 @@ mod tests {
11101110

11111111
use interner::*;
11121112
use compiler::*;
1113+
use compiler::Instruction::*;
11131114
use typecheck::TypeEnvironment;
11141115
use std::io::File;
11151116
use test::Bencher;
@@ -1212,17 +1213,17 @@ instance Test Int where
12121213
main x = primIntAdd (test x) 6";
12131214
let assembly = compile(file);
12141215

1215-
let main = assembly.superCombinators[0];
1216+
let main = &assembly.superCombinators[0];
12161217
assert_eq!(main.name.name, intern("main"));
12171218
assert_eq!(main.instructions, vec![PushInt(6), Push(1), PushDictionaryMember(0), Mkap, Eval, Add, Update(0), Pop(2), Unwind]);
12181219
}
12191220

12201221
#[test]
12211222
fn compile_prelude() {
12221223
let mut type_env = TypeEnvironment::new();
1223-
let prelude = compile_with_type_env(&mut type_env, [], File::open(&Path::new("Prelude.hs")).read_to_str().unwrap().as_slice());
1224+
let prelude = compile_with_type_env(&mut type_env, &[], File::open(&Path::new("Prelude.hs")).read_to_string().unwrap().as_slice());
12241225

1225-
let assembly = compile_with_type_env(&mut type_env, [&prelude], r"main = id (primIntAdd 2 0)");
1226+
let assembly = compile_with_type_env(&mut type_env, &[&prelude], r"main = id (primIntAdd 2 0)");
12261227

12271228
let sc = &assembly.superCombinators[0];
12281229
let id_index = prelude.superCombinators.iter().position(|sc| sc.name.name == intern("id")).unwrap();
@@ -1272,7 +1273,7 @@ newtype Test a = Test [a]
12721273
test = Test [1::Int]";
12731274
let assembly = compile(file);
12741275

1275-
let test = assembly.superCombinators[0];
1276+
let test = &assembly.superCombinators[0];
12761277
assert_eq!(test.instructions, vec![Pack(0, 0), PushInt(1), Pack(1, 2), Update(0), Unwind]);
12771278
}
12781279

@@ -1284,7 +1285,7 @@ fn bench_prelude(b: &mut Bencher) {
12841285
use parser::Parser;
12851286

12861287
let path = &Path::new("Prelude.hs");
1287-
let contents = File::open(path).read_to_str().unwrap();
1288+
let contents = File::open(path).read_to_string().unwrap();
12881289
let mut parser = Parser::new(contents.as_slice().chars());
12891290
let mut module = rename_module(parser.module());
12901291
let mut type_env = TypeEnvironment::new();

core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl <T> Typed for Id<T> {
196196
}
197197

198198

199-
mod ref_ {
199+
pub mod ref_ {
200200
use super::*;
201201
use super::Expr::*;
202202
///Visitor for the types in the core language.

infix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ test = 3 * 4 - 5 * 6").unwrap();
130130
for module in modules.iter_mut() {
131131
v.visit_module(module);
132132
}
133-
assert_eq!(modules.last().unwrap().bindings[0].matches, Simple(rename_expr(op_apply(
133+
assert_eq!(modules.last().unwrap().bindings[0].matches, Match::Simple(rename_expr(op_apply(
134134
op_apply(number(3), intern("*"), number(4)),
135135
intern("-"),
136136
op_apply(number(5), intern("*"), number(6))))));
@@ -146,7 +146,7 @@ test = 3 * 4 * (5 - 6)").unwrap();
146146
for module in modules.iter_mut() {
147147
v.visit_module(module);
148148
}
149-
assert_eq!(modules.last().unwrap().bindings[0].matches, Simple(rename_expr(op_apply(
149+
assert_eq!(modules.last().unwrap().bindings[0].matches, Match::Simple(rename_expr(op_apply(
150150
op_apply(number(3), intern("*"), number(4)),
151151
intern("*"),
152152
paren(op_apply(number(5), intern("-"), number(6)))))));

lambda_lift.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,11 @@ mod tests {
208208
use test::Bencher;
209209
use interner::*;
210210
use lambda_lift::*;
211-
use collections::hashmap::HashSet;
211+
use std::collections::HashSet;
212212
use parser::Parser;
213213
use core::*;
214+
use core::Expr::*;
215+
use core::ref_::*;
214216
use core::translate::translate_module;
215217
use renamer::rename_module;
216218
use typecheck::TypeEnvironment;
@@ -249,12 +251,12 @@ test2 x =
249251
in add x test
250252
in f x".chars());
251253
let module = translate_module(rename_module(parser.module()));
252-
(&mut visitor as &mut Visitor<Id>).visit_module(&module);
254+
visitor.visit_module(&module);
253255
}
254256

255257
fn check_args(expr: &Expr<Id>, args: &[InternedStr]) -> bool {
256258
match expr {
257-
&Lambda(ref arg, ref body) => arg.name.name == args[0] && check_args(*body, args.slice_from(1)),
259+
&Lambda(ref arg, ref body) => arg.name.name == args[0] && check_args(&**body, args.slice_from(1)),
258260
_ => args.len() == 0
259261
}
260262
}
@@ -266,12 +268,11 @@ test2 x =
266268
fn get_let<'a>(expr: &'a Expr<Id>, args: &mut Vec<InternedStr>) -> &'a Expr<Id> {
267269
match expr {
268270
&Apply(ref f, ref arg) => {
269-
let a: &Expr<Id> = *arg;
270-
match a {
271-
&Identifier(ref i) => args.push(i.name.name),
271+
match **arg {
272+
Identifier(ref i) => args.push(i.name.name),
272273
_ => panic!("Expected identifier as argument")
273274
}
274-
get_let(*f, args)
275+
get_let(&**f, args)
275276
}
276277
_ => expr
277278
}
@@ -325,7 +326,7 @@ test2 x =
325326
TypeEnvironment::new().typecheck_module(&mut module);
326327
let m = translate_module(module);
327328
let module = abstract_module(m);
328-
(&mut visitor as &mut Visitor<Id>).visit_module(&module);
329+
visitor.visit_module(&module);
329330
assert_eq!(visitor.count, 2);
330331
}
331332

@@ -342,9 +343,9 @@ test2 x =
342343
}
343344
#[test]
344345
fn no_local_lambdas() {
345-
fn skip_lambdas<'a, T>(expr: &'a Expr<T>) -> &'a Expr<T> {
346+
fn skip_lambdas<T>(expr: &Expr<T>) -> &Expr<T> {
346347
match expr {
347-
&Lambda(_, ref body) => skip_lambdas(*body),
348+
&Lambda(_, ref body) => skip_lambdas(&**body),
348349
_ => expr
349350
}
350351
}
@@ -363,7 +364,7 @@ test2 x =
363364
let m = translate_module(rename_module(parser.module()));
364365
let module = lift_lambdas(m);
365366
for bind in module.bindings.iter() {
366-
(&mut visitor as &mut Visitor<TypeAndStr>).visit_expr(skip_lambdas(&bind.expression));
367+
visitor.visit_expr(skip_lambdas(&bind.expression));
367368
}
368369
}
369370

@@ -373,7 +374,7 @@ test2 x =
373374
use typecheck::test::do_typecheck;
374375

375376
let path = &Path::new("Prelude.hs");
376-
let contents = File::open(path).read_to_str().unwrap();
377+
let contents = File::open(path).read_to_string().unwrap();
377378
let module = do_typecheck(contents.as_slice());
378379
b.iter(|| {
379380
do_lambda_lift(translate_module(module.clone()))

lexer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ impl <Stream : Iterator<Item=char>> Lexer<Stream> {
587587
mod tests {
588588

589589
use lexer::*;
590+
use lexer::TokenEnum::*;
590591

591592
#[test]
592593
fn simple() {

parser.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,8 +1093,10 @@ fn parse_modules_(visited: &mut HashSet<InternedStr>, modules: &mut Vec<Module>,
10931093
mod tests {
10941094

10951095
use interner::*;
1096+
use lexer::{Location, Located };
10961097
use parser::*;
10971098
use module::*;
1099+
use module::Expr::*;
10981100
use typecheck::{identifier, apply, op_apply, number, rational, lambda, let_, case, if_else};
10991101
use std::io::File;
11001102
use test::Bencher;
@@ -1266,7 +1268,7 @@ r"class Eq a => Ord a where_bindings
12661268
".chars());
12671269
let module = parser.module();
12681270

1269-
let cls = module.classes[0];
1271+
let cls = &module.classes[0];
12701272
let a = Type::new_var(intern("a")).var().clone();
12711273
assert_eq!(cls.name, intern("Ord"));
12721274
assert_eq!(cls.variable, a);
@@ -1356,9 +1358,9 @@ infixr 6 `test2`, |<
13561358
test2 x y = 1
13571359
".chars());
13581360
let module = parser.module();
1359-
assert_eq!(module.fixity_declarations.as_slice(), &[
1360-
FixityDeclaration { assoc: RightAssoc, precedence: 5, operators: vec![intern("test")] },
1361-
FixityDeclaration { assoc: RightAssoc, precedence: 6, operators: vec![intern("test2"), intern("|<")] },
1361+
assert_eq!(module.fixity_declarations, [
1362+
FixityDeclaration { assoc: Assoc::Right, precedence: 5, operators: vec![intern("test")] },
1363+
FixityDeclaration { assoc: Assoc::Right, precedence: 6, operators: vec![intern("test2"), intern("|<")] },
13621364
]);
13631365
}
13641366

@@ -1373,7 +1375,7 @@ dummy = 1
13731375
let module = parser.module();
13741376
let data = &module.dataDefinitions[0];
13751377
assert_eq!(data.typ, qualified(Vec::new(), Type::new_op(intern("Test"), Vec::new())));
1376-
assert_eq!(data.deriving.as_slice(), &[intern("Eq"), intern("Show")]);
1378+
assert_eq!(data.deriving, [intern("Eq"), intern("Show")]);
13771379
}
13781380

13791381
#[test]

typecheck.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ use builtins::builtins;
1212
use renamer::*;
1313
use interner::*;
1414

15-
#[cfg(test)]
16-
use module::Alternative;
17-
1815
///Trait which can be implemented by types where types can be looked up by name
1916
pub trait Types {
2017
fn find_type<'a>(&'a self, name: &Name) -> Option<&'a Qualified<Type, Name>>;
@@ -1621,6 +1618,7 @@ fn typecheck_modules_common(modules: Vec<Module>) -> Vec<Module<Name>> {
16211618
pub mod test {
16221619
use interner::*;
16231620
use module::*;
1621+
use module::Expr::*;
16241622
use typecheck::*;
16251623
use renamer::*;
16261624

@@ -1630,7 +1628,7 @@ use std::io::File;
16301628
use test::Bencher;
16311629

16321630
pub fn do_typecheck(input: &str) -> Module<Name> {
1633-
do_typecheck_with(input, [])
1631+
do_typecheck_with(input, &[])
16341632
}
16351633
pub fn do_typecheck_with(input: &str, types: &[&DataTypes]) -> Module<Name> {
16361634
let mut parser = ::parser::Parser::new(input.chars());
@@ -1684,7 +1682,7 @@ fn typecheck_let() {
16841682

16851683
//let test x = add x in test
16861684
let unary_bind = lambda("x", apply(apply(identifier("primIntAdd"), identifier("x")), number(1)));
1687-
let e = let_(vec![Binding { arguments: vec![], name: intern("test"), matches: Simple(unary_bind), typ: Default::default() , where: None }], identifier("test"));
1685+
let e = let_(vec![Binding { arguments: vec![], name: intern("test"), matches: Match::Simple(unary_bind), typ: Default::default() , where_bindings: None }], identifier("test"));
16881686
let mut expr = rename_expr(e);
16891687
env.typecheck_expr(&mut expr);
16901688

@@ -1746,16 +1744,15 @@ fn typecheck_tuple() {
17461744
}
17471745

17481746
#[test]
1749-
fn typecheck_module() {
1747+
fn typecheck_module_() {
17501748

17511749
let file =
17521750
r"data Bool = True | False
17531751
test x = True";
17541752
let module = do_typecheck(file);
17551753

17561754
let typ = function_type_(Type::new_var(intern("a")), bool_type());
1757-
let bind_type0 = module.bindings[0].typ.value;
1758-
assert_eq!(bind_type0, typ);
1755+
assert_eq!(module.bindings[0].typ.value, typ);
17591756
}
17601757

17611758

@@ -1954,8 +1951,7 @@ r"test x = primDoubleAdd 0 x";
19541951
let module = do_typecheck(file);
19551952

19561953
let typ = function_type_(double_type(), double_type());
1957-
let bind_type0 = module.bindings[0].typ.value;
1958-
assert_eq!(bind_type0, typ);
1954+
assert_eq!(module.bindings[0].typ.value, typ);
19591955
}
19601956

19611957
#[test]
@@ -2000,7 +1996,7 @@ main = fmap add2 3");
20001996
#[test]
20011997
fn typecheck_prelude() {
20021998
let path = &Path::new("Prelude.hs");
2003-
let contents = File::open(path).read_to_str().unwrap();
1999+
let contents = File::open(path).read_to_string().unwrap();
20042000
let module = do_typecheck(contents.as_slice());
20052001

20062002
let id = module.bindings.iter().find(|bind| bind.name.as_slice() == "id");
@@ -2014,15 +2010,15 @@ fn typecheck_import() {
20142010

20152011
let prelude = {
20162012
let path = &Path::new("Prelude.hs");
2017-
let contents = File::open(path).read_to_str().unwrap();
2013+
let contents = File::open(path).read_to_string().unwrap();
20182014
do_typecheck(contents.as_slice())
20192015
};
20202016

20212017
let file =
20222018
r"
20232019
test1 = map not [True, False]
20242020
test2 = id (primIntAdd 2 0)";
2025-
let module = do_typecheck_with(file, [&prelude as &DataTypes]);
2021+
let module = do_typecheck_with(file, &[&prelude as &DataTypes]);
20262022

20272023
assert_eq!(module.bindings[0].name.as_slice(), "test1");
20282024
assert_eq!(module.bindings[0].typ.value, list_type(bool_type()));
@@ -2053,7 +2049,7 @@ fn do_expr_simple() {
20532049

20542050
let prelude = {
20552051
let path = &Path::new("Prelude.hs");
2056-
let contents = File::open(path).read_to_str().unwrap();
2052+
let contents = File::open(path).read_to_string().unwrap();
20572053
do_typecheck(contents.as_slice())
20582054
};
20592055

@@ -2065,7 +2061,7 @@ test x = do
20652061
t = [2::Int]
20662062
putStrLn y
20672063
";
2068-
let module = do_typecheck_with(file, [&prelude as &DataTypes]);
2064+
let module = do_typecheck_with(file, &[&prelude as &DataTypes]);
20692065

20702066
assert_eq!(module.bindings[0].typ.value, function_type_(list_type(char_type()), io(unit())));
20712067
}
@@ -2075,7 +2071,7 @@ fn do_expr_pattern() {
20752071

20762072
let prelude = {
20772073
let path = &Path::new("Prelude.hs");
2078-
let contents = File::open(path).read_to_str().unwrap();
2074+
let contents = File::open(path).read_to_string().unwrap();
20792075
do_typecheck(contents.as_slice())
20802076
};
20812077

@@ -2085,7 +2081,7 @@ test x = do
20852081
y:ys <- x
20862082
return y
20872083
";
2088-
let module = do_typecheck_with(file, [&prelude as &DataTypes]);
2084+
let module = do_typecheck_with(file, &[&prelude as &DataTypes]);
20892085

20902086
let var = Type::new_var(intern("a"));
20912087
let t = function_type_(Type::new_var_args(intern("c"), vec![list_type(var.clone())]), Type::new_var_args(intern("c"), vec![var.clone()]));
@@ -2193,7 +2189,7 @@ fn do_expr_wrong_monad() {
21932189

21942190
let prelude = {
21952191
let path = &Path::new("Prelude.hs");
2196-
let contents = File::open(path).read_to_str().unwrap();
2192+
let contents = File::open(path).read_to_string().unwrap();
21972193
do_typecheck(contents.as_slice())
21982194
};
21992195

@@ -2202,7 +2198,7 @@ r"
22022198
test x = do
22032199
putStrLn x
22042200
reverse [primIntAdd 0 0, 1, 2]";
2205-
do_typecheck_with(file, [&prelude as &DataTypes]);
2201+
do_typecheck_with(file, &[&prelude as &DataTypes]);
22062202
}
22072203

22082204
#[test]
@@ -2285,7 +2281,7 @@ test = IntPair (True, False)
22852281
#[bench]
22862282
fn bench_prelude(b: &mut Bencher) {
22872283
let path = &Path::new("Prelude.hs");
2288-
let contents = File::open(path).read_to_str().unwrap();
2284+
let contents = File::open(path).read_to_string().unwrap();
22892285
let mut parser = Parser::new(contents.as_slice().chars());
22902286
let module = rename_module(parser.module());
22912287

0 commit comments

Comments
 (0)