Write parser for temp profile language. Not yet tested.

This commit is contained in:
Mario Hüttel 2021-03-19 16:58:14 +01:00
parent e3e4a6d926
commit 1b4eba1871
2 changed files with 90 additions and 14 deletions

View File

@ -20,6 +20,8 @@
#include <reflow-controller/config-parser/temp-profile-parser.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <fatfs/ff.h>
struct pl_command_list_map {
@ -81,34 +83,113 @@ static int read_line_until_comment(FIL *f, char *buffer, uint32_t buffsize)
return 0;
}
static const struct pl_command_list_map *string_to_command(const char *str)
{
uint32_t i;
const struct pl_command_list_map *ret = NULL;
if (!str)
return NULL;
for (i = 0u; i < _PL_NUM_CMDS; i++) {
if (!strcmp(str, cmd_list_map[i].token)) {
ret = &cmd_list_map[i];
break;
}
}
return ret;
}
static int parse_line(char *line, struct pl_command *cmd)
{
uint8_t token_idx = 0;
char *token;
const char * const delim = " \t";
const struct pl_command_list_map *map;
char *endptr;
if (!line || !cmd)
return -1000;
token = strtok(line, delim);
while (token && token_idx <= PROFILE_LANG_MAX_NUM_ARGS) {
switch (token_idx) {
case 0:
map = string_to_command(token);
cmd->cmd = map->command;
break;
default:
if (!map) {
/* No valid command found */
return -1;
}
cmd->params[token_idx - 1] = strtof(token, &endptr);
if (endptr == token) {
/* Invalid parameter */
return -2;
}
break;
}
token = strtok(NULL, delim);
token_idx++;
}
return 0;
}
enum pl_ret_val temp_profile_parse_from_file(const char *filename,
struct pl_command *cmd_list,
uint32_t cmd_list_length,
struct pl_error *error,
uint32_t *cmds_parsed)
{
FIL script_file;
FRESULT fres;
int res;
enum pl_ret_val ret = PL_RET_SUCCESS;
char workbuff[256];
uint32_t cmd_idx;
if (!filename || !cmd_list || !cmd_list_length || !cmds_parsed || !error)
if (!filename || !cmd_list || !cmd_list_length || !cmds_parsed)
return PL_RET_PARAM_ERR;
error->column = 0;
error->line = 0;
fres = f_open(&script_file, filename, FA_READ);
if (fres != FR_OK) {
error->errormsg = "Error opening file";
ret = PL_RET_DISK_ERR;
goto exit;
}
cmd_idx = 0;
*cmds_parsed = 0;
do {
/* TODO: Read lines and parse them */
/* read in the line */
res = read_line_until_comment(&script_file, workbuff, sizeof(workbuff));
if (res < 0) {
ret = PL_RET_DISK_ERR;
goto exit_close;
}
/* Check if list already full */
if (cmd_idx >= cmd_list_length) {
ret = PL_RET_LIST_FULL;
goto exit_close;
}
/* Parse the line */
res = parse_line(workbuff, &cmd_list[cmd_idx]);
if (res) {
ret = PL_RET_SCRIPT_ERR;
goto exit_close;
}
cmd_idx++;
*cmds_parsed= cmd_idx;
} while (!f_eof(&script_file));
exit_close:
(void)f_close(&script_file);
exit:
return ret;

View File

@ -42,21 +42,16 @@ enum pl_ret_val {
PL_RET_SCRIPT_ERR,
};
struct pl_error {
const char *errormsg;
uint32_t line;
uint32_t column;
};
#define PROFILE_LANG_MAX_NUM_ARGS (8)
struct pl_command {
enum pl_command_type cmd;
float params[8];
float params[PROFILE_LANG_MAX_NUM_ARGS];
};
enum pl_ret_val temp_profile_parse_from_file(const char *filename,
struct pl_command *cmd_list,
uint32_t cmd_list_length,
struct pl_error *error,
uint32_t *cmds_parsed);
#endif /* __CONFIG_PARSER_TEMP_PROFILE_PARSER_H__ */