aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
blob: a575e27aac9ee8fe381b30d4758824e0f7c256e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#![allow(non_snake_case, non_upper_case_globals)]

use crate::ast::*;

// intentionally small: i want to run into errors
/// assumption: the count is instantiated to zero
pub fn unique_ident(count: &mut u8) -> String {
    *count += 1;
    if *count == 0 {
        panic!("we've overflowed!")
    } else {
        format!("{:X}", count)
    }
}

pub fn Ann(expr: Expression, kind: Type) -> Expression {
    Expression::Annotation { expr: Box::new(expr), kind }
}

pub fn Const(term: Term) -> Expression {
    Expression::Constant { term }
}

pub fn Var(id: &str) -> Expression {
    Expression::Variable { id: String::from(id) }
}

pub fn Abs(param: &str, func: Expression) -> Expression {
    Expression::Abstraction {
        param: String::from(param),
        func: Box::new(func)
    }
}

pub fn App(func: Expression, arg: Expression) -> Expression {
    Expression::Application {
        func: Box::new(func),
        arg: Box::new(arg)
    }
}

pub fn Cond(if_cond: Expression, if_then: Expression, if_else: Expression) -> Expression {
    Expression::Conditional {
        if_cond: Box::new(if_cond),
        if_then: Box::new(if_then),
        if_else: Box::new(if_else)
    }
}

pub fn Func(from: Type, to: Type) -> Type {
    Type::Function(Box::new(from), Box::new(to))
}

pub const Empty: Type = Type::Empty;
pub const Error: Type = Type::Empty;
pub const Unit: Type = Type::Unit;
pub const Bool: Type = Type::Boolean;
pub const Nat: Type = Type::Natural;
pub const Int: Type = Type::Integer;

pub fn Float(term: f32) -> Term {
    Term::Float(term)
}

pub fn Str(data: &str) -> Term {
    Term::String(data.into())
}

pub fn Union(data: Term) -> Term {
    Term::Union(Box::new(data))
}