pa1.2: add unit tests

This commit is contained in:
xinyangli 2024-01-13 10:38:20 +08:00
parent 9cca9de2a8
commit e19e89f70e
No known key found for this signature in database
8 changed files with 235 additions and 14 deletions

View file

@ -5,8 +5,8 @@
%%
0[xX][0-9a-fA-F]+ { yylval = strtol(yytext, NULL, 16); return HEX_NUMBER; }
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
0[xX][0-9a-fA-F]+ { yylval = strtoul(yytext, NULL, 16); return HEX_NUMBER; }
[0-9]+ { yylval = strtoul(yytext, NULL, 10); return NUMBER; }
[+\-*/()] { return *yytext; }
[ \t] { }
. { printf("Unexpected character: %s\n", yytext); }

View file

@ -3,8 +3,8 @@
#include <stdlib.h>
#include <stdint.h>
extern int yylex(void);
void yyerror(uint32_t *result, const char *s) {
fprintf(stderr, "Error: %s\n", s);
void yyerror(uint32_t *result, const char *err) {
fprintf(stderr, "Error: %s\n", err);
}
%}
@ -12,7 +12,7 @@
%start input
%define api.value.type { uint32_t }
%parse-param { uint32_t *result }
%left '+' '-'
%left '-' '+'
%left '*' '/'
%%
@ -21,13 +21,19 @@ input
;
expression
: expression '+' expression { $$ = $1 + $3; }
: number { $$ = $1; }
| expression '+' expression { $$ = $1 + $3; }
| expression '-' expression { $$ = $1 - $3; }
| expression '*' expression { $$ = $1 * $3; }
| expression '/' expression { $$ = $1 / $3; }
| expression '/' expression {
if($3 == 0) {
fprintf(stderr, "Error: divide by zero at %u / %u\n", $1, $3);
YYABORT;
};
$$ = $1 / $3;
}
| '-' number { $$ = -$2; }
| '(' expression ')' { $$ = $2; }
| number { $$ = $1; }
number
: NUMBER

View file

@ -38,11 +38,11 @@ static int cmd_info(char *args);
static int cmd_info_r(char *args);
static int cmd_info_w(char *args);
static struct CMDTable {
static struct CommandTable {
const char *name;
const char *description;
int (*handler)(char *);
struct CMDTable *subcommand;
struct CommandTable *subcommand;
int nr_subcommand;
} cmd_info_table[] =
{
@ -191,7 +191,8 @@ static int cmd_x(char *args) {
word_t n = parse_uint(arg, &res);
if (!res)
goto wrong_usage;
arg = strtok(NULL, " ");
// No deliminter here, just pass all the remain argument to `parse_expr()`
arg = strtok(NULL, "");
word_t addr = parse_expr(arg, &res);
if (!res)
goto wrong_usage;
@ -205,7 +206,7 @@ static int cmd_x(char *args) {
return 0;
wrong_usage:
printf("Invalid argument for command x: %s\n", args);
printf("Invalid argument for command x: %s\n", arg);
printf("Usage: x [N: uint] [EXPR: <expr>]\n");
return 0;
}
@ -230,7 +231,7 @@ wrong_usage:
return 0;
}
static int cmd_help_print(char *args, struct CMDTable *cur_cmd_table,
static int cmd_help_print(char *args, struct CommandTable *cur_cmd_table,
int cur_nr_cmd) {
int i;
char *arg = strtok(NULL, " ");
@ -270,7 +271,7 @@ static int cmd_help(char *args) {
printf("-- %s\n", cmd_table[i].description);
// Print available subcommands
for (int j = 0; j < cmd_table[i].nr_subcommand; j++) {
struct CMDTable *sub_cmd_table = cmd_table[i].subcommand;
struct CommandTable *sub_cmd_table = cmd_table[i].subcommand;
printf(" > %s -- %s\n", sub_cmd_table[j].name,
sub_cmd_table[j].description);
}