-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
128 lines (120 loc) · 3.34 KB
/
lib.rs
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
//! "sakruamml-rust" is a MML/ABC to MIDI compier.
//! This compiler that converts the text of "cde" into MIDI files.
//! It is a tool that allows you to easily create music.
pub mod sakura_version;
pub mod sakura_message;
pub mod cursor;
pub mod token;
pub mod lexer;
pub mod song;
pub mod svalue;
pub mod midi;
pub mod sutoton;
pub mod runner;
pub mod mml_def;
pub mod song_test;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
/// Debug level - no info
pub const SAKURA_DEBUG_NONE: u32 = 0;
/// Debug level - show info
pub const SAKURA_DEBUG_INFO: u32 = 1;
// ------------------------------------------
// Sakura Functions for JavaScript
// ------------------------------------------
/// get sakura compiler version info
#[wasm_bindgen]
pub fn get_version() -> String {
sakura_version::SAKURA_VERSION.to_string()
}
/// SakuraCompiler Object
#[wasm_bindgen]
pub struct SakuraCompiler {
song: song::Song,
log_str: String,
lang: String,
debug_level: u32,
}
#[wasm_bindgen]
impl SakuraCompiler {
/// new object
pub fn new() -> Self {
SakuraCompiler {
song: song::Song::new(),
log_str: "".to_string(),
debug_level: 0,
lang: "en".to_string(),
}
}
/// compile to MIDI data
pub fn compile(&mut self, source: &str) -> Vec<u8> {
if self.debug_level > 0 {
self.song.debug = true;
}
self.song.set_language(&self.lang);
// convert sutoton
let source_mml = sutoton::convert(source);
// parse MML
let tokens = lexer::lex(&mut self.song, &source_mml, 0);
// run Tokens
runner::exec(&mut self.song, &tokens);
// generate MIDI
let bin = midi::generate(&mut self.song);
// get log text
let log_text = self.song.get_logs_str();
self.log_str.push_str(&log_text);
bin
}
/// set message language
pub fn set_language(&mut self, code: &str) {
self.lang = code.to_string();
}
/// get log text
pub fn get_log(&self) -> String {
self.log_str.to_string()
}
/// set debug level
pub fn set_debug_level(&mut self, level: u32) {
self.debug_level = level;
}
}
/// compile source to MIDI data
#[wasm_bindgen]
pub fn compile_to_midi(source: &str, debug_level: u32) -> Vec<u8> {
let mut song = song::Song::new();
if debug_level >= 1 {
song.debug = true;
}
let source_mml = sutoton::convert(source);
let tokens = lexer::lex(&mut song, &source_mml, 0);
runner::exec(&mut song, &tokens);
let bin = midi::generate(&mut song);
bin
}
// ------------------------------------------
// Functions for Rust Native
// ------------------------------------------
/// compiler result struct
#[derive(Debug)]
pub struct SakuraResult {
/// MIDI binary data
pub bin: Vec<u8>,
/// MIDI binary data
pub log: String,
}
/// compile source to MIDI data
pub fn compile(source: &str, debug_level: u32) -> SakuraResult {
let mut song = song::Song::new();
if debug_level >= 1 {
song.debug = true;
}
let source_mml = sutoton::convert(source);
let tokens = lexer::lex(&mut song, &source_mml, 0);
runner::exec(&mut song, &tokens);
let bin = midi::generate(&mut song);
let log_text = song.get_logs_str();
SakuraResult {
bin,
log: log_text
}
}