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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::{NyarType, NyarValue, Symbol};
use indexmap::IndexMap;
use nyar_error::FileSpan;
use std::slice::Iter;

pub mod operations;

/// `function`
pub struct FunctionType {
    pub symbol: Symbol,
    pub export: bool,
    pub entry: bool,
    pub input: IndexMap<String, ParameterType>,
    pub output: Vec<NyarType>,
    pub body: FunctionBody,
    pub span: FileSpan,
}

pub struct ParameterType {
    pub name: Symbol,
    pub type_hint: NyarType,
    pub span: FileSpan,
}

impl FunctionType {
    pub fn new(path: Symbol) -> Self {
        Self {
            symbol: path,
            export: false,
            entry: false,
            input: IndexMap::default(),
            output: vec![],
            body: FunctionBody::default(),
            span: Default::default(),
        }
    }
    pub fn name(&self) -> String {
        self.symbol.to_string()
    }
    pub fn with_public(self) -> Self {
        Self { export: true, ..self }
    }
    pub fn with_inputs<I>(mut self, inputs: I) -> Self
    where
        I: IntoIterator<Item = ParameterType>,
    {
        for i in inputs {
            self.input.insert(i.name.to_string(), i);
        }
        self
    }
    pub fn with_outputs<I>(mut self, outputs: I) -> Self
    where
        I: IntoIterator<Item = NyarType>,
    {
        self.output = outputs.into_iter().collect();
        self
    }
    pub fn with_operations<I>(mut self, operations: I) -> Self
    where
        I: IntoIterator<Item = Operation>,
    {
        self.body.codes = operations.into_iter().collect();
        self
    }
}

#[derive(Default)]
pub struct FunctionBody {
    codes: Vec<Operation>,
}

impl<'i> IntoIterator for &'i FunctionBody {
    type Item = &'i Operation;
    type IntoIter = Iter<'i, Operation>;

    fn into_iter(self) -> Self::IntoIter {
        self.codes.iter()
    }
}

#[derive(Debug)]
pub enum Operation {
    Sequence {
        items: Vec<Operation>,
    },
    CallFunction {
        name: Symbol,
        input: Vec<Operation>,
    },
    GetVariable {
        kind: VariableKind,
        variable: Symbol,
    },
    SetVariable {
        kind: VariableKind,
        variable: Symbol,
    },
    TeeVariable {
        variable: Symbol,
    },
    Loop {
        r#continue: Symbol,
        r#break: Symbol,
        body: Vec<Operation>,
    },
    Goto {
        label: Symbol,
    },
    Drop,
    Return,
    Unreachable,

    /// `if cond { } { }`
    Conditional {
        condition: Vec<Operation>,
        then: Vec<Operation>,
        r#else: Vec<Operation>,
    },
    Constant {
        value: NyarValue,
    },
    NativeSum {
        native: NyarType,
        terms: Vec<Operation>,
    },
    Convert {
        from: NyarType,
        into: NyarType,
        code: Vec<Operation>,
    },
    Transmute {
        from: NyarType,
        into: NyarType,
        code: Vec<Operation>,
    },
    NativeEqual {
        native: NyarType,
        terms: Vec<Operation>,
    },
    NativeEqualZero {
        native: NyarType,
        term: Box<Operation>,
    },
}

#[derive(Debug)]
pub enum VariableKind {
    Global,
    Local,
    Table,
}