about summary refs log tree commit diff
path: root/corp/tvixbolt/src/main.rs
blob: 122a84e0ac6a0577815106ea21c6711a2d62369b (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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use std::fmt::Write;

use std::rc::Rc;
use tvix_eval::observer::DisassemblingObserver;
use web_sys::HtmlTextAreaElement;
use yew::prelude::*;
use yew::TargetCast;

enum Msg {
    CodeChange(String),
}

struct Model {
    code: String,
}

impl Component for Model {
    type Message = Msg;
    type Properties = ();

    fn create(_ctx: &Context<Self>) -> Self {
        Self {
            code: String::new(),
        }
    }

    fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            Msg::CodeChange(new_code) => {
                self.code = new_code;
                true
            }
        }
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        // This gives us a component's "`Scope`" which allows us to send messages, etc to the component.
        let link = ctx.link();
        html! {
            <div class="container">
                <h1>{"tvixbolt"}</h1>
                <form>
                  <fieldset>
                    <legend>{"Input"}</legend>

                    <div class="form-group">
                        <label for="code">{"Nix code:"}</label>
                        <textarea
                         oninput={link.callback(|e: InputEvent| {
                             let ta = e.target_unchecked_into::<HtmlTextAreaElement>().value();
                             Msg::CodeChange(ta)

                         })}
                         id="code" cols="30" rows="10">
                        </textarea>
                    </div>

                <div class="form-group">
                <label for="disable-bytecode">{"Disassemble:"}</label>
                <input for="disable-bytecode" type="checkbox" checked=true disabled=true />
                </div>
                  </fieldset>
                </form>
                <hr />
                <h2>{"Result:"}</h2>
                {eval(&self.code).display()}
            </div>
        }
    }
}

#[derive(Default)]
struct Output {
    parse_errors: String,
    warnings: String,
    compiler_errors: String,
    runtime_errors: String,
    output: String,
    bytecode: Vec<u8>,
}

fn maybe_show(title: &str, s: &str) -> Html {
    if s.is_empty() {
        html! {}
    } else {
        html! {
            <>
              <h3>{title}</h3>
              <pre>{s}</pre>
            </>
        }
    }
}

impl Output {
    fn display(self) -> Html {
        html! {
            <>
            {maybe_show("Parse errors:", &self.parse_errors)}
            {maybe_show("Warnings:", &self.warnings)}
            {maybe_show("Compiler errors:", &self.compiler_errors)}
            {maybe_show("Bytecode:", &String::from_utf8_lossy(&self.bytecode))}
            {maybe_show("Runtime errors:", &self.runtime_errors)}
            {maybe_show("Output:", &self.output)}
            </>
        }
    }
}

fn eval(code: &str) -> Output {
    let mut out = Output::default();

    if code.is_empty() {
        return out;
    }

    let mut codemap = codemap::CodeMap::new();
    let file = codemap.add_file("nixbolt".to_string(), code.into());

    let parsed = rnix::ast::Root::parse(code);
    let errors = parsed.errors();

    if !errors.is_empty() {
        for err in errors {
            writeln!(&mut out.parse_errors, "parse error: {}", err).unwrap();
        }

        return out;
    }

    // If we've reached this point, there are no errors.
    let root_expr = parsed
        .tree()
        .expr()
        .expect("expression should exist if no errors occured");

    let codemap = Rc::new(codemap);
    let mut compilation_observer = DisassemblingObserver::new(codemap, &mut out.bytecode);

    let result = tvix_eval::compile(
        root_expr,
        Some("/nixbolt".into()),
        &file,
        tvix_eval::global_builtins(),
        &mut compilation_observer,
    )
    .unwrap();

    for warning in result.warnings {
        writeln!(
            &mut out.warnings,
            "warning: {:?} at `{}` [line {}]",
            warning.kind,
            file.source_slice(warning.span),
            file.find_line(warning.span.low()) + 1
        )
        .unwrap();
    }

    if !result.errors.is_empty() {
        for error in &result.errors {
            writeln!(
                &mut out.compiler_errors,
                "error: {:?} at `{}` [line {}]",
                error.kind,
                file.source_slice(error.span),
                file.find_line(error.span.low()) + 1
            )
            .unwrap();
        }

        return out;
    }

    let result = tvix_eval::run_lambda(result.lambda);

    match result {
        Ok(value) => writeln!(&mut out.output, "{}", value).unwrap(),
        Err(err) => writeln!(&mut out.runtime_errors, "runtime error: {:?}", err).unwrap(),
    };

    out
}

fn main() {
    yew::start_app::<Model>();
}