|
| 1 | +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// Test a sample usage pattern for regions. Makes use of the |
| 12 | +// following features: |
| 13 | +// |
| 14 | +// - Multiple lifetime parameters |
| 15 | +// - Arenas |
| 16 | + |
| 17 | +extern mod extra; |
| 18 | + |
| 19 | +use extra::arena; |
| 20 | +use extra::arena::Arena; |
| 21 | +use std::hashmap::HashMap; |
| 22 | +use std::cast; |
| 23 | +use std::libc; |
| 24 | +use std::mem; |
| 25 | + |
| 26 | +type Type<'tcx> = &'tcx TypeStructure<'tcx>; |
| 27 | + |
| 28 | +#[deriving(Eq)] |
| 29 | +enum TypeStructure<'tcx> { |
| 30 | + TypeInt, |
| 31 | + TypeFunction(Type<'tcx>, Type<'tcx>), |
| 32 | +} |
| 33 | + |
| 34 | +struct TypeContext<'tcx, 'ast> { |
| 35 | + ty_arena: &'tcx Arena, |
| 36 | + types: ~[Type<'tcx>], |
| 37 | + type_table: HashMap<NodeId, Type<'tcx>>, |
| 38 | + |
| 39 | + ast_arena: &'ast Arena, |
| 40 | + ast_counter: uint, |
| 41 | +} |
| 42 | + |
| 43 | +impl<'tcx,'ast> TypeContext<'tcx, 'ast> { |
| 44 | + fn new(ty_arena: &'tcx Arena, ast_arena: &'ast Arena) |
| 45 | + -> TypeContext<'tcx, 'ast> { |
| 46 | + TypeContext { ty_arena: ty_arena, |
| 47 | + types: ~[], |
| 48 | + type_table: HashMap::new(), |
| 49 | + |
| 50 | + ast_arena: ast_arena, |
| 51 | + ast_counter: 0 } |
| 52 | + } |
| 53 | + |
| 54 | + fn add_type(&mut self, s: TypeStructure<'tcx>) -> Type<'tcx> { |
| 55 | + for &ty in self.types.iter() { |
| 56 | + if *ty == s { |
| 57 | + return ty; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + let ty = self.ty_arena.alloc(|| s); |
| 62 | + self.types.push(ty); |
| 63 | + ty |
| 64 | + } |
| 65 | + |
| 66 | + fn set_type(&mut self, id: NodeId, ty: Type<'tcx>) -> Type<'tcx> { |
| 67 | + self.type_table.insert(id, ty); |
| 68 | + ty |
| 69 | + } |
| 70 | + |
| 71 | + fn ast(&mut self, a: AstKind<'ast>) -> Ast<'ast> { |
| 72 | + let id = self.ast_counter; |
| 73 | + self.ast_counter += 1; |
| 74 | + self.ast_arena.alloc(|| AstStructure { id: NodeId {id:id}, kind: a }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[deriving(Eq, IterBytes)] |
| 79 | +struct NodeId { |
| 80 | + id: uint |
| 81 | +} |
| 82 | + |
| 83 | +type Ast<'ast> = &'ast AstStructure<'ast>; |
| 84 | + |
| 85 | +struct AstStructure<'ast> { |
| 86 | + id: NodeId, |
| 87 | + kind: AstKind<'ast> |
| 88 | +} |
| 89 | + |
| 90 | +enum AstKind<'ast> { |
| 91 | + ExprInt, |
| 92 | + ExprVar(uint), |
| 93 | + ExprLambda(Ast<'ast>), |
| 94 | + // ... |
| 95 | +} |
| 96 | + |
| 97 | +fn compute_types<'tcx,'ast>(tcx: &mut TypeContext<'tcx,'ast>, |
| 98 | + ast: Ast<'ast>) -> Type<'tcx> |
| 99 | +{ |
| 100 | + match ast.kind { |
| 101 | + ExprInt | ExprVar(_) => { |
| 102 | + let ty = tcx.add_type(TypeInt); |
| 103 | + tcx.set_type(ast.id, ty) |
| 104 | + } |
| 105 | + |
| 106 | + ExprLambda(ast) => { |
| 107 | + let arg_ty = tcx.add_type(TypeInt); |
| 108 | + let body_ty = compute_types(tcx, ast); |
| 109 | + let lambda_ty = tcx.add_type(TypeFunction(arg_ty, body_ty)); |
| 110 | + tcx.set_type(ast.id, lambda_ty) |
| 111 | + } |
| 112 | + |
| 113 | + // ... |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +pub fn main() { |
| 118 | + let ty_arena = arena::Arena::new(); |
| 119 | + let ast_arena = arena::Arena::new(); |
| 120 | + let mut tcx = TypeContext::new(&ty_arena, &ast_arena); |
| 121 | + let ast = tcx.ast(ExprInt); |
| 122 | + let ty = compute_types(&mut tcx, ast); |
| 123 | + assert_eq!(*ty, TypeInt); |
| 124 | +} |
0 commit comments