feat(asmformat): Remove special treatment of oscillator types to ease the parsing of asmformat

BREAKING CHANGE: They are now numeric values like all the rest macro parameters, instead of %defined constants.
This commit is contained in:
Veikko Sariola
2020-12-06 15:05:12 +02:00
parent 1b1a4af5ea
commit 9e4bee1b67
46 changed files with 95 additions and 129 deletions

View File

@ -2,7 +2,6 @@ package go4k
import (
"bufio"
"errors"
"fmt"
"regexp"
"strconv"
@ -33,10 +32,6 @@ func DeserializeAsm(asmcode string) (*Song, error) {
}
return ret, nil
}
typeReg, err := regexp.Compile(`TYPE\s*\(\s*(SINE|TRISAW|PULSE|GATE|SAMPLE)\s*\)`) // matches TYPE(TRISAW), groups "TRISAW"
if err != nil {
return nil, err
}
wordReg, err := regexp.Compile(`\s*([a-zA-Z_][a-zA-Z0-9_]*)([^;\n]*)`) // matches a word and "the rest", until newline or a comment
if err != nil {
return nil, err
@ -131,24 +126,6 @@ func DeserializeAsm(asmcode string) (*Song, error) {
if err != nil {
return nil, fmt.Errorf("Error parsing parameters: %v", err)
}
if unittype == "oscillator" {
match := typeReg.FindStringSubmatch(rest)
if match == nil {
return nil, errors.New("Oscillator should define a type")
}
switch match[1] {
case "SINE":
parameters["type"] = Sine
case "TRISAW":
parameters["type"] = Trisaw
case "PULSE":
parameters["type"] = Pulse
case "GATE":
parameters["type"] = Gate
case "SAMPLE":
parameters["type"] = Sample
}
}
unit := Unit{Type: unittype, Parameters: parameters}
instr.Units = append(instr.Units, unit)
}
@ -310,20 +287,7 @@ func SerializeAsm(song *Song) (string, error) {
for _, unit := range instrument.Units {
row := []string{fmt.Sprintf("SU_%v", strings.ToUpper(unit.Type))}
for _, parname := range paramorder[unit.Type] {
if unit.Type == "oscillator" && parname == "type" {
switch unit.Parameters["type"] {
case Sine:
row = append(row, "TYPE(SINE)")
case Trisaw:
row = append(row, "TYPE(TRISAW)")
case Pulse:
row = append(row, "TYPE(PULSE)")
case Gate:
row = append(row, "TYPE(GATE)")
case Sample:
row = append(row, "TYPE(SAMPLE)")
}
} else if v, ok := unit.Parameters[parname]; ok {
if v, ok := unit.Parameters[parname]; ok {
row = append(row, fmt.Sprintf("%v(%v)", strings.ToUpper(parname), strconv.Itoa(int(v))))
} else {
return "", fmt.Errorf("The parameter map for unit %v does not contain %v, even though it should", unit.Type, parname)