Add multiplication.
Supports negative numbers too.
This commit is contained in:
parent
e3afe52ab7
commit
653736622f
10
src/exec.c
10
src/exec.c
@ -23,11 +23,6 @@ ASTNumData exec_call(AST* ast) {
|
|||||||
ASTCallData* calldata = (ASTCallData*)ast->data;
|
ASTCallData* calldata = (ASTCallData*)ast->data;
|
||||||
if (!strcmp(calldata->to, "+") && calldata->argc == 2) {
|
if (!strcmp(calldata->to, "+") && calldata->argc == 2) {
|
||||||
|
|
||||||
/*
|
|
||||||
ASTNumData* n1 = (ASTNumData*)calldata->argv[0]->data;
|
|
||||||
ASTNumData* n2 = (ASTNumData*)calldata->argv[1]->data;
|
|
||||||
*/
|
|
||||||
|
|
||||||
ASTNumData n1 = exec_expr(calldata->argv[0]);
|
ASTNumData n1 = exec_expr(calldata->argv[0]);
|
||||||
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
||||||
|
|
||||||
@ -37,6 +32,11 @@ ASTNumData exec_call(AST* ast) {
|
|||||||
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
||||||
|
|
||||||
return n1 - n2;
|
return n1 - n2;
|
||||||
|
} else if (!strcmp(calldata->to, "*") && calldata->argc == 2) {
|
||||||
|
ASTNumData n1 = exec_expr(calldata->argv[0]);
|
||||||
|
ASTNumData n2 = exec_expr(calldata->argv[1]);
|
||||||
|
|
||||||
|
return n1 * n2;
|
||||||
}
|
}
|
||||||
return -1000;
|
return -1000;
|
||||||
}
|
}
|
||||||
|
@ -21,10 +21,13 @@
|
|||||||
|
|
||||||
%define parse.error verbose
|
%define parse.error verbose
|
||||||
|
|
||||||
%token<fval> NUM
|
|
||||||
%token NEG
|
|
||||||
%token<strval> CALL
|
%token<strval> CALL
|
||||||
|
%token<fval> NUM
|
||||||
|
|
||||||
|
%token NEG
|
||||||
%token PLUS
|
%token PLUS
|
||||||
|
%token MULT
|
||||||
|
|
||||||
%token NL
|
%token NL
|
||||||
%type<ast> exp
|
%type<ast> exp
|
||||||
|
|
||||||
@ -63,4 +66,29 @@ exp:
|
|||||||
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("-", 2, argv));
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("-", 2, argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
| NUM MULT NUM {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init($1));
|
||||||
|
argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init($3));
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("*", 2, argv));
|
||||||
|
}
|
||||||
|
| NEG NUM MULT NUM {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$2));
|
||||||
|
argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init($4));
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("*", 2, argv));
|
||||||
|
}
|
||||||
|
| NEG NUM MULT NEG NUM {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$2));
|
||||||
|
argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$5));
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("*", 2, argv));
|
||||||
|
}
|
||||||
|
| NUM MULT NEG NUM {
|
||||||
|
AST** argv = calloc(2, sizeof(AST*));
|
||||||
|
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init($1));
|
||||||
|
argv[1] = ast_init(AST_TYPE_NUM, ast_num_data_init(-$4));
|
||||||
|
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init("*", 2, argv));
|
||||||
|
}
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
@ -66,6 +66,7 @@ int yylex() {
|
|||||||
case '+': return PLUS;
|
case '+': return PLUS;
|
||||||
case '\n': return NL;
|
case '\n': return NL;
|
||||||
case '-': return NEG;
|
case '-': return NEG;
|
||||||
|
case '*': return MULT;
|
||||||
default: return CALL;
|
default: return CALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user