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
extern crate kaiju_compiler_core as compiler_core;
extern crate kaiju_core as core;
extern crate libloading;
#[macro_use]
extern crate lazy_static;
extern crate relative_path;

pub mod external_deep_validator;
pub mod fs_module_reader;

use crate::core::assembly::*;
use crate::core::error::*;
use crate::core::program::*;
use crate::core::validator::*;
use crate::fs_module_reader::*;
use std::fs::{read_to_string, write};

pub fn load_opdescs(paths: &[String]) -> SimpleResult<OpsDescriptor> {
    if paths.is_empty() {
        Ok(OpsDescriptor::default())
    } else {
        let mut descs = vec![];
        for path in paths {
            match read_to_string(&path) {
                Ok(desc) => match compile_ops_descriptor(&desc) {
                    Ok(desc) => descs.push(desc),
                    Err(err) => return Err(SimpleError::new(format!("{:?}: {}", path, err.pretty))),
                },
                Err(err) => return Err(SimpleError::new(format!("{:?}: {}", path, err))),
            }
        }
        Ok(OpsDescriptor::merge(&descs))
    }
}

pub fn compile_program<V>(input: &str, opsdesc: &OpsDescriptor) -> SimpleResult<Program>
where
    V: DeepValidator,
{
    compiler_core::compile_program::<V, _>(input, FsModuleReader::default(), opsdesc)
}

pub fn compile_program_and_write_pst<V>(
    input: &str,
    output: &str,
    opsdesc: &OpsDescriptor,
    pretty: bool,
) -> SimpleResult<()>
where
    V: DeepValidator,
{
    let program = compile_program::<V>(input, opsdesc)?;
    match program.to_json(pretty) {
        Ok(json) => {
            if let Err(err) = write(output, &json) {
                Err(SimpleError::new(format!("{:?}: {}", output, err)))
            } else {
                Ok(())
            }
        }
        Err(err) => Err(SimpleError::new(format!("{:?}: {}", output, err))),
    }
}

pub fn compile_program_and_write_bin<V>(
    input: &str,
    output: &str,
    opsdesc: &OpsDescriptor,
) -> SimpleResult<()>
where
    V: DeepValidator,
{
    let program = compile_program::<V>(input, opsdesc)?;
    match encode_assembly(&program, opsdesc) {
        Ok(bytes) => {
            if let Err(err) = write(output, &bytes) {
                Err(SimpleError::new(format!("{:?}: {}", output, err)))
            } else {
                Ok(())
            }
        }
        Err(err) => Err(SimpleError::new(format!("{:?}: {}", output, err.message))),
    }
}