Skip to content

Commit

Permalink
関数の引数で与えた文字列を関数の中で展開できない #27 #37
Browse files Browse the repository at this point in the history
  • Loading branch information
kujirahand committed Jan 4, 2025
1 parent b043896 commit 2c6ab1d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,18 @@ cargo build --release

## Basic Usage

Please make text file "test.mml".  Execute the following command to generate a MIDI file.
Please make text file `test.mml`. And execute the following command to generate a MIDI file `test.mid`.

```sh
sakuramml test.mml
./sakuramml test.mml test.mid
```

### Check MIDI file

You can check MIDI file with `--dump` option.

```sh
./sakuramml --dump test.mid
```

### MML Basic
Expand Down
37 changes: 32 additions & 5 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,11 @@ fn read_def_user_function(cur: &mut TokenCursor, song: &mut Song) -> Token {
let args: Vec<&str> = args_str.split(",").collect();
let mut arg_types: Vec<char> = vec![];
let mut arg_names: Vec<String> = vec![];
let mut arg_def_values: Vec<SValue> = vec![];
for i in 0..args.len() {
let name = args[i].trim().to_string();
if name.len() == 0 { continue; }
let mut def_v: SValue = SValue::from_i(0);
// include space?
if name.contains(" ") {
// split string by " "
Expand All @@ -329,10 +331,18 @@ fn read_def_user_function(cur: &mut TokenCursor, song: &mut Song) -> Token {
let name_s = splited[1].trim();
// get type
let mut type_sf = 'I';
if type_s == "Str" || type_s == "STR" || type_s == "S" { type_sf = 'S'; }
if type_s == "Int" || type_s == "INT" || type_s == "I" { type_sf = 'I'; }
if type_s == "Array" || type_s == "ARRAY" || type_s == "A" { type_sf = 'A'; }
song.variables_insert(name_s, SValue::new());
if type_s == "Int" || type_s == "INT" || type_s == "I" {
type_sf = 'I';
}
if type_s == "Str" || type_s == "STR" || type_s == "S" {
type_sf = 'S';
def_v = SValue::from_str("");
}
if type_s == "Array" || type_s == "ARRAY" || type_s == "A" {
type_sf = 'A';
def_v = SValue::from_int_array(vec![]);
}
song.variables_insert(name_s, def_v.clone()); // add name to local variables
arg_types.push(type_sf);
arg_names.push(name_s.to_string());
} else {
Expand All @@ -341,6 +351,20 @@ fn read_def_user_function(cur: &mut TokenCursor, song: &mut Song) -> Token {
arg_types.push('i');
arg_names.push(name);
}
// check def value
cur.skip_space();
if cur.eq_char('=') {
cur.next();
def_v = read_arg_value(cur, song);
// check var type
match &def_v {
SValue::Int(_) => arg_types[i] = 'I',
SValue::Str(_, _) => arg_types[i] = 'S',
SValue::IntArray(_) => arg_types[i] = 'A',
_ => {}
}
}
arg_def_values.push(def_v);
}
// get body
cur.skip_space_ret();
Expand All @@ -365,6 +389,7 @@ fn read_def_user_function(cur: &mut TokenCursor, song: &mut Song) -> Token {
let mut func_obj = SFunction::new(&func_name, body_tok, func_id, lineno);
func_obj.arg_names = arg_names;
func_obj.arg_types = arg_types;
func_obj.arg_def_values = arg_def_values;
song.functions[func_id] = func_obj;
Token::new_empty(&format!("DefineFunction::{}", func_name), lineno)
}
Expand Down Expand Up @@ -838,9 +863,11 @@ fn check_variables(cur: &mut TokenCursor, song: &mut Song, cmd: String) -> Optio
return Some(tok);
}
// variables?
println!("@@@check_variables:{}", cmd);
match song.variables_get(&cmd) {
Some(sval) => {
// get variable
println!("@@@:{:?}", sval);
// get variable
return Some(read_variables(cur, song, &cmd, sval.clone()));
}
None => {}
Expand Down
6 changes: 6 additions & 0 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2004,4 +2004,10 @@ mod tests {
let song = exec_easy("INT A=30; PRINT(-A)");
assert_eq!(song.get_logs_str(), "[PRINT](0) -30");
}
#[test]
fn extract_function_args() { // 関数の引数で与えた文字列を関数の中で展開できない #27
let song = exec_easy("Function EXT_MML(STR AA){ AA }; EXT_MML{ l4cdeg } ");
let pos = song.tracks[0].timepos;
assert_eq!(pos, song.timebase * 4);
}
}
2 changes: 2 additions & 0 deletions src/song.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ pub struct SFunction {
pub func_id: usize,
pub arg_names: Vec<String>,
pub arg_types: Vec<char>, // S: string, I: int, A: array
pub arg_def_values: Vec<SValue>,
}

impl SFunction {
Expand All @@ -511,6 +512,7 @@ impl SFunction {
func_id,
arg_names: vec![],
arg_types: vec![],
arg_def_values: vec![],
}
}
}
Expand Down

0 comments on commit 2c6ab1d

Please sign in to comment.