aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 69af68293b62226c14fa8ad3405e90331ce2ebdf (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
use std::path::{Path, PathBuf};

use sylt::run_file;

struct Args {
    file: Option<PathBuf>,
    print: bool,
}

macro_rules! link {
    ([ $( $ident:tt ),* ]) => {
        vec![
            $( (stringify!($ident).to_string(), $ident), )*
        ]
    }
}

fn main() {
    let args = parse_args();
    let file = args.file.unwrap_or_else(|| Path::new("progs/tests/simple.sy").to_owned());
    let errs = match run_file(&file, args.print, link!([extern_test])) {
        Err(it) => it,
        _ => return,
    };
    for err in errs.iter() {
        println!("{}", err);
    }
    println!(" {} errors occured.", errs.len());
}

fn parse_args() -> Args {
    let mut args = Args {
        file: None,
        print: false,
    };

    for s in std::env::args().skip(1) {
        let path = Path::new(&s).to_owned();
        if path.is_file() {
            args.file = Some(path);
        } else if "-p" == s {
            args.print = true;
        } else {
            eprintln!("Invalid argument {}.", s);
        }
    };
    args
}

#[sylt_macro::extern_link]
pub fn f(x: sylt::Value, _typecheck: bool) -> Result<sylt::Value, sylt::error::ErrorKind> {
    Ok(x)
}

#[sylt_macro::extern_link(g)]
pub fn f2(x: sylt::Value, _typecheck: bool) -> Result<sylt::Value, sylt::error::ErrorKind> {
    Ok(x)
}

mod m1 {
    mod m2 {
        #[sylt_macro::extern_link(h)]
        pub fn f2(x: sylt::Value, _typecheck: bool) -> Result<sylt::Value, sylt::error::ErrorKind> {
            Ok(x)
        }
    }
}

sylt_macro::extern_function!(
    extern_test
    [sylt::Value::Float(x), sylt::Value::Float(y)] -> sylt::Type::Float => {
        Ok(sylt::Value::Float(x + y))
    },
    [sylt::Value::Float(x)] -> sylt::Type::Float => {
        Ok(sylt::Value::Float(*x))
    },
);